How to have ONE codebase that works after both direct upload and save() with power cycle (run in no-save AND save mode) #1199
Unanswered
espruino-discuss2
asked this question in
Tutorials
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Posted at 2017-02-09 by @allObjects
This tutorial was triggered by the forum post about [code not running after save() and power cycle]
(http://forum.espruino.com/comments/13437285/) 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, thereforesetTimeout(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:
Beta Was this translation helpful? Give feedback.
All reactions