UNPKG

stk500

Version:

Fully javascript stk500v1 programmer. Allows you to program Arduinos straight from node (or browser for that matter). No more avrdude system calls or using the arduino IDE.

48 lines (45 loc) 1.27 kB
var Statics = require('./statics'); var startingBytes = [ Statics.Resp_STK_INSYNC ]; module.exports = function (stream, timeout, responseLength, callback) { var buffer = Buffer.alloc(0); var started = false; var timeoutId = null; var handleChunk = function (data) { var index = 0; while (!started && index < data.length) { var byte = data[index]; if (startingBytes.indexOf(byte) !== -1) { data = data.slice(index, data.length - index); started = true; } index++; } if (started) { buffer = Buffer.concat([buffer, data]); } if (buffer.length > responseLength) { // or ignore after return finished(new Error('buffer overflow '+buffer.length+' > '+responseLength)); } if (buffer.length == responseLength) { finished(); } }; var finished = function (err) { if (timeoutId) { clearTimeout(timeoutId); } // VALIDATE TERMINAL BYTE? stream.removeListener('data', handleChunk); callback(err, buffer); }; if (timeout && timeout > 0) { timeoutId = setTimeout(function () { timeoutId = null; finished(new Error('receiveData timeout after ' + timeout + 'ms')); }, timeout); } stream.on('data', handleChunk); };