mapid.py library

This commit is contained in:
Malte Bublitz 2023-10-15 18:01:09 +02:00
parent 187e017622
commit fa2cc9b5d3
Signed by: malte70
GPG key ID: 605DA5C729F9C184
3 changed files with 79 additions and 12 deletions

35
mapid-count.py Normal file → Executable file
View file

@ -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__":

53
mapid.py Normal file
View file

@ -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)

View file

@ -1,4 +1,5 @@
# requirements.txt
serial
pyserial>=3.5
python-dotenv>=0.5