Belay - Interface with hardware from your PC from within a single Python program
Belay lets you write and run Python code on a host like PC and execute pieces of that code down on a connected microcontroller from inside your host Python session. You write python code and mark up which code will run on the microcontroller (MCU). The rest of the code runs on the host. At runtime Belay will transfer the MCU targeted code down to the microcontroller. The host Python code can then call that downloaded MCU based code as if it were running locally.
MicroPython CircuitPython and host Python
Belay runs on Python3 on the host. It can download and interact with MicroPython or CircuitPython runtimes on the microcontroller.
Code on both the Host and the Microcontroller
All the Python code starts on the PC on the left. For this example, everything is in one file like the led flasher above. Both PC and MCU code is in the same Python source file. We copy any libraries from the PC to the RP2040 /pydrive. This can be done externally or using the drive replicator in Belay.
The @device.task functions are downloaded to the microcontroller and remote executers are built, one for each downloaded function. The PC/Mac/Linux-based Python code actually calls a remote executer that acts as a proxy for and is paired with the function that was sent to the microcontroller. Host side code can be any mix of local and remote calls.
- main.py holds all the code for both the host and microcontroller in a single file.
- There is a main.py python program that we will execute on the PC.
- All of the functions tagged with @Belay.Task are downloaded to the PC and executers are created.
- The @Belay.Setup method is run creating the global context
- The code in main.py outside the @Belay tags runs on the PC sometimes pausing to call the @Belay.Task functions that are deployed microcontroller
Blinky Light Code Example
- This sample downloads the set_led() method to the microcontroller.
- It then runs setup() which runs that code on the microcontroller to create the led variable.
- Finally, it loops on the host invoking set_led() in an alternating on/off pattern. set_led() is on the MCU so each set_led() call runs remotely over the connected REPL session.
Setup the connection with the micropython board.# This also executes a few common imports on-device.device = belay.Device(args.port)
# This runs in a global scope on the MCU making the variables available@device.setupdef setup(): # The function name doesn't matter, but is "setup" by convention. from machine import Pin led = Pin(25, Pin.OUT)
# This sends the function's code over to the board.# Calling the local ``set_led`` function will execute it on-device.@device.taskdef set_led(state): # Configuration for a Pi Pico board. print(f"Printing from device; turning LED to {state}.") led.value(state)
setup()# Calls set_led() that is actually running on the microcontrollerwhile True: set_led(True) time.sleep(0.5) set_led(False) time.sleep(0.5)
Segregating Host/PC and Microcontroller functionality
- Using Classes to partition microcontroller example on GitHub
- Isolating microcontroller python in its own files example on GitHub
- There is a python file with a class definition in it that holds all the code we want to run on the Microcontroller.
- There is a main.py python program that we will execute on the PC.
- The main.py imports the tagged python file with the class Definition.
- All of the functions in the class that are tagged with @Belay.Task are downloaded to the PC and executers are created.
- The @Belay.Setup() method is run creating a global context.
- All the code in main.py runs on the PC sometimes pausing to call the @Belay.Task functions that are deployed microcontroller
Comments
Post a Comment