Skip to content

Commit

Permalink
better debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
nitriques committed Oct 30, 2014
1 parent 49ab44b commit 3d90952
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/tosr0x.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ var resolved = function (err, rej, callback) {
return !err;
};

var log = function (t, cmd, msg) {
if (!t._options.debug) {
return;
}
if (!msg) {
msg = cmd;
cmd = 'log';
}
if (cmd !== 'dir') {
msg = '[Tosr0x] ' + msg;
}
console[cmd](msg);
};

var T = exports.Tosr0x = function (uri, options) {
this._serial = null;
this._uri = uri || '/dev/ttyUSB0';
Expand Down Expand Up @@ -115,7 +129,7 @@ T.create = function (port, options) {
if (!port) {
return T.fromPortScan(options);
} else {
return new Promise(function (res) {
return p(function (res) {
process.nextTick(function () {
res(new T(port, options));
});
Expand All @@ -139,10 +153,9 @@ var bufferIsNullTerminated = function (buffer) {
var dataListener = function (t) {
return function dataListener(data) {
var newReads = [];
console.dir(data);
console.dir(t.reads);

t.buffer += data.toString();
log(t, 'Received ' + data.toString('hex'));

_.forEach(t.reads, function (r) {
var callR = function (data) {
Expand All @@ -157,24 +170,20 @@ var dataListener = function (t) {
callR(t.buffer);
}
} else {
t.buffer = '';
callR(data);
}
});

console.dir(t.reads);
t.reads = newReads;
if (bufferIsNullTerminated(t.buffer)) {
t.buffer = '';
}

console.dir(t.reads);
};
};

var errorListener = function (t) {
return function errorListener(err) {
//t._serial = null;
//t._state = {};
throw err;
};
};
Expand All @@ -184,7 +193,7 @@ var closeListener = function (t) {
t._serial = null;
t._state = {};
t.reads = [];
console.log('Port closed.');
log(t, 'Port closed.');
};
};

Expand Down Expand Up @@ -228,7 +237,7 @@ T.prototype.close = function (callback) {
var t = this;
return p(function close(res, rej) {
if (!t._serial) {
console.error('Already closed.');
log(t, 'error', 'Already closed.');
// soft fail
call(callback, null, t);
res(t);
Expand Down Expand Up @@ -264,10 +273,9 @@ T.prototype.send = function (cmd, callback) {
var t = this;
var s = this._serial;
return ptime(function send(res, rej) {
console.log('------------------------------');
console.log('Sending ' + cmd);
log(t, '------------------------------');
log(t, 'Sending ' + cmd);
s.write(cmd, function (err, bytesWritten) {
console.log(bytesWritten + ' bytes sent');
if (resolved(err, rej, callback)) {
s.drain(function (err) {
if (resolved(err, rej, callback)) {
Expand Down

0 comments on commit 3d90952

Please sign in to comment.