Skip to content

Commit 14d6d38

Browse files
committed
Multi-profiles support
1 parent 0cc72d9 commit 14d6d38

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

keyboard/__init__.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
(
3333
ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE,
3434
TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|',
35-
CAPS, A, S, L2D, F, G, H, J, K, L, SCC, '"', ENTER,
35+
CAPS, A, S, D, F, G, H, J, K, L, SCC, '"', ENTER,
3636
LSHIFT,Z, X, C, V, L3B, N, M, ',', '.', '/', RSHIFT,
3737
LCTRL, LGUI, LALT, SPACE, RALT, MENU, L1, RCTRL
3838
),
@@ -136,6 +136,7 @@ def send_text(self, text):
136136
class Keyboard:
137137
def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
138138
self.keymap = KEYMAP
139+
self.profiles = {}
139140
self.pairs = pairs
140141
self.verbose = verbose
141142
self.pairs_handler = None
@@ -194,15 +195,23 @@ def check(self):
194195
self.leds = leds
195196
self.backlight.set_hid_leds(leds)
196197
self.log('keyboard leds {}'.format(bin(leds)))
197-
198+
198199
def setup(self):
199200
convert = lambda a: array.array('H', (get_action_code(k) for k in a))
200201
self.actonmap = tuple(convert(layer) for layer in self.keymap)
202+
self.action_maps = {}
203+
for key in self.profiles:
204+
self.action_maps[key] = tuple(convert(layer) for layer in self.profiles[key])
201205

202206
for pair in self.pairs:
203207
for key in pair:
204208
self.pair_keys.add(key)
205209

210+
def get_actionmap(self):
211+
if self.current_channel() in self.action_maps:
212+
return self.action_maps[self.current_channel()]
213+
return self.actonmap
214+
206215
def start_advertising(self):
207216
self.ble.start_advertising(self.advertisement)
208217
self.backlight.set_bt_led(self.ble_id)
@@ -225,7 +234,7 @@ def change_bt(self, n):
225234

226235
if 0 > n or n > 9:
227236
return
228-
237+
229238
uid = self.uid[n:n+6]
230239
uid[-1] = uid[-1] | 0xC0
231240
address = _bleio.Address(uid, _bleio.Address.RANDOM_STATIC)
@@ -242,6 +251,7 @@ def change_bt(self, n):
242251
print(e)
243252
self.log(self.ble._adapter.address)
244253
self.start_advertising()
254+
print(self.current_channel())
245255

246256
def toggle_bt(self):
247257
if self.ble.connected:
@@ -251,17 +261,24 @@ def toggle_bt(self):
251261
self.stop_advertising()
252262
else:
253263
self.start_advertising()
264+
print(self.current_channel())
265+
266+
def current_channel(self):
267+
if self.ble.connected:
268+
return "BT%d" % self.ble_id
269+
return "USB"
254270

255271
def toggle_usb(self):
256272
if usb_is_connected():
257273
self.usb_status = 1 if self.usb_status == 3 else 3
274+
print(self.current_channel())
258275

259276
def action_code(self, position):
260277
position = COORDS[position]
261278
layer_mask = self.layer_mask
262-
for layer in range(len(self.actonmap) - 1, -1, -1):
279+
for layer in range(len(self.get_actionmap()) - 1, -1, -1):
263280
if (layer_mask >> layer) & 1:
264-
code = self.actonmap[layer][position]
281+
code = self.get_actionmap()[layer][position]
265282
if code == 1: # TRANSPARENT
266283
continue
267284
return code

0 commit comments

Comments
 (0)