Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Micropython to JavaScript in order to create an online simulator #792

Closed
5 tasks
kisvegabor opened this issue Feb 2, 2019 · 456 comments
Closed
5 tasks
Assignees
Labels
pinned Not closed automatically

Comments

@kisvegabor
Copy link
Member

kisvegabor commented Feb 2, 2019

In continue of #687

Discuss how to concert micropython to javascript to have an online simulator where people can easily try LittlevGL in their browser without downloading anything.


Summarizing what's left before release:

Later (once the initial version is up on https://littlevgl.com):

  • Ability to resize the canvas (up to a set maximum)
  • Possibly more
@embeddedt
Copy link
Member

@amirgon That port of Micropython builds properly but I ran into errors while trying to run it. I think that Emscripten has evolved since it was made.

@amirgon
Copy link
Contributor

amirgon commented Feb 2, 2019

@embeddedt Did you see micropython-ports-wasm?
It looks rather new and active (initial commit 3 days ago), @pmp-p claims he got Micropython working in WASM.

@embeddedt
Copy link
Member

@amirgon That's working out a lot better. Thanks! I'll let you know about my progress.

@stale
Copy link

stale bot commented Feb 24, 2019

This issue or pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Feb 24, 2019
@embeddedt
Copy link
Member

I haven't had a lot of time to work on this lately, but I hope to get to it at some point.

@stale stale bot removed the stale label Feb 24, 2019
@kisvegabor
Copy link
Member Author

Shall we "pin" this issue to leave it opened as a reminder?

@embeddedt
Copy link
Member

@kisvegabor Sure.

@embeddedt embeddedt added the pinned Not closed automatically label Feb 24, 2019
@amirgon
Copy link
Contributor

amirgon commented Mar 13, 2019

micropython/micropython#3575
Micropython now officially has an Emscripten port! micropython/micropython@7d675f3

lvgl+micropython could make a nice interactive lvgl demo on lvgl web page.

@embeddedt
Copy link
Member

@amirgon I'll see what I can do, but I have limited time right now.

@kisvegabor
Copy link
Member Author

@amirgon Awesome! It should be much easier with this.

@kisvegabor
Copy link
Member Author

@embeddedt Did you have a chance to make some experiments with Emscripten and Micropython?

@embeddedt
Copy link
Member

embeddedt commented Apr 14, 2019

@kisvegabor I tried to work with it but I found that I don't understand Micropython well enough to be able to make it work with LittlevGL.

I haven't had time to look at it again lately.

@kisvegabor
Copy link
Member Author

kisvegabor commented Apr 15, 2019

Would you like to warm it up? Probably @amirgon could help to understand the Mpy specific parts.
Or shall we postpone it? (It could be a great thing though but I understand if you have more important things now)

@embeddedt
Copy link
Member

I will try to experiment with it again this week.

@kisvegabor
Copy link
Member Author

Awesome!

@embeddedt
Copy link
Member

@amirgon I made some more progress on this, but it looks like Emscripten doesn't support SDL threads properly at the moment... could you figure out if the binding can be modified to avoid the need for another thread?

@amirgon
Copy link
Contributor

amirgon commented Apr 20, 2019

@embeddedt The micropython binding was designed to be single threaded. You can run everything (micropython+lvgl+display driver) on the same thread.
I don't think you should use SDL at all. You probably need a special display driver (and input device driver) for Emscripten.
How does Emscripten write to the screen and read mouse input? If you want I can help creating the drivers for it.

@embeddedt
Copy link
Member

@amirgon Emscripten uses SDL2 for graphics.

The micropython binding was designed to be single threaded. You can run everything (micropython+lvgl+display driver) on the same thread.

Is SDL_CreateThread supposed to be called here then?

https://github.com/littlevgl/lv_binding_micropython/blob/da8588210df7e34644f94fe3bf605d70cb01058a/driver/SDL/modSDL.c#L32

@amirgon
Copy link
Contributor

amirgon commented Apr 20, 2019

Is SDL_CreateThread supposed to be called here then?

@embeddedt The only purpose of the SDL thread over there is to call lv_tick_inc and lv_task_handler indirectly, using mp_sched_schedule.
This will schedule lv_task_handler to run next time micropython is ready, on the same thread micropython is running.
If you have other ways to schedule things in Emscripten (something like javascript's setTimeout), that would be good enough instead of SDL thread for calling these functions periodically.

@embeddedt
Copy link
Member

@amirgon Okay, I'll see what I can do.

@amirgon
Copy link
Contributor

amirgon commented Apr 20, 2019

@embeddedt How do you call SDL main loop (monitor_sdl_refr_core) if you don't use SDL threads?

@embeddedt
Copy link
Member

embeddedt commented Apr 20, 2019

@amirgon I'm now using an Emscripten callback function to call the lv_tick_inc, lv_task_handler, and monitor_sdl_refr_core functions. I no longer use mp_sched_schedule.

Of course, I don't fully understand the Micropython architecture yet, so it's possible this will all fall apart shortly. 😄

@amirgon
Copy link
Contributor

amirgon commented Apr 20, 2019

I no longer use mp_sched_schedule

@embeddedt mp_sched_schedule ensures that a function is called when micropython is ready, and not in the middle of the execution of some micropython statement.
Depending on how you schedule micropython main loop and how you schedule lv_task_handler, this might be ok (or not).

@embeddedt
Copy link
Member

I got it to work with mp_sched_schedule. Now I have to figure out how to get the REPL working so that people can put in their own code.

Screenshot from 2019-04-20 18-40-14

@amirgon
Copy link
Contributor

amirgon commented Apr 20, 2019

Nice progress @embeddedt ! 👍

@embeddedt
Copy link
Member

It seems to work for me now as well; I think the problem is fixed.

@kisvegabor
Copy link
Member Author

@amirgon @embeddedt
What is still missing to release the simulator? The "inti script" feature is still not added, right?

@embeddedt
Copy link
Member

I need to update the simulator to work with the latest version of lv_micropython. Currently it's using an old snapshot from September/October.

The init script feature should be supported in lvgl/lv_micropython#15.

@amirgon
Copy link
Contributor

amirgon commented Dec 6, 2019

The init script feature should be supported in littlevgl/lv_micropython#15.

@embeddedt what about lvgl/lv_micropython#17? I think it's important to include this as well.

Did you get a chance to make some progress with other features we discussed?

@stepansnigirev
Copy link
Contributor

@amirgon, regarding lvgl/lv_micropython#17
I am traveling at the moment and unfortunately can't work on this pull request until ~mid next week.

@amirgon
Copy link
Contributor

amirgon commented Dec 6, 2019

@amirgon, regarding littlevgl/lv_micropython#17
I am traveling at the moment and unfortunately can't work on this pull request until ~mid next week.

No problem @stepansnigirev, we can add this later.

@embeddedt
Copy link
Member

embeddedt commented Dec 6, 2019

Did you get a chance to make some progress with other features we discussed?

I haven't had a chance to work on them yet.

@kisvegabor
Copy link
Member Author

@embeddedt
Are there any difficulties with updating the project or is it just a simple submodule update?

@amirgon @embeddedt
It'd be great to add a blog post and an Announcement on the forum about the simulator. You know it much better than me, so can you help with that?

I'll write to the hackaday too. They were already interested in the Micropythin binding once.

@barbiani
Copy link

barbiani commented Dec 7, 2019 via email

@pmp-p
Copy link

pmp-p commented Dec 7, 2019

@barbiani while wasm is now an official standard https://www.w3.org/2019/12/pressrelease-wasm-rec.html.en and emscripten uses clang 10.

The javascript simulator actually uses the (soon to be deprecated) emscripten fastcomp compiler (clang 8) because the project wants to match what official micropython javascript port may do.

@amirgon
Copy link
Contributor

amirgon commented Dec 7, 2019

You guys ever heard of moddable.com?

Could be interesting in other context - js binding to lvgl. If anyone is willing to take this effort, because I'm too invested in Micropython for now...

@embeddedt
Copy link
Member

Are there any difficulties with updating the project or is it just a simple submodule update?

For some reason it doesn't seem to work properly if I just merge master. I haven't figured out what the culprit is, but I'll most likely have it working by the end of the weekend.

Could be interesting in other context - js binding to lvgl.

I thought of this as well, but then I realized that most people using JavaScript with a user interface will be using it in a web browser, where LittlevGL isn't very necessary or practical.

The javascript simulator actually uses the (soon to be deprecated) emscripten fastcomp compiler (clang 8) because the project wants to match what official micropython javascript port may do.

We're trying to remain aligned with upstream MicroPython as much as possible to ease the merging process.

@amirgon
Copy link
Contributor

amirgon commented Dec 7, 2019

I thought of this as well, but then I realized that most people using JavaScript with a user interface will be using it in a web browser, where LittlevGL isn't very necessary or practical.

@embeddedt I think that JavaScript in this context is used like Micopython and Lua on embedded device, where there is no web browser.
It's part of the trend these days to move from C to higher level languages (python, js) on embedded devices. I've seen multiple js implementations for embedded such as low.js, Espruino, jerryscript, duktape and now moddable.
On each of these, JavaScript is used on an embedded device with no web browser, so lvgl binding is applicable just like we did for Micropython.
I don't know how popular JavaScript-on-embedded-devices is going to get, but it is definitely gaining some momentum.

@barbiani
Copy link

barbiani commented Dec 7, 2019 via email

@embeddedt
Copy link
Member

QuickJS is another small and fast implementation of JS. Someone was able to make React run on a Raspberry Pi using it. I think it would fit for sure on an STM32F7 and probably even an F4.

I'd be willing to work on making a JS binding for LittlevGL if there was substantial interest. The main problem I see is that there are so many different implementations (JerryScript, QuickJS, etc.) which don't use the same API for bindings. I suppose using a script like what you use for MicroPython would help with that.

I don't know how popular JavaScript-on-embedded-devices is going to get, but it is definitely gaining some momentum.

It's useful because it enables IoT developers to write the same type of code for both the browser interface and the device backend.

@amirgon
Copy link
Contributor

amirgon commented Dec 8, 2019

QuickJS is another small and fast implementation of JS. Someone was able to make React run on a Raspberry Pi using it. I think it would fit for sure on an STM32F7 and probably even an F4.

  • Not sure about "fast". It's fully interpreted, as opposed to others which run bytecode.
  • It's not small. See this. The core is 700KB and he needed another 10MB of "auxiliary environment" to make it work...
  • He is running it on a Linux OS on a Raspberry Pi. If you call that an "embedded device" this is really the very high end.

I'd be willing to work on making a JS binding for LittlevGL if there was substantial interest. The main problem I see is that there are so many different implementations (JerryScript, QuickJS, etc.) which don't use the same API for bindings.

If you are thinking about this, I suggest taking a close look at the other options. Which devices are supported? ROM/RAM requirements? Performance?

One great thing about Micropython is that it can run in a variety of ways:

  • Parse and run micropython source code on the device (REPL)
  • Run precompiled bytecode
  • Compile Micropython to native instructions (native code emitter)
  • Compile Micropython to non compliant but very fast native instructions (viper code emitter)

I wonder if any of the Javascript engines above expose such range of options.

I suppose using a script like what you use for MicroPython would help with that.

Probably, but I'm not sure how much you could reuse the existing Micropython binding script for that. It's really geared towards Micropython.

@embeddedt
Copy link
Member

It's fully interpreted, as opposed to others which run bytecode.

I'm pretty sure it can compile to bytecode as well. See page 3 of this PDF.

he needed another 10MB of "auxiliary environment" to make it work...

Much of that "auxiliary environment" is compilation infrastructure which won't be in the final bytecode/script. JavaScript keeps compilers and libraries together in one folder; his measurement includes both.

The core is 700KB

Most of that is probably React (which is fairly large). The author of QuickJS claims that it takes up "190 KiB of x86 code for a simple hello world program."

He is running it on a Linux OS on a Raspberry Pi. If you call that an "embedded device" this is really the very high end.

My point is that it could easily be run on a smaller device, just like how MicroPython runs on Linux but can also run on STM32, ESP32, etc.

I'm not sure how much you could reuse the existing Micropython binding script for that. It's really geared towards Micropython.

Of course. I would have to write a new script that does the same type of thing, but for JavaScript instead.

@kisvegabor
Copy link
Member Author

I also see great potential in a JS binding. As Python and JavaScript are the two most popular languages it'd be great to have bindings to them.

BTW, JerryScript also claims that it runs with 64k RAM, 200 kB ROM.

I'm not sure how much you could reuse the existing Micropython binding script for that. It's really geared towards Micropython.

Of course. I would have to write a new script that does the same type of thing, but for JavaScript instead.

At least the parser of the MicroPython can be used, can't it? And we already know lvgl has the conventions to generate a binding.

Next year lvgl will have a new website where the bindings (and the simulator(s)) should be clearly highlighted. Some videos and tutorials also will be created as the bindings and the simulator really makes a difference compared to other gui libraries. Actually I don't know about any serious embedded UI library which has binding to any higher-level languages.

@amirgon
Copy link
Contributor

amirgon commented Dec 9, 2019

BTW, JerryScript also claims that it runs with 64k RAM, 200 kB ROM.

Selecting the right js engine is a critical decision, since there are so many options.
Imagine you create binding for js engine X but everyone are using js engine Y so they can't use your js binding.
Beside RAM/ROM/performance consideration, another important consideration is the ecosystem and adoption. How much is it used and adopted? Which hardware platforms are supported? What libraries / drivers are available?

At least the parser of the MicroPython can be used, can't it? And we already know lvgl has the conventions to generate a binding.

The parser itself (pycparser) can be used, of course.

@amirgon
Copy link
Contributor

amirgon commented Dec 17, 2019

We need to take into account lvgl versions on https://sim.littlevgl.com/.
Suppose we move to v7.0 which is not backward compatible - we wouldn't want all the simulator links of v6 scripts to stop working.

So I suggest something like:

  • sim.littlevgl.com/v6/micropython/ports/javascript/lvgl_editor.html
  • sim.littlevgl.com/v7/micropython/ports/javascript/lvgl_editor.html

What do you think?

@embeddedt
Copy link
Member

As long as it's not a requirement to continue updating the "v6" folder once "v7" is released, it shouldn't be any extra work. It just means adding some subdirectories to https://github.com/littlevgl/sim.

I've moved the existing lv_micropython submodule to a new "v6" directory.

@amirgon
Copy link
Contributor

amirgon commented Dec 17, 2019

I've moved the existing lv_micropython submodule to a new "v6" directory.

Once you deploy it - it will break existing links. (I fixed the one I posted today on lvgl/blog#20)

You would also need to update the html on https://sim.littlevgl.com/, because micropython link is now broken.

@embeddedt
Copy link
Member

embeddedt commented Dec 18, 2019

Once you deploy it - it will break existing links.

Good point. I will put back the old micropython in the root directory, but it will be permanently fixed at v6, for backwards compatibility. All changes will only happen in the subdirectories.

@amirgon
Copy link
Contributor

amirgon commented Dec 18, 2019 via email

@embeddedt
Copy link
Member

Yes; that would probably be a lot easier.

@embeddedt
Copy link
Member

I've added the symlink; it seems to work properly.

@kisvegabor
Copy link
Member Author

I've added the symlink; it seems to work properly.

Great!


Is there any major things with the simulator?
I know there is a list with the possible improvements but the current version can be considered a v1.0, right?
If you agree I suggest closing this issue and open a new one with the remaining ideas/things to do.

@amirgon
Copy link
Contributor

amirgon commented Dec 22, 2019

If you agree I suggest closing this issue and open a new one with the remaining ideas/things to do.

Ok. The simulator is usable, but I think should be polished and styled a little.
For the reference here is a list of improvements we discussed.

@embeddedt
Copy link
Member

Opened #1320.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pinned Not closed automatically
Projects
None yet
Development

No branches or pull requests

6 participants