diff --git a/src/pythonAPI/whspah/__init__.py b/src/pythonAPI/whspah/__init__.py index bcc3462..c83698e 100644 --- a/src/pythonAPI/whspah/__init__.py +++ b/src/pythonAPI/whspah/__init__.py @@ -4,7 +4,7 @@ local and free way to interact with CaiXianlin shock collars. """ from .driver import Transmitter -from .driver import MODES +from .constants import MODES from . import exceptions from . import utils diff --git a/src/pythonAPI/whspah/constants.py b/src/pythonAPI/whspah/constants.py new file mode 100644 index 0000000..5540e01 --- /dev/null +++ b/src/pythonAPI/whspah/constants.py @@ -0,0 +1,18 @@ +""" +Holds constants used throughout WHSPAH. +""" + +from enum import Enum + +# The hardware magic string. +HARDWARE_MAGIC = b'/WHSPAH\x00' + +class MODES(Enum): + """ + The different commands or "modes" of the shockers. + """ + + SHOCK = 1 + VIBRATE = 2 + BEEP = 3 + LIGHT = 4 diff --git a/src/pythonAPI/whspah/driver.py b/src/pythonAPI/whspah/driver.py index 4ab59a9..adfd3e4 100644 --- a/src/pythonAPI/whspah/driver.py +++ b/src/pythonAPI/whspah/driver.py @@ -13,6 +13,7 @@ import os import serial +from whspah.constants import MODES, HARDWARE_MAGIC import whspah.exceptions as exceptions import whspah.utils as utils @@ -20,17 +21,6 @@ CONNECTION_READ_TIMEOUT = 2.5 TX_PING_INTERVAL = 1 RX_PING_TIMEOUT = 5 - -class MODES(Enum): - """ - The different commands or "modes" of the shockers. - """ - - SHOCK = 1 - VIBRATE = 2 - BEEP = 3 - LIGHT = 4 - class STATES(Enum): """ The different states of the transmitter object. @@ -184,7 +174,7 @@ class Transmitter: select.select([self._ser], [], [], CONNECTION_READ_TIMEOUT) st = time.perf_counter() # Wait for WHSPAH message - while (self._ser.read_until(b'\x00') != utils.HARDWARE_MAGIC) and\ + while (self._ser.read_until(b'\x00') != HARDWARE_MAGIC) and\ time.perf_counter() - st < CONNECTION_READ_TIMEOUT: time.sleep(0.1) @@ -214,7 +204,7 @@ class Transmitter: receivedChecksum = buffer[-2] expectedChecksum = calculateChecksum(buffer[:-2]) - if cmdID == ord('/') and buffer != utils.HARDWARE_MAGIC: + if cmdID == ord('/') and buffer != HARDWARE_MAGIC: message = buffer[1:-1].decode() self._log.info('<- %s', message) else: diff --git a/src/pythonAPI/whspah/utils.py b/src/pythonAPI/whspah/utils.py index a3629d8..deb26e3 100644 --- a/src/pythonAPI/whspah/utils.py +++ b/src/pythonAPI/whspah/utils.py @@ -11,11 +11,7 @@ import io import serial.tools.list_ports import serial -# The hardware magic string. -# I couldn't really think of a better place to put this, -# so I figured utils is the closest we have to a globals.py file. -# I don't want to create a globals.py file for this one variable. -HARDWARE_MAGIC = b'/WHSPAH\x00' +from whspah.constants import HARDWARE_MAGIC def _testPortForWHSPAH(resultList: list, port: os.PathLike, baud: int, timeout: float) -> None: """