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
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

View File

@ -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;

View File

@ -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();

View File

@ -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<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) {
JsVar *dataTime = jsvNewFromString("X");
#ifdef STM32
if(bytesize == 7 && parity > 0) {
dataTime->varData.str[0] = event.data.chars[i] & 0x7F;
int i, chars = IOEVENTFLAGS_GETCHARS(event.flags);
while (chars) {
for (i=0;i<chars;i++) {
char ch = (char)(event.data.chars[i] & ((1<<bytesize)-1)); // mask
jsvStringIteratorAppend(&it, ch);
}
else if(bytesize == 8 && parity > 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);