Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions code.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,53 @@
),
)

# Use different keymaps on different connections
# Valid keys are "USB" and "BT0"-"BT9"
# Connection not in this map will use default keymap defined above.
keyboard.profiles = {
"BT1": (
# layer 0
(
ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE,
TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|',
CAPS, A, S, L2D, F, G, H, J, K, L, SCC, '"', ENTER,
LSFT4, Z, X, C, V, L3B, N, M, ',', '.', '/', RSFT4,
LCTRL, LALT, LGUI, SPACE, MENU, RALT, L1, RCTRL
),

# layer 1
(
'`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL,
___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___,
___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___,BOOT, ___,MACRO(1), ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___, ___
),

# Other layers...
),
"BT2": (
# layer 0
(
ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE,
TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|',
CAPS, A, S, L2D, F, G, H, J, K, L, SCC, '"', ENTER,
LSFT4, Z, X, C, V, L3B, N, M, ',', '.', '/', RSFT4,
LCTRL, LGUI, LALT, SPACE, RALT, MENU, L1, RCTRL
),

# layer 1
(
'`', F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL,
___, ___, UP, ___, ___, ___, ___, ___, ___, ___,SUSPEND,___,___,___,
___,LEFT,DOWN,RIGHT,___, ___, ___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___,BOOT, ___,MACRO(2), ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___, ___
),

# Other layers...
)
}

def macro_handler(dev, n, is_down):
if is_down:
Expand Down
47 changes: 39 additions & 8 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(
ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '-', '=', BACKSPACE,
TAB, Q, W, E, R, T, Y, U, I, O, P, '[', ']', '|',
CAPS, A, S, L2D, F, G, H, J, K, L, SCC, '"', ENTER,
CAPS, A, S, D, F, G, H, J, K, L, SCC, '"', ENTER,
LSHIFT,Z, X, C, V, L3B, N, M, ',', '.', '/', RSHIFT,
LCTRL, LGUI, LALT, SPACE, RALT, MENU, L1, RCTRL
),
Expand Down Expand Up @@ -136,6 +136,7 @@ def send_text(self, text):
class Keyboard:
def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
self.keymap = KEYMAP
self.profiles = {}
self.pairs = pairs
self.verbose = verbose
self.pairs_handler = None
Expand All @@ -148,6 +149,8 @@ def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
self.usb_status = 0
self.leds = None

self._current_conn = ""

self.data = array.array('L', microcontroller.nvm[:272])
if self.data[0] != 0x424b5950:
self.data[0] = 0x424b5950
Expand All @@ -164,15 +167,32 @@ def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
self.ble_hid = HID(ble_hid.devices)
self.usb_hid = HID(usb_hid.devices)

def update_current_conn(self):
if usb_is_connected() and self.usb_status == 3:
conn = "USB"
elif self.ble.connected:
conn = "BT%d" % self.ble_id
else:
conn = ""
if conn != self._current_conn:
self._current_conn = conn
if conn in self.action_maps:
self.current_keymap = self.action_maps[self._current_conn]
else:
self.current_keymap = self.actonmap
print("Current connection changed to %s" % self._current_conn)

def check(self):
if self.adv_timeout:
if self.ble.connected:
self.adv_timeout = 0
self.backlight.set_bt_led(None)

for c in self.ble.connections:
if c.connection_interval > 11.25:
c.connection_interval = 11.25
try:
if c.connection_interval > 11.25:
c.connection_interval = 11.25
except _bleio.BluetoothError:
self.log("failed to set ble connection interval")
self.log('ble connection interval {}'.format(c.connection_interval))
elif time.time() > self.adv_timeout:
self.stop_advertising()
Expand All @@ -194,14 +214,20 @@ def check(self):
self.leds = leds
self.backlight.set_hid_leds(leds)
self.log('keyboard leds {}'.format(bin(leds)))

self.update_current_conn()

def setup(self):
convert = lambda a: array.array('H', (get_action_code(k) for k in a))
self.actonmap = tuple(convert(layer) for layer in self.keymap)

self.action_maps = {}
for key in self.profiles:
self.action_maps[key] = tuple(convert(layer) for layer in self.profiles[key])

for pair in self.pairs:
for key in pair:
self.pair_keys.add(key)
self.update_current_conn()

def start_advertising(self):
self.ble.start_advertising(self.advertisement)
Expand Down Expand Up @@ -251,17 +277,22 @@ def toggle_bt(self):
self.stop_advertising()
else:
self.start_advertising()
self.update_current_conn()

def toggle_usb(self):
if usb_is_connected():
self.usb_status = 1 if self.usb_status == 3 else 3
if self.usb_status == 1:
self.usb_status = 3
else:
self.usb_status = 1
self.update_current_conn()

def action_code(self, position):
position = COORDS[position]
layer_mask = self.layer_mask
for layer in range(len(self.actonmap) - 1, -1, -1):
for layer in range(len(self.current_keymap) - 1, -1, -1):
if (layer_mask >> layer) & 1:
code = self.actonmap[layer][position]
code = self.current_keymap[layer][position]
if code == 1: # TRANSPARENT
continue
return code
Expand Down