Skip to content

Commit 7f70aad

Browse files
committed
add docstring
1 parent ae55bc3 commit 7f70aad

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

keyboard/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969

7070
@micropython.asm_thumb
7171
def mem(r0):
72+
"""Read memory from the address"""
7273
ldr(r0, [r0, 0])
7374

7475

@@ -82,6 +83,7 @@ def reset_into_bootloader():
8283

8384

8485
def is_tapped(matrix, key):
86+
"""Check if the key is tapped (press & release quickly)"""
8587
n = len(matrix)
8688
if n == 0:
8789
n = matrix.wait(500 - matrix.ms(matrix.time() - matrix.get_keydown_time(key)))
@@ -94,6 +96,7 @@ def is_tapped(matrix, key):
9496
200 - matrix.ms(matrix.time() - matrix.get_keydown_time(key))
9597
)
9698
if n == 2 and target == matrix.view(1):
99+
# Fast typing: A down, B down, A up, B up
97100
return True
98101

99102
return False

keyboard/matrix.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55

66

77
class Matrix:
8+
"""
9+
Implement the drive of keyboard matrix and provide an event queue.
10+
"""
11+
812
# ROWS = (P0_05, P0_06, P0_07, P0_08, P1_09, P1_08, P0_12, P0_11)
913
# COLS = (P0_19, P0_20, P0_21, P0_22, P0_23, P0_24, P0_25, P0_26)
1014
ROWS = ()
1115
COLS = ()
16+
17+
# direction of diode
1218
ROW2COL = False
1319

1420
def __init__(self):
@@ -42,6 +48,11 @@ def __init__(self):
4248
self._debounce_time = 20000000
4349

4450
def scan(self):
51+
"""
52+
Scan keyboard matrix and save key event into the queue.
53+
54+
:return: length of the key event queue.
55+
"""
4556
t = time.monotonic_ns()
4657

4758
# use local variables to speed up
@@ -83,7 +94,8 @@ def scan(self):
8394

8495
return self.length
8596

86-
def wait(self, timeout=0):
97+
def wait(self, timeout=1000):
98+
"""Wait for a new key event or timeout"""
8799
last = self.length
88100
if timeout:
89101
end_time = time.monotonic_ns() + timeout * 1000000
@@ -98,13 +110,15 @@ def wait(self, timeout=0):
98110
return n
99111

100112
def put(self, data):
113+
"""Put a key event into the queue"""
101114
self.queue[self.head] = data
102115
self.head += 1
103116
if self.head >= self.keys:
104117
self.head = 0
105118
self.length += 1
106119

107120
def get(self):
121+
"""Remove and return the first event from the queue."""
108122
data = self.queue[self.tail]
109123
self.tail += 1
110124
if self.tail >= self.keys:
@@ -113,24 +127,31 @@ def get(self):
113127
return data
114128

115129
def view(self, n):
130+
"""Return the specified event"""
116131
return self.queue[(self.tail + n) % self.keys]
117132

118133
def __getitem__(self, n):
134+
"""Return the specified event"""
119135
return self.queue[(self.tail + n) % self.keys]
120136

121137
def __len__(self):
138+
"""Return the number of events in the queue"""
122139
return self.length
123140

124141
def get_keydown_time(self, key):
142+
"""Return the key pressed time"""
125143
return self.t0[key]
126144

127145
def get_keyup_time(self, key):
146+
"""Return the key released time"""
128147
return self.t1[key]
129148

130149
def time(self):
150+
"""Return current time"""
131151
return time.monotonic_ns()
132152

133153
def ms(self, t):
154+
"""Convert time to milliseconds"""
134155
return t // 1000000
135156

136157
@property
@@ -139,7 +160,9 @@ def debounce_time(self):
139160

140161
@debounce_time.setter
141162
def debounce_time(self, t):
163+
"""Set debounce time"""
142164
self._debounce_time = t * 1000000
143165

144166
def suspend(self):
167+
"""Suspend keyboard"""
145168
pass

0 commit comments

Comments
 (0)