Skip to content

Commit

Permalink
python: add music browser experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed May 14, 2024
1 parent d1c84ed commit 384e35e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Here we go, a sound browser!
- `button_b` scrolls right
- `pin_logo` replays the current sound

### [Browse All Music](./src/browse-all-music.py)

Here we go, a music browser!

- `button_a` scrolls left
- `button_b` scrolls right
- `pin_logo` replays the current sound

### [Press a Button](./src/press-a-button.py)

The buttons `button_a` and `button_b` have 3 methods:
Expand Down
38 changes: 38 additions & 0 deletions python/src/browse-all-music.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Imports go at the top
import music as Music
from microbit import *

songs: "list[str]" = [music for music in dir(Music) if music.isupper()]
index: int = -1
COUNT: int = len(songs)


def browse_left(index: int) -> int:
return (index - 1) % COUNT


def browse_right(index: int) -> int:
return (index + 1) % COUNT


# Code in a 'while True:' loop repeats forever
while True:
if button_a.was_pressed():
index = browse_left(index)
elif button_b.was_pressed():
index = browse_right(index)
elif pin_logo.is_touched():
print("INFO: not doing anything since the last song is already in a loop")

song: str

if index == -1:
song = "BA_DING"
else:
song = songs[index]

print("INFO: song: " + song)
display.scroll(song)
Music.play(getattr(Music, song))

sleep(1000)

0 comments on commit 384e35e

Please sign in to comment.