refactoring

This commit is contained in:
Maxim Devaev
2023-03-21 03:43:20 +02:00
parent 4220fe5908
commit ecf3faf984
3 changed files with 100 additions and 128 deletions

View File

@@ -19,62 +19,50 @@
# #
# ========================================================================== #
from .... import aiomulti
from ....keyboard.mappings import KEYMAP
# =====
class Keyboard:
def __init__(self) -> None:
self.__notifier = aiomulti.AioProcessNotifier()
self.__leds = aiomulti.AioSharedFlags({
"num": False,
"caps": False,
"scroll": False,
}, self.__notifier, type=bool)
self.__modifiers = 0x00
}, aiomulti.AioProcessNotifier(), bool)
self.__modifiers = 0
self.__active_keys: list[int] = []
def key(self, key: str, state: bool) -> list[int]:
modifier = self.__is_modifier(key)
code = self.__keycode(key)
if not state:
if not modifier and code in self.__active_keys:
self.__active_keys.remove(code)
if modifier and self.__modifiers:
self.__modifiers &= ~code
if state:
if not modifier and len(self.__active_keys) < 6:
self.__active_keys.append(code)
if modifier:
self.__modifiers |= code
return self.__key()
async def leds(self) -> dict:
leds = await self.__leds.get()
return leds
def set_leds(self, led_byte: int) -> None:
num = bool(led_byte & 1)
caps = bool((led_byte >> 1) & 1)
scroll = bool((led_byte >> 2) & 1)
self.__leds.update(num=num, caps=caps, scroll=scroll)
self.__leds.update(
num=bool(led_byte & 1),
caps=bool((led_byte >> 1) & 1),
scroll=bool((led_byte >> 2) & 1),
)
def __key(self) -> list[int]:
async def get_leds(self) -> dict[str, bool]:
return (await self.__leds.get())
def process_key(self, key: str, state: bool) -> list[int]:
code = KEYMAP[key].usb.code
is_modifier = KEYMAP[key].usb.is_modifier
if state:
if is_modifier:
self.__modifiers |= code
elif len(self.__active_keys) < 6 and code not in self.__active_keys:
self.__active_keys.append(code)
else:
if is_modifier:
self.__modifiers &= ~code
elif code in self.__active_keys:
self.__active_keys.remove(code)
cmd = [
0x00, 0x02, 0x08,
self.__modifiers,
0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
counter = 0
for code in self.__active_keys:
cmd[5 + counter] = code
counter += 1
0, 0x02, 0x08, self.__modifiers, 0,
0, 0, 0, 0, 0, 0,
]
for (index, code) in enumerate(self.__active_keys):
cmd[index + 5] = code
return cmd
def __keycode(self, key: str) -> int:
return KEYMAP[key].usb.code
def __is_modifier(self, key: str) -> bool:
return KEYMAP[key].usb.is_modifier