-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoblePuck.js
129 lines (113 loc) · 3.36 KB
/
noblePuck.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
var noble = require('noble');
var NORDIC_SERVICE = "6e400001b5a3f393e0a9e50e24dcca9e";
var NORDIC_TX = "6e400002b5a3f393e0a9e50e24dcca9e";
var NORDIC_RX = "6e400003b5a3f393e0a9e50e24dcca9e";
var CHUNKSIZE = 16;
var txCharacteristic;
var rxCharacteristic;
var txDataQueue = [];
var txInProgress = false;
var puckId = '[PUCK_ID]';
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning();
} else {
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
console.log('peripheral found with id: ' + peripheral.id);
if (peripheral.id === puckId) {
noble.stopScanning();
connectToPeriphal(peripheral);
}
});
function connectToPeriphal(peripheral) {
peripheral.connect(function(err) {
peripheral.discoverServices([NORDIC_SERVICE], function(err, services) {
services.forEach(function(service) {
console.log('found service:', service.uuid);
service.discoverCharacteristics([], function(err, characteristics) {
characteristics.forEach(function(characteristic) {
console.log('found characteristic:', characteristic.uuid);
if (NORDIC_TX == characteristic.uuid) {
txCharacteristic = characteristic;
}
if (NORDIC_RX == characteristic.uuid) {
rxCharacteristic = characteristic;
}
});
if (rxCharacteristic && txCharacteristic) {
readPuckInfos();
} else {
console.log('missing characteristics');
}
});
});
});
});
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function readPuckInfos() {
eval('E.getTemperature()\n', function(data) {
console.log('temp: ', data, '°C');
});
eval('Puck.getBatteryPercentage()\n', function(data) {
console.log('battery: ', data, '%');
});
}
function write(data, callback) {
if (data) {
txDataQueue.push({data:data,callback:callback});
}
if (!txInProgress) {
writeChunk();
}
}
function writeChunk() {
var chunk;
if (!txDataQueue.length) {
return;
}
var txItem = txDataQueue[0];
if (txItem.data.length <= CHUNKSIZE) {
chunk = txItem.data;
txItem.data = undefined;
} else {
chunk = txItem.data.substr(0,CHUNKSIZE);
txItem.data = txItem.data.substr(CHUNKSIZE);
}
txInProgress = true;
txCharacteristic.write(new Buffer(str2ab(chunk)), false, function(err){
if (!err) {
if (!txItem.data) {
txDataQueue.shift();
if (txItem.callback) {
rxCharacteristic.read(function(error, data) {
var dataString = data.toString();
var dataArray = dataString.split("\r\n");
if(dataArray.length > 0) {
var result = dataArray[1].replace('=', '');
txItem.callback(result);
}
});
}
}
txInProgress = false;
writeChunk();
}
});
}
function eval(data, callback) {
write(data, callback);
}
}