Skip to content

Commit 6e861b9

Browse files
committed
updated puck.js lib
1 parent 3f15c5f commit 6e861b9

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

Diff for: js/puck.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
--------------------------------------------------------------------
3-
Puck.js BLE Interface library
4-
Copyright 2020 Gordon Williams ([email protected])
3+
Puck.js BLE Interface library for Nordic UART
4+
Copyright 2021 Gordon Williams ([email protected])
5+
https://github.com/espruino/EspruinoWebTools
56
--------------------------------------------------------------------
67
This Source Code Form is subject to the terms of the Mozilla Public
78
License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -47,6 +48,13 @@ Or more advanced usage with control of the connection
4748
});
4849
});
4950
51+
ChangeLog:
52+
53+
...
54+
1v00 : Added Promises to write/eval
55+
1v01 : Raise default Chunk Size to 20
56+
Auto-adjust chunk size up if we receive >20 bytes in a packet
57+
5058
*/
5159
(function (root, factory) {
5260
if (typeof define === 'function' && define.amd) {
@@ -88,15 +96,15 @@ Or more advanced usage with control of the connection
8896
window.location = "https://itunes.apple.com/us/app/webble/id1193531073";
8997
} else {
9098
if (confirm("This Web Browser doesn't support Web Bluetooth.\nPlease click Ok to see instructions for enabling it."))
91-
window.location = "https://www.espruino.com/Puck.js+Quick+Start";
99+
window.location = "https://www.espruino.com/Quick+Start+BLE#with-web-bluetooth";
92100
}
93101
return false;
94102
}
95103

96104
var NORDIC_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
97105
var NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
98106
var NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
99-
var CHUNKSIZE = 16;
107+
var DEFAULT_CHUNKSIZE = 20;
100108

101109
function log(level, s) {
102110
if (puck.log) puck.log(level, s);
@@ -140,6 +148,7 @@ Or more advanced usage with control of the connection
140148
var rxCharacteristic;
141149
var txDataQueue = [];
142150
var flowControlXOFF = false;
151+
var chunkSize = DEFAULT_CHUNKSIZE;
143152

144153
connection.close = function() {
145154
connection.isOpening = false;
@@ -154,6 +163,7 @@ Or more advanced usage with control of the connection
154163
btServer = undefined;
155164
txCharacteristic = undefined;
156165
rxCharacteristic = undefined;
166+
chunkSize = DEFAULT_CHUNKSIZE;
157167
}
158168
};
159169

@@ -173,12 +183,12 @@ Or more advanced usage with control of the connection
173183
}
174184
var txItem = txDataQueue[0];
175185
puck.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
176-
if (txItem.data.length <= CHUNKSIZE) {
186+
if (txItem.data.length <= chunkSize) {
177187
chunk = txItem.data;
178188
txItem.data = undefined;
179189
} else {
180-
chunk = txItem.data.substr(0,CHUNKSIZE);
181-
txItem.data = txItem.data.substr(CHUNKSIZE);
190+
chunk = txItem.data.substr(0,chunkSize);
191+
txItem.data = txItem.data.substr(chunkSize);
182192
}
183193
connection.txInProgress = true;
184194
log(2, "Sending "+ JSON.stringify(chunk));
@@ -203,10 +213,9 @@ Or more advanced usage with control of the connection
203213
filters:[
204214
{ namePrefix: 'Puck.js' },
205215
{ namePrefix: 'Pixl.js' },
216+
{ namePrefix: 'Jolt.js' },
206217
{ namePrefix: 'MDBT42Q' },
207-
{ namePrefix: 'RuuviTag' },
208-
{ namePrefix: 'iTracker' },
209-
{ namePrefix: 'Thingy' },
218+
{ namePrefix: 'Bangle.js' },
210219
{ namePrefix: 'Espruino' },
211220
{ services: [ NORDIC_SERVICE ] }
212221
], optionalServices: [ NORDIC_SERVICE ]}).then(function(device) {
@@ -240,6 +249,10 @@ Or more advanced usage with control of the connection
240249
rxCharacteristic.addEventListener('characteristicvaluechanged', function(event) {
241250
var dataview = event.target.value;
242251
var data = ab2str(dataview.buffer);
252+
if (puck.increaseMTU && (data.length > chunkSize)) {
253+
log(2, "Received packet of length "+data.length+", increasing chunk size");
254+
chunkSize = data.length;
255+
}
243256
if (puck.flowControl) {
244257
for (var i=0;i<data.length;i++) {
245258
var ch = data.charCodeAt(i);
@@ -386,6 +399,10 @@ Or more advanced usage with control of the connection
386399
var puck = {
387400
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
388401
debug : 1,
402+
/** When we receive more than 20 bytes, should we increase the chunk size we use
403+
for writing to match it? Normally this is fine but it seems some phones have
404+
a broken bluetooth implementation that doesn't allow it. */
405+
increaseMTU : true,
389406
/// Should we use flow control? Default is true
390407
flowControl : true,
391408
/// Used internally to write log information - you can replace this with your own function

0 commit comments

Comments
 (0)