Skip to content

Commit a076d02

Browse files
committed
Added Jeopardy controller code
1 parent 542281c commit a076d02

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# SPDX-FileCopyrightText: 2021 Dylan 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 2 input buttons
42+
buttonA = digitalio.DigitalInOut(board.BUTTON_A)
43+
buttonA.direction = digitalio.Direction.INPUT
44+
buttonA.pull = digitalio.Pull.DOWN
45+
46+
buttonB = digitalio.DigitalInOut(board.BUTTON_B)
47+
buttonB.direction = digitalio.Direction.INPUT
48+
buttonB.pull = digitalio.Pull.DOWN
49+
50+
btn1 = digitalio.DigitalInOut(board.D10) # Marked A3
51+
btn1.direction = digitalio.Direction.INPUT
52+
btn1.pull = digitalio.Pull.UP
53+
54+
btn2 = digitalio.DigitalInOut(board.D9) # Marked A2
55+
btn2.direction = digitalio.Direction.INPUT
56+
btn2.pull = digitalio.Pull.UP
57+
58+
btn3 = digitalio.DigitalInOut(board.D3) # Marked SCL A4
59+
btn3.direction = digitalio.Direction.INPUT
60+
btn3.pull = digitalio.Pull.UP
61+
62+
central = digitalio.DigitalInOut(board.D0) # Marked RX A6
63+
central.direction = digitalio.Direction.INPUT
64+
central.pull = digitalio.Pull.UP
65+
66+
led = digitalio.DigitalInOut(board.D2) # Marked SDA A5
67+
led.switch_to_output()
68+
led.value = False
69+
70+
buttons = [btn1, btn2, btn3]
71+
upper = len(buttons) - 1
72+
73+
ble_enabled = digitalio.DigitalInOut(board.SLIDE_SWITCH)
74+
ble_enabled.direction = digitalio.Direction.INPUT
75+
ble_enabled.pull = digitalio.Pull.UP
76+
77+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.5)
78+
# Use default HID descriptor
79+
hid = HIDService()
80+
device_info = DeviceInfoService(
81+
software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries"
82+
)
83+
advertisement = ProvideServicesAdvertisement(hid)
84+
advertisement.appearance = 961
85+
scan_response = Advertisement()
86+
87+
ble = adafruit_ble.BLERadio()
88+
if ble.connected:
89+
for c in ble.connections:
90+
c.disconnect()
91+
92+
if ble_enabled.value:
93+
print("advertising")
94+
ble.start_advertising(advertisement, scan_response)
95+
96+
k = Keyboard(hid.devices)
97+
kl = KeyboardLayoutUS(k)
98+
99+
wave_file = open("jeopardy.wav", "rb")
100+
wave = WaveFile(wave_file)
101+
audio = AudioOut(board.SPEAKER)
102+
103+
while True:
104+
if ble_enabled.value:
105+
while not ble.connected:
106+
pass
107+
if ble.connected:
108+
print("Connected")
109+
led.value = True
110+
time.sleep(0.1)
111+
led.value = False
112+
time.sleep(0.1)
113+
led.value = True
114+
time.sleep(0.1)
115+
led.value = False
116+
117+
while ble.connected or not ble_enabled.value:
118+
if not central.value:
119+
led.value = True
120+
print("Running")
121+
while True:
122+
i = random.randint(0, upper)
123+
if not buttons[i].value:
124+
break
125+
126+
audio.play(wave)
127+
if i == 0:
128+
print("Button 1")
129+
pixels.fill((0, 0, 255))
130+
if ble_enabled.value:
131+
kl.write("Button 1")
132+
elif i == 1:
133+
print("Button 2")
134+
pixels.fill((0, 255, 0))
135+
if ble_enabled.value:
136+
kl.write("Button 2")
137+
elif i == 2:
138+
print("Button 3")
139+
pixels.fill((255, 255, 255))
140+
if ble_enabled.value:
141+
kl.write("Button 3")
142+
143+
if not ble_enabled.value:
144+
print(
145+
"BLE HID has been disabled, slide the slide switch to the left to re-enable"
146+
)
147+
148+
print("Finished")
149+
led.value = False
150+
151+
while central.value:
152+
pass
153+
154+
print("reset")
155+
pixels.fill((0, 0, 0))
156+
led.value = True
157+
time.sleep(0.5)
158+
led.value = False
159+
print("Ready")
160+
if ble_enabled.value:
161+
if not ble.connected:
162+
break
163+
else:
164+
continue
165+
break
Binary file not shown.

0 commit comments

Comments
 (0)