Skip to content

Commit 90c4da8

Browse files
authored
Merge pull request #981 from tannewt/swap_hid_api
Provide USB HID devices directly rather than implicitly.
2 parents 1fe1e15 + 0dd7ea3 commit 90c4da8

File tree

24 files changed

+54
-29
lines changed

24 files changed

+54
-29
lines changed

Adafruit_pIRKey/NEC_keyboard_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
import adafruit_dotstar
1111
import pulseio
1212
import board
13+
import usb_hid
1314

1415
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
1516

1617
# The keyboard object!
1718
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
18-
keyboard = Keyboard()
19+
keyboard = Keyboard(usb_hid.devices)
1920
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
2021

2122
# our infrared pulse decoder helpers

Adafruit_pIRKey/raw_code_keyboard/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import adafruit_dotstar
99
import adafruit_irremote
1010
import time
11+
import usb_hid
1112

1213
# The keyboard object!
1314
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
14-
keyboard = Keyboard()
15+
keyboard = Keyboard(usb_hid.devices)
1516
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
1617

1718
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)

Arcade_Button_Control_Box/Arcade_Button_Control_Box.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from adafruit_hid.keyboard import Keyboard
55
from adafruit_hid.keycode import Keycode
66
import board
7+
import usb_hid
78

89
# A simple neat keyboard demo in circuitpython
910

@@ -16,7 +17,7 @@
1617
controlkey = Keycode.LEFT_CONTROL
1718

1819
# the keyboard object!
19-
kbd = Keyboard()
20+
kbd = Keyboard(usb_hid.devices)
2021
# our array of button objects
2122
buttons = []
2223
leds = []

CPX_GBoard/arcade_hid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""
1414

1515
import board
16+
import usb_hid
1617
from adafruit_hid.keyboard import Keyboard
1718
from adafruit_hid.keycode import Keycode
1819
from digitalio import DigitalInOut, Direction, Pull
@@ -28,7 +29,7 @@
2829
button_b.direction = Direction.INPUT
2930
button_b.pull = Pull.UP
3031

31-
kbd = Keyboard()
32+
kbd = Keyboard(usb_hid.devices)
3233

3334

3435
def touch_a():

