You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • 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.

About

Avatar for Gordon @Gordon started