New Espruino IDE

Posted on
  • I've just pushed out a new IDE (0.68.5) - across Chrome, Native, Website and NPM.

    There are a few nice improvements:

    • There's a new intro screen which looks a bit better, and which will show you up to date news.
    • If a device is running new firmware (cutting edge, or 1v96 when released) then it'll report back which modules are included in it - and these will be used when figuring out which new modules to get off the internet.
    • The longstanding issue with Blockly blocks being misshapen on first startup is now fixed, and you can choose which blocks are shown in settings with a nicer UI.
    • The Assembler now supports templated strings, so you can do one big multiline blob of assembly
    • The Assembler itself supports a few more operations and has one or two ops fixed
    • The compiler service for compiled functions has had a whole bunch of improvements, and is now available for Nordic devices (like Puck.js) with 1v96 (or cutting edge) firmware as well - just right now not through the website-based IDE until the compiler service gets upgraded to HTTPS.

    Leaving the best till last:

    You can now dynamically add C functions into Espruino, as part of JS code

    As a totally random example, here's a function to calculate Fibonacci numbers, written in C, JavaScript, and compiled JavaScript:

    var c = E.compiledC(`
    // int fibo(int)
    
    int fibo(int n){
     if(n <= 1){
      return n;
     }
     int fibo = 1;
     int fiboPrev = 1;
     for(int i = 2; i < n; ++i){
      int temp = fibo;
      fibo += fiboPrev;
      fiboPrev = temp;
     }
     return fibo;
    }
    `);
    
    
    function fibo(n){
     if(n <= 1){
      return n;
     }
     var fibo = 1;
     var fiboPrev = 1;
     for(var i = 2; i < n; ++i){
      var temp = fibo;
      fibo += fiboPrev;
      fiboPrev = temp;
     }
     return fibo;
    }
    
    function compiled_fibo(n){
      "compiled";
     if(n <= 1){
      return n;
     }
     var fibo = 1;
     var fiboPrev = 1;
     for(var i = 2; i < n; ++i){
      var temp = fibo;
      fibo += fiboPrev;
      fiboPrev = temp;
     }
     return fibo;
    }
    

    This is still extremely early days, so expect some things not to work. However importantly you can upload multiple C functions that can interact... You just put the prototype (as used for the Assembler) for each function you want to export at the top:

    var c = E.compiledC(`
    // void press(bool)
    // int get()
    
    int data;
    
    void press(bool state){
      data++;
    }
    
    int get() {
     int r = data;
     data = 0;
     return r;
    }
    `);
    
    setWatch(c.press, BTN1, {repeat:true, edge:"both", irq:true});
    
    setInterval(function() {
      print(c.get());
    }, 10000);
    

    In the example above, a C function is called from an interrupt when a pin changes state (so runs very quickly), and it then changes a variable which can then be read back with normal JavaScript.

    You can even access data stored in flat strings:

    var c = E.compiledC(`
    // int sum(int, int)
    
    int sum(int len, unsigned char *data){
      int s = 0;
      while (len--)
        s += *(data++);
      return s;
    }
    `);
    
    
    var str  = E.toString("\1\2\3\4\5\6"); // create a flat string
    print(c.sum(str.length, E.getAddressOf(str,true)));
    

    Hope you have fun with this! It could potentially be used to create modules for all kind of things - for example MP3 decoders, realtime audio effects, or accessing on-chip peripherals that aren't exposed in Espruino normally.

  • Neat! Best of both worlds! ...and answer to some performance challenges.

  • Oh shit, that's something I didn't see coming! Inline motherfucking c!

  • It's been possible for a while but I never enabled it because there didn't seem to be a nice way of mixing C with JS - but templated strings are perfect as backtick and dollar aren't part of the C language, so you can just paste code in verbatim - you just have to be a bit careful about using them in strings.

  • Very nice and usefull.
    New post opend in main forum.

  • Post above was moved.

    IF ANYONE HAS TROUBLE WITH THIS, PLEASE POST IN THE MAIN FORUM - I'd prefer this news thread didn't turn into a massive troubleshooting thread :)

  • I'm using Raspberry Pi and EspruinoHub to connect to Puck, if I understand it correctly I should update EspruinoHub to get the new IDE on my browser (Chrome). What's the safest way to do that without breaking anything? Thanks.

  • Yes - if you followed the GitHub instructions to install then on the Pi you want:

    cd EspruinoHub
    git pull
    cd EspruinoWebIDE
    git pull
    cd EspruinoTools
    git pull
    

    Then restart and you're sorted!

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

New Espruino IDE

Posted by Avatar for Gordon @Gordon

Actions