|
| 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 | +from digitalio import DigitalInOut, Direction |
| 13 | +import neopixel |
| 14 | +from adafruit_motor import servo |
| 15 | +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff |
| 16 | +from adafruit_led_animation.animation.pulse import Pulse |
| 17 | +from adafruit_led_animation.color import RED, BLACK, GREEN |
| 18 | +import adafruit_display_text.label |
| 19 | +import displayio |
| 20 | +import framebufferio |
| 21 | +import rgbmatrix |
| 22 | +import terminalio |
| 23 | +import adafruit_vl53l4cd |
| 24 | + |
| 25 | +distance_trigger = 90 # cm |
| 26 | +text="Here lies Fred" |
| 27 | +text_color = 0xff0000 |
| 28 | +# how often to check for a new trigger from ToF |
| 29 | +pause_time = 30 # seconds |
| 30 | +# speed for scrolling the text on the matrix |
| 31 | +scroll_time = 0.1 # seconds |
| 32 | + |
| 33 | +displayio.release_displays() |
| 34 | + |
| 35 | +# enable external power pin |
| 36 | +# provides power to the external components |
| 37 | +external_power = DigitalInOut(board.EXTERNAL_POWER) |
| 38 | +external_power.direction = Direction.OUTPUT |
| 39 | +external_power.value = True |
| 40 | + |
| 41 | +i2c = board.I2C() |
| 42 | +vl53 = adafruit_vl53l4cd.VL53L4CD(i2c) |
| 43 | + |
| 44 | +vl53.inter_measurement = 0 |
| 45 | +vl53.timing_budget = 200 |
| 46 | + |
| 47 | +matrix = rgbmatrix.RGBMatrix( |
| 48 | + width=64, height=32, bit_depth=4, |
| 49 | + rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12], |
| 50 | + addr_pins=[board.D25, board.D24, board.A3, board.A2], |
| 51 | + clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1) |
| 52 | + |
| 53 | +display = framebufferio.FramebufferDisplay(matrix, auto_refresh=True) |
| 54 | + |
| 55 | +line1 = adafruit_display_text.label.Label( |
| 56 | + terminalio.FONT, |
| 57 | + color=text_color, |
| 58 | + text=text) |
| 59 | +line1.x = 1 |
| 60 | +line1.y = 14 |
| 61 | + |
| 62 | +def scroll(line): |
| 63 | + line.x = line.x - 1 |
| 64 | + line_width = line.bounding_box[2] |
| 65 | + if line.x < -line_width: |
| 66 | + line.x = display.width |
| 67 | + |
| 68 | +g = displayio.Group() |
| 69 | +g.append(line1) |
| 70 | + |
| 71 | +display.show(g) |
| 72 | + |
| 73 | +wavs = [] |
| 74 | +for filename in os.listdir('/tomb_sounds'): |
| 75 | + if filename.lower().endswith('.wav') and not filename.startswith('.'): |
| 76 | + wavs.append("/tomb_sounds/"+filename) |
| 77 | + |
| 78 | +audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) |
| 79 | +mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, |
| 80 | + bits_per_sample=16, samples_signed=True, buffer_size=32768) |
| 81 | + |
| 82 | +mixer.voice[0].level = 1 |
| 83 | +audio.play(mixer) |
| 84 | +wav_length = len(wavs) - 1 |
| 85 | + |
| 86 | +def open_audio(num): |
| 87 | + n = wavs[num] |
| 88 | + f = open(n, "rb") |
| 89 | + w = audiocore.WaveFile(f) |
| 90 | + return w |
| 91 | + |
| 92 | +PIXEL_PIN = board.EXTERNAL_NEOPIXELS |
| 93 | +BRIGHTNESS = 0.3 |
| 94 | +NUM_PIXELS = 2 |
| 95 | + |
| 96 | +PIXELS = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, auto_write=True) |
| 97 | +pulse = Pulse(PIXELS, speed=0.05, color=RED, period=3) |
| 98 | +COLORS = [RED, GREEN, BLACK] |
| 99 | + |
| 100 | +SERVO_PIN = board.EXTERNAL_SERVO |
| 101 | +PWM = pwmio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50) |
| 102 | +SERVO = servo.Servo(PWM) |
| 103 | +SERVO.angle = 0 |
| 104 | + |
| 105 | +clock = ticks_ms() |
| 106 | +the_time = 5000 |
| 107 | +x = 0 |
| 108 | +scroll_clock = ticks_ms() |
| 109 | +scroll_time = int(scroll_time * 1000) |
| 110 | +pause_clock = ticks_ms() |
| 111 | +pause_time = pause_time * 1000 |
| 112 | +pause = False |
| 113 | + |
| 114 | +vl53.start_ranging() |
| 115 | + |
| 116 | +while True: |
| 117 | + |
| 118 | + vl53.clear_interrupt() |
| 119 | + |
| 120 | + if vl53.distance < distance_trigger: |
| 121 | + if not pause: |
| 122 | + print("Distance: {} cm".format(vl53.distance)) |
| 123 | + SERVO.angle = 90 |
| 124 | + wave = open_audio(random.randint(0, wav_length)) |
| 125 | + mixer.voice[0].play(wave) |
| 126 | + while mixer.playing: |
| 127 | + pulse.color = COLORS[x] |
| 128 | + pulse.animate() |
| 129 | + if ticks_diff(ticks_ms(), scroll_clock) >= scroll_time: |
| 130 | + scroll(line1) |
| 131 | + display.refresh(minimum_frames_per_second=0) |
| 132 | + scroll_clock = ticks_add(scroll_clock, scroll_time) |
| 133 | + x = (x + 1) % 2 |
| 134 | + pause = True |
| 135 | + print("paused") |
| 136 | + pause_clock = ticks_add(pause_clock, pause_time) |
| 137 | + else: |
| 138 | + if ticks_diff(ticks_ms(), pause_clock) >= pause_time: |
| 139 | + print("back to sensing") |
| 140 | + pause = False |
| 141 | + print("still paused") |
| 142 | + if ticks_diff(ticks_ms(), scroll_clock) >= scroll_time: |
| 143 | + print("Distance: {} cm".format(vl53.distance)) |
| 144 | + scroll(line1) |
| 145 | + display.refresh(minimum_frames_per_second=0) |
| 146 | + scroll_clock = ticks_add(scroll_clock, scroll_time) |
| 147 | + SERVO.angle = 0 |
| 148 | + pulse.color = COLORS[2] |
| 149 | + pulse.animate() |
0 commit comments