-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
MicroPython Support #3559
Comments
It's not possible Flipper zero has this specifications
While micropython requires about 64kb, it is recommended to use more. Even when using 64kb, flipper will not have enough space, so it is not possible. |
I'm not so sure about this and my research so far points out that it's might possible. I'm only using the Flipper's RAM for my tests so far (by developing a FAP, which cannot use the ROM). I've also compiled the Python files on the Flipper and didn't use the cross compiler feature. |
So far so good. I think I've done enough research on this to proof that it's possible. What I have achieved so far:
The app works, but is a bit fuzzy and crashes sometimes before it's fully loaded (possibly due to memory fragmentation). So I think this is as far as I could go with only using the SRAM. More could be achieved by using the ROM. But as far as I understand, this would mean making the project to a part of the firmware. I need some input from you firmware developers now before going further:
I also could use some directions in terms of the requirements:
|
I've managed to do a quick and dirty integration of my little project in a fork of the latest firmware release:
Still enough space left - as far as I can tell! This quick and dirty version shipps the whole MicroPython compiler and runtime environment and can execute Python scripts from the SD card on the CLI interface using the
The application just allocates 50% of the free SRAM memory for the Python runtime environment (which is about 73 kB in the example output above). This is a big improvement in comparison to the FAP version, where the limit was about 16 kB. The 73 kB should already be enough to do something real. But I don't think that this is the upper limit. |
Houston, we have a REPL Screencast_20240405_143412-1.webm |
For those who are interested, I've uploaded a firmware update (based on 0.100.3) with the current progress:
Screencast_20240408_043208-1.webm |
@ofabel, thank you for your hard work here! It is really nice to see some folks not only wishing to have support for specific technology but also working hard on it. From community to community! |
Actually around 32k because you forgot that core2 exists and 32k is a really dangerous watermark for internal storage. |
Perhaps the FAP version is more attainable, even if it comes at a cost of less usable memory? It's a really cool incentive, even though the flash space concerns are understandable. Perhaps python modules could become plugins embedded in the FAP file, which means users would not pay the code size cost of modules they don't need. |
@unnamedd, many thanks for the feedback! @DrZlo13, thanks a lot for making thinks clear! What would be an acceptable watermark for the internal storage? I have to say, for now I'm just working on making things work and didn't optimize anything. I think there are a lot of options to shrink binary size of the MicroPython library. It might also be an option to make the REPL command a FAP instead and just store the compiler and runtime on the flash. |
So I did the math: right now, my additions to the original 0.100.3 firmware enlarges the resulting binary about 100 K. From official the MicroPython FAQ:
Removing the compiler and REPL feature from the library (my additional logic around the repl not included) should reduce the resulting binary by about 20 K. As @CookiePLMonster suggested, I could also imagine a solution where some parts run as FAPs. A reasonable solution could be only shipping the runtime with the firmware in ROM and provide the compiler as an additional FAP from the SD card. Users would be able to execute compiled |
This sounds reasonable to me - I wasn't aware MicroPython doesn't interpret .py files as-is, but the fact it's Micro explains it. Do note that there is a precedent to this already - VGM is an official Flipper's component, yet it can only be updated only with an external FAP. An official external MicroPython compiler FAP would not be out of place. |
@CookiePLMonster, thanks for the hint about the VGM app - I wasn't aware of that 👍
Well, MicroPython can interpret Python files as-is - but you can compile and ship the library also without the compiler. In fact, even the normal version of CPython contains such a compiler. Because this is how most interpreted languages work these days, they compile the programm code to language dependent byte code and then execute the byte code in the runtime environment. |
Update on the binary size: After some optimizations and sorting out the compiler "free" flash space went up to 229.78 K. So the runtime requires around 70 K. But I think I can still do better than this. @DrZlo13 is this more of an acceptable watermark for the internal storage?
|
Since splitting runtime and compiler is no easy task, I decided to postpone this until I know what an acceptable watermark for the internal storage could be. For now I'm compiling the firmware with So I continued adding new features:
To test canvas and interrupt support, I created a quick and dirty Tic-Tac-Toe game (see source code for details): Screencast_20240413_114606.webmThe canvas API is straight forward and nothing unusual. Interrupts are implemented in the form of a Python decorator: import time
import flipperzero as f0
exit = False
@f0.on_input
def input_handler(button, type):
global exit
if button == f0.INPUT_BUTTON_BACK and type == f0.INPUT_TYPE_SHORT:
exit = True
while not exit
time.sleep_ms(10) For those who are interested, I've uploaded firmware update with the current progress. |
I've invested some time this weekend to give the FAP version a makeover. It's a bit more stable now but still crashes often when started (possibly due to memory fragmentation in RAM). For those who wan't to try it: https://github.com/ofabel/mp-flipper/releases/download/v0.4.0-beta.1/mp_flipper_app.fap Install instructions can be found here - but I think those who read this posts here are capable of installing it without guidance. I'm not sure if I also should add this to the official application catalog. It's just not stable enough from my point of view. |
This is very impressive @ofabel, well done! However, I just wanted to bring up a few concerns, because while it would seem possible on OFW, I think there should also be a discussion of what benefit this brings... Flipper JS is already a thing, adding Python support would not really expand what is possible AFAIK. It would simply be another language to do the same things with same difficulty (as in, JS and Python are more approachable than C or C++, they have a similar level of difficulty), so then it would be yet another surface area to maintain, to develop modules for to interact with Flipper hardware, and to provide documentation for. All of that sounds like a lot compared to the small gain of "write it in python instead of js" being the only difference. Also I know this is largely irrelevant in this thread as this is about OFW, but CFWs tend to have more internal flash used due to additional functionality, on Momentum for example we currently have 44KB total available, we can't even flash with COMPACT=0, and adding JS support was already a monumental task to find the smallest bits of optimizations in firmware code and making just about everything possible (CLI commands, Settings menus) external. There's nothing really left we could cut to make space for this without removing functionality. About an acceptable mark for internal storage, from my experience maintaining a CFW, I know that under 28KB total (as seen in Settings > Storage > About Internal Storage) is a non-starter, with 28KB and less you will not even be able to install the firmware in most cases. Personally, I find that 36KB usually allows enough space for the settings files stored on internal flash, I myself am not comfortable shipping the firmware with less than that. But should also consider that there needs to be enough space to allow for future developments of the firmware, so what @DrZlo13 would consider an acceptable amount I would imagine to be more than that, but of course I can't answer for him. With all those in mind, I personally think that a FAP solution on the application catalog would be best if possible, but of course I can't speak as to what the Flipper team will decide. |
have you tried with #3572 ? |
Thanks a lot for bringing these points up for discussion @Willy-JL! I had the same concerns when I started the project. And I still have some doubts on why should the original firmware support JavaScript and MicroPython together. From my point of view, the mjs implementation of JavaScript has some big limitations and it is indeed not much more than a scripting language. MicroPython on the other hand is much closer to the language standard (which of course also results in a larger binary size). So if we take a look at the available features, you can do much more with MicroPython. So MicroPython might could be used for more complex things, like implementing a whole app. While JavaScript can be used for simpler tasks, like Bad USB scripts. But this is just my opinion. I would totally understand if @DrZlo13 & the team don't wan't to include this into the original firmware. In this case, I might start my own fork of the firmware (streamlined with the original firmware, just with additional MicroPython support). The FAP version could still be around (maybe with limited feature support). |
Not yet, but looks promising! |
Describe the enhancement you're suggesting.
Since Python is a very popular programming language, I think it would be great to add support for this on the Flipper Zero. My first research using MicroPython already shows that this is absolutely possible.
Anything else?
I'm willing to contribute to this project. So this issue is just to let you know about. Checkout the proof of concept or the firmware fork for the progress. There is also a FAP version available (currently in public beta).
Progress
flipperzero
Python module with full feature supportThe text was updated successfully, but these errors were encountered: