-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
291 lines (242 loc) · 6.51 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var noble = require('noble');
var lightCharacteristic;
var loggingOn = false;
var readingX=0;
var readingY=64;
var readingZ=0;
var t1=0;
var t2=0;
var t3=0;
var l=0;
var jumpcount=0;
var echologlevel=2; //how much info the program should output
var jumpresettime = 900; //jump reset time in milliseconds
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
//
// Once the BLE radio has been powered on, it is possible
// to begin scanning for services. Pass an empty array to
// scan for all services (uses more time and power).
//
console.log('Scanning for devices...');
noble.startScanning(['ffa0'], false); // ffa0: accelerometer service
}
else {
noble.stopScanning();
}
})
noble.on('discover', function(peripheral) {
noble.stopScanning();
//console.log('found peripheral:', peripheral.advertisement);
/*********************************
peripheral = {
id: "<id>",
address: "<BT address">, // Bluetooth Address of device, or 'unknown' if not known
addressType: "<BT address type>", // Bluetooth Address type (public, random), or 'unknown' if not known
connectable: <connectable>, // true or false, or undefined if not known
advertisement: {
localName: "<name>",
txPowerLevel: <int>,
serviceUuids: ["<service UUID>", ...],
manufacturerData: <Buffer>,
serviceData: [
{
uuid: "<service UUID>"
data: <Buffer>
},
...
]
},
rssi: <rssi>
};
*******************************/
//check its connectable then connect
peripheral.connect(function(err) {
// discover services
peripheral.discoverServices(["ffa0",'ffd0'], function(err, services) {
services.forEach(function(service) {
//console.log('found service:', service.uuid);
// discover characteristics
if (service.uuid=='ffa0') {
service.discoverCharacteristics(["ffa3","ffa4","ffa5"], function(err, characteristics) {
characteristics.forEach(function(characteristic) {
switch(characteristic.uuid) {
case "ffa3":
var dimension=1;
break;
case "ffa4":
var dimension=2;
break;
case "ffa5":
var dimension=3;
break;
}
var basetime = Date.now()
characteristic.notify(true, function() {});
characteristic.on('data', function(data, isNotification) {
logdata(dimension,data.readInt8(0),Date.now()-basetime);
});
});
console.log('Jump device found.\nLogging started...');
loggingOn=true;
setTimeout(function () {
lighton();
}, 300);
});
} else if (service.uuid =='ffd0'){
service.discoverCharacteristics(["ffd2"], function(err, characteristics) {
characteristics.forEach(function(characteristic) {
lightCharacteristic = characteristic ;
});
});
}
});
});
});
});
function errorlog(err) {
console.log(err);
}
function logdata(variable,data,timest) {
if (loggingOn) {
loginfo="";
//test for event (takeoff/landing)
//T1 can be initiated anytime
if (variable == 2){
if (readingY>96 && data<64){
t1=timest;
loginfo="T1 (0)";
}
}
//if T1 initiated test for other events
if (t1 != 0) {
//landing: y var condition
if ( (variable == 2) && (readingY<64 && data>96) ){
l=timest;
tldiff=l-t1;
loginfo="L ("+tldiff+")";
jumpcount++;
}
// T2 and T3, z var conditions
if (variable == 3){
if (readingZ< -30 && data> -30){
diff=timest-t1;
if (diff<200) {
loginfo="T2 ("+diff+")";
t2=timest;
t2diff=diff;
}
} else if (readingZ < -15 && data> -15){
diff=timest-t1;
if (diff<200) {
loginfo="T3 ("+diff+")";
t3=timest;
t3diff=diff;
}
}
}
}
//store value **** APPLY REAL TIME SERIES LOGGING HERE ****
if (variable == 1){
readingX=data;
} else if (variable == 2){
readingY=data;
} else if (variable == 3){
readingZ=data;
}
// output to console
if (echologlevel==2) {
console.log(" ",timest, readingX, readingY, readingZ, loginfo);
} else if ((echologlevel==1) && (l != 0)) {
console.log(" ",t1, "T1 (0)");
if (t2 != 0) {
console.log(" ",t2, "T2 (",t2diff,")");
}
if (t3 != 0) {
console.log(" ",t3, "T3 (",t3diff,")");
}
console.log(" ",timest, loginfo);
} else if ((echologlevel==0) && (l != 0)) {
if (t3 != 0 ) {
tluse=tldiff-t3diff;
tlmethod=3;
} else if (t2 != 0 ) {
tluse=tldiff-t2diff;
tlmethod=2;
} else {
tluse=tldiff;
tlmethod=1;
}
h=getjumpheight(tluse);
if (jumpcount == 1) {
console.log(" jump #\t\tt (ms)\t\th (cm)\t\tmethod");
}
console.log(" ",jumpcount,"\t\t",tluse,"\t\t",h,"\t\t",tlmethod);
}
// reset jump if landed or timeout
if ((timest-t1 >= jumpresettime) || (l != 0)) {
jumpreset();
}
}
}
function jumpreset(){
t1=0;
t2=0;
t3=0;
t2diff=0;
t3diff=0;
l=0;
tldiff=0;
}
function lighton() {
var light = new Buffer(1);
light.writeUInt8(1);
lightCharacteristic.write(light, false, function() {});
}
function lightoff() {
var light = new Buffer(1);
light.writeUInt8(0);
lightCharacteristic.write(light, false, function() {});
}
function getjumpheight(tms) {
//9.81*(tms/1000)^2/8*100
return Math.round(9.81*(tms/1000)*(tms/1000)/8*100);
}
// service.on('characteristicsDiscover', callback(characteristics));
/*********************
characteristic = {
uuid: "<uuid>",
// properties: 'broadcast', 'read', 'writeWithoutResponse', 'write', 'notify', 'indicate', 'authenticatedSignedWrites', 'extendedProperties'
properties: [...]
};
*************/
// READ & WRITE
// characteristic.read([callback(error, data)]);
// characteristic.write(data, notify[, callback(error)]);
// NOTIFY
// characteristic.notify(notify[, callback(error)]); //true/false
// characteristic.on('notify', callback(state));
// characteristic.on('data', callback(data, isNotification));
// exit program stuff
var stdin = process.stdin;
// without this, we would only get streams once enter is pressed
stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();
// i don't want binary, do you?
stdin.setEncoding( 'utf8' );
// on any data into stdin
stdin.on( 'data', function( key ){
// ctrl-c ( end of text )
if ( key === '\u0003' ) {
setTimeout(function () {
process.exit();
}, 1000);
loggingOn=false;
console.log('Logging stopped');
lightoff();
}
// write the key to stdout all normal like
//process.stdout.write( key );
});