The Web IDE's upload (of the code in the Web IDE editor / pane on the right) with the Upload/send to Board Button does as first task a pattern search for all require("moduleNameAsStringLiteral") in the source code and uploads the modules into Espruino's module cache. It looks first in sandbox's folder, then - when connected - on the Web in http://www.espruino.com/modules folder. If the moduleNameAsStringLiteral is a url (starts w/ http...), it looks right away at that URLocation.
The code that runs and where it runs on upload's first step:
Search of modules required by the source, retrieving the module source code, and sending it to Espruino is Web IDE code and it runs in the Browser.
Receiving the module source code, executing / interpreting it, and storing it in the cache is Espruino firmware and it runs on the (Espruino) Board.
Part of later code is available for use in the console / left pane of Web IDE: see Modules reference, most important Modules.addCached("moduleName","moduleSourceCodeAsString");. In other words: if you enter so into the console / left pane of the Web IDE, it will upload your module into Espruino's module cache. Try it yourself: in console...
Enter Modules.cached("myModule","return 'valueReturnedFromMyModule';");
Enter require("myModule");
(Scroll to the right if line(s) seem to be cut off.)
The response in the console upon 2nd entry is: valueReturnedFromMyModule (which in this case is a string).
At runtime (disconnected from the IDE or entered in the console / left pane of the IDE), Espruino board and firmware is the context and looks first in its module cache for the module and second on a connected SD card. Latter allows to dynamically - at runtime - load modules into module cache (and unload from module cache) modules as needed to overcome eventual memory (RAM) constraints.
If you use require(...) in your application code like this var mNX = "moduleNameX"; require(mNX); - require with a variable, the upload' search task can not detect the need for the module, is not retrieving it, and is not sending it to the board. When it then comes to execution of the require statement, Espruino looks for the module as described in previous paragraphs.
Now you may conclude that you can send module code to the board in the console with Modules.addCached(...); and then upload application code like require(stringVariable); from the Web IDE editor. For obvious reasons, the execution will complain about missing module/SD card, because before uploading from the editor, Espruino is clearing the module cache (as well as the RAM, where the code first goes, before Espruino is told to save() into the flash EEPROM where is will 'survive' powercycles and resets by reloading the RAM form flash EEPROM (and run onInit() and E.on("init",...) registered functions after such events).
But what you can do is send your code directly to Espruino in the console by (copying and) pasting the source there. It is not the most convenient way but it works, as we partially shown above, and will show here again (with a slightly different sample code ...Scroll to the right if line(s) seem to be cut off.*)
):
Enter into console with copy-paste below lines:
this.pin = pin;pinMode(pin, "output"); this.switchOff(); }; var p = S.prototype;```
digitalWrite(this.pin, b || (typeof b === "undefined")); }; p.switchOff = function() {```
} }; })();```
Enter into console with copy-paste below lines:
setTimeout("red.switchOff()"), 5000);```
This creates a Switch-module and a Switch on any pin you connect to. In latter entry, it creates a switch on the red LED (LED1), turns it on for a second, then off. If you (are quick enough to) enter red.isOn(), it returns true if it is switched on.
The Switch 'understands the messages' (or 'the protocol') isOn(), switchOff(), and switchOn(). switchOn(literalValueOrVariable) is the same as switchOn() when literalValueOrVariableevaluates to true, such as non-zero number, not empty string, and - of course - true, etc.; otherwise, it is the same as switchOff`().
*PS: Code partially verified. Will complete verification when back in 'lab'.
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.
The Web IDE's upload (of the code in the Web IDE editor / pane on the right) with the Upload/send to Board Button does as first task a pattern search for all
require("moduleNameAsStringLiteral")
in the source code and uploads the modules into Espruino's module cache. It looks first in sandbox's folder, then - when connected - on the Web inhttp://www.espruino.com/modules
folder. If themoduleNameAsStringLiteral
is a url (starts w/http...
), it looks right away at that URLocation.The code that runs and where it runs on upload's first step:
Part of later code is available for use in the console / left pane of Web IDE: see Modules reference, most important
Modules.addCached("moduleName","moduleSourceCodeAsString");
. In other words: if you enter so into the console / left pane of the Web IDE, it will upload your module into Espruino's module cache. Try it yourself: in console...Modules.cached("myModule","return 'valueReturnedFromMyModule';");
require("myModule");
(Scroll to the right if line(s) seem to be cut off.)
The response in the console upon 2nd entry is:
valueReturnedFromMyModule
(which in this case is a string).At runtime (disconnected from the IDE or entered in the console / left pane of the IDE), Espruino board and firmware is the context and looks first in its module cache for the module and second on a connected SD card. Latter allows to dynamically - at runtime - load modules into module cache (and unload from module cache) modules as needed to overcome eventual memory (RAM) constraints.
If you use
require(...)
in your application code like thisvar mNX = "moduleNameX"; require(mNX);
- require with a variable, the upload' search task can not detect the need for the module, is not retrieving it, and is not sending it to the board. When it then comes to execution of the require statement, Espruino looks for the module as described in previous paragraphs.Now you may conclude that you can send module code to the board in the console with
Modules.addCached(...);
and then upload application code likerequire(stringVariable);
from the Web IDE editor. For obvious reasons, the execution will complain about missing module/SD card, because before uploading from the editor, Espruino is clearing the module cache (as well as the RAM, where the code first goes, before Espruino is told tosave()
into the flash EEPROM where is will 'survive' powercycles and resets by reloading the RAM form flash EEPROM (and runonInit()
andE.on("init",...)
registered functions after such events).But what you can do is send your code directly to Espruino in the console by (copying and) pasting the source there. It is not the most convenient way but it works, as we partially shown above, and will show here again (with a slightly different sample code ...Scroll to the right if line(s) seem to be cut off.*)
):
Enter into console with copy-paste below lines:
this.pin = pin;pinMode(pin, "output"); this.switchOff(); }; var p = S.prototype;```
digitalWrite(this.pin, b || (typeof b === "undefined")); }; p.switchOff = function() {```
} }; })();```
Enter into console with copy-paste below lines:
setTimeout("red.switchOff()"), 5000);```
This creates a Switch-module and a Switch on any pin you connect to. In latter entry, it creates a switch on the red LED (LED1), turns it on for a second, then off. If you (are quick enough to) enter
red.isOn()
, it returns true if it is switched on.The
Switch
'understands the messages' (or 'the protocol')isOn()
,switchOff()
, andswitchOn()
.switchOn(literalValueOrVariable)
is the same asswitchOn()
whenliteralValueOrVariable
evaluates to true, such as non-zero number, not empty string, and - of course - true, etc.; otherwise, it is the same asswitchOff`()
.*PS: Code partially verified. Will complete verification when back in 'lab'.