|
| 1 | +import time |
| 2 | +import board |
| 3 | +import digitalio |
| 4 | +import displayio |
| 5 | +import audiocore |
| 6 | +from audiopwmio import PWMAudioOut as AudioOut |
| 7 | +import adafruit_imageload |
| 8 | +from adafruit_gizmo import tft_gizmo |
| 9 | +from adafruit_display_shapes.circle import Circle |
| 10 | + |
| 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 | + |
| 22 | +# setup for the Gizmo TFT |
| 23 | +display = tft_gizmo.TFT_Gizmo() |
| 24 | + |
| 25 | +# loading the background image |
| 26 | +bg_bitmap, bg_palette = adafruit_imageload.load("/clouds_bg.bmp", |
| 27 | + bitmap=displayio.Bitmap, |
| 28 | + palette=displayio.Palette) |
| 29 | +bg_grid = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette) |
| 30 | + |
| 31 | +# loading the crescent moon bitmap sequence |
| 32 | +bitmap, palette = adafruit_imageload.load("/moon_anime.bmp", |
| 33 | + bitmap=displayio.Bitmap, |
| 34 | + palette=displayio.Palette) |
| 35 | +# makes the black background transparent so we only see the cresent moon |
| 36 | +palette.make_transparent(0) |
| 37 | + |
| 38 | +tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette, width = 1, height = 1, |
| 39 | + tile_height = 120, tile_width = 120, |
| 40 | + default_tile = 0) |
| 41 | + |
| 42 | +# two circles for the center "jewel" |
| 43 | +jewel_outline = Circle(x0=120, y0=120, r=40, fill=0xfbf236) |
| 44 | +jewel = Circle(x0=120, y0=120, r=35, fill=0xf70570) |
| 45 | + |
| 46 | +# adding the two jewel circle elements to a group |
| 47 | +jewel_splash = displayio.Group(max_size=20) |
| 48 | +jewel_splash.append(jewel_outline) |
| 49 | +jewel_splash.append(jewel) |
| 50 | + |
| 51 | +# making a group for the crescent moon sequence |
| 52 | +# scale is 2 because at full 240x240 resolution image is too big |
| 53 | +moon_group = displayio.Group(scale = 2) |
| 54 | +# group to hold all of the display elements |
| 55 | +main_group = displayio.Group() |
| 56 | + |
| 57 | +# adding the crescent moon tile grid to the moon group |
| 58 | +moon_group.append(tile_grid) |
| 59 | +# adding the background to the main group |
| 60 | +main_group.append(bg_grid) |
| 61 | +# adding the moon group to the main group |
| 62 | +main_group.append(moon_group) |
| 63 | +# adding the jewel circles to the main group |
| 64 | +main_group.append(jewel_splash) |
| 65 | + |
| 66 | +# showing the main group on the display |
| 67 | +display.show(main_group) |
| 68 | + |
| 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 | + |
| 78 | +# tracks the tilegrid index location for the crescent moon |
| 79 | +moon = 0 |
| 80 | +# holds time.monotonic() |
| 81 | +crescent = 0 |
| 82 | +# a button debouncing |
| 83 | +a_pressed = False |
| 84 | +# b button debouncing |
| 85 | +b_pressed = False |
| 86 | +# tracks if music is playing |
| 87 | +music_playing = False |
| 88 | +# tracks if animation is paused |
| 89 | +animation_pause = False |
| 90 | + |
| 91 | +while True: |
| 92 | + # button debouncing |
| 93 | + if not button_a.value and a_pressed: |
| 94 | + a_pressed = False |
| 95 | + if not button_b.value and b_pressed: |
| 96 | + b_pressed = False |
| 97 | + # runs crescent moon animation |
| 98 | + if not music_playing and not animation_pause: |
| 99 | + # every .8 seconds... |
| 100 | + if (crescent + .8) < time.monotonic(): |
| 101 | + # the moon animation cycles |
| 102 | + tile_grid[0] = moon |
| 103 | + # moon is the tilegrid index location |
| 104 | + moon += 1 |
| 105 | + # resets timer |
| 106 | + crescent = time.monotonic() |
| 107 | + # resets tilegrid index |
| 108 | + if moon > 35: |
| 109 | + moon = 0 |
| 110 | + # if music is NOT playing and you press the a button... |
| 111 | + if not music_playing and (button_a.value and not a_pressed): |
| 112 | + # music begins playing and will loop |
| 113 | + speaker.play(wav, loop = True) |
| 114 | + a_pressed = True |
| 115 | + # music_playing state is updated |
| 116 | + 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() |
| 123 | + a_pressed = True |
| 124 | + # music_playing state is updated |
| 125 | + music_playing = False |
| 126 | + # debugging REPL message |
| 127 | + print("music stopped") |
| 128 | + # if the animation IS playing and you press the b button... |
| 129 | + if not animation_pause and (button_b.value and not b_pressed): |
| 130 | + # the animation pauses by updating the animation_pause state |
| 131 | + animation_pause = True |
| 132 | + b_pressed = True |
| 133 | + # debugging REPL message |
| 134 | + print("animation paused") |
| 135 | + # if the animation is PAUSED and you press the b button... |
| 136 | + if animation_pause and (button_b.value and not b_pressed): |
| 137 | + # the animation begins again by updating the animation_pause state |
| 138 | + animation_pause = False |
| 139 | + b_pressed = True |
| 140 | + # debugging REPL message |
| 141 | + print("animation running again") |
0 commit comments