code.py
1 # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 4 # General imports 5 import time 6 import random 7 import board 8 import digitalio 9 import neopixel 10 11 # HID imports 12 from adafruit_hid.keyboard import Keyboard 13 from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS 14 15 # BLE imports 16 import adafruit_ble 17 from adafruit_ble.advertising import Advertisement 18 from adafruit_ble.advertising.standard import ProvideServicesAdvertisement 19 from adafruit_ble.services.standard.hid import HIDService 20 from adafruit_ble.services.standard.device_info import DeviceInfoService 21 22 try: 23 from audiocore import WaveFile 24 except ImportError: 25 from audioio import WaveFile 26 27 try: 28 from audioio import AudioOut 29 except ImportError: 30 try: 31 from audiopwmio import PWMAudioOut as AudioOut 32 except ImportError: 33 pass # not always supported by every board! 34 35 # Enable the speaker 36 spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) 37 spkrenable.direction = digitalio.Direction.OUTPUT 38 spkrenable.value = True 39 40 41 # Make the input buttons 42 btn1 = digitalio.DigitalInOut(board.D10) # Marked A3 43 btn1.direction = digitalio.Direction.INPUT 44 btn1.pull = digitalio.Pull.UP 45 46 btn2 = digitalio.DigitalInOut(board.D9) # Marked A2 47 btn2.direction = digitalio.Direction.INPUT 48 btn2.pull = digitalio.Pull.UP 49 50 btn3 = digitalio.DigitalInOut(board.D3) # Marked SCL A4 51 btn3.direction = digitalio.Direction.INPUT 52 btn3.pull = digitalio.Pull.UP 53 54 central = digitalio.DigitalInOut(board.D0) # Marked RX A6 55 central.direction = digitalio.Direction.INPUT 56 central.pull = digitalio.Pull.UP 57 58 led = digitalio.DigitalInOut(board.D2) # Marked SDA A5 59 led.switch_to_output() 60 led.value = False 61 62 buttons = [btn1, btn2, btn3] 63 upper = len(buttons) - 1 64 65 ble_enabled = digitalio.DigitalInOut(board.SLIDE_SWITCH) 66 ble_enabled.direction = digitalio.Direction.INPUT 67 ble_enabled.pull = digitalio.Pull.UP 68 69 pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.5) 70 # Use default HID descriptor 71 hid = HIDService() 72 device_info = DeviceInfoService( 73 software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries" 74 ) 75 advertisement = ProvideServicesAdvertisement(hid) 76 advertisement.appearance = 961 77 scan_response = Advertisement() 78 79 ble = adafruit_ble.BLERadio() 80 if ble.connected: 81 for c in ble.connections: 82 c.disconnect() 83 84 if ble_enabled.value: 85 print("advertising") 86 ble.start_advertising(advertisement, scan_response) 87 88 k = Keyboard(hid.devices) 89 kl = KeyboardLayoutUS(k) 90 91 wave_file = open("jeopardy.wav", "rb") 92 wave = WaveFile(wave_file) 93 audio = AudioOut(board.SPEAKER) 94 95 while True: 96 if ble_enabled.value: 97 while not ble.connected: 98 pass 99 if ble.connected: 100 print("Connected") 101 led.value = True 102 time.sleep(0.1) 103 led.value = False 104 time.sleep(0.1) 105 led.value = True 106 time.sleep(0.1) 107 led.value = False 108 109 while ble.connected or not ble_enabled.value: 110 if not central.value: 111 led.value = True 112 print("Running") 113 while True: 114 i = random.randint(0, upper) 115 if not buttons[i].value: 116 break 117 118 audio.play(wave) 119 if i == 0: 120 print("Button 1") 121 pixels.fill((0, 0, 255)) 122 if ble_enabled.value: 123 kl.write("Button 1") 124 elif i == 1: 125 print("Button 2") 126 pixels.fill((0, 255, 0)) 127 if ble_enabled.value: 128 kl.write("Button 2") 129 elif i == 2: 130 print("Button 3") 131 pixels.fill((255, 255, 255)) 132 if ble_enabled.value: 133 kl.write("Button 3") 134 135 if not ble_enabled.value: 136 print( 137 "BLE HID has been disabled, slide the slide switch to the left to re-enable" 138 ) 139 140 print("Finished") 141 led.value = False 142 143 while central.value: 144 pass 145 146 print("reset") 147 pixels.fill((0, 0, 0)) 148 led.value = True 149 time.sleep(0.5) 150 led.value = False 151 print("Ready") 152 if ble_enabled.value: 153 if not ble.connected: 154 break 155 else: 156 continue 157 break