Skip to content

Commit

Permalink
properly handle STRING_DATA responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed Jul 31, 2013
1 parent 483188c commit 1f5db9f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/firmata.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,14 @@ SYSEX_RESPONSE[I2C_REPLY] = function(board) {
*/

SYSEX_RESPONSE[STRING_DATA] = function(board) {
var string = new Buffer(board.currentBuffer.slice(2, -1)).toString('utf8').replace(/\0/g, '');
var bytes = board.currentBuffer.slice(2, -1);
var data = new Buffer(bytes.length / 2);
for (var i = 0, length = bytes.length; i < length; i += 2) {
var code = (bytes[i] & 0x7F) | ((bytes[i + 1] & 0x7F) << 7);
// debug('<-%s:%d-%d', code, bytes[i] & 0x7F, (bytes[i + 1] & 0x7F) << 7);
data[i / 2] = code;
}
var string = data.toString('utf8');
debug('receiving a %d-sized string="%s"', string.length, string);
board.emit('string', string);
};
Expand Down Expand Up @@ -557,6 +564,7 @@ Board.prototype.sendString = function(string) {
data.push(START_SYSEX);
data.push(STRING_DATA);
for (var i = 0, length = bytes.length; i < length; i++) {
// debug('->%s:%d,%d', bytes[i], bytes[i] & 0x7F, (bytes[i] >> 7) & 0x7F)
data.push(bytes[i] & 0x7F);
data.push((bytes[i] >> 7) & 0x7F);
}
Expand Down

0 comments on commit 1f5db9f

Please sign in to comment.