From fa2cc9b5d389c349ff1688bb17a53601d5d8c368 Mon Sep 17 00:00:00 2001 From: Malte Bublitz Date: Sun, 15 Oct 2023 18:01:09 +0200 Subject: [PATCH] mapid.py library --- mapid-count.py | 35 ++++++++++++++++++++++---------- mapid.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 3 files changed, 79 insertions(+), 12 deletions(-) mode change 100644 => 100755 mapid-count.py create mode 100644 mapid.py diff --git a/mapid-count.py b/mapid-count.py old mode 100644 new mode 100755 index c233aea..ccefd4c --- a/mapid-count.py +++ b/mapid-count.py @@ -4,17 +4,16 @@ import time import sys import serial - -SERIAL_PORT = "/dev/cu.usbserial-A700e0gN" -SERIAL_SPEED = 9600 -#SERIAL_SPEED = 19200 -#SERIAL_SPEED = 115200 +import mapid -arduino = serial.Serial(port=SERIAL_PORT, baudrate=SERIAL_SPEED, timeout=.1) +# print(mapid.get_serial_dev()); sys.exit(1) +SERIAL_DEV = mapid.get_serial_dev() +SERIAL_SPEED = mapid.get_serial_speed() def write_read(x): + arduino = serial.Serial(port=SERIAL_DEV, baudrate=SERIAL_SPEED, timeout=.1) arduino.write(bytes(x, 'utf-8')) time.sleep(0.05) data = arduino.readline() @@ -22,6 +21,7 @@ def write_read(x): def countdown(start: int): + arduino = serial.Serial(port=SERIAL_DEV, baudrate=SERIAL_SPEED, timeout=.1) for i in range(start, -1, -1): arduino.write(bytes("CLS\r\n", 'utf8')) arduino.write(bytes("LINE 1\r\n", 'utf8')) @@ -35,16 +35,29 @@ def countdown(start: int): arduino.write(bytes("ECHO BOOM!!!\r\n", 'utf8')) +def countdown2(start: int): + m = mapid.MAPID_CP() + m.cls() + for i in range(start, -1, -1): + m.cls() + m.line(1) + m.echo("Countdown") + m.line(2) + print("\t" + str(i)) + m.echo(str(i)) + time.sleep(1) + m.cls() + m.line(2) + m.echo("BOOM!!!") + + def main(): while True: num = int(input("Enter a number (-1=exit): ")) if num < 0: sys.exit(0) - #arduino.write(bytes("CLS\r\n", 'utf8')) - #arduino.write(bytes("LINE 1\r\n", 'utf8')) - #arduino.write(bytes("ECHO Countdown\r\n", 'utf8')) - # arduino.write(bytes("ECHO C " + str(num) + "\r\n", 'utf8')) - countdown(num) + # countdown(num) + countdown2(num) if __name__ == "__main__": diff --git a/mapid.py b/mapid.py new file mode 100644 index 0000000..058c4e3 --- /dev/null +++ b/mapid.py @@ -0,0 +1,53 @@ +import os +import time +import dotenv # +import serial + + +DOTENV_LOADED = False + +def load_dotenv(): + basedir = os.path.abspath(os.path.dirname(__file__)) + dotenv.load_dotenv(os.path.join(basedir, ".env")) + global DOTENV_LOADED + DOTENV_LOADED = True + + +def get_serial_dev(): + global DOTENV_LOADED + if not DOTENV_LOADED: + load_dotenv() + return os.environ.get("SERIAL_DEV") + + +def get_serial_speed(): + global DOTENV_LOADED + if not DOTENV_LOADED: + load_dotenv() + return os.environ.get("SERIAL_SPEED") + + +class MAPIDCP: + arduino = None + + def __init__(self): + self.arduino = serial.Serial(port=get_serial_dev(), baudrate=get_serial_speed(), timeout=.1) + + def _cmd(self, cmd: str): + self.arduino.write(bytes(cmd + "\r\n", "utf8")) + time.sleep(0.1) + + def cls(self): + self._cmd("CLS") + + def led(self, state: bool): + if state: + self._cmd("ON") + else: + self._cmd("OFF") + + def line(self, line_no: int): + self._cmd("LINE " + str(line_no)) + + def echo(self, text: str): + self._cmd("ECHO " + text) diff --git a/requirements.txt b/requirements.txt index 624744f..7dabe6c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ # requirements.txt -serial +pyserial>=3.5 +python-dotenv>=0.5