Skip to content

Commit

Permalink
python: update symbol browser experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed May 22, 2024
1 parent d2fee3b commit 9e6fc45
Showing 1 changed file with 94 additions and 32 deletions.
126 changes: 94 additions & 32 deletions python/src/browse-all-symbols.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,112 @@
# Imports go at the top
from microbit import *

images: "list[str]" = [image for image in dir(Image) if image.isupper()]
index: int = -1
COUNT: int = len(images)

class ImageBrowser:
LOGO: Image = Image.HAPPY

def browse_left(index: int) -> int:
"""Scroll carousel to the left.
PLAY: Image = Image(
"00000:"
"09900:"
"09090:"
"09900:"
"00000:"
) # fmt: off

:param index: the current position
:return: the new position
"""
return (index - 1) % COUNT
PAUSE: Image = Image(
"00000:"
"09090:"
"09090:"
"09090:"
"00000:"
) # fmt: off

STOP: Image = Image(
"00000:"
"09990:"
"09090:"
"09990:"
"00000:"
) # fmt: off

def browse_right(index: int) -> int:
"""Scroll carousel to the right.
FWD: Image = Image(
"00000:"
"90090:"
"99099:"
"90090:"
"00000:"
) # fmt: off

:param index: the current position
:return: the new position
"""
return (index + 1) % COUNT
BWD: Image = Image(
"00000:"
"09009:"
"99099:"
"09009:"
"00000:"
) # fmt: off

def __init__(self) -> None:
"""Initialize ."""

display.show(self.LOGO)
sleep(1_000)

self.index: int = 0
self.images: "list[str]" = [image for image in dir(Image) if image.isupper()]
self.image: str = self.images[self.index]
self.size: int = len(self.images)

print("INFO: images=" + str(self.images))

def browse_left(self) -> None:
"""Scroll carousel to the left."""

self.index = (self.index - 1) % self.size
self.image = self.images[self.index]

self.show()

def browse_right(self) -> None:
"""Scroll carousel to the right."""

self.index = (self.index + 1) % self.size
self.image = self.images[self.index]

self.show()

def show(self) -> None:
"""Show the current image."""

print("INFO: image: " + self.image)
display.scroll(self.image)

def play(self) -> None:
"""Play the current image."""

display.show(getattr(Image, self.image))


print()
print("Image Browser")
print()

app: ImageBrowser = ImageBrowser()


# Code in a 'while True:' loop repeats forever
while True:
display.show(Image.ARROW_E)
display.show(app.BWD)
sleep(500)
display.show(Image.ARROW_W)
display.show(app.FWD)
sleep(500)

if button_a.was_pressed():
index = browse_left(index)
elif button_b.was_pressed():
index = browse_right(index)

image: str

if index == -1:
image = "SAD"
else:
image = images[index]

print("INFO: image: " + image)
display.scroll(image)
display.show(getattr(Image, image))
app.show()
sleep(500)

app.play()
sleep(1_000)

if button_a.was_pressed():
app.browse_left()
elif button_b.was_pressed():
app.browse_right()

0 comments on commit 9e6fc45

Please sign in to comment.