It's often not practical to make code that works both after direct upload and after save() and restart
Conclusion and recommendation: ...indeed... therefore, always choose the format that works for/after save()... to be on the safe side.
But this is a bit cumbersome while developing, because you need to save the uploaded code every time and do a power-cycle - un-plug and re-plug - to get the code started. To get around this and speed up development, use this stream lined development process.
1. Use function onInit() { ... } rather than E.on('init', function(){ ... });, and start your code after uploading by entering onInit() in the console / left hand pane of the Espruino Web IDE.
Having to enter onInit() in the console every time after upload of the code to start it is still somewhat cumbersome, therefore
Add - for development time - setTimeout(onInit,500); as last line in your code, which makes your overall code structure look like this:
// ... your application code
// ...
// ...
// initialization / start-up code
function onInit() {
// ...
}
// convenience line for dev phase; REMOVE for final save() and normal runtime
setTimeout(onInit,500);
This last line starts your code automatically after upload has completed in the same way as later on a power cycle will restart it on normal runtime after power cycle / reset. The timeout is added to allow - on the Espruino side - the upload process to complete and do so decoupled from your code start / execution.
3. REMOVE this last line setTimeout(onInit,500); when entering the final development phase where you test save() and power-cycling.
Bonus for using setTimeout(onInit, ...:
You can pass development time parameters
setTimeout(onInit, 500, true, "otherRuntimeParm_mode"); // enable debug, etc.
that overwrite runtime parameters - for example for debugging w/ console output while developing - which automatically go away for the normal runtime. Your overall code may then look something like this complete code template good for code running after both direct upload AND save() :
Code Template for development AND runtime:
var dbg = false; // ***d***e***b***u***g*** of for normal runtime
var mode = "normal"; // other runtime parameter
// ...
// your app code using global ```dbg``` and ```mode``` runtime vars to
// control debug/normal runtime behavior, for ex. console output, etc.
// ...
if (dbg) console.log("....");
// ...
// initialization / start-up code
function onInit(dbgParm, otherParm_mode, ...) {
// accepting / overwriting operation parms while under development
dbg = dbgParm;
mode = (mode === undefined) ? mode : otherParm_mode;
// other initialization / startup code
// ...
}
// convenience line for dev phase; REMOVE for final save() and normal runtime
setTimeout(onInit, 500, true, "otherRuntimeParm_mode"); // enable debug, etc.
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.
This tutorial was triggered by the forum post about code not running after save() and power cycle and related response, which states:
Conclusion and recommendation: ...indeed... therefore, always choose the format that works for/after
save()
... to be on the safe side.But this is a bit cumbersome while developing, because you need to save the uploaded code every time and do a power-cycle - un-plug and re-plug - to get the code started. To get around this and speed up development, use this stream lined development process.
function onInit() { ... }
rather thanE.on('init', function(){ ... });
, and start your code after uploading by enteringonInit()
in the console / left hand pane of the Espruino Web IDE.Having to enter
onInit()
in the console every time after upload of the code to start it is still somewhat cumbersome, thereforeAdd - for development time -
setTimeout(onInit,500);
as last line in your code, which makes your overall code structure look like this:This last line starts your code automatically after upload has completed in the same way as later on a power cycle will restart it on normal runtime after power cycle / reset. The timeout is added to allow - on the Espruino side - the upload process to complete and do so decoupled from your code start / execution.
setTimeout(onInit,500);
when entering the final development phase where you testsave()
and power-cycling.Bonus for using
setTimeout(onInit, ...
:You can pass development time parameters
that overwrite runtime parameters - for example for debugging w/ console output while developing - which automatically go away for the normal runtime. Your overall code may then look something like this complete code template good for code running after both direct upload AND save() :
Code Template for development AND runtime: