Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile microbit python code locally?

I am running Ubuntu 22.04 with xorg. I need to find a way to compile microbit python code locally to a firmware hex file. Firstly, I followed the guide here https://microbit-micropython.readthedocs.io/en/latest/devguide/flashfirmware.html.

After a lot of debugging, I got to this point: https://pastebin.com/MGShD31N

However, the file platform.h does exist.

sawntoe@uwubuntu:~/Documents/Assignments/2022/TVP/micropython$ ls /home/sawntoe/Documents/Assignments/2022/TVP/micropython/yotta_modules/mbed-classic/api/platform.h
/home/sawntoe/Documents/Assignments/2022/TVP/micropython/yotta_modules/mbed-classic/api/platform.h
sawntoe@uwubuntu:~/Documents/Assignments/2022/TVP/micropython$ 

At this point, I gave up on this and tried using Mu editor with the AppImage. However, Mu requires wayland, and I am on xorg.

Does anyone have any idea if this is possible? Thanks.

like image 711
sean teo Avatar asked Oct 15 '25 15:10

sean teo


2 Answers

Mu and the uflash command are able to retrieve your Python code from .hex files. Using uflash you can do the following for example:

uflash my_script.py

I think that you want is somehow possible to do, but its harder than just using their web python editor: https://python.microbit.org/v/2

like image 109
Peter Till Avatar answered Oct 17 '25 04:10

Peter Till


Peter Till answers the original question. The additional below adds to this answer by showing how to automate the build and load process. I use Debian. The original question states that Ubuntu is used, which is built on Debian.

A script to find and mount the micro:bit

When code is loaded to the micro:bit, the board is dismounted from the system. So each time you have new code to load, you have to remount the board.

I modified a script to find and mount the micro:bit.

#!/bin/bash
 
BASEPATH="/media/$(whoami)/"
MICRO="MICROBIT"
 
if [ $# -eq 0 ]
then
    echo "no argument supplied, use 'mount' or 'unmount'"
    exit 1
fi
 
if [ $1 == "--help" ]
then
    echo "mounts or unmounts a BBC micro:bit"
    echo "args: mount - mount the microbit, unmout - unmount the microbit"
fi
 
# how many MICRO found in udiksctl dump
RESULTS=$(udisksctl dump | grep IdLabel | grep -c -i $MICRO)
 
case "$RESULTS" in
 
0 )     echo "no $MICRO found in 'udkisksctl dump'"
        exit 0
        ;;
 
1 )     DEVICELABEL=$(udisksctl dump | grep IdLabel | grep -i $MICRO | cut -d ":" -f 2 | sed 's/^[ \t]*//')
        DEVICE=$(udisksctl dump | grep -i "IdLabel: \+$DEVICELABEL" -B 12 | grep " Device:" | cut -d ":" -f 2 | sed 's/^[ \t]*//')
        DEVICEPATH="$BASEPATH""$DEVICELABEL"
        echo "found one $MICRO, device: $DEVICE"
 
        if [[ -z $(mount | grep "$DEVICE") ]]
        then
            echo "$DEVICELABEL was unmounted"
            if [ $1 == "mount" ]
            then
                udisksctl mount -b "$DEVICE"
                exit 0
            fi
        else
                echo "$DEVICELABEL was mounted"
                if [ $1 == "unmount" ]
                then
                    udisksctl unmount -b "$DEVICE"
                    exit 0
                fi
        fi
        ;;
 
* )     echo "more than one $MICRO found"
        ;;
 
    esac
 
echo "exiting without doing anything"

I alias this script to mm in my .bashrc file.

Automate mounting the micro:bit and flashing the python file

I use the inotifywait command to run mm and to then run uflash to load the .py file I am working on. Each time that the python file is saved, the aliased command mm is run followed by the uflash command.

while inotifywait -e modify <your_file>.py ; do mm && uflash <your_file>.py ; done
like image 36
Oppy Avatar answered Oct 17 '25 04:10

Oppy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!