run JavaScript code from SD Card

Posted on
  • Him,
    is it possible to load script files from SD card and execute the code/functions in the current environment?

    This would be really helpfull to add additional effect code via WiFi and store it for later use on SD card ;)

  • Yes, I think it's as easy as eval(require("fs").readFileSync()) - eval should use the current scope.

    Maybe a 'nicer' way of doing it is:

    var myEffect = new Function("any","args","callback",require­("fs").readFileSync());
    // file might contain: "console.log(any, args); callback();"
    myEffect("Hello","World", function() { 
     ...
    });
    

    So then you pass in what you want as arguments, and you load the code in as a function - so it's easy to just delete that function later if you want to free the memory.

    ... maybe wait for 1v80 before you try writing to the SD card though - @JumJum discovered a bug in 1v73 - 1v79 that could corrupt SD card contents.

  • @Gordon you are the man! Thx for the fast reply and the awesome short snippet.
    Really good idea to put content to function and execute them directly.
    Do you have any hints for me, when the 1v80 will release? Or did the bugfix already exsists on GitHub?

    Before I implement this stuff, I have to wait for the ESP12 board from OSHPark and try out the WiFi communication. Next step is to add small library to stream JavaScript content to Pico. Last step is to store this content (effect functions) to SD for later use...

  • Sounds great!

    You can pick up one of the GitHub builds from here

    Choose the right binary from the page above, and copy/paste the link into the flasher page in the IDE. That should have the SD card fix...

  • I just try to flash this one:
    http://www.espruino.com/binaries/git/com­mits/a11edb58f9300cf95742ac0611cbabe9ad6­7ed4c/espruino_1v80_pico_1r3_wiznet.bin
    everything worked on flashing progress.
    But now my demo-code brings some errors:

    function flash_led1(led_on) {
      digitalWrite(LED1, led_on);
    }
    function pulse_led2() {
      digitalPulse(LED2, 1, 150);
    }
    // when button is pressed
    setWatch(function(e) {
      console.log("Press at "+e.time);
      digitalWrite(LED2, 0);
      flash_led1(1); 
    }, BTN, {repeat: true, edge: "rising", debounce: 50});
    // when button is pressed
    setWatch(function(e) {
      console.log("Release at "+e.time);
      flash_led1(0);
      pulse_led2(); 
    }, BTN, {repeat: true, edge: "falling", debounce: 50});
    

    Errors:

    Press at
    Uncaught Error: Field or method "time" does not already exist, and can't create it on undefined
     at line 2 col 28
      console.log("Press at "+e.time);
                                ^
    in function called from system
    Release at
    Uncaught Error: Field or method "time" does not already exist, and can't create it on undefined
     at line 2 col 30
      console.log("Release at "+e.time);
                                  ^
    in function called from system
    
  • This is a bug with debounce.

    See: https://github.com/espruino/Espruino/iss­ues/537

  • thx @DrAzzy. I can modify the demo code to call getTime() instead...

  • Yes, sorry - I'll try and get that fixed for the next release.

  • Fixed now, it'll be in the 1v80 release. Available from http://www.espruino.com/binaries/git/com­mits/03bc4de4ed9fee0a7863c0e55040ad79f2a­2fa35 in a few minutes

  • @Gordon you are Mr.FixitFast ;)
    I'll try this later - I'm currently at work...

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

run JavaScript code from SD Card

Posted by Avatar for Jorgen @Jorgen

Actions