Make call back with >1 char (#383)

This commit is contained in:
Gordon Williams 2014-06-19 10:25:49 +01:00
parent 69d49566be
commit 004b07d7da
4 changed files with 34 additions and 52 deletions

View File

@ -23,6 +23,7 @@
Remove JSV_NAME flag - paving the way for more efficient variable storage 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) Store only 32 bit time for events (work out full 64 bits in event loop)
Increase event buffer size to 128 (from 64) 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];' 1v64 : Fix 'a=[2,3,4];delete a[1];'
Make sure parseInt("0x01",16)==1 and parseInt("0x01")==1 but parseInt("0b01",16)==0 Make sure parseInt("0x01",16)==1 and parseInt("0x01")==1 but parseInt("0b01",16)==0

View File

@ -221,6 +221,12 @@ bool jshHasEvents() {
return ioHead!=ioTail; 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 jshGetEventsUsed() {
int spaceUsed = (ioHead >= ioTail) ? ((int)ioHead-(int)ioTail) : /*or rolled*/((int)ioHead+IOBUFFERMASK+1-(int)ioTail); int spaceUsed = (ioHead >= ioTail) ? ((int)ioHead-(int)ioTail) : /*or rolled*/((int)ioHead+IOBUFFERMASK+1-(int)ioTail);
return spaceUsed; return spaceUsed;

View File

@ -100,6 +100,8 @@ static inline void jshPushIOCharEvents(IOEventFlags channel, char *data, unsigne
bool jshPopIOEvent(IOEvent *result); ///< returns true on success bool jshPopIOEvent(IOEvent *result); ///< returns true on success
/// Do we have any events pending? Will jshPopIOEvent return true? /// Do we have any events pending? Will jshPopIOEvent return true?
bool jshHasEvents(); 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 /// How many event blocks are left? compare this to IOBUFFERMASK
int jshGetEventsUsed(); int jshGetEventsUsed();

View File

@ -1283,71 +1283,44 @@ void jsiIdle() {
JsVar *callback = jsvFindChildFromString(usartClass, USART_CALLBACK_NAME, false); JsVar *callback = jsvFindChildFromString(usartClass, USART_CALLBACK_NAME, false);
if (callback) { if (callback) {
int i, c = IOEVENTFLAGS_GETCHARS(event.flags); /* 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
// Part of hackish solution to 7 bit support on STM32 * do it in the IRQ */
#ifdef STM32
unsigned char bytesize = 8; unsigned char bytesize = 8;
unsigned char parity = 0; JsVar *options = jsvObjectGetChild(usartClass, DEVICE_OPTIONS_NAME, 0);
JsVar *options = jsvObjectGetChild(usartClass, DEVICE_OPTIONS_NAME, 0); if(jsvIsObject(options))
if(jsvIsObject(options)) {
bytesize = (unsigned char)jsvGetIntegerAndUnLock(jsvObjectGetChild(options, "bytesize", 0)); 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); jsvUnLock(options);
#endif
for (i=0; i<c; i++) { JsVar *data = jsvNewWithFlags(JSV_OBJECT);
JsVar *data = jsvNewWithFlags(JSV_OBJECT); JsVar *stringData = jsvNewFromEmptyString();
if (data && stringData) {
JsvStringIterator it;
jsvStringIteratorNew(&it, stringData, 0);
if (data) { int i, chars = IOEVENTFLAGS_GETCHARS(event.flags);
JsVar *dataTime = jsvNewFromString("X"); while (chars) {
for (i=0;i<chars;i++) {
#ifdef STM32 char ch = (char)(event.data.chars[i] & ((1<<bytesize)-1)); // mask
if(bytesize == 7 && parity > 0) { jsvStringIteratorAppend(&it, ch);
dataTime->varData.str[0] = event.data.chars[i] & 0x7F;
} }
else if(bytesize == 8 && parity > 0) { // look down the stack and see if there is more data
dataTime->varData.str[0] = event.data.chars[i] & 0xFF; if (jshIsTopEvent(eventType)) {
} jshPopIOEvent(&event);
else { chars = IOEVENTFLAGS_GETCHARS(event.flags);
dataTime->varData.str[0] = event.data.chars[i]; } else
} chars = 0;
#else
dataTime->varData.str[0] = event.data.chars[i];
#endif
if (dataTime) jsvUnLock(jsvAddNamedChild(data, dataTime, "data"));
jsvUnLock(dataTime);
} }
jsvUnLock(jsvAddNamedChild(data, stringData, "data"));
// Now run the handler
if (!jsiExecuteEventCallback(callback, data, 0)) { if (!jsiExecuteEventCallback(callback, data, 0)) {
jsError("Error processing Serial data handler - removing it."); jsError("Error processing Serial data handler - removing it.");
jsvSetValueOfName(callback, 0); jsvSetValueOfName(callback, 0);
} }
jsvUnLock(data);
} }
jsvUnLock(stringData);
jsvUnLock(data);
} }
jsvUnLock(callback); jsvUnLock(callback);
jsvUnLock(usartClass); jsvUnLock(usartClass);