Skip to content

Instantly share code, notes, and snippets.

@bricklife
Last active October 18, 2022 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricklife/fd325ba3d3a3fe1939c70dc5f581c3c8 to your computer and use it in GitHub Desktop.
Save bricklife/fd325ba3d3a3fe1939c70dc5f581c3c8 to your computer and use it in GitHub Desktop.
A sample code of `Python (advanced)` for lego-spikeprime-mindstorms-vscode
# 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
@bricklife
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment