Skip to content

Commit a816743

Browse files
committed
Add Serial.flush() to allow us to wait for transmission on a device to finish
1 parent 4485ec3 commit a816743

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ESP32: Advertising name is now 'Espruino abcd'
2626
ESP32: BLUETOOTH UART IS ENABLED BY DEFAULT - disable with `NRF.setServices({},{uart:false})`
2727
Bangle.js: GPS event now works even if only GGA NMEA events are enabled (which you might do for speed)
28+
Add Serial.flush() to allow us to wait for transmission on a device to finish
2829

2930
2v17 : Bangle.js: When reading file info from a filename table, do it in blocks of 8 (20% faster file search)
3031
Bangle.js2: Increase flash buffer size from 16->32 bytes (5% performance increase)

src/jsdevices.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,10 @@ IOEventFlags jshGetDeviceToTransmit() {
284284
}
285285

286286
/**
287-
* Try and get a character for transmission.
287+
* Try and get a character for transmission on a device.
288288
* \return The next byte to transmit or -1 if there is none.
289289
*/
290-
int jshGetCharToTransmit(
291-
IOEventFlags device // The device being looked at for a transmission.
292-
) {
290+
int jshGetCharToTransmit(IOEventFlags device) {
293291
if (DEVICE_HAS_DEVICE_STATE(device)) {
294292
volatile JshSerialDeviceState *deviceState = &jshSerialDeviceStates[TO_SERIAL_DEVICE_STATE(device)];
295293
if ((*deviceState)&SDS_XOFF_PENDING) {
@@ -324,12 +322,32 @@ int jshGetCharToTransmit(
324322
return -1; // no data :(
325323
}
326324

325+
/// Wait for all data in the transmit queue to be written
327326
void jshTransmitFlush() {
328327
jsiSetBusy(BUSY_TRANSMIT, true);
329328
while (jshHasTransmitData()) ; // wait for send to finish
330329
jsiSetBusy(BUSY_TRANSMIT, false);
331330
}
332331

332+
/// Wait for all data in the transmit queue to be written for a specific device
333+
void jshTransmitFlushDevice(IOEventFlags device) {
334+
jsiSetBusy(BUSY_TRANSMIT, true);
335+
bool deviceHasData = false;
336+
do {
337+
deviceHasData = false;
338+
// Check TX queue to see if there is any data to send
339+
unsigned char tempTail = txTail;
340+
while (txHead != tempTail) {
341+
if (IOEVENTFLAGS_GETTYPE(txBuffer[tempTail].flags) == device) {
342+
deviceHasData = true;
343+
break;
344+
}
345+
tempTail = (unsigned char)((tempTail+1)&TXBUFFERMASK);
346+
}
347+
} while (deviceHasData);
348+
jsiSetBusy(BUSY_TRANSMIT, false);
349+
}
350+
333351
/**
334352
* Discard all the data waiting for transmission.
335353
*/

src/jsdevices.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ void jshTransmit(IOEventFlags device, unsigned char data);
217217
void jshTransmitPrintf(IOEventFlags device, const char *fmt, ...);
218218
/// Wait for transmit to finish
219219
void jshTransmitFlush();
220+
/// Wait for all data in the transmit queue to be written for a specific device
221+
void jshTransmitFlushDevice(IOEventFlags device);
220222
/// Clear everything from a device
221223
void jshTransmitClearDevice(IOEventFlags device);
222224
/// Move all output from one device to another

src/jswrap_serial.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,19 @@ Return a string containing characters that have been received
539539
}
540540
Pipe this USART to a stream (an object with a 'write' method)
541541
*/
542+
543+
544+
/*JSON{
545+
"type" : "method",
546+
"class" : "Serial",
547+
"name" : "flush",
548+
"ifndef" : "SAVE_ON_FLASH",
549+
"generate" : "jswrap_serial_flush"
550+
}
551+
Flush this serial stream (pause execution until all data has been sent)
552+
*/
553+
void jswrap_serial_flush(JsVar *parent) {
554+
IOEventFlags device = jsiGetDeviceFromClass(parent);
555+
if (device == EV_NONE) return;
556+
jshTransmitFlushDevice(device);
557+
}

src/jswrap_serial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ void jswrap_serial_print(JsVar *parent, JsVar *str);
2525
void jswrap_serial_println(JsVar *parent, JsVar *str);
2626
void jswrap_serial_write(JsVar *parent, JsVar *data);
2727
void jswrap_serial_inject(JsVar *parent, JsVar *args);
28+
void jswrap_serial_flush(JsVar *parent);
2829

2930
#endif // JSWRAP_SERIAL_H_

0 commit comments

Comments
 (0)