MicroPython documentation
Start micropython
- Ensure your badge is upgraded to the latest version. instructions
- After flashing has finished, press reset so that the main menu appears (with green buttons: Ota, Hello, Micropython, Retro-go)
- Connect with Fri3d ViperIDE This will give a timeout error (because the micropython REPL does not answer, this is OK)
- Now you can see on the bottom of the screen the serial logging of the badge
- On the badge select micropython en press A
- The badge reboots and you will see the serial output of the boot process. Do not interrupt this process!! Micropython is extracting the files to the fat partition.
- After a while you will see a REPL promt
>>>
- You can now disconnect and reconnect the Fri3d ViperIDE
improved main.py
By default, the badge will not reboot into MicroPython on reset.
Should you want this, you need to confirm the switch to MicroPython was successful.
You can do this like this:
To switch back you need to do this:
now let's put this into action in an improved version of main.py
# Hello and welcome to the code of the Fri3d Badge!
# You have located the main entrypoint where the code of your badge is launched from.
# Feel free to modify this file or any of the files in the fri3d package.
# In case you mess up, just delete this file using the REPL, and it will be restored to the original state.
# Similarly, if you delete the fri3d or user package it will also get restored to its original state
import logging
from fri3d.badge.buttons import buttons
from fri3d import boot
# If you want you can increase the log output level here
logging.basicConfig(level=logging.INFO, force=True)
# create a logger for use in this file
logger = logging.Logger(__file__)
# create a callback function that is executed when the menu button is pressed, it needs to have 1 argument
def menu_button(_):
logger.warning("MENU button pressed, rebooting to main menu")
boot.main_menu()
# assign the callback function to the menu button
buttons.menu.cb = menu_button
logger.info("From now on you can press the MENU button to reboot to the main menu")
# stay in micropython, now that we have a button to go back to the main menu
boot.persist()
Badge Examples
There are examples installed on the badge itself. sources
You can explore these with ViperIDE
If some of the first 4 examples fail to run, this is likely because the first micropython boot was interrupted. The easiest way to fix this is to follow the steps above Start micropython.
How to create and run a new file on your badge in Fri3d ViperIDE
Example to create a file user/marco/first.py
on your badge and run it
1. Connect to your badge with Fri3d ViperIDE
1. on the left side in the files scroll down to the user
folder
1. click on the +
-sign next to the user
folder
1. to create a folder end it with a /
, so for our example fill in marco/
1. now click on the +
-sign next to the marco
folder
1. create a file first.py
1. in the file editor type
How to reboot to the main menu when pressing the MENU button
run this script once in your active session and a press of the MENU button will bring you back to the main badge menu
from fri3d.badge.buttons import buttons
from fri3d import boot
def menu_button(_):
print("MENU button pressed")
boot.main_menu()
buttons.menu.cb = menu_button
How to run a local file
Install mpremote
howto
TLDR; pip install mpremote
Thonny also works fine.
Unfortunately the cleanup of the Display module does not work entirely, so after this has been initialized and your next script tries to initialize it again, this will give errors. You need to perform a hard reset (reset button on the badge)
how to copy a file to the badge
Blink the badge leds 10 times red
import time
from fri3d.badge.leds import leds
for _ in range(10):
leds.fill((255, 0, 0))
leds.write()
time.sleep(0.5)
leds.fill((0, 0, 0))
leds.write()
time.sleep(0.5)
flash leds
import time
from fri3d.badge.leds import leds
# a function to flash the leds
def flash_leds():
logger.debug("Flashing LEDs")
# cycle
for i in range(4 * leds.n):
leds.fill((0, 0, 0))
leds[i % leds.n] = (255, 255, 255)
leds.write()
time.sleep_ms(25)
# bounce
for i in range(4 * leds.n):
leds.fill((0, 0, 128))
if (i // leds.n) % 2 == 0:
leds[i % leds.n] = (0, 0, 0)
else:
leds[leds.n - 1 - (i % leds.n)] = (0, 0, 0)
leds.write()
time.sleep_ms(60)
# fade in/out
for i in range(0, 4 * 256, 8):
for j in range(leds.n):
if (i // 256) % 2 == 0:
val = i & 0xff
else:
val = 255 - (i & 0xff)
leds[j] = (val, 0, 0)
leds.write()
time.sleep(0)
# clear
leds.fill((0, 0, 0))
leds.write()
# call our function to flash the leds
flash_leds()
Start the Fri3d application
Micropython libraries
Micropython quick reference for the esp32
Micropython standard libraries overview doc
The badge has lvgl built-in.
Other interesting links - https://github.com/peterhinch/micropython-samples - https://github.com/mcauser/awesome-micropython