CPX_GBoard/touch_hid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
All text above must be included in any redistribution.
1313
"""
1414

15+
import usb_hid
1516
from adafruit_circuitplayground.express import cpx
1617
from adafruit_hid.keyboard import Keyboard
1718
from adafruit_hid.keycode import Keycode
1819

1920
DOT_DURATION = 0.25
2021
DASH_DURATION = 0.5
2122

22-
kbd = Keyboard()
23+
kbd = Keyboard(usb_hid.devices)
2324

2425
# You can adjust this to get the level of sensitivity you want.
2526
cpx.adjust_touch_threshold(100)

CPX_GBoard/universal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
# from digitalio import DigitalInOut, Direction, Pull
2121
# import board
2222

23-
# Uncomment the next 2 lines if you want to use HID output
23+
# Uncomment the next 3 lines if you want to use HID output
2424
# from adafruit_hid.keyboard import Keyboard
2525
# from adafruit_hid.keycode import Keycode
26+
# import usb_hid
2627

2728
DOT_DURATION = 0.20
2829
DASH_DURATION = 0.5
@@ -40,7 +41,7 @@
4041
# button_b.pull = Pull.UP
4142

4243
# Uncomment the next line if you want to use HID output
43-
# kbd = Keyboard()
44+
# kbd = Keyboard(usb_hid.devices)
4445

4546

4647

CircuitPython_Essentials/CircuitPython_HID_Keyboard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import board
66
import digitalio
7+
import usb_hid
78
from adafruit_hid.keyboard import Keyboard
89
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
910
from adafruit_hid.keycode import Keycode
@@ -20,7 +21,7 @@
2021

2122
# The keyboard object!
2223
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
23-
keyboard = Keyboard()
24+
keyboard = Keyboard(usb_hid.devices)
2425
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
2526

2627
# Make all pin objects inputs with pullups

CircuitPython_Essentials/CircuitPython_HID_Mouse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import analogio
44
import board
55
import digitalio
6+
import usb_hid
67
from adafruit_hid.mouse import Mouse
78

8-
mouse = Mouse()
9+
mouse = Mouse(usb_hid.devices)
910

1011
x_axis = analogio.AnalogIn(board.A0)
1112
y_axis = analogio.AnalogIn(board.A1)

Crank_USB_HID/code.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from adafruit_hid.consumer_control import ConsumerControl
1212
from adafruit_hid.consumer_control_code import ConsumerControlCode
1313
import neopixel
14+
import usb_hid
1415

1516
DOT_COLOR = 0xFF0000 # set to your favorite webhex color
1617
PRESSED_DOT_COLOR = 0x008080 # set to your second-favorite color
@@ -35,7 +36,8 @@
3536
rot_b.pull = Pull.UP
3637

3738
# Used to do HID output, see below
38-
kbd = Keyboard()
39+
kbd = Keyboard(usb_hid.devices)
40+
consumer_control = ConsumerControl(usb_hid.devices)
3941

4042
# time keeper, so we know when to turn off the LED
4143
timestamp = time.monotonic()
@@ -111,12 +113,12 @@
111113

112114
# Check if rotary encoder went up
113115
if encoder_direction == 1:
114-
ConsumerControl().send(ConsumerControlCode.VOLUME_DECREMENT) #Turn Down Volume
116+
consumer_control.send(ConsumerControlCode.VOLUME_DECREMENT) #Turn Down Volume
115117
# kbd.press(Keycode.LEFT_ARROW)
116118
# kbd.release_all()
117119
# Check if rotary encoder went down
118120
if encoder_direction == -1:
119-
ConsumerControl().send(ConsumerControlCode.VOLUME_INCREMENT) #Turn Up Volume
121+
consumer_control.send(ConsumerControlCode.VOLUME_INCREMENT) #Turn Up Volume
120122
# kbd.press(Keycode.RIGHT_ARROW)
121123
# kbd.release_all()
122124
# Button was 'just pressed'

Foul_Fowl/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010

1111
import board
12+
import usb_hid
1213
from adafruit_hid.keyboard import Keyboard
1314
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
1415
from adafruit_hid.keycode import Keycode
@@ -30,7 +31,7 @@
3031
buttons = []
3132

3233
# the keyboard object!
33-
kbd = Keyboard()
34+
kbd = Keyboard(usb_hid.devices)
3435
# we're americans :)
3536
layout = KeyboardLayoutUS(kbd)
3637

GemmaM0_Password_Vault/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import board
77
import touchio
8+
import usb_hid
89
from adafruit_hid.keyboard import Keyboard
910
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
1011
from digitalio import DigitalInOut, Direction
@@ -19,7 +20,7 @@
1920
# the keyboard object
2021
# sleep for a bit to avoid a race condition on some systems
2122
time.sleep(1)
22-
kbd = Keyboard()
23+
kbd = Keyboard(usb_hid.devices)
2324
layout = KeyboardLayoutUS(kbd)
2425

2526
while True:

GemmaM0_Radio_Tuning_Knob/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66

77
import board
8+
import usb_hid
89
from adafruit_hid.keyboard import Keyboard
910
from adafruit_hid.keycode import Keycode
1011
from analogio import AnalogIn
@@ -41,7 +42,7 @@ def spamKey(code):
4142
Keycode.LEFT_BRACKET]
4243
spamRate = [0.01, 0.05, 0.125, 0.25, 0.5,
4344
0.5, 0.5, 0.25, 0.125, 0.05, 0.01]
44-
kbd = Keyboard()
45+
kbd = Keyboard(usb_hid.devices)
4546
kbd.press(knobkeys[code]) # which keycode is entered
4647
kbd.release_all()
4748
time.sleep(spamRate[code]) # how fast the key is spammed

Giant_Mechanical_Keyboard/code.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import board
1212
import neopixel
13+
import usb_hid
1314
from adafruit_hid.keyboard import Keyboard
1415
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
1516
from adafruit_hid.keycode import Keycode
@@ -34,7 +35,7 @@
3435
buttonspressedlast = [False, False, False]
3536

3637
# the keyboard object!
37-
kbd = Keyboard()
38+
kbd = Keyboard(usb_hid.devices)
3839
# we're americans :)
3940
layout = KeyboardLayoutUS(kbd)
4041

Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_HIDKeyboard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44

55
import board
6+
import usb_hid
67
from adafruit_hid.keyboard import Keyboard
78
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
89
from adafruit_hid.keycode import Keycode
@@ -21,7 +22,7 @@
2122
# the keyboard object!
2223
# sleep for a bit to avoid a race condition on some systems
2324
time.sleep(1)
24-
kbd = Keyboard()
25+
kbd = Keyboard(usb_hid.devices)
2526
# we're americans :)
2627
layout = KeyboardLayoutUS(kbd)
2728

Introducing_Gemma_M0/Gemma_HIDkeyboard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44

55
import board
6+
import usb_hid
67
from adafruit_hid.keyboard import Keyboard
78
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
89
from adafruit_hid.keycode import Keycode
@@ -21,7 +22,7 @@
2122
# the keyboard object!
2223
# sleep for a bit to avoid a race condition on some systems
2324
time.sleep(1)
24-
kbd = Keyboard()
25+
kbd = Keyboard(usb_hid.devices)
2526
# we're americans :)
2627
layout = KeyboardLayoutUS(kbd)
2728

Launch_Deck_Trellis_M4/launch_deck_trellis_m4.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
import random
77
import adafruit_trellism4
8+
import usb_hid
89
from adafruit_hid.keyboard import Keyboard
910
from adafruit_hid.keycode import Keycode
1011
from adafruit_hid.consumer_control import ConsumerControl
@@ -80,8 +81,8 @@
8081

8182
TOTAL_SNORE = SNORE_PAUSE + SNORE_UP + SNORE_DOWN
8283

83-
kbd = Keyboard()
84-
cc = ConsumerControl()
84+
kbd = Keyboard(usb_hid.devices)
85+
cc = ConsumerControl(usb_hid.devices)
8586

8687
trellis = adafruit_trellism4.TrellisM4Express(rotation=ROTATION)
8788
for button in keymap:

Make_It_A-Mouse/cpx-mouse-buttons.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import usb_hid
12
from adafruit_circuitplayground.express import cpx
23
from adafruit_hid.mouse import Mouse
34

4-
m = Mouse()
5+
m = Mouse(usb_hid.devices)
56

67
while True:
78
if cpx.button_a:

Make_It_A-Mouse/mouse-cursor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import usb_hid
12
from adafruit_circuitplayground.express import cpx
23
from adafruit_hid.mouse import Mouse
34

4-
m = Mouse()
5+
m = Mouse(usb_hid.devices)
56
cpx.adjust_touch_threshold(200)
67

78
while True:

Make_It_A_Keyboard/keyboard-multimedia.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import usb_hid
12
from adafruit_circuitplayground.express import cpx
23
from adafruit_hid.consumer_control import ConsumerControl
34
from adafruit_hid.consumer_control_code import ConsumerControlCode
45

5-
cc = ConsumerControl()
6+
cc = ConsumerControl(usb_hid.devices)
67

78
while True:
89
if cpx.button_a:

Make_It_A_Keyboard/keycodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import usb_hid
12
from adafruit_circuitplayground.express import cpx
23
from adafruit_hid.keyboard import Keyboard
34
from adafruit_hid.keycode import Keycode
45

5-
kbd = Keyboard()
6+
kbd = Keyboard(usb_hid.devices)
67

78
while True:
89
if cpx.button_a:

Make_It_A_Keyboard/strings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import usb_hid
12
from adafruit_circuitplayground.express import cpx
23
from adafruit_hid.keyboard import Keyboard
34
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
45

5-
kbd = Keyboard()
6+
kbd = Keyboard(usb_hid.devices)
67
layout = KeyboardLayoutUS(kbd)
78

89
while True:

Make_It_Log/time-light-temp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from digitalio import DigitalInOut, Direction, Pull
99
import analogio
1010
import board
11+
import usb_hid
1112
from adafruit_hid.keyboard import Keyboard
1213
from adafruit_hid.keycode import Keycode
1314
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
@@ -26,7 +27,7 @@
2627
# Set the keyboard object!
2728
# Sleep for a bit to avoid a race condition on some systems
2829
time.sleep(1)
29-
kbd = Keyboard()
30+
kbd = Keyboard(usb_hid.devices)
3031
layout = KeyboardLayoutUS(kbd) # US is only current option...
3132

3233
led = DigitalInOut(board.D13) # Set up red LED "D13"

Minecraft_Gesture_Controller/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# General purpose libraries
2020
import touchio
2121
# Libraries for HID Keyboard & Mouse
22+
import usb_hid
2223
from adafruit_hid.keyboard import Keyboard
2324
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
2425
from adafruit_hid.keycode import Keycode
@@ -37,11 +38,11 @@
3738
# Keyboard & Mouse Setup
3839

3940
# The keyboard object!
40-
kbd = Keyboard()
41+
kbd = Keyboard(usb_hid.devices)
4142
# we're americans :)
4243
layout = KeyboardLayoutUS(kbd)
4344
# The mouse object!
44-
mouse = Mouse()
45+
mouse = Mouse(usb_hid.devices)
4546

4647
# Accelerometer Setup
4748

Rotary_Encoder/rotary_encoder_volume.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import rotaryio
22
import board
33
import digitalio
4+
import usb_hid
45
from adafruit_hid.consumer_control import ConsumerControl
56
from adafruit_hid.consumer_control_code import ConsumerControlCode
67

@@ -10,7 +11,7 @@
1011

1112
encoder = rotaryio.IncrementalEncoder(board.D10, board.D9)
1213

13-
cc = ConsumerControl()
14+
cc = ConsumerControl(usb_hid.devices)
1415

1516
button_state = None
1617
last_position = encoder.position

0 commit comments

Comments
 (0)