forked from Megabytemb/kano-wand-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
214 lines (176 loc) · 7.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var just = require('string-just');
var kano = require('./kano_info.json');
var async = require("async");
var gestureSpells = require("./gesture-spells");
const { Observable, Subject, ReplaySubject, from, of, range } = require('rxjs');
const { map, filter, switchMap } = require('rxjs/operators');
const Conv = require('./conversion');
const width = 800;
const height = 600
const conv = new Conv(width, height);
var gr = new gestureSpells()
class Wand {
constructor() {
this.name = null;
this.buttonCharacteristic = null;
this.vibrateCharacteristic = null;
this.quaternionsCharacteristic = null;
this.quaternionsResetCharacteristic = null;
this.currentSpell = [];
this.buttonPressed = false;
this.timeUp = new Date();
this.timeDown = new Date();
this.resetTimeout = 0.2 // determins a quick press for wand reset (milliseconds)
this.spells = new Subject();
this.positions = new Subject();
}
static uInt8ToUInt16(byteA, byteB) {
const number = (((byteB & 0xff) << 8) | byteA);
const sign = byteB & (1 << 7);
if (sign) {
return 0xFFFF0000 | number;
}
return number;
}
processCharacteristic(characteristic) {
{
if (compareUUID(characteristic.uuid, kano.SENSOR.QUATERNIONS_CHAR)) {
this.logWithName("Found position characteristic");
this.quaternionsCharacteristic = characteristic;
}
if (compareUUID(characteristic.uuid, kano.IO.USER_BUTTON_CHAR)) {
this.logWithName("Found button characteristic");
this.buttonCharacteristic = characteristic;
}
if (compareUUID(characteristic.uuid, kano.SENSOR.QUATERNIONS_RESET_CHAR)) {
this.logWithName("Found ResetChar characteristic");
this.quaternionsResetCharacteristic = characteristic;
}
if (compareUUID(characteristic.uuid, kano.IO.VIBRATOR_CHAR)) {
this.logWithName("Found vibrate characteristic");
this.vibrateCharacteristic = characteristic;
}
}
}
vibrate(pattern) {
var vibrate = Buffer.alloc(1);
vibrate.writeUInt8(pattern,0)
this.vibrateCharacteristic.write(vibrate, true);
}
init(peripheral, name) {
this.name = name || peripheral.advertisement.localName;
this.logWithName("init");
var serviceUUIDs = [kano.SENSOR.SERVICE.replace(/-/g, "").toLowerCase(), kano.IO.SERVICE.replace(/-/g, "").toLowerCase(), kano.INFO.SERVICE.replace(/-/g, "").toLowerCase()];
const $this = this;
return new Promise((resolve, reject) => {
async.waterfall([
function(callback) {
this.logWithName("Discovering services...");
peripheral.discoverServices(serviceUUIDs, callback);
}.bind(this),
function(services, callback) {
this.logWithName("Found", services.length, "services");
var tasks = []
services.forEach(function(service) {
tasks.push(function(callback) {
this.logWithName("Discovering characteristics for service with UUID", service.uuid);
service.discoverCharacteristics([], callback);
}.bind(this))
}.bind(this))
async.parallel(tasks, callback);
}.bind(this),
function (characteristics, callback) {
characteristics = characteristics.flat();
characteristics.forEach(this.processCharacteristic, this)
callback();
}.bind(this),
this.subscribe_position.bind(this),
this.subscribe_button.bind(this),
this.reset_position.bind(this)
], function (err, result) {
this.logWithName("Wand ready!");
resolve(true);
}.bind(this));
});
}
subscribe_button(callback) {
this.logWithName("Subscribe to button characteristic")
this.buttonCharacteristic.on('read', this.onButtonUpdate.bind(this));
this.buttonCharacteristic.subscribe(callback);
}
onButtonUpdate(data, isNotification) {
const raw = data.readUIntBE(0, 1);
const pressed = raw == 1 ? true : false;
this.buttonPressed = pressed;
// timing
if (pressed) {
this.timeUp = new Date();
} else {
this.timeDown = new Date();
}
var seconds = (this.timeDown.getTime() - this.timeUp.getTime()) / 1000;
if (pressed) {
this.spell = null;
} else if (seconds < this.resetTimeout) { // not pressed
this.reset_position();
} else if (this.currentSpell.length > 5) { // not pressed
this.currentSpell = this.currentSpell.splice(5);
let flippedPositions = [];
this.currentSpell.forEach((entry) => {
flippedPositions.push(Wand.flipCord(entry));
})
const positions = this.currentSpell;
gr.recognise(flippedPositions)
.then((data) =>{
data.positions = flippedPositions;
this.spells.next(data);
});
this.currentSpell = [];
}
}
static flipCord(cords) {
const x = cords[0]
const y = cords [1]
const iy = height - (y);
return [x, iy];
}
subscribe_position(callback) {
this.logWithName("Subscribe to motion characteristic")
this.quaternionsCharacteristic.on('read', this.onMotionUpdate.bind(this));
this.quaternionsCharacteristic.subscribe(callback);
}
onMotionUpdate(data, isNotification) {
let y = data.readInt16LE(0);
let x = data.readInt16LE(2);
let w = data.readInt16LE(4);
let z = data.readInt16LE(6);
const pos = conv.position([x, y, z, w]);
let pitch = `Pitch: ${just.ljust(z.toString(), 16, " ")}`;
let roll = `Roll: ${just.ljust(w.toString(), 16, " ")}`;
// this.logWithName(`${pitch}${roll}(x, y): (${x.toString()}, ${y.toString()})`)
// this.logWithName(this.getXY(x, y))
if (this.buttonPressed) {
this.currentSpell.push([pos.x, pos.y]);
this.positions.next([pos.x, pos.y]);
}
}
reset_position(callback) {
this.logWithName("Reset position");
var reset = Buffer.alloc(1);
reset.writeUInt8(1,0)
this.quaternionsResetCharacteristic.write(reset, true);
if (typeof(callback) == typeof(Function)) {
callback();
}
}
logWithName(args) {
let logs = ["[" + this.name + "]"].concat(args);
console.log(logs);
}
}
function compareUUID(val1, val2) {
val1 = val1.replace(/-/g, "").toLowerCase();
val2 = val2.replace(/-/g, "").toLowerCase();
return val1 === val2;
};
module.exports = Wand;