diff --git a/db-handler/app/constants.py b/db-handler/app/constants.py index be15b6f..425334b 100644 --- a/db-handler/app/constants.py +++ b/db-handler/app/constants.py @@ -25,5 +25,5 @@ LOGGER_TIME_FORMAT = '%I:%M:%S %p' # Values for microcontroller and servos/actuators -FREE = 1 # 1 is sent to the microcontroller if user is not busy -BUSY = 0 # 0 is sent to the microcontroller if user is busy +FREE = 0 # 0 is sent to the microcontroller if user is not busy +BUSY = 1 # 1 is sent to the microcontroller if user is busy diff --git a/figurines/microcontroller_comms.py b/figurines/microcontroller_comms.py index 35d427d..42f2aa6 100644 --- a/figurines/microcontroller_comms.py +++ b/figurines/microcontroller_comms.py @@ -26,6 +26,7 @@ _LOGGER = logging.getLogger(__name__) logging.getLogger(__name__).setLevel(logging.DEBUG) + def check_presence(port: str, interval: int = READ_INTERVAL): """Checks the presence of a port, checking if it is still open. This is run on a seperate thread. Will also read data from serial port if read_state is set to true @@ -42,7 +43,7 @@ def check_presence(port: str, interval: int = READ_INTERVAL): _LOGGER.error("Status Thread [stopped]") _LOGGER.error("PRC [Stopping serial]") serial_disconnect(serial_port) - + # Reads data from serial port if read_state: serial_read_data(serial_port) @@ -83,6 +84,7 @@ def initialise_serial() -> Serial | None: _LOGGER.debug(e) return None + def ping_api() -> dict | None: """Sends a GET request to the database API and gets data @@ -101,6 +103,7 @@ def ping_api() -> dict | None: _LOGGER.debug(e) return None + def serial_disconnect(serial_port: Serial): """Disconnects the current serial port, flushing the input and output buffers @@ -114,6 +117,7 @@ def serial_disconnect(serial_port: Serial): serial_port.close() _LOGGER.info("Closed serial port.") + def serial_send_data(data: dict): """Sends figurine data to the serial port @@ -137,6 +141,7 @@ def serial_send_data(data: dict): _LOGGER.debug(e) serial_port = None + def serial_read_data(serial_port: Serial): """Reads any data from the serial port and logs it @@ -169,6 +174,7 @@ def convert_response(response: dict) -> bytes: data = 'FIG' + com_string return bytes(data, encoding='utf-8') + def start_thread(port: str): """Starts a thread to then check the ports @@ -186,6 +192,7 @@ def start_thread(port: str): _LOGGER.error("Start() method called multiple times on the same thread object") _LOGGER.debug(e) + def main(): """Main program loop which will constantly attempt to connect to a serial device and when connected will write/read to serial. """ @@ -205,7 +212,6 @@ def main(): _LOGGER.error("Could not find valid serial connection and timed out, try again") continue - while serial_port is not None: try: serial_port.read() @@ -226,6 +232,7 @@ def main(): continue if response is None: + _LOGGER.debug("No response") continue if response != figurine_status: @@ -233,8 +240,5 @@ def main(): serial_send_data(response) - - - if __name__ == "__main__": main()