Last active
October 14, 2021 02:04
-
-
Save awonak/c7426ecbe273b3103b50664623c5a055 to your computer and use it in GitHub Desktop.
EuroPi Bootloader Script Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Each script defined in its own file. This is a generic example script. | |
class ExampleScript: | |
def __init__(self, name: str): | |
self.name = name | |
def run(self): | |
while True: | |
print("Running {}".format(self.name)) | |
sleep_ms(500) | |
## main.py (bootloader and main script execution) | |
from utime import sleep_ms | |
import uasyncio as asyncio | |
from europi import knob_1, button_1, digital_1, digital_2, digital_3, digital_4 | |
#from script_a import ScriptA | |
#from script_b import ScriptB | |
#etc... | |
# Register each script individually in the scripts list. | |
# This demonstrates 15 random example scripts. | |
scripts = [] | |
for i in range(16): | |
scripts.append(ExampleScript("Script " + str(i))) | |
SCRIPT_COUNT = len(scripts) | |
outputs = [digital_1, digital_2, digital_3, digital_4] | |
def display(choice: int): | |
# Convert the choice into 4 bit binary used to show choice in the 4 digital out LEDs. | |
b = bin(choice).replace('0b', '') | |
while len(b) < 4: | |
b = "0" + b | |
# Convert each positional binary value into corresponding digital output LED value. | |
for i, output in enumerate(outputs): | |
output.value(int(b[i])) | |
return b | |
def reset_display(): | |
for output in outputs: | |
output.value(0) | |
# Main script execution. | |
async def main(script: any): | |
# Display the selected script. | |
print("Preparing to run {}".format(script.name)) | |
sleep_ms(500) | |
# Execute script in main async loop. | |
loop = asyncio.get_event_loop() | |
loop.create_task(script.run()) | |
loop.run_forever() | |
# Bootloader script selection. | |
while True: | |
choice = int((knob_1.percent() - 0.001) * SCRIPT_COUNT) | |
b = display(choice) | |
# Debug logging. | |
print("choice: {} display: {} button: {}".format(choice, b, button_1.pin.value())) | |
# If button 1 is pressed, execute the currently selected script. | |
if button_1.pin.value() == 0: | |
reset_display() | |
try: | |
# Execute the main loop with the currently selected script. | |
asyncio.run(main(scripts[choice])) | |
finally: | |
# This line will stop previous run() method. Use this if entering bootloader from button press sequence. | |
asyncio.new_event_loop() | |
sleep_ms(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment