@@ -51,9 +51,12 @@ Or more advanced usage with control of the connection
51
51
ChangeLog:
52
52
53
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
54
+ 1.02: Puck.write/eval now wait until they have received data with a newline in (if requested)
55
+ and return the LAST received line, rather than the first (as before)
56
+ Added configurable timeouts for write/etc
57
+ 1.01: Raise default Chunk Size to 20
58
+ Auto-adjust chunk size up if we receive >20 bytes in a packet
59
+ 1.00: Added Promises to write/eval
57
60
58
61
*/
59
62
( function ( root , factory ) {
@@ -328,31 +331,37 @@ ChangeLog:
328
331
function onWritten ( ) {
329
332
if ( callbackNewline ) {
330
333
connection . cb = function ( d ) {
331
- var newLineIdx = connection . received . indexOf ( "\n" ) ;
332
- if ( newLineIdx >= 0 ) {
333
- var l = connection . received . substr ( 0 , newLineIdx ) ;
334
- connection . received = connection . received . substr ( newLineIdx + 1 ) ;
335
- connection . cb = undefined ;
336
- if ( cbTimeout ) clearTimeout ( cbTimeout ) ;
337
- cbTimeout = undefined ;
338
- if ( callback )
339
- callback ( l ) ;
340
- isBusy = false ;
341
- handleQueue ( ) ;
342
- }
334
+ // if we hadn't got a newline this time (even if we had one before)
335
+ // then ignore it (https://github.com/espruino/BangleApps/issues/3771)
336
+ if ( ! d . includes ( "\n" ) ) return ;
337
+ // now return the LAST received non-empty line
338
+ var lines = connection . received . split ( "\n" ) ;
339
+ var idx = lines . length - 1 ;
340
+ while ( lines [ idx ] . trim ( ) . length == 0 && idx > 0 ) idx -- ; // skip over empty lines
341
+ var line = lines . splice ( idx , 1 ) [ 0 ] ; // get the non-empty line
342
+ connection . received = lines . join ( "\n" ) ; // put back other lines
343
+ // remove handler and return
344
+ connection . cb = undefined ;
345
+ if ( cbTimeout ) clearTimeout ( cbTimeout ) ;
346
+ cbTimeout = undefined ;
347
+ if ( callback )
348
+ callback ( line ) ;
349
+ isBusy = false ;
350
+ handleQueue ( ) ;
343
351
} ;
344
352
}
345
353
// wait for any received data if we have a callback...
346
- var maxTime = 300 ; // 30 sec - Max time we wait in total, even if getting data
347
- var dataWaitTime = callbackNewline ? 100 /*10 sec if waiting for newline*/ : 3 /*300ms*/ ;
354
+ var maxTime = puck . timeoutMax ; // Max time we wait in total, even if getting data
355
+ var dataWaitTime = callbackNewline ? puck . timeoutNewline : puck . timeoutNormal ;
348
356
var maxDataTime = dataWaitTime ; // max time we wait after having received data
357
+ const POLLINTERVAL = 100 ;
349
358
cbTimeout = setTimeout ( function timeout ( ) {
350
359
cbTimeout = undefined ;
351
- if ( maxTime ) maxTime -- ;
352
- if ( maxDataTime ) maxDataTime -- ;
360
+ if ( maxTime > 0 ) maxTime -= POLLINTERVAL ;
361
+ if ( maxDataTime > 0 ) maxDataTime -= POLLINTERVAL ;
353
362
if ( connection . hadData ) maxDataTime = dataWaitTime ;
354
- if ( maxDataTime && maxTime ) {
355
- cbTimeout = setTimeout ( timeout , 100 ) ;
363
+ if ( maxDataTime > 0 && maxTime > 0 ) {
364
+ cbTimeout = setTimeout ( timeout , POLLINTERVAL ) ;
356
365
} else {
357
366
connection . cb = undefined ;
358
367
if ( callback )
@@ -362,7 +371,7 @@ ChangeLog:
362
371
connection . received = "" ;
363
372
}
364
373
connection . hadData = false ;
365
- } , 100 ) ;
374
+ } , POLLINTERVAL ) ;
366
375
}
367
376
368
377
if ( connection && ( connection . isOpen || connection . isOpening ) ) {
@@ -397,6 +406,7 @@ ChangeLog:
397
406
// ----------------------------------------------------------
398
407
399
408
var puck = {
409
+ version : "1.02" ,
400
410
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
401
411
debug : 1 ,
402
412
/** When we receive more than 20 bytes, should we increase the chunk size we use
@@ -405,6 +415,12 @@ ChangeLog:
405
415
increaseMTU : true ,
406
416
/// Should we use flow control? Default is true
407
417
flowControl : true ,
418
+ /// timeout (in ms) in .write when waiting for any data to return
419
+ timeoutNormal : 300 ,
420
+ /// timeout (in ms) in .write/.eval when waiting for a newline
421
+ timeoutNewline : 10000 ,
422
+ /// timeout (in ms) to wait at most
423
+ timeoutMax : 30000 ,
408
424
/// Used internally to write log information - you can replace this with your own function
409
425
log : function ( level , s ) { if ( level <= this . debug ) console . log ( "<BLE> " + s ) } ,
410
426
/// Called with the current send progress or undefined when done - you can replace this with your own function
@@ -418,7 +434,6 @@ ChangeLog:
418
434
write : write ,
419
435
/// Evaluate an expression and call cb with the result. Creates a connection if it doesn't exist
420
436
eval : function ( expr , cb ) {
421
-
422
437
const response = write ( '\x10Bluetooth.println(JSON.stringify(' + expr + '))\n' , true )
423
438
. then ( function ( d ) {
424
439
try {
@@ -428,8 +443,6 @@ ChangeLog:
428
443
return Promise . reject ( d ) ;
429
444
}
430
445
} ) ;
431
-
432
-
433
446
if ( cb ) {
434
447
return void response . then ( cb , ( err ) => cb ( null , err ) ) ;
435
448
} else {
0 commit comments