Skip to content

Commit

Permalink
Revert revert revert
Browse files Browse the repository at this point in the history
  • Loading branch information
lucashicks1 committed Oct 25, 2023
1 parent d6cf229 commit 54c08e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions db-handler/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 9 additions & 5 deletions figurines/microcontroller_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
"""
Expand All @@ -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()
Expand All @@ -226,15 +232,13 @@ def main():
continue

if response is None:
_LOGGER.debug("No response")
continue

if response != figurine_status:
figurine_status = response
serial_send_data(response)





if __name__ == "__main__":
main()

0 comments on commit 54c08e4

Please sign in to comment.