Skip to content

Commit fb4c126

Browse files
committed
add support for power button
1 parent 563dd1e commit fb4c126

File tree

1 file changed

+15
-3
lines changed
  • CircuitPython_NeXT_Keyboard_RP2040

1 file changed

+15
-3
lines changed

CircuitPython_NeXT_Keyboard_RP2040/code.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
import board
77
import rp2pio
88
import usb_hid
9+
from keypad import Keys
910
from adafruit_hid.consumer_control import ConsumerControl
1011
from adafruit_hid.keyboard import Keyboard
12+
from adafruit_hid.keyboard import Keycode
1113
from adafruit_pioasm import Program
1214
from adafruit_ticks import ticks_add, ticks_less, ticks_ms
1315

16+
# Customize the power key's keycode. You can change it to `Keycode.POWER` if
17+
# you really want to accidentally power off your computer!
18+
POWER_KEY_SENDS = Keycode.F1
19+
1420
from next_keycode import (
1521
cc_value,
1622
is_cc,
@@ -20,9 +26,11 @@
2026
shift_modifiers,
2127
)
2228

23-
NEXT_SERIAL_BUS_FREQUENCY = (
24-
18958 # 455kHz/24 https://journal.spencerwnelson.com/entries/nextkb.html
25-
)
29+
# according to https://journal.spencerwnelson.com/entries/nextkb.html the
30+
# keyboard's timing source is a 455MHz crystal, and the serial data rate is
31+
# 1/24 the crystal frequency. This differs by a few percent from the "50us" bit
32+
# time reported in other sources.
33+
NEXT_SERIAL_BUS_FREQUENCY = round(455_000 / 24)
2634

2735
pio_program = Program(
2836
"""
@@ -162,6 +170,7 @@ def handle_report(self, report_value):
162170
else:
163171
self.set_key_state(code, make)
164172

173+
keys = Keys([board.SCK], value_when_pressed=False)
165174

166175
handler = KeyboardHandler()
167176

@@ -181,6 +190,9 @@ def handle_report(self, report_value):
181190

182191
try:
183192
while True:
193+
if (event := keys.events.get()):
194+
handler.set_key_state(POWER_KEY_SENDS, event.pressed)
195+
184196
sm.write(QUERY)
185197
deadline = ticks_add(ticks_ms(), 100)
186198
while ticks_less(ticks_ms(), deadline):

0 commit comments

Comments
 (0)