|
| 1 | +# Lucio 2020 |
| 2 | +# Feather M4 + Propmaker + amps + lots of neopixels |
| 3 | +import board |
| 4 | +import busio |
| 5 | +from digitalio import DigitalInOut, Direction, Pull |
| 6 | +import audioio |
| 7 | +import audiomixer |
| 8 | +import audiomp3 |
| 9 | +import adafruit_lis3dh |
| 10 | +import neopixel |
| 11 | +from adafruit_led_animation.animation.solid import Solid |
| 12 | +from adafruit_led_animation.animation.comet import Comet |
| 13 | +from adafruit_led_animation.animation.pulse import Pulse |
| 14 | +from adafruit_led_animation.helper import PixelSubset |
| 15 | +from adafruit_led_animation.group import AnimationGroup |
| 16 | +from adafruit_led_animation.color import RED, ORANGE, WHITE |
| 17 | + |
| 18 | +ORANGE_DIM = 0x801400 # half value version |
| 19 | +RED_DIM = 0x800000 |
| 20 | + |
| 21 | +# ---Set Volume Max Here--- |
| 22 | +VOLUME_MULT = 0.65 # 1 = full volume, 0.1 is very quiet, 0 is muted |
| 23 | + |
| 24 | +# ---SWITCH/BUTTON SETUP--- |
| 25 | +mode_switch = DigitalInOut(board.D9) |
| 26 | +mode_switch.switch_to_input(pull=Pull.UP) |
| 27 | +mode_state = mode_switch.value |
| 28 | +trig_button = DigitalInOut(board.A4) |
| 29 | +trig_button.switch_to_input(pull=Pull.UP) |
| 30 | +alt_button = DigitalInOut(board.A5) |
| 31 | +alt_button.switch_to_input(pull=Pull.UP) |
| 32 | + |
| 33 | +# ---ACCELEROMETER SETUP--- |
| 34 | +# Set up accelerometer on I2C bus, 4G range: |
| 35 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 36 | +int1 = DigitalInOut(board.D6) |
| 37 | +accel = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) |
| 38 | + |
| 39 | +# ---SPEAKER SETUP--- |
| 40 | +enable = DigitalInOut(board.D10) |
| 41 | +enable.direction = Direction.OUTPUT |
| 42 | +enable.value = True |
| 43 | +# Set up speakers and mixer. Stereo files, where music has empty right channel, FX empty left |
| 44 | +speaker = audioio.AudioOut(board.A0, right_channel=board.A1) |
| 45 | +mixer = audiomixer.Mixer(channel_count=2, buffer_size=2304, sample_rate=22050) |
| 46 | + |
| 47 | +# ---NEOPIXEL SETUP--- |
| 48 | +pixel_pin = board.D5 |
| 49 | +pixel_num = 154 |
| 50 | +pixels = neopixel.NeoPixel( |
| 51 | + pixel_pin, pixel_num, brightness=0.6, auto_write=False, pixel_order=neopixel.GRBW |
| 52 | +) |
| 53 | +# ^ change pixel_order depending on RGB vs. RGBW pixels |
| 54 | + |
| 55 | + |
| 56 | +# ---Pixel Map--- |
| 57 | +# this is the physical order in which the strips are plugged |
| 58 | +pixel_stripA = PixelSubset(pixels, 0, 18) # 18 pixel strip |
| 59 | +pixel_stripB = PixelSubset(pixels, 18, 36) # 18 pixel strip |
| 60 | +pixel_jewel = PixelSubset(pixels, 36, 43) # 7 pixel jewel |
| 61 | +pixel_ringsAll = PixelSubset(pixels, 43, 151) # all of the rings |
| 62 | +# or use rings individually: |
| 63 | +# pixel_ringA = PixelSubset(pixels, 43, 59) # 16 pixel ring |
| 64 | +# pixel_ringB = PixelSubset(pixels, 59, 75) # 16 pixel ring |
| 65 | +# pixel_ringC = PixelSubset(pixels, 75, 91) # 16 pixel ring |
| 66 | +# pixel_ringD = PixelSubset(pixels, 91, 151) # 60 pixel ring |
| 67 | + |
| 68 | +# ---BPM--- |
| 69 | +BPM = 128 |
| 70 | +BEAT = 60 / BPM # quarter note beat |
| 71 | +b16TH = BEAT / 4 # 16TH note |
| 72 | +b64TH = BEAT / 16 # sixty-fourth |
| 73 | + |
| 74 | +# ---Anim Setup--- |
| 75 | +# heal color mode |
| 76 | +# Pulse 'speed' = smoothness |
| 77 | +pulse_rings_m0 = Pulse(pixel_ringsAll, speed=0.01, color=ORANGE, period=BEAT) |
| 78 | +pulse_jewel_m0 = Pulse(pixel_jewel, speed=0.01, color=ORANGE, period=BEAT) |
| 79 | +comet_stripA_m0 = Comet( |
| 80 | + pixel_stripA, speed=b64TH, color=ORANGE, tail_length=9, bounce=False |
| 81 | +) |
| 82 | +comet_stripB_m0 = Comet( |
| 83 | + pixel_stripB, speed=b64TH, color=ORANGE, tail_length=9, bounce=False |
| 84 | +) |
| 85 | + |
| 86 | +# speed color mode |
| 87 | +pulse_rings_m1 = Pulse(pixel_ringsAll, speed=0.02, color=RED, period=BEAT / 2) |
| 88 | +pulse_jewel_m1 = Pulse(pixel_jewel, speed=0.02, color=RED, period=BEAT / 2) |
| 89 | +comet_stripA_m1 = Comet( |
| 90 | + pixel_stripA, speed=b64TH, color=RED, tail_length=9, bounce=False |
| 91 | +) |
| 92 | +comet_stripB_m1 = Comet( |
| 93 | + pixel_stripB, speed=b64TH, color=RED, tail_length=9, bounce=False |
| 94 | +) |
| 95 | + |
| 96 | +solid_white = Solid(pixel_ringsAll, color=WHITE) |
| 97 | + |
| 98 | +# ---Anim Modes--- |
| 99 | +vu_strip_animations_mode0 = AnimationGroup(comet_stripA_m0, comet_stripB_m0, sync=True) |
| 100 | +vu_strip_animations_mode1 = AnimationGroup(comet_stripA_m1, comet_stripB_m1, sync=True) |
| 101 | + |
| 102 | +# ---Audio Setup--- |
| 103 | +if mode_state: |
| 104 | + BGM = "/lucio/bgmheal.mp3" |
| 105 | +else: |
| 106 | + BGM = "/lucio/bgmspeed.mp3" |
| 107 | +sample0 = audiomp3.MP3Decoder(open(BGM, "rb")) |
| 108 | +FX = "/lucio/shoot.mp3" |
| 109 | +sample1 = audiomp3.MP3Decoder(open(FX, "rb")) |
| 110 | +speaker.play(mixer) |
| 111 | +mixer.voice[0].play(sample0, loop=True) |
| 112 | +mixer.voice[0].level = 0.3 * VOLUME_MULT |
| 113 | +mixer.voice[1].level = 0.7 * VOLUME_MULT |
| 114 | + |
| 115 | + |
| 116 | +while True: |
| 117 | + if mode_state: # heal mode on startup |
| 118 | + vu_strip_animations_mode0.animate() |
| 119 | + pulse_rings_m0.animate() |
| 120 | + pulse_jewel_m0.animate() |
| 121 | + else: # speed mode on startup |
| 122 | + vu_strip_animations_mode1.animate() |
| 123 | + pulse_rings_m1.animate() |
| 124 | + pulse_jewel_m1.animate() |
| 125 | + |
| 126 | + # Change modes |
| 127 | + if mode_switch.value: |
| 128 | + if mode_state == 0: # state has changed, toggle it |
| 129 | + BGM = "/lucio/bgmheal.mp3" |
| 130 | + sample0.file = open(BGM, "rb") |
| 131 | + mixer.voice[0].play(sample0, loop=True) |
| 132 | + vu_strip_animations_mode0.animate() |
| 133 | + pulse_rings_m0.animate() |
| 134 | + pulse_jewel_m0.animate() |
| 135 | + mode_state = 1 |
| 136 | + else: |
| 137 | + if mode_state == 1: |
| 138 | + BGM = "/lucio/bgmspeed.mp3" |
| 139 | + sample0.file = open(BGM, "rb") |
| 140 | + mixer.voice[0].play(sample0, loop=True) |
| 141 | + vu_strip_animations_mode1.animate() |
| 142 | + pulse_rings_m1.animate() |
| 143 | + pulse_jewel_m1.animate() |
| 144 | + mode_state = 0 |
| 145 | + |
| 146 | + x, _, _ = accel.acceleration # get accelerometer values |
| 147 | + |
| 148 | + if not mixer.voice[1].playing: |
| 149 | + if not trig_button.value: # trigger squeezed |
| 150 | + FX_sample = "/lucio/shoot.mp3" |
| 151 | + sample1.file = open(FX_sample, "rb") |
| 152 | + mixer.voice[1].play(sample1) |
| 153 | + if mode_state: |
| 154 | + solid_white.animate() |
| 155 | + else: |
| 156 | + solid_white.animate() |
| 157 | + |
| 158 | + if not alt_button.value: # alt trigger squeezed |
| 159 | + FX_sample = "/lucio/alt_shoot.mp3" |
| 160 | + sample1.file = open(FX_sample, "rb") |
| 161 | + mixer.voice[1].play(sample1) |
| 162 | + if mode_state: |
| 163 | + solid_white.animate() |
| 164 | + else: |
| 165 | + solid_white.animate() |
| 166 | + |
| 167 | + if accel.acceleration.x > 8: # reload |
| 168 | + FX_sample = "/lucio/reload.mp3" |
| 169 | + sample1.file = open(FX_sample, "rb") |
| 170 | + mixer.voice[1].play(sample1) |
| 171 | + if mode_state: |
| 172 | + solid_white.animate() |
| 173 | + else: |
| 174 | + solid_white.animate() |
| 175 | + |
| 176 | + if accel.acceleration.x < -8: # Ultimate |
| 177 | + FX_sample = "/lucio/ultimate.mp3" |
| 178 | + sample1.file = open(FX_sample, "rb") |
| 179 | + mixer.voice[1].play(sample1) |
| 180 | + if mode_state: |
| 181 | + solid_white.animate() |
| 182 | + else: |
| 183 | + solid_white.animate() |
0 commit comments