@AdaMan82, with the code you just mastered, you may very much enjoy lines 235..257 of the code in this conversation. The last 3 paragraphs of the text include the description.
If you want to run, study and observe yourself the section I'm talking about, upload the snippet below. After uploading, you enter in the console lg(b,n,"Tn") - with b = 0 or 1 and n = 0..9, for example: lg(0,5,"T5"), or lg(1,3,"T3").
It does not matter how many time and in what frequency you call lg(), the (message) string is logged in the console right away, the red/error LED1 - or green/ok LED2 - blinks (the message code as) Tn in Morse code as words - correctly timed - until all done. Since all timings are implemented w/ setTimeout(...), the code is not (noticeably) blocking any other code, even though (JS) execution is single threaded... This ***event drivenness *** of code execution is the beauty of Espruino vs. the ugliness of forced sequencing with blocking delays (by killing CPU cycles w/ NOPs) in infinite main loop driven code execution, such as in, for example, Arduino coding model (Application events, such as timeouts, can be emulated with checking for pre-calculated time replacing the delays, but the main loop is always going on, hence infinite... and consuming power... see Arduino and RTC using the 1Hz 'beat' of the RTC as a time base for implementing a time and event management).
Setting logging = false, you will see just the blinking.
Instead of using a queue (append to string), sequencing could also be solved using Promises ('time recursion').
var logging = true; // log activities to console (when an 'output' is connected)
var cds=""; // c-oded (breaks, led colors G/R, Ts and) d-igit-s - 'queue'
var cl=null; // ok/error green/red c-oded l-ed
var bts = {"L":300,"S":80,"P":120,"B":450}; // long/short-on/off b-link t-ime-s in [ms]
var bds = ["LLLLL", "SLLLL", "SSLLL", "SSSLL", "SSSSL", "SSSSS", "LSSSS", "LLSSS", "LLLSS", "LLLLS"]; // Long/Short coding of m-essage c-ode (mc), 0..9
function lg(ok,mc,m) { // l-o-g ok/err m-essage / LED blink m-essage c-ode
if (logging) console.log(m);
var l = cds.length; // = 0 if all blinking incl. Pause was done (queue empty)
cds += "BB"+((ok) ? "G" : "R")+"LB"+bds[mc]; // 'feeding' blinking queue
if (l === 0) bl(); // start blink/work queue on 1st entries/was empty
}
function bl() { // 'bl-ink' Long/Short/Break+Pause 'recursively'
var c = cds.charAt(0); // c-har defining next LED/time
if (c=="G") { cl = LED2; cds = cds.substr(1); c = cds.charAt(0); }
else if (c=="R") { cl = LED1; cds = cds.substr(1); c = cds.charAt(0); }
if (c!="B") cl.set(); // turn LED on when not Break
setTimeout(function(){ // for Long/Short/Break
cl.reset(); // turn LED off
setTimeout(function(){ // for Pause
cds = cds.substr(1); // remove just completed L/S/B+P time
if (cds.length > 0) setTimeout(bl,1); // blink until done
},bts.P); // Pause between Long and Short (and Break)
},bts[c]); // Long / Short (on), Break (stay off)
}
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.
@AdaMan82, with the code you just mastered, you may very much enjoy lines
235..257
of the code in this conversation. The last 3 paragraphs of the text include the description.If you want to run, study and observe yourself the section I'm talking about, upload the snippet below. After uploading, you enter in the console
lg(b,n,"Tn")
- with b = 0 or 1 and n = 0..9, for example:lg(0,5,"T5")
, orlg(1,3,"T3")
.It does not matter how many time and in what frequency you call
lg()
, the (message) string is logged in the console right away, the red/error LED1 - or green/ok LED2 - blinks (the message code as)Tn
in Morse code as words - correctly timed - until all done. Since all timings are implemented w/setTimeout(...)
, the code is not (noticeably) blocking any other code, even though (JS) execution is single threaded... This ***event drivenness *** of code execution is the beauty of Espruino vs. the ugliness of forced sequencing with blocking delays (by killing CPU cycles w/ NOPs) in infinite main loop driven code execution, such as in, for example, Arduino coding model (Application events, such as timeouts, can be emulated with checking for pre-calculated time replacing the delays, but the main loop is always going on, hence infinite... and consuming power... see Arduino and RTC using the 1Hz 'beat' of the RTC as a time base for implementing a time and event management).Setting
logging = false
, you will see just the blinking.Instead of using a queue (append to string), sequencing could also be solved using Promises ('time recursion').