Skip to content

Commit 92dddaf

Browse files
committed
Fixed receive message handling in the Scratch extension file.
1 parent eb16db3 commit 92dddaf

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

scratch2/extensions/gnuRadioExtension.js

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
var txMsgStart = false;
1313
var rxMsgStart = false;
1414
var radioRunning = false;
15-
var rxMsgBufSize = 255;
15+
var rxMsgBufSize = 1024;
16+
var rxMsgBufThreshold = 768;
1617
var rxMsgBuffer = Buffer.alloc(rxMsgBufSize);
1718
var rxMsgOffset = 0;
1819
var messageBitRate = 1200;
@@ -111,44 +112,24 @@
111112
if (rxMsgPipe == null) {
112113
this._errorMessage("Radio Not Running");
113114
callback("");
115+
} else if (rxMsgOffset >= rxMsgBufThreshold) {
116+
this._processRxMessage(callback);
114117
} else {
115118
fs.read(rxMsgPipe, rxMsgBuffer, rxMsgOffset, rxMsgBufSize-rxMsgOffset, null,
116119
function(err, len, buf) {
117120
if (err == null) {
118121
if (len == 0) {
119-
this._receiveMessage(callback);
122+
setTimeout(this._receiveMessage, 1000, callback);
120123
} else {
121-
var rxMsgString;
122124
rxMsgOffset += len;
123-
for (var i = 0; i < rxMsgOffset; i++) {
124-
125-
// On detecting an end of line character, copy
126-
// the line to the received message string and
127-
// shift the residual buffer contents down.
128-
if (buf[i] == 10) {
129-
rxMsgString = buf.toString('ascii', 0, i);
130-
if (i == rxMsgOffset-1) {
131-
rxMsgOffset = 0;
132-
} else {
133-
buf.copy(buf, 0, i+1, rxMsgOffset-i-1);
134-
rxMsgOffset -= i+1;
135-
}
136-
break;
137-
}
138-
}
139-
140-
// Invoke callback or retry.
141-
if (rxMsgString == null) {
142-
if (rxMsgOffset >= rxMsgBufSize) {
143-
rxMsgOffset -= 1;
144-
}
145-
this._receiveMessage(callback);
146-
} else {
147-
callback(rxMsgString);
148-
}
125+
this._processRxMessage(callback);
149126
}
150127
} else if (err.code == "EAGAIN") {
151-
this._receiveMessage(callback);
128+
if (rxMsgOffset > 0) {
129+
this._processRxMessage(callback);
130+
} else {
131+
setTimeout(this._receiveMessage, 1000, callback);
132+
}
152133
} else {
153134
this._errorMessage("Rx Message Error : " + err.code);
154135
callback("");
@@ -158,6 +139,37 @@
158139
}
159140
}
160141

142+
// Process a received message.
143+
this._processRxMessage = function(callback) {
144+
var rxMsgString;
145+
for (var i = 0; i < rxMsgOffset; i++) {
146+
147+
// On detecting an end of line character, copy the line to
148+
// the received message string and shift the residual buffer
149+
// contents down.
150+
if (rxMsgBuffer[i] == 10) {
151+
rxMsgString = rxMsgBuffer.toString('ascii', 0, i);
152+
if (i == rxMsgOffset-1) {
153+
rxMsgOffset = 0;
154+
} else {
155+
rxMsgBuffer.copy(rxMsgBuffer, 0, i+1, rxMsgOffset);
156+
rxMsgOffset -= i+1;
157+
}
158+
break;
159+
}
160+
}
161+
162+
// Invoke callback or retry.
163+
if (rxMsgString == null) {
164+
if (rxMsgOffset >= rxMsgBufSize) {
165+
rxMsgOffset -= 1;
166+
}
167+
setTimeout(this._receiveMessage, 1000, callback);
168+
} else {
169+
callback(rxMsgString);
170+
}
171+
}
172+
161173
// Cleanup function when the extension is unloaded.
162174
ext._shutdown = function() {
163175
if (commandPipe != null) {
@@ -226,6 +238,9 @@
226238
txMsgPipe = null;
227239
}
228240
if (rxMsgPipe != null) {
241+
// TODO: This discards the local message buffer but we don't
242+
// currently discard any residual input FIFO file contents.
243+
rxMsgOffset = 0;
229244
fs.closeSync(rxMsgPipe);
230245
rxMsgPipe = null;
231246
}

0 commit comments

Comments
 (0)