Skip to content

Commit 9124ee6

Browse files
committed
Changing code to use circuitplayground helper lib
Updated code to use the adafruit_circuitplayground helper library for audio and the buttons
1 parent 06a6c79 commit 9124ee6

File tree

1 file changed

+9
-40
lines changed
  • Circuit_Playground_Sailor_Moon_Star_Locket

1 file changed

+9
-40
lines changed

Circuit_Playground_Sailor_Moon_Star_Locket/code.py

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
import time
2-
import board
3-
import digitalio
42
import displayio
5-
import audiocore
6-
from audiopwmio import PWMAudioOut as AudioOut
3+
from adafruit_circuitplayground import cp
74
import adafruit_imageload
85
from adafruit_gizmo import tft_gizmo
96
from adafruit_display_shapes.circle import Circle
107

11-
# setup for the speaker
12-
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
13-
speaker_enable.switch_to_output(value=True)
14-
15-
# setup for the CP Bluefruit's buttons
16-
button_a = digitalio.DigitalInOut(board.BUTTON_A)
17-
button_a.switch_to_input(pull=digitalio.Pull.DOWN)
18-
19-
button_b = digitalio.DigitalInOut(board.BUTTON_B)
20-
button_b.switch_to_input(pull=digitalio.Pull.DOWN)
21-
228
# setup for the Gizmo TFT
239
display = tft_gizmo.TFT_Gizmo()
2410

@@ -66,15 +52,6 @@
6652
# showing the main group on the display
6753
display.show(main_group)
6854

69-
# setting up the audio output
70-
speaker = AudioOut(board.SPEAKER)
71-
# importing the .wav file
72-
moonlight = "/moonlight_densetsu.wav"
73-
# function to open the .wav file
74-
data = open(moonlight, "rb")
75-
# decoding the .wav file
76-
wav = audiocore.WaveFile(data)
77-
7855
# tracks the tilegrid index location for the crescent moon
7956
moon = 0
8057
# holds time.monotonic()
@@ -90,9 +67,9 @@
9067

9168
while True:
9269
# button debouncing
93-
if not button_a.value and a_pressed:
70+
if not cp.button_a and a_pressed:
9471
a_pressed = False
95-
if not button_b.value and b_pressed:
72+
if not cp.button_b and b_pressed:
9673
b_pressed = False
9774
# runs crescent moon animation
9875
if not music_playing and not animation_pause:
@@ -108,32 +85,24 @@
10885
if moon > 35:
10986
moon = 0
11087
# if music is NOT playing and you press the a button...
111-
if not music_playing and (button_a.value and not a_pressed):
88+
if not music_playing and (cp.button_a and not a_pressed):
11289
# music begins playing and will loop
113-
speaker.play(wav, loop = True)
114-
a_pressed = True
115-
# music_playing state is updated
11690
music_playing = True
117-
# debugging REPL message
118-
print("music playing")
119-
# if music IS playing and you press the a button...
120-
if music_playing and (button_a.value and not a_pressed):
121-
# music stops
122-
speaker.stop()
12391
a_pressed = True
92+
print("music playing")
93+
# song plays once
94+
cp.play_file("moonlight_densetsu.wav")
12495
# music_playing state is updated
12596
music_playing = False
126-
# debugging REPL message
127-
print("music stopped")
12897
# if the animation IS playing and you press the b button...
129-
if not animation_pause and (button_b.value and not b_pressed):
98+
if not animation_pause and (cp.button_b and not b_pressed):
13099
# the animation pauses by updating the animation_pause state
131100
animation_pause = True
132101
b_pressed = True
133102
# debugging REPL message
134103
print("animation paused")
135104
# if the animation is PAUSED and you press the b button...
136-
if animation_pause and (button_b.value and not b_pressed):
105+
if animation_pause and (cp.button_b and not b_pressed):
137106
# the animation begins again by updating the animation_pause state
138107
animation_pause = False
139108
b_pressed = True

0 commit comments

Comments
 (0)