Skip to content

Commit 94495f5

Browse files
authored
Merge pull request #2138 from adafruit/ble_rotary_remote
Adding code for BLE rotary Learn Guide
2 parents b978d23 + b78da77 commit 94495f5

File tree

1 file changed

+185
-0
lines changed
  • BLE_ANO_Rotary_Encoder_Remote

1 file changed

+185
-0
lines changed

BLE_ANO_Rotary_Encoder_Remote/code.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
from digitalio import DigitalInOut, Direction, Pull
7+
import rotaryio
8+
from adafruit_debouncer import Button
9+
from adafruit_hid.keyboard import Keyboard
10+
from adafruit_hid.keycode import Keycode
11+
import adafruit_ble
12+
from adafruit_ble.advertising import Advertisement
13+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
14+
from adafruit_ble.services.standard.hid import HIDService
15+
16+
# pin assignments for the rotary encoder
17+
ENCA = board.SDA
18+
ENCB = board.SCL
19+
COMA = board.D5
20+
CENTER = board.D6
21+
RIGHT = board.D9
22+
UP = board.D10
23+
LEFT = board.D11
24+
DOWN = board.D12
25+
COMB = board.D13
26+
27+
# mode slide switch pin
28+
SWITCH = board.A1
29+
30+
# Rotary encoder setup
31+
encoder = rotaryio.IncrementalEncoder(ENCA, ENCB)
32+
last_position = 0
33+
34+
# setting the COMA and COMB pins to LOW aka GND
35+
com_a = DigitalInOut(COMA)
36+
com_a.switch_to_output()
37+
com_a = False
38+
com_b = DigitalInOut(COMB)
39+
com_b.switch_to_output()
40+
com_b = False
41+
42+
# mode switch setup
43+
SWITCH = DigitalInOut(board.A1)
44+
SWITCH.direction = Direction.INPUT
45+
SWITCH.pull = Pull.UP
46+
47+
# encoder button pins
48+
enc_buttons = (
49+
CENTER,
50+
UP,
51+
LEFT,
52+
DOWN,
53+
RIGHT,
54+
)
55+
56+
# array for the encoder buttons
57+
inputs = []
58+
59+
# setting the encoder buttons as inputs
60+
for enc in enc_buttons:
61+
enc_button = DigitalInOut(enc)
62+
enc_button.pull = Pull.UP
63+
# adding to the inputs array with the Button Class of the Debouncer lib
64+
inputs.append(Button(enc_button))
65+
66+
# streaming mode keycodes
67+
CHILL_CODES = (
68+
Keycode.SPACE,
69+
Keycode.F,
70+
Keycode.LEFT_ARROW,
71+
Keycode.M,
72+
Keycode.RIGHT_ARROW,
73+
)
74+
75+
# doom mode keycodes
76+
DOOM_CODES = (
77+
Keycode.CONTROL,
78+
Keycode.UP_ARROW,
79+
Keycode.LEFT_ARROW,
80+
Keycode.DOWN_ARROW,
81+
Keycode.RIGHT_ARROW,
82+
)
83+
84+
# streaming state
85+
chill = True
86+
# doom state
87+
doom = False
88+
89+
# BLE HID setup
90+
hid = HIDService()
91+
92+
advertisement = ProvideServicesAdvertisement(hid)
93+
advertisement.appearance = 961
94+
scan_response = Advertisement()
95+
scan_response.complete_name = "CircuitPython HID"
96+
97+
# BLE instance
98+
ble = adafruit_ble.BLERadio()
99+
100+
# keyboard HID setup
101+
kbd = Keyboard(hid.devices)
102+
103+
# BLE advertisement
104+
if not ble.connected:
105+
print("advertising")
106+
ble.start_advertising(advertisement, scan_response)
107+
else:
108+
print("connected")
109+
print(ble.connections)
110+
111+
while True:
112+
# check for BLE connection
113+
while not ble.connected:
114+
pass
115+
# while BLE connected
116+
while ble.connected:
117+
# mode switch
118+
# selects whether to be in streaming mode or doom mode
119+
# affects the keycodes assigned to the encoder's inputs
120+
if not SWITCH.value:
121+
chill = False
122+
doom = True
123+
if SWITCH.value:
124+
chill = True
125+
doom = False
126+
127+
# rotary encoder position tracking
128+
position = encoder.position
129+
# if the encoder is turned to the right
130+
if position > last_position:
131+
# if in streaming mode
132+
if chill:
133+
# send UP arrow for volume
134+
kbd.send(Keycode.UP_ARROW)
135+
# if in doom mode
136+
if doom:
137+
# send period for right strafe
138+
kbd.send(Keycode.PERIOD)
139+
# reset encoder position
140+
last_position = position
141+
# if the encoder is turned to the left
142+
if position < last_position:
143+
# if in streaming mode
144+
if chill:
145+
# send DOWN arrow for volume
146+
kbd.send(Keycode.DOWN_ARROW)
147+
# if in doom mode
148+
if doom:
149+
# send comma for left strafe
150+
kbd.send(Keycode.COMMA)
151+
# reset encoder position
152+
last_position = position
153+
154+
# for loop for keycodes
155+
for i in range(5):
156+
# update state of the buttons
157+
inputs[i].update()
158+
# if you press the center button for a long press
159+
if inputs[0].long_press:
160+
# sends space key
161+
# used in Doom for use/open
162+
kbd.send(Keycode.SPACE)
163+
# if a press is detected...
164+
if inputs[i].pressed:
165+
# if in streaming mode
166+
if chill:
167+
# send the streaming keycodes
168+
kbd.press(CHILL_CODES[i])
169+
# if in doom mode
170+
if doom:
171+
# send the doom keycodes
172+
kbd.press(DOOM_CODES[i])
173+
# if a button is released...
174+
if inputs[i].released:
175+
# if in streaming mode
176+
if chill:
177+
# release the streaming keycodes
178+
kbd.release(CHILL_CODES[i])
179+
# if in doom mode
180+
if doom:
181+
# release the doom keycodes
182+
kbd.release(DOOM_CODES[i])
183+
184+
# if BLE disconnects, begin advertising again
185+
ble.start_advertising(advertisement)

0 commit comments

Comments
 (0)