-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathledButton.js
executable file
·121 lines (97 loc) · 4.12 KB
/
ledButton.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
// RFduino Node Example
//
// Read the button state and control the LED on the RFduino RGB Pushbutton shield
// http://www.rfduino.com/rfd22122-rgb-pushbutton-shield-accessory-board.html
// https://github.com/RFduino/RFduino/blob/master/libraries/RFduinoBLE/examples/LedButton/LedButton.ino
//
// (c) 2014 Don Coleman
var noble = require('noble'),
rfduino = require('./rfduino'),
_ = require('underscore');
// TODO why does this need to be wrapped?
var stop = function() {
noble.stopScanning();
};
noble.on('scanStart', function() {
console.log('Scan started');
setTimeout(stop, 5000);
});
noble.on('scanStop', function() {
console.log('Scan stopped');
});
var onDeviceDiscoveredCallback = function(peripheral) {
console.log('\nDiscovered Peripherial ' + peripheral.uuid);
// This example needs an RFduino running the *ledbtn* service
if (_.contains(peripheral.advertisement.serviceUuids, rfduino.serviceUUID)) {
console.log('RFduino is advertising \'' + rfduino.getAdvertisedServiceName(peripheral) + '\' service.');
peripheral.on('connect', function() {
peripheral.discoverServices();
});
peripheral.on('disconnect', function() {
console.log('Disconnected');
});
peripheral.on('servicesDiscover', function(services) {
var rfduinoService;
for (var i = 0; i < services.length; i++) {
if (services[i].uuid === rfduino.serviceUUID) {
rfduinoService = services[i];
break;
}
}
if (!rfduinoService) {
console.log('Couldn\'t find the RFduino service.');
return;
}
rfduinoService.on('characteristicsDiscover', function(characteristics) {
console.log('Discovered ' + characteristics.length + ' service characteristics');
var receiveCharacteristic;
var sendCharacteristic;
for (var i = 0; i < characteristics.length; i++) {
if (characteristics[i].uuid === rfduino.receiveCharacteristicUUID) {
receiveCharacteristic = characteristics[i];
break;
}
}
// receives the state of the button press on the rfduino shield
if (receiveCharacteristic) {
receiveCharacteristic.on('read', function(data, isNotification) {
var buttonPressed = (data.readUInt8(0) === 1);
if (buttonPressed) {
console.log("Button pressed");
} else {
console.log("Button released");
}
});
console.log('Subscribing for button notifications');
receiveCharacteristic.notify(true);
}
// TODO combine with loop above
for (var i = 0; i < characteristics.length; i++) {
if (characteristics[i].uuid === rfduino.sendCharacteristicUUID) {
sendCharacteristic = characteristics[i];
break;
}
}
// toggle the LED on and off once a second
var buffer = new Buffer(1);
setInterval(function() {
// toggle between 0 and 1
if (buffer[0] === 0x1) {
buffer[0] = 0x0;
} else {
buffer[0] = 0x1;
}
sendCharacteristic.write(buffer, false)
}, 1000);
});
rfduinoService.discoverCharacteristics();
});
peripheral.connect();
}
};
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning([rfduino.serviceUUID], false);
}
});
noble.on('discover', onDeviceDiscoveredCallback);