@GeekBot, asynch has its challenges, especially in a single tasked world as JavaScript lives in.
Agreed, the example has not the best introductory comment and is a bit terse... a better - more self explanatory and comprehensive - version would be something like that:
// (Try to) Send a simple command and wait for a response within 1 second timeout
var t0 = getTime();
at.cmd("AT+EO\r\n", 1 * 1000, function(d) {
if (d===undefined) { // we timed out!
console.log("For what ever reason device did not respond within 1000 second.");
} else { // we got response within timeout time
console.log("Device response is " + d + " and was within " + (getTime() - t0) + " seconds.");
}
});
Written in a different form:
// definition of a callback function that takes one argument and expects t0 as global var.
var callback = function(responseData) {
if (responseData===undefined) { // we timed out!
console.log("For what ever reason device did not respond within 1000 second.");
} else { // we got response within timeout time
console.log( "Device responded within " + (getTime() - t0) + " seconds with: "
+responseData );
}
};
// (Try to) Send simple command and wait for a response within 1 second timeout
var t0 = getTime();
at.cmd("AT+EO\r\n", 1 * 1000, callback);
To be clear, the 1 second is true only if there is no other command going on, because the AT library can handle multiple commands which are stored in the at.waiting array - like a 'FIFO' memory/queue: first come first served - like standing in line at a grocery store (never mind the joke of 'LOST' memory: last out, still there...).
When it is the turn of the command request - as passed with .cmd(...), at sets a timeout as specified by the request and sends the command to the device. When the device responds within the timeout time, at calls the callback with the response value (and clears the still pending timeout); and if the device does not respond within the set timeout time, at calls the callback with undefined value.
A callback function is something like a (deferred) instruction ('callback') to your friend-and-helper ('at') while telling him/her 'to go for something' (command). When she/he gets that something, your friend-and-helper will follow the given instructions...
Another illustration is: you leaving a or your callback number when the called person does not take - more precisely - miss the call. When the called person will see the missed call and callback number, the person will call (you) back the call(back)number and give (you) the opportunity to say what (you) wanted to (be) said in the first place... (at least you/we hope that will happen... on multiple levels...). The timeout in this example? - What about: After that some time (timeout) of the call, you will go to the bar of your choice to get a drink without your friend's input (or company).
I hope this helps somehow... if not, tell me more about the part you would like more information about...
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@GeekBot, asynch has its challenges, especially in a single tasked world as JavaScript lives in.
Agreed, the example has not the best introductory comment and is a bit terse... a better - more self explanatory and comprehensive - version would be something like that:
Written in a different form:
To be clear, the 1 second is true only if there is no other command going on, because the AT library can handle multiple commands which are stored in the
at.waiting
array - like a 'FIFO' memory/queue: first come first served - like standing in line at a grocery store (never mind the joke of 'LOST' memory: last out, still there...).When it is the turn of the command request - as passed with
.cmd(...)
,at
sets a timeout as specified by the request and sends the command to the device. When the device responds within the timeout time,at
calls the callback with the response value (and clears the still pending timeout); and if the device does not respond within the set timeout time,at
calls the callback withundefined
value.A callback function is something like a (deferred) instruction ('callback') to your friend-and-helper ('at') while telling him/her 'to go for something' (command). When she/he gets that something, your friend-and-helper will follow the given instructions...
Another illustration is: you leaving a or your callback number when the called person does not take - more precisely - miss the call. When the called person will see the missed call and callback number, the person will call (you) back the call(back)number and give (you) the opportunity to say what (you) wanted to (be) said in the first place... (at least you/we hope that will happen... on multiple levels...). The timeout in this example? - What about: After that some time (timeout) of the call, you will go to the bar of your choice to get a drink without your friend's input (or company).
I hope this helps somehow... if not, tell me more about the part you would like more information about...