From 004b07d7dab4bd53fbb1b494da4b21300efb51cc Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 19 Jun 2014 10:25:49 +0100 Subject: [PATCH] Make call back with >1 char (#383) --- ChangeLog | 1 + src/jsdevices.c | 6 ++++ src/jsdevices.h | 2 ++ src/jsinteractive.c | 77 +++++++++++++++------------------------------ 4 files changed, 34 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index 32e79ce74..b9b837274 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,6 +23,7 @@ Remove JSV_NAME flag - paving the way for more efficient variable storage Store only 32 bit time for events (work out full 64 bits in event loop) Increase event buffer size to 128 (from 64) + Make `Serial.onData` call back with >1 char (#383) 1v64 : Fix 'a=[2,3,4];delete a[1];' Make sure parseInt("0x01",16)==1 and parseInt("0x01")==1 but parseInt("0b01",16)==0 diff --git a/src/jsdevices.c b/src/jsdevices.c index 74bf0dee2..7f8a6886f 100644 --- a/src/jsdevices.c +++ b/src/jsdevices.c @@ -221,6 +221,12 @@ bool jshHasEvents() { return ioHead!=ioTail; } +/// Check if the top event is for the given device +bool jshIsTopEvent(IOEventFlags eventType) { + if (ioHead==ioTail) return false; + return IOEVENTFLAGS_GETTYPE(ioBuffer[ioTail].flags) == eventType; +} + int jshGetEventsUsed() { int spaceUsed = (ioHead >= ioTail) ? ((int)ioHead-(int)ioTail) : /*or rolled*/((int)ioHead+IOBUFFERMASK+1-(int)ioTail); return spaceUsed; diff --git a/src/jsdevices.h b/src/jsdevices.h index de0cad037..e42944551 100644 --- a/src/jsdevices.h +++ b/src/jsdevices.h @@ -100,6 +100,8 @@ static inline void jshPushIOCharEvents(IOEventFlags channel, char *data, unsigne bool jshPopIOEvent(IOEvent *result); ///< returns true on success /// Do we have any events pending? Will jshPopIOEvent return true? bool jshHasEvents(); +/// Check if the top event is for the given device +bool jshIsTopEvent(IOEventFlags eventType); /// How many event blocks are left? compare this to IOBUFFERMASK int jshGetEventsUsed(); diff --git a/src/jsinteractive.c b/src/jsinteractive.c index 3c3ebe04d..552ee0654 100644 --- a/src/jsinteractive.c +++ b/src/jsinteractive.c @@ -1283,71 +1283,44 @@ void jsiIdle() { JsVar *callback = jsvFindChildFromString(usartClass, USART_CALLBACK_NAME, false); if (callback) { - int i, c = IOEVENTFLAGS_GETCHARS(event.flags); - -// Part of hackish solution to 7 bit support on STM32 -#ifdef STM32 + /* work out byteSize. On STM32 we fake 7 bit, and it's easier to + * check the options and work out the masking here than it is to + * do it in the IRQ */ unsigned char bytesize = 8; - unsigned char parity = 0; - JsVar *options = jsvObjectGetChild(usartClass, DEVICE_OPTIONS_NAME, 0); - - if(jsvIsObject(options)) { + JsVar *options = jsvObjectGetChild(usartClass, DEVICE_OPTIONS_NAME, 0); + if(jsvIsObject(options)) bytesize = (unsigned char)jsvGetIntegerAndUnLock(jsvObjectGetChild(options, "bytesize", 0)); - JsVar *v = jsvObjectGetChild(options, "parity", 0); - - if(jsvIsString(v)) { - parity = 0xFF; - char s[8] = ""; - - jsvGetString(v, s, sizeof(s) - 1); - - if(!strcmp(s, "o") || !strcmp(s, "odd")) { - parity = 1; - } - else if(!strcmp(s, "e") || !strcmp(s, "even")) { - parity = 2; - } - } - else if(jsvIsInt(v)) { - parity = (unsigned char)jsvGetInteger(v); - } - - jsvUnLock(v); - } - jsvUnLock(options); -#endif - for (i=0; i 0) { - dataTime->varData.str[0] = event.data.chars[i] & 0x7F; + int i, chars = IOEVENTFLAGS_GETCHARS(event.flags); + while (chars) { + for (i=0;i 0) { - dataTime->varData.str[0] = event.data.chars[i] & 0xFF; - } - else { - dataTime->varData.str[0] = event.data.chars[i]; - } -#else - dataTime->varData.str[0] = event.data.chars[i]; -#endif - - if (dataTime) jsvUnLock(jsvAddNamedChild(data, dataTime, "data")); - jsvUnLock(dataTime); + // look down the stack and see if there is more data + if (jshIsTopEvent(eventType)) { + jshPopIOEvent(&event); + chars = IOEVENTFLAGS_GETCHARS(event.flags); + } else + chars = 0; } + jsvUnLock(jsvAddNamedChild(data, stringData, "data")); + // Now run the handler if (!jsiExecuteEventCallback(callback, data, 0)) { jsError("Error processing Serial data handler - removing it."); jsvSetValueOfName(callback, 0); } - jsvUnLock(data); } + jsvUnLock(stringData); + jsvUnLock(data); } jsvUnLock(callback); jsvUnLock(usartClass);