|
| 1 | +# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# BLE Ouija Board |
| 5 | + |
| 6 | +import time |
| 7 | +import random |
| 8 | +import board |
| 9 | +import digitalio |
| 10 | +import neopixel |
| 11 | +from adafruit_motorkit import MotorKit |
| 12 | +from adafruit_ble import BLERadio |
| 13 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 14 | +from adafruit_ble.services.nordic import UARTService |
| 15 | +from adafruit_bluefruit_connect.packet import Packet |
| 16 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 17 | +import audiocore |
| 18 | +import audiomixer |
| 19 | +import audiobusio |
| 20 | + |
| 21 | +# Prep the status LEDs on the Feather |
| 22 | +blue_led = digitalio.DigitalInOut(board.BLUE_LED) |
| 23 | +red_led = digitalio.DigitalInOut(board.RED_LED) |
| 24 | +blue_led.direction = digitalio.Direction.OUTPUT |
| 25 | +red_led.direction = digitalio.Direction.OUTPUT |
| 26 | + |
| 27 | +ble = BLERadio() |
| 28 | +uart_service = UARTService() |
| 29 | +advertisement = ProvideServicesAdvertisement(uart_service) |
| 30 | + |
| 31 | +num_motors = 1 # up to 4 motors depending on the prop you are driving |
| 32 | + |
| 33 | +motorwing = MotorKit(i2c=board.I2C()) |
| 34 | +motorwing.frequency = 122 # tune this 50 - 200 range |
| 35 | +max_throttle = 0.65 # tune this 0.2 - 1 range |
| 36 | + |
| 37 | +# # make arrays for all the things we care about |
| 38 | +motors = [None] * num_motors |
| 39 | + |
| 40 | +motors[0] = motorwing.motor1 |
| 41 | + |
| 42 | +# # set motors to "off" |
| 43 | +for i in range(num_motors): |
| 44 | + motors[i].throttle = None |
| 45 | + |
| 46 | +# - Audio setup |
| 47 | +audio = audiobusio.I2SOut(bit_clock=board.TX, word_select=board.MISO, data=board.RX) |
| 48 | +mixer = audiomixer.Mixer(voice_count=2, sample_rate=11025, channel_count=1, |
| 49 | + bits_per_sample=16, samples_signed=True) |
| 50 | +audio.play(mixer) # attach mixer to audio playback |
| 51 | +wav_files = (('spooky_ouija.wav', 0.07, True), ('lars_ouija.wav', 0.09, False)) |
| 52 | +# open samples |
| 53 | +for i in range(len(wav_files)): |
| 54 | + wave = audiocore.WaveFile(open(wav_files[i][0], "rb")) |
| 55 | + mixer.voice[i].level = 0 # start up with level down |
| 56 | + mixer.voice[i].play(wave, loop=wav_files[i][2]) |
| 57 | + |
| 58 | +# - NeoPixels |
| 59 | +fire_color = 0xcc6600 |
| 60 | +fade_by = -1 |
| 61 | +num_leds = 7 |
| 62 | +max_bright = 0.5 |
| 63 | +led_pin = board.D6 |
| 64 | +leds = neopixel.NeoPixel(led_pin, num_leds, brightness=0.0, auto_write=False) |
| 65 | +leds.fill(fire_color) |
| 66 | +leds.show() |
| 67 | + |
| 68 | +last_time = 0 |
| 69 | +next_duration = 0.2 |
| 70 | + |
| 71 | +print("BLE Ouija board") |
| 72 | +print("Use Adafruit Bluefruit app to connect") |
| 73 | + |
| 74 | + |
| 75 | +while True: |
| 76 | + blue_led.value = False |
| 77 | + ble.name = 'Ouija' |
| 78 | + ble.start_advertising(advertisement) |
| 79 | + |
| 80 | + while not ble.connected: |
| 81 | + # Wait for a connection. |
| 82 | + pass |
| 83 | + blue_led.value = True # turn on blue LED when connected |
| 84 | + while ble.connected: |
| 85 | + if uart_service.in_waiting: |
| 86 | + # Packet is arriving. |
| 87 | + red_led.value = False # turn off red LED |
| 88 | + packet = Packet.from_stream(uart_service) |
| 89 | + if isinstance(packet, ButtonPacket) and packet.pressed: |
| 90 | + red_led.value = True # blink to show a packet has been received |
| 91 | + if packet.button == ButtonPacket.RIGHT: # > button pressed |
| 92 | + print("forward") |
| 93 | + motors[0].throttle = max_throttle |
| 94 | + time.sleep(0.1) # wait a moment |
| 95 | + |
| 96 | + elif packet.button == ButtonPacket.LEFT: # < button |
| 97 | + print("reverse") |
| 98 | + motors[0].throttle = max_throttle * -1 |
| 99 | + time.sleep(0.1) |
| 100 | + |
| 101 | + elif packet.button == ButtonPacket.DOWN: # v button |
| 102 | + print("stop") |
| 103 | + motors[0].throttle = None |
| 104 | + time.sleep(0.1) |
| 105 | + |
| 106 | + elif packet.button == ButtonPacket.BUTTON_1: # 1 button |
| 107 | + print("on BG music") |
| 108 | + mixer.voice[0].level = wav_files[0][1] |
| 109 | + time.sleep(0.1) |
| 110 | + |
| 111 | + elif packet.button == ButtonPacket.BUTTON_3: # 3 button |
| 112 | + print("off BG music") |
| 113 | + mixer.voice[0].level = 0.0 |
| 114 | + time.sleep(0.1) |
| 115 | + |
| 116 | + elif packet.button == ButtonPacket.BUTTON_2: # 2 button |
| 117 | + print("on led & Lars") |
| 118 | + leds.brightness = max_bright |
| 119 | + mixer.voice[1].play(wave, loop=False) |
| 120 | + mixer.voice[1].level = wav_files[1][1] |
| 121 | + time.sleep(0.1) |
| 122 | + |
| 123 | + elif packet.button == ButtonPacket.BUTTON_4: # 4 button |
| 124 | + print("off led & Lars") |
| 125 | + leds.brightness = 0 |
| 126 | + mixer.voice[1].level = 0.0 |
| 127 | + time.sleep(0.1) |
| 128 | + |
| 129 | + # fade down all LEDs |
| 130 | + leds[:] = [[min(max(i+fade_by, 0), 255) for i in l] for l in leds] |
| 131 | + leds.show() |
| 132 | + |
| 133 | + # add new fire to leds |
| 134 | + if time.monotonic() - last_time > next_duration: |
| 135 | + last_time = time.monotonic() |
| 136 | + next_duration = random.uniform(0.95, 1.95) # tune these nums |
| 137 | + # for i in range( 1): |
| 138 | + c = fire_color # get our color |
| 139 | + c = (c >> 16 & 0xff, c >> 8 & 0xff, c & 0xff) # make it a tuple |
| 140 | + leds[random.randint(0, num_leds-1)] = c |
0 commit comments