-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python: add music browser experiment
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
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
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
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) |