Installing an NPM package on Bangle.js 2?

Posted on
  • For a project I would like to run a discrete wavelets tranform on my Bangle.js 2 watch.

    Because of that, I am wondering if it is possible to install an existing npm package (namely https://www.npmjs.com/package/discrete-w­avelets ) in Espruino, to make its functionality available within my JS project.

    Concrete questions I am pondering about:

    • Is it possible at all to install an npm package in Espruino?
    • Which npm package size is okay for the Bangle.js 2 watch - especially when it comes to memory?
    • Can the code of the npm package be stored in and run from the Bangle.js 2 flash memory, instead of RAM? How would I achieve this (or prevent that it moves to RAM for execution)?
    • Could I precompile the npm JS code (to C, to binary), to make it run faster?

    Thanks in advance for any help and hints! :-)

  • I would grab the unified version from cdn and put it in the espruino ide and see what happens. Usual approach is to fetch the library as a single javascript file and save a copy in the bangle repo, e.g. see suncalc.js

  • Thanks a lot!

    How would I find "the unified version" and which CDN would I use? Also, how would I fetch the library as a single JS file? Sorry, I am a bit of a newbie with this ...

    I noticed that my library in question (https://www.npmjs.com/package/discrete-w­avelets) also contains TS code. In another post (https://forum.espruino.com/conversations­/341987/) it was mentioned that TS needs to be built/compiled before uploading it to Bangle. I am wondering what would be the easiest way to achieve that?

  • I think I found what you meant. The npm website for the package mentions a CDN option (https://cdn.jsdelivr.net/npm/discrete-wa­velets@5/dist/discrete-wavelets.umd.min.­js) which seems to be the minified, single-file version of that module. :-)

    I will try to get this running as described by you.

  • Indeed. Also if you trop the .min from the name you get a human readable version:
    https://cdn.jsdelivr.net/npm/discrete-wa­velets@5/dist/discrete-wavelets.umd.js

  • Thank you, that worked really well! 🙂

    In case anybody else is looking for it, here is what I did:

    • Download UMD file for the npm module in question. E.g. https://cdn.jsdelivr.net/npm/discrete-wa­velets@5/dist/discrete-wavelets.umd.js
    • (I learnt: UMD = Universal Module Definition, patterns for JavaScript modules that work everywhere.)
    • For testing, store this file in Flash on the watch in Device Storage, using the Web IDE.
    • In Web IDE, create a sample code file to the right, referencing this module file and using its functionality. Run that.

    Example for the above module:

    // Upload discrete-wavelets.umd.js to Bangle local storage
    Number.isInteger = it => isFinite(it) && Math.floor(it) === it; // To replace missing Number class in Espruino
    var wt = require("discrete-wavelets.umd.js"); // load from local storage
    var coeffs = wt.dwt([1, 2, 3, 4], 'haar');
    console.log(coeffs);
    // expected output: Array [[2.1213203435596425, 4.9497474683058326], [-0.7071067811865475, -0.7071067811865475]]
    

    When using the library with a real-world sample of 250 values (not just [1,2,3,4]), I seem to get a timeout problem ("Prompt not detected - upload failed"). But I look into solving this using the advice from https://forum.espruino.com/conversations­/289212/ .

  • When using the library with a real-world sample of 250 values (not just [1,2,3,4]), I seem to get a timeout problem ("Prompt not detected - upload failed")

    Yes, that's just that it's taking a while to execute - and when you upload via the IDE it checks that after the upload completes the device responds within a certain time.

    If you can upload the minified file (discrete-wavelets.umd.min.js) instead, that will run a bit faster... And looking at the code there it's quite simple and there are likely quite a few things you could do to get it running more efficiently (like telling Espruino to keep functions that get called often in RAM, and maybe even asking it to JIT compile some) if you do need the extra speed.

  • Thank you for the ideas, Gordon! :-)

    I actually looked into the minified file fist, but found the renamed functions rather confusing. Checking out the map file attached didn't make this smoother, at least not at first glance.

    I also searched for "semi-minified" options, to minify but keep function names, but could not find such a tool yet.

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

Installing an NPM package on Bangle.js 2?

Posted by Avatar for user157902 @user157902

Actions