Espruino emulator - Very Alpha!

Posted on
  • Hey, y'all!

    I'm working on a Espruino Emulator, written in JS and ment to be run by the browser. My final goal is to have an emulator capable of running any code that can be run in a espruino device and also emulate periphereals like sensors, actuators, output devices, etc.

    I was able to run a simple app with an emulated SSD1306 (which renders to an html canvas) and an emulated Rotary Encoder (which pulses the stream of data to simulate the encoder movement).

    I have the code in espruino-emulator I will be slowly improving it and adding documentation. Anyone is invited to collaborate and ask for improvements.

    As a caveat, I don't have many sensors or devices (I only own an Arduino Uno and a few ESP8266) and it's not easy to get any of them here in Argentina (and when you find one it is expensive as well).

    Hope is helpful for anyone.


  • Hi @mfgea,

    did a similar thing when developing a Modular and extensible UI framework and ui elements.

    Developing (at that time) in the Browser was much more comfortable with all the debugging help available. Today, Espruino has built-in debugger. I though still prefer to develop particualar things in the emulator, because it gives me more options.

    I used also an emulater to develop a memory manager for 256-Kbit (32 K × 8) Serial (SPI) F-RAM - Ferroelecric RAM - SPI challenges.

    What I liked the most is the easy way to add testing... see: link to mm.html

  • Very, cool, @allObjects!

    I wrote it because I wanted to develop without having the thing (my ESP8266) attached and writing to it every time I wanted to test a screen render.

    This, also, is part of a dev environment I'm working on (­319462), starting with a sample boirlerplate and example app written using Typescript, as well as some utility functions to compile, minify and upload to the device, all from VSCode.

    I'm planning on moving the emulator as an npm module; this would let me write Typescript (with the benefits of modularization, typed variables, autocompletion, in-place docs, etc) and also test the running code in a simple to use environment.

    I'll keep working on it and try to keep it updated while working on my projects (not much time, bc it's all I have) but can help anyone interested on it.

  • @mfgea

    You might want to look at the Linux compile for espruino. It allows you to run on the same code as the physical boards.

    I guess you are on windows? If you are on 64bit windows can you can run the WLS (windows Linux subsystem) to get the environment.

  • As @Wilberforce says, there's a proper Linux build of Espruino (I haven't tried for Mac, but it shouldn't be too hard to compile). It has a Telnet server too so you can push code to it straight from the CLI tools.

    Making a full JS emulator is cool, but it'll be a lot of work to implement the full API in a way that behaves exactly the same - it might be easier to start with an actual Espruino build and then work on implementing the hardware emulation.

    By default it accesses pins exposed via the filesystem (eg. if you're on a Pi), but you could tweak that quite easily so that you could interface it to your hardware emulator. In fact as you're working in JS you could look at compiling Espruino with Emscripten - you could then hook onto the actual GPIO implementation with JavaScript. It'd also be a lot easier to port to the browser if you needed :)

    I'm kind of surprised there isn't a library of emulated hardware already that could be used somehow, even if it were made for Arduino. You could do all kinds of fun stuff like hooking into real hardware using sigrok and then using an emulated LCD (for example) to debug.

  • Thanks, @Wilberforce and @Gordon!

    Actually I'm running on a Mac, which should not make much difference anyway, so I'll try compiling for linux. Of course, doing a complete emulator that behaves just the same would be very hard, and also maintaining could be an exhaustive task, so your solution, with the possibility of attaching some emulated components, would be ideal.

    I started in the browser because it is an extremely simple i/o interface, so, if I succeed with the linux compile, I'll try to attach it to the browser somehow, maybe through a browser extension or something.

    I'm also surprised there's nothing like I'm suggesting, as I believe could be quite handy for everyone.

    Right now, my emulator works for me, as I'm trying to develop the logic around a thermostat (SSD1306, DHT22, Rotary encoder and some LEDs) and I'm not worried about constraints of the device, but that would be helpful to implement as well.

    So, I'll try all those things. Thanks again!

  • as you're working in JS you could look at compiling Espruino with Emscripten

    A while ago I played with that idea for a bit. Got to the point where I got the minimum running within node.js but for more it would require some clever work on implementing the JS<->C trampoline properly.

  • The emulation I see only worth for developing logic... if you try to do more, you really end up with something very challenging.... even though pc/laptop is about 100 times faster, getting timings right is a delicate thing... after all, it is hardware you try to emulate.

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

Espruino emulator - Very Alpha!

Posted by Avatar for mfgea @mfgea