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
|
import json
import time
from kmk.modules import Module
from usb_cdc import data
class Client(Module):
""" Listen the connections from host, and apply the actions depending of
the json command.
"""
def __init__(self):
self.last_name = None
def during_bootup(self, keyboard):
try:
# Do not set any timeout, we check before reading a string if there
# is any content to read, but block to be sure to read everything.
data.timeout = None
except AttributeError:
pass
def process_key(self, keyboard, key, is_pressed, int_coord):
return key
def before_hid_send(self, keyboard):
# Serial.data isn't initialized yet.
if not data or not data.connected:
current_name = keyboard.keymap[0].name
if current_name != "Disconnected":
self.last_name = current_name
keyboard.keymap[0].name = "Disconnected"
time.sleep(1)
return
if self.last_name is not None:
keyboard.keymap[0].name = self.last_name
self.last_name = None
# Nothing to parse.
if data.in_waiting <= 0:
return
line = data.readline()
if not line:
return
try:
content = line.decode().strip()
if not content:
# If strip produced an empty string, stop here.
return
jdata = json.loads(content)
except ValueError as err:
# In circuitPython, json error are reported as ValueError.
# seet https://docs.circuitpython.org/en/latest/docs/library/json.html#json.load
print(err, line)
return
# Json is valid, try to read the command, and consider the input is a
# direct raw layer instruction if this does not work.
actions = {
"layer" : lambda x : keyboard.keymap[0].load(x),
}
try:
for action, args in jdata.items():
actions[action](args)
return
except Exception as err:
pass
try:
keyboard.keymap[0].load(jdata)
except Exception as err:
print("rawdata:", err)
return
def before_matrix_scan(self, keyboard):
""" Default implementation, do nothing.
"""
def after_matrix_scan(self, keyboard):
""" Default implementation, do nothing.
"""
def after_hid_send(self, keyboard):
""" Default implementation, do nothing.
"""
def on_powersave_enable(self, keyboard):
""" Default implementation, do nothing.
"""
def on_powersave_disable(self, keyboard):
""" Default implementation, do nothing.
"""
|