Last active
October 18, 2022 00:51
-
-
Save bricklife/fd325ba3d3a3fe1939c70dc5f581c3c8 to your computer and use it in GitHub Desktop.
A sample code of `Python (advanced)` for lego-spikeprime-mindstorms-vscode
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
# LEGO type:advanced slot:0 autostart | |
from runtime.virtualmachine import VirtualMachine # Needed for newer hub versions (3.1.43+) | |
import hub | |
from util.rotation import rotate_hub_display_to_value | |
rotates = ["1", "3", "4", "2"] | |
rotate_index = 0 | |
async def on_left_button_pressed(vm, stack): | |
print("Left button pressed") | |
global rotate_index | |
rotate_index = (rotate_index + 1) % 4 | |
rotate_hub_display_to_value(rotates[rotate_index]) | |
async def on_right_button_pressed(vm, stack): | |
print("Right button pressed") | |
global rotate_index | |
rotate_index = (rotate_index - 1) % 4 | |
rotate_hub_display_to_value(rotates[rotate_index]) | |
async def on_start(vm, stack): | |
print("on_start() called") | |
blinking_animation = [ | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:00000:77077:00000", | |
"77077:00000:00000:00000:00000", | |
"77077:00000:00000:88088:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
"77077:00000:99099:99099:00000", | |
] | |
blinking_frames = [hub.Image(frame) for frame in blinking_animation] | |
vm.system.display.show(blinking_frames, clear=False, delay=round(1000 / 8), loop=True, fade=1) | |
def setup(rpc, system, stop): | |
vm = VirtualMachine(rpc, system, stop, "something_unique") | |
vm.register_on_start("another_unique_string", on_start) | |
vm.register_on_button("left_button_pressed", on_left_button_pressed, "left", "pressed") | |
vm.register_on_button("right_button_pressed", on_right_button_pressed, "right", "pressed") | |
return vm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All pieces of code are from https://github.com/PeterStaev/lego-spikeprime-mindstorms-vscode/blob/master/ADVANCED-FEATURES.md