from buzzer_music import music
from time import sleep, ticks_ms, ticks_diff
from machine import Pin, I2C
import ssd1306
# Song titles for the menu (corresponding to the songs list)
song_titles = [
"Shake It Off",
"Firestone",
"Blinding Lights",
"Bad Romance"
]
# Define the song data
songs = [
# Taylor Swift - Shake It Off
{
'notes': '12 A3 1 51;16 A3 1 51;20 A3 1 51 0.5984252095222473;22 A3 1 51;24 A3 1 51;28 A3 1 51;44 C4 1 51;48 C4 1 51;52 C4 1 51 0.5984252095222473;54 C4 1 51;56 C4 1 51;60 C4 1 51;112 G3 1 51;140 A3 1 51;144 A3 1 51;148 A3 1 51 0.5984252095222473;150 A3 1 51;152 A3 1 51;156 A3 1 51;176 C4 1 51;180 C4 1 51 0.5984252095222473;182 C4 1 51;184 C4 1 51;208 G3 1 51;240 G3 1 51;278 A3 1 51;291 G3 1 51;295 C4 1 51;303 C4 1 51;308 C4 1 51 0.5984252095222473;334 G3 1 51;0 D7 2 24;2 B6 2 24;4 A6 2 24;6 G6 2 24;8 B6 4 24;30 D6 2 24;32 D7 2 24;34 B6 2 24;36 A6 2 24;38 G6 2 24;40 B6 4 24;64 D7 2 24;66 B6 2 24;68 A6 2 24;70 G6 2 24;72 B6 2 24;74 A6 2 24;76 G6 4 24;84 E6 4 24;88 D6 4 24;96 D7 2 24;98 B6 2 24;100 A6 2 24;102 G6 2 24;104 B6 2 24;106 A6 2 24;108 G6 4 24;116 E6 4 24;120 D6 4 24;124 B5 2 24;126 D6 2 24;128 D7 2 24;130 B6 2 24;132 A6 2 24;134 G6 2 24;136 B6 4 24;158 D6 2 24;160 D7 2 24;162 B6 2 24;164 A6 2 24;166 G6 2 24;168 B6 4 24;186 D6 2 24;188 D6 4 24;192 D7 2 24;194 B6 2 24;196 A6 2 24;198 G6 2 24;200 B6 2 24;202 A6 2 24;204 G6 4 24;212 E6 4 24;216 D6 4 24;224 D7 2 24;226 B6 2 24;228 A6 2 24;230 G6 2 24;232 B6 2 24;234 A6 2 24;236 G6 4 24;244 E6 4 24;248 D6 3 24;252 B6 4 24;256 A6 4 24;260 G6 4 24;264 A6 6 24;272 E6 6 24;280 A6 3 24;284 B6 3 24;288 A6 3 24;292 G6 3 24;296 A6 6 24;304 E6 4 24;310 E6 2 24;312 A6 3 24;316 B6 3 24;320 A6 3 24;324 G6 3 24;328 A6 6 24;336 G6 6 24;344 A6 3 24;348 B6 3 24;352 G6 3 24;356 E6 2 24;358 G6 2 24;360 A6 3 24;364 A6 2 24;366 B6 2 24;368 A6 2 24;370 G6 4 24;374 G6 6 24;388 E6 2 24;390 G6 2 24;392 A6 2 24;394 A6 2 24;396 A6 2 24;398 B6 2 24;400 G6 4 24;420 E6 2 24;422 G6 2 24;424 A6 2 24;426 A6 2 24;428 A6 2 24;430 B6 2 24;432 G6 4 24;456 A6 2 24;458 A6 2 24;460 A6 2 24;462 B6 2 24;464 G6 4 24;486 G6 2 24;488 A6 2 24;490 B6 2 24;492 G6 4 24;498 G6 2 24;500 A6 2 24;502 B6 2 24;504 G6 4 24;516 G6 4 24;520 A6 2 24;522 A6 2 24;524 A6 2 24;526 B6 2 24;528 G6 4 24;548 E6 2 24;550 G6 2 24;552 A6 2 24;554 A6 2 24;556 A6 2 24;558 B6 2 24;560 G6 4 24;580 E6 2 24;582 G6 2 24;584 A6 2 24;586 A6 2 24;588 A6 2 24;590 B6 2 24;592 G6 4 24;614 G6 2 24;616 A6 2 24;618 B6 2 24;620 G6 4 24;626 G6 2 24;628 A6 2 24;630 B6 2 24;632 G6 4 24;468 D6 4 47;472 D6 4 47;476 B5 4 47;480 A5 4 47;484 G5 2 47;532 D6 4 47;536 D6 4 47;540 B5 4 47;544 A5 4 47;564 D6 4 47;568 D6 4 47;572 B5 4 47;576 A5 4 47;596 D6 4 47;600 D6 4 47;604 B5 4 47;608 A5 4 47;508 D7 2 47;510 E7 4 47;514 G7 4 47;80 G3 1 51;92 G3 1 51;220 G3 1 51;404 D6 4 47;408 D6 4 47;412 B5 4 47;416 A5 4 47;436 D6 4 47;440 D6 4 47;444 B5 4 47;448 A5 4 47;452 G5 4 47;612 G5 2 47;380 G3 1 51;384 G3 1 51;172 C4 1 51;251 G3 1 51;271 A3 1 51;355 G3 1 51',
'flashing_interval': 240,
'stop_after': 77000,
'lyrics': [
{"t": 0, "l": "I stay out"},
{"t": 1462, "l": "too late"},
{"t": 3410, "l": "Got nothin'"},
{"t": 4872, "l": "in my brain"},
{"t": 7308, "l": "That's what"},
{"t": 8526, "l": "people say"},
{"t": 10962, "l": "Mmm-mmm"},
{"t": 13400, "l": "That's what"},
{"t": 14618, "l": "people say"},
{"t": 17056, "l": "Mmm-mmm"},
{"t": 19494, "l": "I go on"},
{"t": 20712, "l": "too many"},
{"t": 21930, "l": "dates"},
{"t": 24368, "l": "But I can't"},
{"t": 25586, "l": "make 'em"},
{"t": 26804, "l": "stay"},
{"t": 29242, "l": "At least"},
{"t": 30460, "l": "that's what"},
{"t": 31678, "l": "people say"},
{"t": 34116, "l": "Mmm-mmm"},
{"t": 36554, "l": "That's what"},
{"t": 37772, "l": "people say"},
{"t": 40210, "l": "Mmm-mmm"},
{"t": 42648, "l": "But I keep"},
{"t": 43866, "l": "cruisin'"},
{"t": 46304, "l": "Can't stop,"},
{"t": 47522, "l": "won't stop"},
{"t": 48740, "l": "movin'"},
{"t": 51178, "l": "It's like I"},
{"t": 52396, "l": "got this"},
{"t": 53614, "l": "music"},
{"t": 56052, "l": "In my mind"},
{"t": 57270, "l": "sayin' it's"},
{"t": 58488, "l": "gonna be"},
{"t": 59706, "l": "alright"},
{"t": 62144, "l": "'Cause the"},
{"t": 63362, "l": "players"},
{"t": 64580, "l": "gonna play"},
{"t": 67018, "l": "play, play,"},
{"t": 68236, "l": "play, play"},
{"t": 70674, "l": "And the"},
{"t": 71892, "l": "haters"},
{"t": 73110, "l": "gonna hate"},
{"t": 75548, "l": "hate, hate,"},
{"t": 76766, "l": "hate, hate"}
]
},
# Kygo - Firestone
{
'notes': '0 D6 3 7;3 B5 1 7;6 B5 2 7;8 D6 2 7;10 B5 1 7;12 E6 1 7;13 F#6 3 7;16 E6 3 7;19 E6 1 7;22 A5 2 7;25 A5 1 7;26 A6 2 7;28 F#6 1 7;30 E6 1 7;32 D6 3 7;35 B5 1 7;38 B5 2 7;40 D6 2 7;42 B5 1 7;44 E6 1 7;45 F#6 3 7;48 E6 3 7;51 E6 1 7;54 D6 2 7;57 D6 1 7;58 F#6 2 7;60 E6 2 7;62 D6 1 7;64 D6 3 7;67 B5 1 7;70 B5 2 7;72 D6 2 7;74 B5 1 7;76 E6 1 7;77 F#6 3 7;80 E6 3 7;83 E6 1 7;86 A5 2 7;89 A5 1 7;90 A6 2 7;92 F#6 1 7;94 E6 1 7;96 D6 3 7;99 D6 1 7;102 D6 2 7;104 D6 2 7;106 D6 2 7;108 E6 1 7;109 F#6 3 7;112 D6 3 7;126 D6 2 7;125 E6 1 7;124 D6 1 7;122 E6 1 7;120 F#6 1 7;118 A6 1 7;119 D6 1 7;117 D6 1 7;115 D6 1 7;0 D6 3 7;3 B5 1 7;6 B5 2 7;8 D6 2 7;10 B5 1 7;12 E6 1 7;13 F#6 3 7;16 E6 3 7;19 E6 1 7;22 A5 2 7;25 A5 1 7;26 A6 2 7;28 F#6 1 7;30 E6 1 7;32 D6 3 7;35 B5 1 7;38 B5 2 7;40 D6 2 7;42 B5 1 7;44 E6 1 7;45 F#6 3 7;48 E6 3 7;51 E6 1 7;54 D6 2 7;57 D6 1 7;58 F#6 2 7;60 E6 2 7;62 D6 1 7;64 D6 3 7;67 B5 1 7;70 B5 2 7;72 D6 2 7;74 B5 1 7;76 E6 1 7;77 F#6 3 7;80 E6 3 7;83 E6 1 7;86 A5 2 7;89 A5 1 7;90 A6 2 7;92 F#6 1 7;94 E6 1 7;96 D6 3 7;99 D6 1 7;102 D6 2 7;104 D6 2 7;106 D6 2 7;108 E6 1 7;109 F#6 3 7;112 D6 3 7;126 D6 2 7;125 E6 1 7;124 D6 1 7;122 E6 1 7;120 F#6 1 7;118 A6 1 7;119 D6 1 7;117 D6 1 7;115 D6 1 7;0 D6 3 7;3 B5 1 7;6 B5 2 7;8 D6 2 7;10 B5 1 7;12 E6 1 7;13 F#6 3 7;16 E6 3 7;19 E6 1 7;22 A5 2 7;25 A5 1 7;26 A6 2 7;28 F#6 1 7;30 E6 1 7;32 D6 3 7;35 B5 1 7;38 B5 2 7;40 D6 2 7;42 B5 1 7;44 E6 1 7;45 F#6 3 7;48 E6 3 7;51 E6 1 7;54 D6 2 7;57 D6 1 7;58 F#6 2 7;60 E6 2 7;62 D6 1 7;64 D6 3 7;67 B5 1 7;70 B5 2 7;72 D6 2 7;74 B5 1 7;76 E6 1 7;77 F#6 3 7;80 E6 3 7;83 E6 1 7;86 A5 2 7;89 A5 1 7;90 A6 2 7;92 F#6 1 7;94 E6 1 7;96 D6 3 7;99 D6 1 7;102 D6 2 7;104 D6 2 7;106 D6 2 7;108 E6 1 7;109 F#6 3 7;112 D6 3 7;126 D6 2 7;125 E6 1 7;124 D6 1 7;122 E6 1 7;120 F#6 1 7;118 A6 1 7;119 D6 1 7;117 D6 1 7;115 D6 1 7',
'flashing_interval': 240,
'stop_after': 31003,
'lyrics': [
{"t": 0, "l": " "},
]
},
# The Weeknd - Blinding Lights
{
'notes': '128 C6 4 14;158 A#5 2 14;154 A#5 2 14;156 C6 2 14;152 C6 2 14;160 C6 4 14;164 A#5 2 14;166 A#5 4 14;170 C6 2 14;172 G5 4 14;190 A#5 2 14;186 A#5 2 14;188 C6 2 14;184 C6 2 14;192 C6 4 14;196 A#5 2 14;198 A#5 4 14;202 C6 2 14;204 G5 4 14;216 A#5 4 14;220 C6 4 14;224 F5 4 14;240 D#6 4 14;244 D6 4 14;248 C6 4 14;252 A#5 4 14;256 C6 4 14;286 A#5 2 14;282 A#5 2 14;284 C6 2 14;280 C6 2 14;288 C6 4 14;292 A#5 2 14;294 A#5 4 14;298 C6 2 14;300 G5 4 14;318 A#5 2 14;314 A#5 2 14;316 C6 2 14;312 C6 2 14;320 C6 4 14;324 A#5 2 14;326 A#5 4 14;330 C6 2 14;332 G5 4 14;344 A#5 4 14;348 C6 4 14;352 F5 4 14;388 C5 4 14;392 D#5 2 14;394 F5 6 14;400 G5 4 14;404 C5 4 14;408 D#5 2 14;412 F5 4 14;420 C5 4 14;424 D#5 2 14;426 F5 6 14;432 G5 4 14;436 C5 4 14;440 D#5 2 14;444 F5 4 14;452 C5 4 14;456 D#5 2 14;458 F5 6 14;464 G5 4 14;468 C5 4 14;472 D#5 2 14;476 F5 4 14;480 D5 2 14;488 C5 2 14;502 A#5 4 14;368 D#6 4 14;372 D6 4 14;376 C6 4 14;380 A#5 4 14;512 F5 25 14;538 F5 2 14 1.5;540 G5 4 14;544 F5 4 14;550 D#5 4 14;548 D#5 2 14;554 C5 2 14;556 D#5 4 14;572 D#5 4 14 1.100000023841858;576 A#5 2 14 1.100000023841858;578 G5 4 14 1.100000023841858;582 F5 4 14 1.100000023841858;586 D#5 4 14 1.100000023841858;592 A#5 2 14 1.100000023841858;594 G5 4 14 1.100000023841858;598 F5 4 14 1.100000023841858;602 D#5 4 14 1.100000023841858;608 F5 4 14 1.100000023841858;640 F5 25 14;666 F5 2 14 1.5;668 G5 4 14;672 F5 4 14;678 D#5 4 14;676 D#5 2 14;682 C5 2 14;684 D#5 4 14;700 D#5 4 14 1.100000023841858;704 A#5 2 14 1.100000023841858;706 G5 4 14 1.100000023841858;710 F5 4 14 1.100000023841858;714 D#5 4 14 1.100000023841858;720 A#5 2 14 1.100000023841858;722 G5 4 14 1.100000023841858;726 F5 4 14 1.100000023841858;730 D#5 4 14 1.100000023841858;736 F5 4 14 1.100000023841858;16 F5 2 14;22 C5 4 14;32 F5 4 14;40 F5 4 14;46 D#5 2 14;48 F5 2 14;50 G5 4 14;58 D#5 6 14;8 F5 4 14;0 F5 4 14;14 D#5 2 14;18 G5 3 14;26 D#5 6 14;54 C5 4 14;80 A#5 2 14;82 G5 3 14;86 F5 2 14;90 D#5 4 14;74 D#5 4 14;70 F5 2 14;66 G5 3 14;64 A#5 2 14;96 F5 6 14;510 C5 2 14',
'flashing_interval': 240,
'stop_after': 77000,
'lyrics': [
{"t": 0, "l": "I've been"},
{"t": 836, "l": "tryna call"},
{"t": 1672, "l": "I've been"},
{"t": 2508, "l": "on my own"},
{"t": 3344, "l": "for too long"},
{"t": 10032, "l": "I'm blinded"},
{"t": 13391, "l": "by the lights"},
{"t": 16750, "l": "No, I can't"},
{"t": 20109, "l": "sleep until"},
{"t": 23468, "l": "I feel your"},
{"t": 26827, "l": "touch"},
{"t": 29242, "l": "I'm drowning"},
{"t": 32601, "l": "in the night"},
{"t": 35960, "l": "When I'm like"},
{"t": 39319, "l": "this, you're"},
{"t": 42678, "l": "the one I"},
{"t": 46037, "l": "trust"},
{"t": 66976, "l": "I'm blinded"},
{"t": 70335, "l": "by the lights"},
{"t": 73694, "l": "No, I can't"}
]
},
# Lady Gaga - Bad Romance
{
'notes': '8 F5 6 43;14 E5 2 43;16 F5 2 43;18 E5 2 43;20 D5 10 43;32 C5 4 43;36 D5 4 43;30 B4 2 43;40 E5 4 43;44 E5 2 43;46 D5 2 43;48 E5 2 43;50 D5 2 43;52 C5 2 43;54 C5 4 43;70 C5 2 43;64 C5 2 43;66 D5 2 43;68 E5 2 43;72 F5 6 43;78 E5 2 43;80 F5 2 43;82 E5 2 43;84 D5 10 43;96 C5 4 43;100 D5 4 43;94 B4 2 43;104 E5 4 43;108 E5 2 43;110 D5 2 43;112 E5 2 43;114 D5 2 43;116 C5 2 43;118 C5 4 43;136 A4 4 43;140 A4 4 43;144 E5 2 43;146 E5 2 43;148 F5 2 43;150 E5 2 43;154 A4 2 43;156 A4 4 43;160 E5 2 43;162 E5 2 43;164 F5 2 43;166 E5 2 43;168 A4 4 43;172 A4 4 43;176 E5 2 43;178 E5 2 43;180 F5 2 43;182 E5 2 43;186 C5 2 43;188 C5 2 43;190 A4 2 43;192 C5 2 43;194 A4 2 43;196 C5 4 43;200 A4 4 43;204 A4 4 43;208 E5 2 43;210 E5 2 43;212 F5 2 43;214 E5 2 43;218 A4 2 43;220 A4 4 43;224 E5 2 43;226 E5 2 43;228 F5 2 43;230 E5 2 43;232 A4 4 43;236 A4 4 43;240 E5 2 43;242 E5 2 43;244 F5 2 43;246 E5 2 43;250 C5 2 43;252 C5 2 43;254 A4 2 43;256 C5 2 43;258 A4 2 43;260 C5 4 43;6 C5 2 43;4 E5 2 43;2 D5 2 43;0 C5 2 43;266 A4 2 43;268 A4 2 43;270 G4 2 43;272 A4 2 43;274 A4 4 43;278 A4 2 43;280 C5 2 43;282 A4 2 43;284 G4 2 43;286 A4 2 43;288 G4 4 43;298 A4 2 43;300 A4 2 43;302 G4 2 43;304 A4 2 43;306 G4 2 43;308 A4 2 43;310 A4 2 43;312 C5 2 43;314 A4 2 43;316 G4 2 43;318 A4 2 43;320 G4 2 43;322 E4 2 43;324 G4 2 43;326 A4 2 43;328 A4 4 43;344 E5 4 43;348 C5 4 43;352 A4 2 43;354 E4 2 43;356 G4 2 43;358 A4 2 43;360 A4 4 43;510 F5 6 43;516 E5 2 43;518 F5 2 43;520 E5 2 43;522 D5 10 43;534 C5 4 43;538 D5 4 43;532 B4 2 43;542 E5 4 43;546 E5 2 43;548 D5 2 43;550 E5 2 43;552 D5 2 43;554 C5 2 43;556 C5 4 43;572 C5 2 43;566 C5 2 43;568 D5 2 43;570 E5 2 43;574 F5 6 43;580 E5 2 43;582 F5 2 43;584 E5 2 43;586 D5 10 43;598 C5 4 43;602 D5 4 43;596 B4 2 43;606 E5 4 43;610 E5 2 43;612 D5 2 43;614 E5 2 43;616 D5 2 43;618 C5 2 43;620 C5 4 43;508 C5 2 43;506 E5 2 43;504 D5 2 43;502 C5 2 43;384 A5 2 43;386 A5 2 43;388 A5 2 43;390 A5 2 43;392 A5 2 43;394 A5 2 43;396 A5 2 43;398 G5 2 43;400 G5 2 43;402 G5 2 43;404 D5 4 43;408 D5 2 43;410 D5 2 43;412 E5 4 43;416 E5 2 43;418 E5 2 43;420 E5 2 43;422 G5 4 43;426 D5 2 43;428 D5 2 43;430 C5 4 43;438 C5 2 43;440 D5 2 43;442 E5 2 43;444 C5 2 43;446 F5 2 43;468 D5 4 43;448 A5 2 43;450 A5 2 43;452 A5 2 43;454 A5 2 43;456 A5 2 43;458 G5 2 43;460 G5 2 43;462 G5 2 43;464 G5 2 43;466 G5 2 43;472 D5 2 43;474 D5 2 43;476 E5 4 43;480 E5 2 43;482 E5 2 43;484 E5 2 43;486 G#5 4 43;490 D5 2 43;492 D5 2 43;494 C5 4 43;542 C5 4 43;546 C5 2 43;548 B4 2 43;550 C5 2 43;552 B4 2 43;554 A4 2 43;556 A4 4 43;606 B4 4 43;610 B4 2 43;612 B4 2 43;614 B4 2 43;616 B4 2 43;618 A4 2 43;620 A4 4 43',
'flashing_interval': 240,
'stop_after': 75000,
'lyrics': [
{"t":0, "l":"Oh, caught in a bad romance"},
{"t":7744, "l":"Oh, caught in a bad romance"},
{"t":8712, "l":"Rah rah ah ah ah"},
{"t":10712, "l":"Roma roma ma"},
{"t":12712, "l":"Gaga ooh la la"},
{"t":14712, "l":"Want your bad romance"},
{"t":8712, "l":"Rah rah ah ah ah"},
{"t":10712, "l":"Roma roma ma"},
{"t":12712, "l":"Gaga ooh la la"},
{"t":14712, "l":"Want your bad romance"},
{"t":16456, "l":"I want your ugly"},
{"t":18456, "l":"I want your disease"},
{"t":20456, "l":"I want your everything"},
{"t":22456, "l":"as long as it's free"},
{"t":24456, "l":"I want your love"},
{"t":26456, "l":"Love, love, love"},
{"t":28456, "l":"I want your love"},
{"t":32186, "l":"You know that I want you"},
{"t":34186, "l":"And you know that I need you"},
{"t":36186, "l":"I want it bad"},
{"t":38186, "l":"your bad romance"},
{"t":41624, "l":"I want your love"},
{"t":43624, "l":"and I want your revenge"},
{"t":45624, "l":"You and me could write a bad romance"},
{"t":47624, "l":"I want your love"},
{"t":49624, "l":"and all your lover's revenge"},
{"t":51624, "l":"You and me could write a bad romance"},
{"t":53624, "l":"Oh-oh-oh-oh-oh"},
{"t":55624, "l":"Caught in a bad romance"},
{"t":57624, "l":"Oh-oh-oh-oh-oh"},
{"t":59624, "l":"Caught in a bad romance"},
{"t":60722, "l":"Rah rah ah ah ah"},
{"t":62722, "l":"Roma roma ma"},
{"t":64722, "l":"Gaga ooh la la"},
{"t":66722, "l":"Want your bad romance"}
],
}
]
# Display
i2c = I2C(0, scl=Pin(5), sda=Pin(4))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Setup the buzzer on pin GP0
mySong = None # Will be initialized when a song is selected
# Setup the button on pin GP1 as an input with a pull-down resistor
button = Pin(1, Pin.IN, Pin.PULL_DOWN)
# Setup the LED on pin GP2
led = Pin(2, Pin.OUT)
wristbands = Pin(6, Pin.OUT)
# Variables for menu and song selection
current_song_idx = 0
menu_mode = True
last_button_state = 0
last_debounce_time = 0
debounce_delay = 50 # milliseconds
button_press_start = 0
long_press_duration = 1000 # milliseconds for long press
waiting_for_release = False
# Variables for non-blocking LED blinking
led_state = 0
last_blink_time = 0
# Tracking stop and lyrics
start_time = 0
current_lyric_idx = 0
# Display the current menu selection
def update_menu_display():
oled.fill(0)
oled.text("Select Song:", 5, 5)
oled.text(song_titles[current_song_idx], 5, 25)
oled.text("Press to cycle", 5, 45)
oled.text("Hold to play", 5, 55)
oled.show()
# Display prompt to release button
def prompt_release_button():
oled.fill(0)
oled.text("Release button", 5, 15)
oled.text("to start song", 5, 25)
oled.show()
# Initialize menu display
update_menu_display()
# Main loop
while True:
current_time = ticks_ms()
button_state = button.value()
if menu_mode:
if waiting_for_release:
if button_state == 0:
# Button released, start the song
cur_song = songs[current_song_idx]
mySong = music(cur_song['notes'], pins=[Pin(0)])
mySong.restart()
start_time = current_time
current_lyric_idx = 0
menu_mode = False
waiting_for_release = False
oled.fill(0)
oled.text(cur_song['lyrics'][0]['l'], 5, 15)
oled.show()
else:
# Debounce button
if button_state != last_button_state:
last_debounce_time = current_time
last_button_state = button_state
if ticks_diff(current_time, last_debounce_time) > debounce_delay:
if button_state == 1:
if button_press_start == 0:
button_press_start = current_time
elif ticks_diff(current_time, button_press_start) > long_press_duration:
# Long press detected, prompt to release button
waiting_for_release = True
prompt_release_button()
else:
if button_press_start != 0 and ticks_diff(current_time, button_press_start) < long_press_duration:
# Short press, cycle to next song
current_song_idx = (current_song_idx + 1) % len(songs)
update_menu_display()
button_press_start = 0
else:
wristbands.value(1)
# Song playing mode
# Stop Music
if ticks_diff(current_time, start_time) > cur_song['stop_after']:
wristbands.value(0)
print("song done")
mySong.stop()
menu_mode = True
current_song_idx = 0
update_menu_display()
led.value(0)
led_state = 0
continue
# Start playing the music
mySong.tick()
# Update lyrics if available
if current_lyric_idx < len(cur_song['lyrics']) - 1:
next_lyric_time = cur_song['lyrics'][current_lyric_idx + 1]['t']
if ticks_diff(current_time, start_time) >= next_lyric_time:
current_lyric_idx += 1
oled.fill(0)
oled.text(cur_song['lyrics'][current_lyric_idx]['l'], 5, 15)
oled.show()
# Non-blocking LED blinking
if ticks_diff(current_time, last_blink_time) > cur_song['flashing_interval']:
last_blink_time = current_time
led_state = 1 - led_state
led.value(led_state)
# Check for button press to stop song and return to menu
if button_state != last_button_state:
last_debounce_time = current_time
last_button_state = button_state
if ticks_diff(current_time, last_debounce_time) > debounce_delay and button_state == 1:
mySong.stop()
menu_mode = True
current_song_idx = 0
update_menu_display()
led.value(0)
led_state = 0
# Wait to avoid busy-waiting
sleep(0.03)