How to make a C++ library available in Espruino?

Posted on
  • I was inspired by this (https://www.espruino.com/Bangle.js+EdgeI­mpulse) project, running tflite models from Edge Impulse on my Bangle.js 2 watch.

    I would like to take this a step further and also run preprocessing code that can be added to Edge Impulse projects ("DSP block"), not just the tflite model. Edge Impulse offers to export the complete inference code (DSP and model processing) as a C++ library - optimised for embedded devices. Details see https://docs.edgeimpulse.com/docs/run-in­ference/cpp-library/deploy-your-model-as­-a-c-library .

    My question is: What is the easiest way to make a C/C++ library available in Espruino? I read in the Espruino docs that Inline C is available, also recompiling the firmware, but somehow I do not understand what the steps are (or if it is even feasible). Maybe there is an example of how to add a custom C/C++ library to Espruino that I could follow?

    I am also not sure how to call functions/methods exposed by such a library. Using Inline C, or do I need to write a JS wrapper?

    Any thoughts are highly appreciated! :-)

  • Hi - you probably want to look at:

    Although if you're going to do that with Edge Impulse then you're not going to be using the built-in Tensorflow so you'll probably want to disable that so you don't have two versions!

    If you just have DSP code that runs first, you probably want to look at whether Inline C will do what you need

  • Hi Gordon, thanks a lot! I will work through the examples. :-)

    Just for understanding: How would I disable the built-in Tensorflow module, if it becomes necessary?

    Running only the DSP code (and using Espruino's built-in Tensorflow module) would be an option that I would also prefer. However, the DSP code from EI uses libraries, partly with C++. I could not figure out yet if it would be possible to "inline" lenghty C/C++ code into Espruino and if Espruino compiler can compile C++ code, too? Maybe merge the code from EI into one file beforehand (as per https://www.reddit.com/r/cpp/comments/j2­hg59/tool_to_merge_multiple_cc_files_int­o_a_single/ )?

  • I could not figure out yet if it would be possible to "inline" lenghty C/C++ code into Espruino

    Inline C is meant for short bits of code because the code+data is linked into small binary blob that runs from RAM. There is not that much RAM available for large code. It can be abused a lot and there could be hacks to the Inline C compiler to link it in different way so that it runs code from flash but nobody did that. Also Inline C is easier to use if you pass just simple types or pointers to flat byte arrays via (up to 4) parameters. Using JS variables/objects from inline C is a bit more complicated. All that is easier when code is built as part of Espruino firmware so for something that is not short and relatively simple linking the library to the firmware is better way and would take less space. And BTW the inline C source is actually passed to c++ compiler so c++ syntax in inline C works. The compiled javascript https://www.espruino.com/Compilation is translating JS into C++ and then compiling it. Here is reference counting class stolen from it https://gist.github.com/fanoush/4a5dcf77­7503461297cedf7e21e3c6b3#file-swd-js-L46­ and used as part of Inline C https://gist.github.com/fanoush/4a5dcf77­7503461297cedf7e21e3c6b3#file-swd-js-L12­5

    Anyway, all the magic of calling native code is done by E.nativeCall() which is pretty generic so you can build your own compiler and linker and as long as you get your executable binary into ram or internal flash of the device you can run it.

    and if Espruino compiler can compile C++ code, too?

    tensorflow is c++ and is used as part of Espruino build, check https://github.com/espruino/Espruino/tre­e/master/libs/tensorflow

  • Hi Fanoush, thanks a lot for the very helpful explanations and background information. :-)

    I suppose for a start I will focus on extending the firmware (following the examples Gordon sent) as that seems to be (as you also wrote) the more straightforward approach. It is great to know that C++ as part of the libraries I need to embed is no hurdle. :-)

  • How would I disable the built-in Tensorflow module, if it becomes necessary?

    You literally just have to comment out the TENSORFLOW line here: https://github.com/espruino/Espruino/blo­b/master/boards/BANGLEJS2.py#L40

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

How to make a C++ library available in Espruino?

Posted by Avatar for user157902 @user157902

Actions