allow matrix sync on BLE split

typo
This commit is contained in:
Kyle Brown
2020-10-27 18:16:38 -07:00
parent 1af888f9c1
commit 994012b249
5 changed files with 53 additions and 42 deletions

View File

@@ -22,6 +22,7 @@ class BLE_Split(Extension):
self._is_target = True
self._connection_count = 0
self._uart = None
self._uart_connection = None
self._advertisment = None
self._advertising = False
@@ -42,7 +43,7 @@ class BLE_Split(Extension):
self.connect()
# Attempt to sanely guess a coord_mapping if one is not provided.
if not keyboard.coord_mapping:
if not keyboard.coord_mapping or (self.split_flip and not self._is_target):
keyboard.coord_mapping = []
rows_to_calc = len(keyboard.row_pins)
@@ -58,12 +59,11 @@ class BLE_Split(Extension):
def before_matrix_scan(self, keyboard_state):
self.check_all_connections()
if self._is_target:
return self._receive_from_initiator()
return self._receive()
def after_matrix_scan(self, keyboard_state, matrix_update):
if matrix_update is not None and not self._is_target:
self._send_to_target(matrix_update)
if matrix_update:
self._send(matrix_update)
def check_all_connections(self):
self._connection_count = len(self._ble.connections)
@@ -83,39 +83,30 @@ class BLE_Split(Extension):
def initiator_scan(self):
self._uart = None
self._uart_connection = None
# See if any existing connections are providing UARTService.
self._connection_count = len(self._ble.connections)
if self._connection_count > 0 and not self._uart:
for connection in self._ble.connections:
if UARTService in connection:
self._uart = connection
self._uart_connection = connection
self._uart = self._uart_connection[UARTService]
break
if not self._uart:
print('Scanning')
self._ble.stop_scan()
for adv in self._ble.start_scan(ProvideServicesAdvertisement, timeout=20):
print('Scanning')
if UARTService in adv.services:
self._uart = self._ble.connect(adv)
self._uart_connection = self._ble.connect(adv)
self._uart = self._uart_connection[UARTService]
self._ble.stop_scan()
print('Scan complete')
break
self._ble.stop_scan()
return
def send(self, data):
if self._uart and self._uart.connected:
try:
self._uart[UARTService].write(data)
except OSError:
try:
self._uart.disconnect()
except: # noqa: E722
print('UART disconnect failed')
print('Connection error')
self._uart = None
return
def target_advertise(self):
self._ble.stop_advertising()
print('Advertising')
@@ -145,12 +136,23 @@ class BLE_Split(Extension):
self._ble_last_scan = ticks_ms()
return self
def _send_to_target(self, update):
update[1] += self.split_offsets[update[0]]
if self._uart is not None and self._uart.connected:
self.send(update)
def _send(self, update):
if self._uart:
try:
if not self._is_target:
update[1] += self.split_offsets[update[0]]
self._uart.write(update)
except OSError:
try:
self._uart.disconnect()
except: # noqa: E722
print('UART disconnect failed')
print('Connection error')
self._uart_connection = None
self._uart = None
return
def _receive_from_initiator(self):
def _receive(self):
if self._uart is not None and self._uart.in_waiting > 0 or self._uart_buffer:
while self._uart.in_waiting >= 3:
self._uart_buffer.append(self._uart.read(3))

View File

@@ -2,7 +2,7 @@ from kmk.extensions import Extension
from kmk.keys import make_consumer_key
class Layers(Extension):
class MediaKeys(Extension):
def __init__(self):
# Consumer ("media") keys. Most known keys aren't supported here. A much
# longer list used to exist in this file, but the codes were almost certainly

View File

@@ -62,8 +62,11 @@ class RGB(Extension):
self.sat_step = sat_step
self.val_step = val_step
self.hue = hue_default
self.hue_default = hue_default
self.sat = sat_default
self.sat_default = sat_default
self.val = val_default
self.val_default = val_default
self.breathe_center = breathe_center
self.knight_effect_length = knight_effect_length
self.val_limit = val_limit
@@ -132,6 +135,11 @@ class RGB(Extension):
on_press=self._rgb_mode_knight,
on_release=handler_passthrough,
)
make_key(
names=('RGB_RESET', 'RGB_RST'),
on_press=self._rgb_reset,
on_release=handler_passthrough,
)
def after_hid_send(self, keyboard):
if self.animation_mode:
@@ -611,3 +619,12 @@ class RGB(Extension):
self.effect_init = True
self.animation_mode = AnimationModes.KNIGHT
return state
def _rgb_reset(self, key, state, *args, **kwargs):
self.hue = self.hue_default
self.sat = self.sat_default
self.val = self.val_default
if self.animation_mode == AnimationModes.STATIC_STANDBY:
self.animation_mode = AnimationModes.STATIC
self._do_update()
return state

View File

@@ -79,13 +79,13 @@ class Split(Extension):
def before_matrix_scan(self, keyboard_state):
if self._is_target:
return self._receive_from_initiator()
return self._receive()
def after_matrix_scan(self, keyboard_state, matrix_update):
if matrix_update is not None and not self._is_target:
self._send_to_target(matrix_update)
self._send(matrix_update)
def _send_to_target(self, update):
def _send(self, update):
if self.split_target_left:
update[1] += self.split_offsets[update[0]]
else:
@@ -93,7 +93,7 @@ class Split(Extension):
if self._uart is not None:
self._uart.write(update)
def _receive_from_initiator(self):
def _receive(self):
if self._uart is not None and self._uart.in_waiting > 0 or self._uart_buffer:
if self._uart.in_waiting >= 60:
# This is a dirty hack to prevent crashes in unrealistic cases