-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_matrix.py
More file actions
91 lines (74 loc) · 3 KB
/
Copy pathinput_matrix.py
File metadata and controls
91 lines (74 loc) · 3 KB
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
'''This is a standard pick_keeb input module with init() and
get_state() functions to handle keyboard matrix scanning
using pio. '''
from input import InputModule
from machine import Pin
class InputModule(InputModule):
'''This is a standard pick_keeb input module with init() and
get_state() functions to handle keyboard matrix scanning
using pio. '''
def __init__(self, input_state):
super().__init__(input_state)
# DIODE_DIR = 'COL2ROW' # or 'ROW2COL'
self.COL2ROW = False
self.ROWS = [8, 9, 10, 11]
self.COLS = [16, 17, 18, 20, 19]
if self.COL2ROW:
self.DRIVEN_PINS = self.COLS
self.READ_PINS = self.ROWS
else:
self.DRIVEN_PINS = self.ROWS
self.READ_PINS = self.COLS
self.DRIVEN_PINS = [Pin(i, Pin.OUT) for i in self.DRIVEN_PINS]
self.READ_PINS = [Pin(i, Pin.IN, Pin.PULL_DOWN) for i in self.READ_PINS]
self.keys_bits_offset = 0
def get_num_keys(self):
'''Standard pico_keeb input module function that tells
the main program how many keys this module handles so the main
program can allocate memory for it.'''
return 20
def init(self, keys_bits_offset, pio_machine_num):
'''Init is a standard function for pico_keeb input modules that
we use to store a referenc to the global InputState oject so
any inputs can be recorded in it each tick without any allocation.
We also can perform any needed module initialization here, like
pio state machines as well as other hardware setup.'''
self.keys_bits_offset = keys_bits_offset
def update_state(self):
'''update_state is a standarc pico_keeb function. It
updates the global InputState object, captured from the init function,
with data from this module.'''
key_count = 0
for dp in self.DRIVEN_PINS:
dp.value(1)
for rp in self.READ_PINS:
# Set the corresponding bit in the keys field
key_value = 1 if rp.value() else 0
self.set_key_state(key_count, key_value)
key_count += 1
dp.value(0)
if __name__ == "__main__":
from time import sleep
# It's kinda dumb to copy this class here for testing, but I don't want to have
# main.py on the pico while doing development because the board will try to run it
# at boot and cause probs. So here we are!
class InputState:
def __init__(self, num_keys):
self.keys = 0
self.wheel = []
self.mouse_x = 0
self.mouse_y = 0
self.mouse_enable = 0
def clear_deltas(self):
self.wheel = []
self.mouse_x = 0
self.mouse_y = 0
self.mouse_enable = 0
state = InputState(20)
matrix = InputModuleMatrix(state)
matrix.init(0, 0)
while True:
state.clear_deltas()
matrix.update_state()
print(matrix.state.keys)
sleep(0.5)