forked from qbalsdon/pico_rgb_keypad_hid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
executable file
·128 lines (119 loc) · 4.49 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import time
import board
import busio
import usb_hid
from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_dotstar
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from digitalio import DigitalInOut, Direction, Pull
#------------------------------------
USE_DISPLAY = True
if USE_DISPLAY:
from picodisplay import *
picoDisplay = PicoDisplay()
picoDisplay.setBacklightPercent(10)
wallpapers = [ picoDisplay.getAndroid, picoDisplay.getTeams, picoDisplay.getDota ]
#------------------------------------
from constants import *
from keypad import *
from keyconfig.adb import *
from keyconfig.teams import *
from keyconfig.dota import *
#------------------------------------
for _ in range(10):
print(" ")
print(" ============ NEW EXECUTION ============ ")
#------------------------------------
interfaces = [ AdbKeypad, TeamsKeypad, DotAKeypad ]
currentInterface = -1
#------------------------------------
# CS : GP17 - 22
# SCLK: GP18 - 24
# MOSI: GP19 - 25
# ----- I2C -----
# INT : GP3 - 05 ? Not used!
# SDA : GP4 - 06
# SCL : GP5 - 07
cs = DigitalInOut(board.GP17)
cs.direction = Direction.OUTPUT
cs.value = 0
pixels = adafruit_dotstar.DotStar(board.GP18, board.GP19, BUTTON_COUNT, brightness=0.2, auto_write=True)
i2c = busio.I2C(board.GP5, board.GP4)
device = I2CDevice(i2c, 0x20)
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
#------------------------------------
picoLED = DigitalInOut(board.GP25)
picoLED.direction = Direction.OUTPUT
picoLED.value = 0
#------------------------------------
timeDown = [-1] * BUTTON_COUNT
timeUp = [-1] * BUTTON_COUNT
waiting = [False] * BUTTON_COUNT
keypadButtonStates = [ timeDown, timeUp, waiting ]
#------------------------------------
def setKeyColour(pixel, colour):
pixels[pixel] = (((colour >> 16) & 255), (colour >> 8) & 255, colour & 255)
def swapLayout():
global currentKeypadConfiguration
global currentInterface
currentInterface = (currentInterface + 1) % len(interfaces)
currentKeypadConfiguration = interfaces[currentInterface](kbd, layout, setKeyColour)
currentKeypadConfiguration.introduce()
if USE_DISPLAY:
picoDisplay.render(wallpapers[currentInterface](), 270)
def read_button_states(x, y):
pressed = [0] * BUTTON_COUNT
with device:
device.write(bytes([0x0]))
result = bytearray(2)
device.readinto(result)
b = result[0] | result[1] << 8
for i in range(x, y):
if not (1 << i) & b:
pressed[i] = 1
else:
pressed[i] = 0
return pressed
#------------------------------------
def checkHeldForFlash(heldDownStartMillis):
if heldDownStartMillis > 0:
downTime = timeInMillis() - heldDownStartMillis
picoLED.value = (downTime >= LONG_HOLD and downTime <= LONG_HOLD + 100) or (downTime >= EXTRA_LONG_HOLD and downTime <= EXTRA_LONG_HOLD + 100)
else:
picoLED.value = 0
#------------------------------------
if USE_DISPLAY:
rainbow = picoDisplay.createRainbow()
rainbow.append(picoDisplay.createText("Welcome", COLOUR_BLACK, 30, 40))
picoDisplay.render(rainbow, 270)
#------------------------------------
currentKeypadConfiguration = KeypadInterface(kbd, layout, setKeyColour)
currentKeypadConfiguration.introduce()
#------------------------------------
helpMode=False
while True:
currentKeypadConfiguration.loop()
if USE_DISPLAY:
for displayKeyIndex in range(DISPLAY_BUTTON_COUNT):
buttonValue = checkButton(displayKeyIndex,
not picoDisplay.Buttons[displayKeyIndex].value,
picoDisplay.ButtonStates,
checkHeldForFlash)
if displayKeyIndex == 0 and buttonValue & EVENT_SINGLE_PRESS:
swapLayout()
if displayKeyIndex == 1 and buttonValue & EVENT_SINGLE_PRESS:
helpMode = True
# displayHelpMode()
pressed = read_button_states(0, BUTTON_COUNT)
for keyIndex in range(BUTTON_COUNT):
event = checkButton(keyIndex, pressed[keyIndex], keypadButtonStates, checkHeldForFlash)
if helpMode:
print(currentKeypadConfiguration.helpForKey(keyIndex))
helpMode = False
else:
currentKeypadConfiguration.handleEvent(keyIndex, event)