2023-10-15 18:02:31 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import mapid
|
2023-10-26 15:33:39 +02:00
|
|
|
from serial.serialutil import SerialException
|
2023-10-15 18:02:31 +02:00
|
|
|
|
|
|
|
|
2023-10-27 00:19:10 +02:00
|
|
|
mapid.load_dotenv()
|
2023-10-29 21:43:06 +01:00
|
|
|
FIFO = os.environ.get("FIFO")
|
2023-10-15 18:02:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global FIFO
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
FIFO = sys.argv[1]
|
|
|
|
|
2023-11-08 19:59:03 +01:00
|
|
|
# Write PID
|
|
|
|
FIFO_BRIDGE_PID_FILE = os.environ.get("FIFO_BRIDGE_PID_FILE")
|
|
|
|
f = open(FIFO_BRIDGE_PID_FILE, "w")
|
|
|
|
f.write(os.getpid())
|
|
|
|
f.close()
|
|
|
|
del f
|
|
|
|
|
|
|
|
# Create MAPIDCP object to communicate with the Arduino
|
2023-10-15 18:02:31 +02:00
|
|
|
m = mapid.MAPIDCP()
|
|
|
|
|
2023-11-08 19:59:03 +01:00
|
|
|
# Create FIFO
|
2023-10-15 18:02:31 +02:00
|
|
|
os.mkfifo(FIFO)
|
|
|
|
time.sleep(0.2)
|
|
|
|
print("Created FIFO " + FIFO, file=sys.stderr)
|
2023-10-26 15:33:39 +02:00
|
|
|
|
2023-10-15 18:02:31 +02:00
|
|
|
f = open(FIFO, "r")
|
|
|
|
print("Opened FIFO for reading", file=sys.stderr)
|
2023-11-08 19:59:03 +01:00
|
|
|
|
|
|
|
# Show greeting on screen
|
2023-10-15 18:02:31 +02:00
|
|
|
m.cls()
|
|
|
|
m.line(1)
|
|
|
|
m.echo("fifo_bridge.py")
|
|
|
|
m.line(2)
|
|
|
|
m.echo(FIFO)
|
|
|
|
|
2023-11-08 19:59:03 +01:00
|
|
|
# Main loop: read commands from FIFO and execute it
|
2023-10-15 18:02:31 +02:00
|
|
|
keep_going = True
|
|
|
|
while keep_going:
|
|
|
|
try:
|
|
|
|
line = f.readline().strip("\n")
|
|
|
|
m._cmd(line)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
keep_going = False
|
|
|
|
print()
|
2023-10-26 15:33:39 +02:00
|
|
|
except SerialException:
|
|
|
|
keep_going = False
|
|
|
|
print("Caught a SerialException. Exiting now...", file=sys.stderr)
|
2023-10-15 18:02:31 +02:00
|
|
|
|
2023-11-08 19:59:03 +01:00
|
|
|
# Remove FIFO and PID files
|
2023-10-15 18:02:31 +02:00
|
|
|
os.remove(FIFO)
|
2023-11-08 19:59:03 +01:00
|
|
|
os.remove(FIFO_BRIDGE_PID_FILE)
|
2023-10-15 18:02:31 +02:00
|
|
|
print("Removed FIFO.", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|