Skip to content

Commit

Permalink
Switch to charachorder.py (#125)
Browse files Browse the repository at this point in the history
* refactor: use charachorder.py

* fix: remove pyserial from the repo

* fix: bump cc.py version
  • Loading branch information
GetPsyched authored Jul 29, 2024
1 parent bbbbd75 commit efa88a7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 292 deletions.
261 changes: 0 additions & 261 deletions nexus/CCSerial/CCSerial.py

This file was deleted.

5 changes: 0 additions & 5 deletions nexus/CCSerial/__init__.py

This file was deleted.

48 changes: 23 additions & 25 deletions nexus/Freqlog/Freqlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
from threading import Thread
from typing import Optional

from charachorder import CharaChorder, SerialException
from pynput import keyboard as kbd, mouse
from serial import SerialException

from .backends import Backend, SQLiteBackend
from .Definitions import ActionType, BanlistAttr, BanlistEntry, CaseSensitivity, ChordMetadata, ChordMetadataAttr, \
Defaults, WordMetadata, WordMetadataAttr
from ..CCSerial import CCSerial


class Freqlog:
Expand Down Expand Up @@ -230,21 +229,23 @@ def _get_chords(self):
"""
Get chords from device
"""
logging.info(f"Getting {self.dev.get_chordmap_count()} chords from device")
self.chords = []
started_logging = False # prevent early short-circuit
for chord in self.dev.list_device_chords():
self.chords.append(chord.strip())
if not self.is_logging: # Short circuit if logging is stopped
if started_logging:
logging.info("Stopped getting chords from device")
break
if self.device is None:
return

with self.device:
logging.info(f"Getting {self.device.get_chordmap_count()} chords from device")
self.chords = []
started_logging = False # prevent early short-circuit
for chord, phrase in self.device.get_chordmaps():
self.chords.append(str(phrase).strip())
if not self.is_logging: # Short circuit if logging is stopped
if started_logging:
logging.info("Stopped getting chords from device")
break
else:
started_logging = True
else:
started_logging = True
else:
logging.info(f"Got {len(self.chords)} chords from device")
if self.dev:
self.dev.close()
logging.info(f"Got {len(self.chords)} chords from device")

@staticmethod
def is_backend_initialized(backend_path: str) -> bool:
Expand Down Expand Up @@ -272,12 +273,12 @@ def __init__(self, backend_path: str, password_callback: callable, loggable: boo
:raises cryptography.fernet.InvalidToken: If the password is incorrect
"""
logging.info("Initializing freqlog")
self.dev: CCSerial | None = None
self.device: CharaChorder | None = None
self.chords: list[str] | None = None
self.num_chords: int | None = None

# Get serial device
devices = CCSerial.list_devices()
devices = CharaChorder.list_devices()
if len(devices) == 0:
logging.warning("No CharaChorder devices found")
else:
Expand All @@ -286,8 +287,9 @@ def __init__(self, backend_path: str, password_callback: callable, loggable: boo
logging.debug(f"Other devices: {devices[1:]}")
logging.info(f"Connecting to CharaChorder device at {devices[0]}")
try:
self.dev = CCSerial(devices[0])
self.num_chords = self.dev.get_chordmap_count()
self.device = devices[0]
with self.device:
self.num_chords = self.device.get_chordmap_count()
except SerialException as e:
logging.error(f"Failed to connect to CharaChorder device: {devices[0]}")
logging.error(e)
Expand All @@ -300,11 +302,7 @@ def __init__(self, backend_path: str, password_callback: callable, loggable: boo
logging.info(f"Logging set to freqlog db at {backend_path}")

# Asynchronously get chords from device
if self.dev:
Thread(target=self._get_chords).start()
else: # We're done with device, close it
if self.dev:
self.dev.close()
Thread(target=self._get_chords).start()

self.backend: Backend = SQLiteBackend(backend_path, password_callback, upgrade_callback)
self.q: Queue = Queue()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
charachorder.py~=0.5.2
pynput~=1.7.6
pyinstaller~=5.13
setuptools~=70.3
PySide6~=6.5
pySerial~=3.5
cryptography~=42.0
requests~=2.32.1

0 comments on commit efa88a7

Please sign in to comment.