Loading local modules on Pico

Posted on
  • Hi,

    I'm trying to load a local module in the Pico. I have setup the sandbox and copied my module to the modules folder. When I use require('module') i get the following error:

    ERROR: SD card must be setup with E.connectSDCard first

    Then the module is not found and more error occur. Any clue what I am doing wrong?

    Thanks,
    Mike

  • For starters, have a look at: http://www.espruino.com/File+IO

    From the espruino docs:

    On the original Espruino board there's a Micro SD card slot built-in,
    or on the Espruino Pico you'll have to wire a card up externally and
    then tell Espruino about it with E.connectSDCard:

    Once you have wired a SD card to the appropriate pins on your Pico, tell the Pico about it, like so:

    // Wire up up MOSI, MISO, SCK and CS pins (along with 3.3v and GND)
    SPI1.setup({mosi:B5, miso:B4, sck:B3});
    E.connectSDCard(SPI1, B6 /*CS*/);
    // see what's on the device
    console.log(require("fs").readdirSync())­;
    
  • Thanks, I'll take a look at that.

    I'm still a little confused as to why the SD card needs to be connected. If the modules are cached on the Pico why is the SD card needed?

  • @user63585 It should work without the sd card. Just make sure the directory is in the right path and the module file names match. I can't remember if it loads from online or the projects folder first and have noticed when wifi connection is slow that it will throw the SD card error on me from time to time. Also you are sending from the right side to the WEB IDE?

  • I do not need an SD card to use local modules. Suspect error in the paths, naming of files, or permissions

  • Where did you write the require-statement?

    require("xxx") only works on the right side of the Espruino Web IDE. Written on the left it will produce your mentioned error

    ERROR: SD card must be setup with E.connectSDCard first

  • @luwar, that was it. No errors when loading it from the right side. Thanks.

  • 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:

    1. 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.
    2. 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","moduleSo­urceCodeAsString");. 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...

    1. Enter Modules.cached("myModule","return 'valueReturnedFromMyModule';");
    2. 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.*)
    ):

    1. 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() {```

      } }; })();```

    2. 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 literalValueOrVariable evaluates 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'.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Loading local modules on Pico

Posted by Avatar for user63585 @user63585

Actions