Skip to content

Commit be545f8

Browse files
committed
now issue ble written event for proper IDE throttling
1 parent b3e3c0f commit be545f8

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ on a Puck.js BLE UART connection with:
190190
/ble/data/c7:f9:36:dd:b0:ca/nus/nus_rx => "23\r\n"
191191
```
192192

193+
Once a `/ble/write/DEVICE/SERVICE/CHARACTERISTIC` has been executed, a `/ble/written/DEVICE/SERVICE/CHARACTERISTIC` packet will be sent in response.
194+
193195
**You can also gather historical data over MQTT - see the `History` section
194196
below.**
195197

lib/connect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function setNotBusy() {
191191

192192

193193
/* Write to the given device */
194-
exports.write = function(device, service, characteristic, data) {
194+
exports.write = function(device, service, characteristic, data, callback) {
195195
if (isBusy) {
196196
queue.push(function() { exports.write(device,service,characteristic,data); });
197197
return;
@@ -207,6 +207,7 @@ exports.write = function(device, service, characteristic, data) {
207207
writeToCharacteristic(char, util.obj2buf(data), function() {
208208
log(connection.name+": Written.");
209209
setNotBusy();
210+
if (callback) callback();
210211
});
211212
});
212213
});

lib/mqttclient.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ client.on('message', function (topic, message) {
7474
var device = discovery.inRange[id].peripheral;
7575
var service = attributes.lookup(path[3].toLowerCase());
7676
var charc = attributes.lookup(path[4].toLowerCase());
77-
connect.write(device, service, charc, convertMessage(message));
77+
connect.write(device, service, charc, convertMessage(message), function() {
78+
client.publish("/ble/written/"+topic.substr("/ble/write/".length), message);
79+
});
7880
} else {
7981
log("Write to "+id+" but not in range");
8082
}

www/ide.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var websocket;
1717
var pingInterval;
1818
var onPong;
19+
var onDataWritten;
1920
// IDE IFrame
2021
var IDE = EspruinoIDE(document.getElementById('ideframe'));
2122
IDE.onready = function(data) {
@@ -65,6 +66,7 @@
6566
// Subscribe to data
6667
websocket.subscribe("/ble/data/"+deviceAddress+"/nus/nus_rx");
6768
websocket.subscribe("/ble/pong/"+deviceAddress);
69+
websocket.subscribe("/ble/written/"+deviceAddress+"/nus/nus_tx");
6870
// request notifications of serial RX
6971
sendMessage("/ble/notify/"+deviceAddress+"/nus/nus_rx","");
7072
// send a ping so when we get a pong we know we're connected
@@ -76,8 +78,8 @@
7678
};
7779
IDE.onwritecb = function(data, callback) {
7880
if (!deviceAddress) return;
81+
onDataWritten = callback;
7982
sendMessage("/ble/write/"+deviceAddress+"/nus/nus_tx", data);
80-
setTimeout(callback, 100); // force a delay when sending data
8183
};
8284
IDE.ondisconnect = function(data) {
8385
closeSerial();
@@ -104,6 +106,9 @@
104106
// When we get a 'pong' back, call this - so we know we're connected
105107
if (onPong) onPong();
106108
onPong = undefined;
109+
} else if (deviceAddress && message.destinationName == "/ble/written/"+deviceAddress+"/nus/nus_tx") {
110+
if (onDataWritten) onDataWritten();
111+
onDataWritten = undefined;
107112
} else if (message.destinationName.substr(0,15)=="/ble/advertise/") {
108113
// advertising for new devices
109114
var address = message.destinationName.substr(15);

0 commit comments

Comments
 (0)