async def on_start(vm, stack): hub.display.show(hub.Image.HAPPY) image = hub.Image("90009:" "09090:" "00900:" "09090:" "90009") hub.display.show(image) utime.sleep(1) # or image = hub.Image("90009\n09090\n00900\n09090\n90009") hub.display.show(image) utime.sleep(1) image = hub.Image(2, 2, b'\x08\x08\x08\x08') hub.display.show(image) utime.sleep(1) # all not working hub.Image.width() hub.Image.height() hub.Image.get_pixel(2, 3) hub.Image.set_pixel(3, 1, 6) # you cannot do this on built-in images
def backward(): for i in range(3): hub.display.show(hub.Image("00000:00000:00900:00000:00000")) utime.sleep(0.3) hub.display.show(hub.Image("00000:09990:09990:09990:00000")) utime.sleep(0.3) hub.display.show(hub.Image("99999:99999:99999:99999:99999")) utime.sleep(0.5)
def heart(): print("heart") hub.led for i in range(5): hub.led(i) utime.sleep(1) hub.display.show([ hub.Image("09090:99999:99999:09990:00900"), hub.Image("01010:11111:11111:01110:00100") ], fade=2, loop=1, delay=500) hub.display.show("")
async def on_start(vm, stack): ms_hub.status_light.on("black") rotate_hub_display_to_value("3") blinking_frames = [hub.Image(frame) for frame in looking_animation] vm.system.display.show(blinking_frames, clear=False, delay=1000, loop=True, fade=1) # Calibrate left_hand.run_to_position(0) right_hand.run_to_position(0) while True: color = color_sensor.get_color() if not color is None: stacks = vm.broadcast(color) while any(stack.is_active() for stack in stacks): yield ms_hub.status_light.on("black") vm.system.display.show(blinking_frames, clear=False, delay=1000, loop=True, fade=1) yield
def run_animation(frames, clear=False, delay_ms=500, loop=True, fade=4): while True: for f in frames: start_time_ms = time.ticks_us() img = hub.Image(f) hub.display.show(img) while ((time.ticks_us() - start_time_ms) / 1000 < delay_ms): #print((time.ticks_us() - start_time_us) * 1000) yield if not loop: return
async def on_red(vm, stack): ms_hub.status_light.on("red") angry_frames = [hub.Image(frame) for frame in angry_animation] vm.system.display.show(angry_frames, clear=False, delay=1000, loop=False, fade=1) vm.system.sound.play("/extra_files/No") wheels.move(10, speed=60)
def up(): for i in range(3): hub.display.show(hub.Image("00000:00000:00000:00000:00900")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00000:00900:00900")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00900:00900:00900")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00900:00900:00900:00900")) utime.sleep(0.1) hub.display.show(hub.Image("00900:00900:00900:00900:00900")) utime.sleep(0.1) hub.display.show(hub.Image("00900:00900:00900:00900:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00900:00900:00900:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00900:00900:00000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00900:00000:00000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00000:00000:00000")) utime.sleep(0.15)
async def on_green(vm, stack): ms_hub.status_light.on("green") happy_frames = [hub.Image(frame) for frame in happy_animation] vm.system.display.show(happy_frames, clear=False, delay=round(1000 / 8), loop=True, fade=1) vm.system.sound.play("/extra_files/Chuckle") wheels.move(14 * math.pi, "cm", steering=100, speed=80) vm.system.sound.play("/extra_files/Chuckle") wheels.move(14 * math.pi, "cm", steering=-100, speed=80)
def right(): for i in range(3): hub.display.show(hub.Image("00000:00000:90000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:99000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:99900:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:99990:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:99999:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:09999:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00999:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00099:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00009:00000:00000")) utime.sleep(0.15)
async def on_blue(vm, stack): ms_hub.status_light.on("blue") scared_frames = [hub.Image(frame) for frame in scared_animation] vm.system.display.show(scared_frames, clear=False, delay=round(1000 / 8), loop=True, fade=1) vm.system.sound.play("/extra_files/Scared") wheels.move(-20, speed=80) wait_for_seconds(1)
def progress_bar_image(count, target, count_brightness="9", target_brightness="0"): """ Args: count (int): the number of pixels in the progress bar completed target (int): the number of pixels in the progress bar total count_brightness (str): the intensity of completed pixels target_brightness (str): the intensity of remaining ``target`` pixels Returns: hub.Image: an Image object of the progress bar Example: .. code:: python import hub from spikedev.display import progress_bar_image pb = progress_bar_image(12, 20) hub.display.show(pb) """ if count > target: raise ValueError("count {} must be <= target {}".format(count, target)) result = [] x = 1 y = 5 brightness = "9" for i in range(25): if i and i % 5 == 0: result.append(":") if i == count: brightness = "4" elif i == target: brightness = "0" result.append(brightness) x += 1 if x == 6: x = 1 y -= 1 return hub.Image("".join(result))
async def on_start(vm, stack): ms_hub.status_light.on("black") rotate_hub_display_to_value("3") 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) left_hand = Motor("B") right_hand = Motor("F") # Calibrate left_hand.run_to_position(0) right_hand.run_to_position(0) vm.system.sound.play("/extra_files/Humming") for i in range(2): right_hand.run_for_degrees(-100, 50) right_hand.run_for_degrees(100, 50) left_hand.run_for_degrees(100, 50) left_hand.run_for_degrees(-100, 50) left_turn = MotorPair("B", "E") right_turn = MotorPair("A", "F") vm.system.sound.play("/extra_files/Humming") for i in range(2): right_turn.move(100, "degrees", steering=100, speed=50) right_turn.move(-100, "degrees", steering=100, speed=50) left_turn.move(100, "degrees", steering=-100, speed=50) left_turn.move(-100, "degrees", steering=-100, speed=50) wheels = MotorPair("A", "E") wheels.move(14 * math.pi, "cm", steering=100, speed=80) wheels.move(14 * math.pi, "cm", steering=-100, speed=80) right_hand.run_to_position(270, direction="counterclockwise") wait_for_seconds(1) right_hand.run_for_seconds(1, 50) await vm.system.sound.play_async("/extra_files/Tadaa") wait_for_seconds(1) right_hand.run_to_position(0, direction="counterclockwise")
def clockwise(): for i in range(3): hub.display.show(hub.Image("00900:00000:00000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:00009:00090")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:00009:00990")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:90009:09990")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:90009:90009:09990")) utime.sleep(0.1) hub.display.show(hub.Image("09990:90009:90009:90009:09990")) utime.sleep(0.15)
def screenshot(): """ Take a screenshot of the current screen and return the Image. :return Image: An image representing the current pixels on the screen """ rows = [] for row_i in range(DISPLAY_HEIGHT): row = [] for column_i in range(DISPLAY_WIDTH): row.append(hub.display.get_pixel(column_i, row_i)) rows.append(''.join(str(pixel) for pixel in row)) return hub.Image(':'.join(rows))
async def stack_1(vm, stack): hub.led(*number_color_to_rgb(9)) rotate_hub_display_to_value("2") global g_animation brightness = vm.store.display_brightness() frames = [ hub.Image(convert_animation_frame(frame, brightness)) for frame in g_animation ] vm.system.display.show(frames, clear=False, delay=200, loop=True, fade=2) vm.system.sound.play("/extra_files/Scanning", freq=pitch_to_freq(vm.store.sound_pitch(), 12000, 16000, 20000)) (acceleration, deceleration) = vm.store.motor_acceleration("D") vm.store.motor_last_status( "D", await vm.system.motors.on_port("D").run_for_degrees_async( 500, vm.store.motor_speed("D"), stall=vm.store.motor_stall("D"), stop=vm.store.motor_stop("D"), acceleration=acceleration, deceleration=deceleration)) (acceleration_1, deceleration_1) = vm.store.motor_acceleration("D") vm.store.motor_last_status( "D", await vm.system.motors.on_port("D").run_for_degrees_async( 1000, -vm.store.motor_speed("D"), stall=vm.store.motor_stall("D"), stop=vm.store.motor_stop("D"), acceleration=acceleration_1, deceleration=deceleration_1)) (acceleration_2, deceleration_2) = vm.store.motor_acceleration("D") vm.store.motor_last_status( "D", await vm.system.motors.on_port("D").run_for_degrees_async( 500, vm.store.motor_speed("D"), stall=vm.store.motor_stall("D"), stop=vm.store.motor_stop("D"), acceleration=acceleration_2, deceleration=deceleration_2))
async def stack_1(vm, stack): hub.led(*number_color_to_rgb(9)) rotate_hub_display_to_value("2") global g_animation brightness = vm.store.display_brightness() frames = [hub.Image(convert_animation_frame(frame, brightness)) for frame in g_animation] vm.system.display.show(frames, clear=False, delay=200, loop=True, fade=2) await bigBotCalibrate(vm) (acceleration, deceleration) = vm.store.motor_acceleration("D") vm.store.motor_last_status("D", await vm.system.motors.on_port("D").run_for_degrees_async(648, vm.store.motor_speed("D"), stall=vm.store.motor_stall("D"), stop=vm.store.motor_stop("D"), acceleration=acceleration, deceleration=deceleration)) port = getattr(hub.port, "F", None) if getattr(port, "device", None) and is_type("F", 62): port.device.mode(5, bytes("".join([chr(percent_to_int(round(clamp(to_number(p), 0, 100)), 87)) for p in "100 100 100 100".split()]), "utf-8")) while True: sensor_value = get_sensor_value("F", 0, 200, (62,)) if sensor_value is None: sensor_value = 200 if sensor_value < 10: break yield 0 await vm.system.sound.play_async("/extra_files/Target Acquired", freq=pitch_to_freq(vm.store.sound_pitch(), 12000, 16000, 20000)) (acceleration_1, deceleration_1) = vm.store.motor_acceleration("D") vm.store.motor_last_status("D", await vm.system.motors.on_port("D").run_for_degrees_async(648, -vm.store.motor_speed("D"), stall=vm.store.motor_stall("D"), stop=vm.store.motor_stop("D"), acceleration=acceleration_1, deceleration=deceleration_1))
hub.sound.volume(10) hub.sound.beep(2000, 500, 3) hub.display.show('LEGO ', loop=False, delay=500, clear = True, fade = 0, callback = beep()) # you can set individual ixels hub.display.set_pixel(2,3,9) # x,y, brightness - 0-9 hub.display.get_pixel(2,3) hub.display.clear() hub.display.rotation(90) # can be 0, 90, 180, 270 hub.display.show(hub.Image.HAPPY) utime.sleep_ms(3000) hub.display.rotation(0) hub.display.show(hub.Image.HAPPY) hub.display.show([hub.Image("09090:99999:99999:09990:00900"),hub.Image("01010:11111:11111:01110:00100")],fade=2,loop=1,delay=500) hub.display.show(hub.Image.ALL_CLOCKS, loop=True, delay=100) # note loops forever hub.display.show('LEGO ', loop=True, delay=500) hub.display.show('LEGO ', loop=True, fade=4, delay=1000) hub.display.show(hub.Image.HAPPY, fade=5, delay=2000) hub.display.show(hub.Image.HAPPY, fade=6, delay=2000)
def f2(): print("running f2") hub.display.show( hub.Image("99099:90099:99000:90099:90099") ) beep(2)
# %% [markdown] # # How to display an image # Displaying an image is quite simple. We just need to define which pixels we will turn on and at what intensity. # The pixel definition is done in a string in the shape # # `00000:00000:00000:00000:00000` # # where each number corresponds to a pixel. Each pixel can have a value between `0` (off) to `9` (on at full intensity). # Each group of numbers (from left to right) correspond to a row of the hub (from top to bottom). # Notice the groups (i.e., rows) are separated by a colon `:`. # # Therefore, if we want to turn on the central pixel of the hub at full intensity, we can do the following: # %% print("Displaying example image...") img_example = hub.Image('00000:00000:00900:00000:00000') hub.display.show(img_example) wait_for_seconds(5) print("DONE!") # %% [markdown] # # How to display an animation # After displaying an image, displaying an animation is quite straightforward, since an animation is # basically displaying a succession of images. # # In this example, we will display a very simple animation: a dot moving from top to bottom (with a tail). # However, the basic principle can be translated to more complicated animations. # # I am sure there are plenty of ways to display an animation, but I found a simple way to do this is the following. # # First, we will define the frame of the animation in a list.
''' The Image clas is used to create images that can be displayed easily on the device’s LED matrix. ''' import hub, utime hub.display.show(hub.Image.HAPPY) image = hub.Image("90009:" "09090:" "00900:" "09090:" "90009") hub.display.show(image) utime.sleep(1) # or image = hub.Image("90009\n09090\n00900\n09090\n90009") hub.display.show(image) utime.sleep(1) image = hub.Image(2, 2, b'\x08\x08\x08\x08') hub.display.show(image) utime.sleep(1) # all not working hub.Image.width() hub.Image.height()
""" This product is released under the MIT License (MIT) Copyright (c) 2019, LEGO System A/S, Aastvej 1, 7190 Billund, Denmark See LICENSE.txt for the full MIT license notice. """ import hub import utime from .settings import Sounds frame1 = hub.Image("00000:00000:09000:00000:00000") frame2 = hub.Image("00000:00000:07000:00000:00000") frame3 = hub.Image("00000:00000:07000:00009:00000") frame4 = hub.Image("00000:00000:07000:00007:00000") frame5 = hub.Image("00000:00000:07000:90007:00000") frame6 = hub.Image("00000:00000:07000:70007:00000") frame7 = hub.Image("00000:90000:07000:70007:00000") frame8 = hub.Image("00000:70000:07000:70007:00000") frame9 = hub.Image("00000:70000:07000:70007:00900") frame10 = hub.Image("00000:70000:07000:70007:00700") frame11 = hub.Image("00000:70900:07000:70007:00700") frame12 = hub.Image("00090:70800:07000:70007:00700") frame13 = hub.Image("00080:70800:07000:79007:00700") frame14 = hub.Image("00080:70700:07090:78007:00700") frame15 = hub.Image("00070:70700:07080:78007:90700") frame16 = hub.Image("09070:70700:07070:77007:80700") frame17 = hub.Image("08070:70700:07070:77007:80709") frame18 = hub.Image("08079:70700:07070:77007:70708") frame19 = hub.Image("07078:70700:07070:77907:70708")
if i == count: brightness = "4" elif i == target: brightness = "0" result.append(brightness) x += 1 if x == 6: x = 1 y -= 1 return hub.Image("".join(result)) # hub.display.show(busy_circle, delay=50, fade=1, loop=True) busy_circle = [ hub.Image("00900:00000:00000:00000:00000"), hub.Image("00090:00000:00000:00000:00000"), hub.Image("00000:00009:00000:00000:00000"), hub.Image("00000:00000:00009:00000:00000"), hub.Image("00000:00000:00000:00009:00000"), hub.Image("00000:00000:00000:00000:00090"), hub.Image("00000:00000:00000:00000:00900"), hub.Image("00000:00000:00000:00000:09000"), hub.Image("00000:00000:00000:90000:00000"), hub.Image("00000:00000:90000:00000:00000"), hub.Image("00000:90000:00000:00000:00000"), hub.Image("09000:00000:00000:00000:00000"), ]
# you can set individual ixels hub.display.pixel(2, 3, 9) # x,y, brightness - 0-9 hub.display.pixel(2, 3) # returns pixel value hub.display.clear() hub.display.rotation(90) # can be 0, 90, 180, 270 hub.display.show(hub.Image.HAPPY) utime.sleep_ms(3000) hub.display.rotation(0) hub.display.show(hub.Image.HAPPY) hub.display.show([ hub.Image("09090:99999:99999:09990:00900"), hub.Image("01010:11111:11111:01110:00100") ], fade=2, loop=1, delay=500) hub.display.show(hub.Image.ALL_CLOCKS, loop=True, delay=100) # note loops forever hub.display.show('LEGO ', loop=True, delay=500) hub.display.show('LEGO ', loop=True, fade=4, delay=1000) hub.display.show(hub.Image.HAPPY, fade=5, delay=2000)
print("DONE!") # %% [markdown] # Then, let's proceed with the actual fire! # %% print("Firing blasters...") n_blasters = 3 for ii in range(0, n_blasters): # Play blaster sound. hub.sound.play("/extra_files/Laser") # Display blaster animation. for ii in range(0, n_frames): img = hub.Image(frames[ii]) hub.display.show(img) wait_for_seconds(t_pause) wait_for_seconds(0.5) print("DONE!") # %% print("Target eliminated.") # %% print("-" * 15 + " Execution ended " + "-" * 15 + "\n")
def counterclockwise(): for i in range(3): hub.display.show(hub.Image("00900:00000:00000:00000:00000")) utime.sleep(0.1) hub.display.show(hub.Image("09990:90009:90009:90009:09990")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:90009:90009:09990")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:90009:09990")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:00009:00990")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:00009:00090")) utime.sleep(0.1) hub.display.show(hub.Image("00990:00009:00009:00000:00000")) utime.sleep(0.15) def forward(): for i in range(3): hub.display.show(hub.Image("99999:99999:99999:99999:99999")) utime.sleep(0.3) hub.display.show(hub.Image("00000:09990:09990:09990:00000")) utime.sleep(0.3) hub.display.show(hub.Image("00000:00000:00900:00000:00000")) utime.sleep(0.5) def backward(): for i in range(3):
for f in frames: start_time_ms = time.ticks_us() img = hub.Image(f) hub.display.show(img) while ((time.ticks_us() - start_time_ms) / 1000 < delay_ms): #print((time.ticks_us() - start_time_us) * 1000) yield if not loop: return # Write your program here. mshub.status_light.on('red') hub.display.clear() hub.display.rotation(90) #anim=run_animation(animation_scanning) anim = [] for frame in animation_scanning: img = hub.Image(frame) anim.append(img) hub.display.show(anim) #while True: # try: # anim.send(None) # # do something else... # #print('yield') # except StopIteration as e: # print('animation finished') # break
# slot1module - Put this is slot 1 import hub, time, sys, util def beep(count=1): for _ in range(count): hub.sound.beep(freq=1000, time=25) time.sleep_ms(100) def import_from_slot(slot): # sys.path is typically ['', '_slash_lib'] # add /projects, this is persistent until hub is rebooted if '/projects' not in sys.path: sys.path.append('/projects') # Now import the module path = util.storage.get_path(slot) # './projects/40117' names = path.split('/') # ['.','projects','40117'] name = names[-1] # '40117' return __import__(name) print("loaded slot1module") hub.display.show(hub.Image("99999:90909:00000:00990:009900")) beep(1) time.sleep_ms(1000) m = import_from_slot(2) m.f2()
def test_hub(): assert hub.Image("abc") == "abc"
def wave(): for i in range(2): hub.display.show(hub.Image("00009:00009:00009:00009:00009")) utime.sleep(0.1) hub.display.show(hub.Image("00099:00099:00099:00099:00099")) utime.sleep(0.1) hub.display.show(hub.Image("00999:00999:00999:00999:00999")) utime.sleep(0.1) hub.display.show(hub.Image("09999:09999:09999:09999:09999")) utime.sleep(0.1) hub.display.show(hub.Image("99999:99999:99999:99999:99999")) utime.sleep(0.1) hub.display.show(hub.Image("99990:99990:99990:99990:99990")) utime.sleep(0.1) hub.display.show(hub.Image("99900:99900:99900:99900:99900")) utime.sleep(0.1) hub.display.show(hub.Image("99000:99000:99000:99000:99000")) utime.sleep(0.1) hub.display.show(hub.Image("90000:90000:90000:90000:90000")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00000:00000:00000")) utime.sleep(0.15) hub.display.show(hub.Image("90000:90000:90000:90000:90000")) utime.sleep(0.1) hub.display.show(hub.Image("99000:99000:99000:99000:99000")) utime.sleep(0.1) hub.display.show(hub.Image("99900:99900:99900:99900:99900")) utime.sleep(0.1) hub.display.show(hub.Image("99990:99990:99990:99990:99990")) utime.sleep(0.1) hub.display.show(hub.Image("09999:09999:09999:09999:09999")) utime.sleep(0.1) hub.display.show(hub.Image("00999:00999:00999:00999:00999")) utime.sleep(0.1) hub.display.show(hub.Image("00099:00099:00099:00099:00099")) utime.sleep(0.1) hub.display.show(hub.Image("00009:00009:00009:00009:00009")) utime.sleep(0.1) hub.display.show(hub.Image("00000:00000:00000:00000:00000")) utime.sleep(0.15)
def test_hub2(): assert hub.display.show(hub.Image("abc")) == "abc"