diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eba74f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/Arduino.py b/Arduino.py deleted file mode 100644 index 5bd2e25..0000000 --- a/Arduino.py +++ /dev/null @@ -1,36 +0,0 @@ -import serial #imports pyserial library -import time - -#sends a new drink request -def sendDrink(instructions, exitKey=b'exit\r\n', blockSize=10): - exit = False - try: - arduino = serial.Serial('COM3', 9600) #initialises serial port - time.sleep(2) #waits for arduino boot, can possibly be shorter - arduino.flushInput() - - pumpString = "" - - for pump in instructions: - if pump < blockSize: - pumpString += '0' + str(pump) #makes all digits 2 digits - else: - pumpString += str(pump) #combines pumps to send - arduino.write(str(pumpString).encode()) - - arduino.flushInput() - while exit != True: - try: - command = arduino.readline() - print(command) - except: - print("Keyboard Interrupt") - if command == exitKey: - exit = True - return True - except FileNotFoundError: - print("Serial port not found, may need to manually change com port") - return False - except serial.SerialException: - print("Serial port not found") - return False diff --git a/Arduino_Library.py b/Arduino_Library.py new file mode 100644 index 0000000..2cfbd8d --- /dev/null +++ b/Arduino_Library.py @@ -0,0 +1,35 @@ +import time +from Arduino import Arduino # type: ignore + +async def sendDrink(instructions, exitKey='exit', blockSize=10): + try: + board = Arduino("9600", port="COM3") + pumpString = "" + for pump in instructions: + if pump < blockSize: + pumpString += '0' + str(pump) + else: + pumpString += str(pump) + + board.SoftwareSerial.write(pumpString) + + exit = False + while not exit: + try: + command = board.SoftwareSerial.read() + print(command) + if command == exitKey: + exit = True + except Exception as e: + print(f"Error: {e}") + + board.close() + return True + + except FileNotFoundError: + print("Serial port not found, may need to manually change com port") + return False + except Exception as e: + print(f"Connection error: {e}") + return False + \ No newline at end of file diff --git a/VirtualQueue.py b/VirtualQueue.py index e67a7f9..b4e26e4 100644 --- a/VirtualQueue.py +++ b/VirtualQueue.py @@ -1,12 +1,14 @@ import threading import time -import Arduino from queue import Queue +import Arduino_Library + class ArduinoThread(threading.Thread): drinkCount = 0 def __init__(self, queue): + # Initialize the thread TODO: make it a pool rather then self setting threading.Thread.__init__(self) self.queue = queue self.daemon = True @@ -21,7 +23,7 @@ def run(self): def ServeDrink(self, drink): success = False while success != True: - success = Arduino.sendDrink(drink) + success = Arduino_Library.sendDrink(drink) print("Order " + str(ArduinoThread.drinkCount) + " is finished.") ArduinoThread.drinkCount += 1 self.drinkCount += 1 diff --git a/__pycache__/Arduino.cpython-312.pyc b/__pycache__/Arduino.cpython-312.pyc new file mode 100644 index 0000000..28e8ec1 Binary files /dev/null and b/__pycache__/Arduino.cpython-312.pyc differ