|
| 1 | +# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import random |
| 7 | +import board |
| 8 | +import audiocore |
| 9 | +import audiobusio |
| 10 | +import audiomixer |
| 11 | +import pwmio |
| 12 | +import neopixel |
| 13 | +import adafruit_lis3dh |
| 14 | +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff |
| 15 | +from digitalio import DigitalInOut, Direction, Pull |
| 16 | +from adafruit_motor import servo |
| 17 | +from adafruit_led_animation.animation.comet import Comet |
| 18 | +from adafruit_led_animation.animation.pulse import Pulse |
| 19 | +from adafruit_led_animation.color import RED, BLUE, BLACK |
| 20 | + |
| 21 | +# enable external power pin |
| 22 | +# provides power to the external components |
| 23 | +external_power = DigitalInOut(board.EXTERNAL_POWER) |
| 24 | +external_power.direction = Direction.OUTPUT |
| 25 | +external_power.value = True |
| 26 | + |
| 27 | +i2c = board.I2C() |
| 28 | +int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT) |
| 29 | +lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) |
| 30 | +lis3dh.range = adafruit_lis3dh.RANGE_2_G |
| 31 | + |
| 32 | +switch = DigitalInOut(board.EXTERNAL_BUTTON) |
| 33 | +switch.direction = Direction.INPUT |
| 34 | +switch.pull = Pull.UP |
| 35 | +switch_state = False |
| 36 | + |
| 37 | +wavs = [] |
| 38 | +for filename in os.listdir('/WAVs'): |
| 39 | + if filename.lower().endswith('.wav') and not filename.startswith('.'): |
| 40 | + wavs.append("/WAVs/"+filename) |
| 41 | + |
| 42 | +audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) |
| 43 | +mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, |
| 44 | + bits_per_sample=16, samples_signed=True, buffer_size=32768) |
| 45 | + |
| 46 | +mixer.voice[0].level = 1 |
| 47 | +track_number = 0 |
| 48 | +wav_filename = wavs[track_number] |
| 49 | +wav_file = open(wav_filename, "rb") |
| 50 | +wave = audiocore.WaveFile(wav_file) |
| 51 | +audio.play(mixer) |
| 52 | +mixer.voice[0].play(wave) |
| 53 | + |
| 54 | +def open_audio(num): |
| 55 | + n = wavs[num] |
| 56 | + f = open(n, "rb") |
| 57 | + w = audiocore.WaveFile(f) |
| 58 | + return w |
| 59 | + |
| 60 | +PIXEL_PIN = board.EXTERNAL_NEOPIXELS |
| 61 | +SERVO_PIN = board.EXTERNAL_SERVO |
| 62 | +NUM_PIXELS = 8 |
| 63 | +ORDER = neopixel.GRB |
| 64 | +BRIGHTNESS = 0.6 |
| 65 | + |
| 66 | +PWM = pwmio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50) |
| 67 | +SERVO = servo.Servo(PWM) |
| 68 | + |
| 69 | +pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) |
| 70 | +pixel.brightness = 1 |
| 71 | + |
| 72 | +PIXELS = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, auto_write=False, |
| 73 | + pixel_order=ORDER) |
| 74 | +LARSON = Comet(PIXELS, bounce=True, speed=0.6/NUM_PIXELS, |
| 75 | + tail_length=NUM_PIXELS//2, |
| 76 | + color=(RED[0] * BRIGHTNESS, |
| 77 | + RED[1] * BRIGHTNESS, |
| 78 | + RED[2] * BRIGHTNESS)) |
| 79 | +pulse = Pulse(PIXELS, speed=0.1, color=BLUE, period=3) |
| 80 | + |
| 81 | +SERVO.angle = POSITION = NEXT_POSITION = 90 |
| 82 | +MOVING = False |
| 83 | +START_TIME = ticks_ms() |
| 84 | +DURATION = 1000 |
| 85 | + |
| 86 | +adabot_talk = False |
| 87 | + |
| 88 | +clock = ticks_ms() |
| 89 | +prop_time = 1000 |
| 90 | +adabot_nap = False |
| 91 | + |
| 92 | +mixer.voice[0].play(wave) |
| 93 | +while mixer.playing: |
| 94 | + LARSON.animate() |
| 95 | + |
| 96 | +while True: |
| 97 | + if ticks_diff(ticks_ms(), clock) >= prop_time: |
| 98 | + x, y, z = [ |
| 99 | + value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration |
| 100 | + ] |
| 101 | + if z > 0.9: |
| 102 | + adabot_nap = True |
| 103 | + SERVO.angle = POSITION = NEXT_POSITION = 90 |
| 104 | + LARSON.color=(BLUE[0] * BRIGHTNESS, |
| 105 | + BLUE[1] * BRIGHTNESS, |
| 106 | + BLUE[2] * BRIGHTNESS) |
| 107 | + else: |
| 108 | + adabot_nap = False |
| 109 | + LARSON.color=(RED[0] * BRIGHTNESS, |
| 110 | + RED[1] * BRIGHTNESS, |
| 111 | + RED[2] * BRIGHTNESS) |
| 112 | + if not adabot_nap: |
| 113 | + MOVING = not MOVING |
| 114 | + if MOVING: |
| 115 | + POSITION = NEXT_POSITION |
| 116 | + while abs(POSITION - NEXT_POSITION) < 10: |
| 117 | + NEXT_POSITION = random.uniform(0, 180) |
| 118 | + DURATION = 0.2 + 0.6 * abs(POSITION - NEXT_POSITION) / 180 |
| 119 | + else: |
| 120 | + SERVO.angle = NEXT_POSITION |
| 121 | + DURATION = random.uniform(0.5, 2.5) |
| 122 | + clock = ticks_add(clock, prop_time) |
| 123 | + if MOVING: |
| 124 | + FRACTION = 0.0 / DURATION |
| 125 | + FRACTION = (3 * FRACTION ** 2) - (2 * FRACTION ** 3) |
| 126 | + SERVO.angle = POSITION + (NEXT_POSITION - POSITION) * FRACTION |
| 127 | + if adabot_talk: |
| 128 | + wave = open_audio(random.randint(1, 7)) |
| 129 | + mixer.voice[0].play(wave) |
| 130 | + while mixer.playing: |
| 131 | + LARSON.animate() |
| 132 | + if not mixer.playing: |
| 133 | + adabot_talk = False |
| 134 | + PIXELS.fill(BLACK) |
| 135 | + PIXELS.show() |
| 136 | + elif adabot_nap: |
| 137 | + LARSON.animate() |
| 138 | + else: |
| 139 | + pulse.animate() |
| 140 | + |
| 141 | + if not switch.value and switch_state is False: |
| 142 | + PIXELS.fill(BLACK) |
| 143 | + PIXELS.show() |
| 144 | + adabot_talk = True |
| 145 | + switch_state = True |
| 146 | + if switch.value and switch_state is True: |
| 147 | + switch_state = False |
0 commit comments