Skip to content

Commit

Permalink
Merge pull request #174 from vaishnav98/master
Browse files Browse the repository at this point in the history
Support nodestyle callbacks
  • Loading branch information
jadonk authored Jun 22, 2018
2 parents 47454f7 + 0e15de4 commit 481525c
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 56 deletions.
16 changes: 13 additions & 3 deletions src/bonescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ function _onSocketIOLoaded(host, port, socketio) {
if (typeof host == 'undefined') host = '___INSERT_HOST___';
if (typeof port == 'undefined') port = 80;
if (typeof socketio == 'undefined' && typeof io != 'undefined') socketio = io;
var socket = socketio.connect('http://' + host + ':' + port);
var socket;
if (typeof host == 'string')
socket = socketio('http://' + host + ':' + port);
else
socket = socketio('___INSERT_HOST___', {
port: port
});
socket.on('require', getRequireData);
socket.on('bonescript', _seqcall);
socket.on('connect', _bonescript.on.connect);
Expand Down Expand Up @@ -83,6 +89,7 @@ function _onSocketIOLoaded(host, port, socketio) {
' if(callback) {\n' +
' _bonescript._callbacks[_bonescript._seqnum] = callback;\n' +
' calldata.seq = _bonescript._seqnum;\n' +
' calldata.length = callback.length;\n' +
' _bonescript._seqnum++;\n' +
' }\n' +
' socket.emit("' + m.module + '$' + m.data[x].name + '", calldata);\n' +
Expand All @@ -102,14 +109,17 @@ function _onSocketIOLoaded(host, port, socketio) {
function _seqcall(data) {
if ((typeof data.seq != 'number') || (typeof _bonescript._callbacks[data.seq] != 'function'))
throw "Invalid callback message received: " + JSON.stringify(data);
_bonescript._callbacks[data.seq](data);
if (_bonescript._callbacks[data.seq].length == 1)
_bonescript._callbacks[data.seq](data);
else
_bonescript._callbacks[data.seq](data.err, data.resp);
if (data.oneshot) delete _bonescript._callbacks[data.seq];
}

// Require must be synchronous to be able to return data structures and
// functions and therefore cannot call socket.io. All exported modules must
// be exported ahead of time.
function myrequire(module) {
var myrequire = function (module) {
if (typeof _bonescript == 'undefined')
throw 'No BoneScript modules are not currently available';
if (typeof _bonescript.modules[module] == 'undefined')
Expand Down
32 changes: 26 additions & 6 deletions src/hw_mainline.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,29 @@ var readPinMux = function (pin, mode, callback) {
if (err) {
mode.err = 'readPinctrl error: ' + err;
if (debug) winston.debug(mode.err);
callback(mode);
if (callback.length == 1) {
winston.warning("single argument callbacks will be deprecated.please use node-style error-first callbacks: callback(err,response)");
callback(mode);
} else
callback(mode.err, data);
}
mode = parse.modeFromPinctrl(data, muxRegOffset, 0x44e10800, mode);
callback(mode);
if (callback.length == 1) {
winston.warning("single argument callbacks will be deprecated.please use node-style error-first callbacks: callback(err,response)");
callback(mode);
} else
callback(null, mode);
};
var tryPinctrl = function (exists) {
if (exists) {
fs.readFile(pinctrlFile, 'utf8', readPinctrl);
} else {
if (debug) winston.debug('getPinMode(' + pin.key + '): no valid mux data');
callback(mode);
if (callback.length == 1) {
winston.warning("single argument callbacks will be deprecated.please use node-style error-first callbacks: callback(err,response)");
callback(mode);
} else
callback('readPinMux error: no valid mux data', mode);
}
};
if (callback) {
Expand Down Expand Up @@ -223,7 +235,11 @@ var readGPIOValue = function (pin, resp, callback) {
winston.error(resp.err);
}
resp.value = parseInt(data, 2);
callback(resp);
if (callback.length == 1) {
winston.warning("single argument callbacks will be deprecated.please use node-style error-first callbacks: callback(err,response)");
callback(resp);
} else
callback(resp.err, resp.value);
};
fs.readFile(gpioFile, readFile);
return (true);
Expand Down Expand Up @@ -259,7 +275,11 @@ var readAIN = function (pin, resp, callback) {
winston.error(resp.err);
}
resp.value = parseInt(data, 10) / maxValue;
callback(resp);
if (callback.length == 1) {
winston.warning("single argument callbacks will be deprecated.please use node-style error-first callbacks: callback(err,response)");
callback(resp);
} else
callback(resp.err, resp.value);
};
fs.readFile(ainFile, readFile);
return (resp);
Expand Down Expand Up @@ -337,7 +357,7 @@ var writePWMFreqAndValue = function (pin, pwm, freq, value, resp, callback) {
else
tryAgain = false;
callback(null); //async.until requires an err first format callback &
} else { //if there is an error iteration stops, so neglect the error if EACCES thrown
} else { //if there is an error iteration stops, so neglect the error if EACCES thrown
tryAgain = false;
callback(ex2);
}
Expand Down
16 changes: 13 additions & 3 deletions src/hw_simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ var readGPIODirection = function (n, gpio) {
var readPinMux = function (pin, mode, callback) {
winston.info('readPinMux(' + [pin.key] + ')');
if (callback) {
callback(mode);
if (callback.length == 1)
callback(mode);
else
callback(null, mode);
}
return (mode);
};
Expand Down Expand Up @@ -60,7 +63,11 @@ var writeGPIOValue = function (pin, value, callback) {
var readGPIOValue = function (pin, resp, callback) {
winston.info('readGPIOValue(' + [pin.key] + ')');
if (callback) {
callback(0);
resp.value = 0;
if (callback.length == 1)
callback(resp);
else
callback(null, resp.value);
return (true);
}
resp.value = 0;
Expand All @@ -76,7 +83,10 @@ var readAIN = function (pin, resp, callback) {
winston.info('readAIN(' + [pin.key] + ')');
resp.value = 0;
if (callback) {
callback(resp);
if (callback.length == 1)
callback(resp);
else
callback(null, resp.value);
}
return (resp);
};
Expand Down
Loading

0 comments on commit 481525c

Please sign in to comment.