-
Notifications
You must be signed in to change notification settings - Fork 2
/
glove_logger.py
28 lines (25 loc) · 990 Bytes
/
glove_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from serial import Serial
import time
# Set the options for the serial port
port = "/dev/cu.usbmodem141101"
baudrate = 57600
# Clear the movement log file
with open(f"glove.log", "w") as mvmtfile:
mvmtfile.write("")
# Open the serial port for reading
print(f"Reading from serial port with port={port} and baud={baudrate}")
with Serial(port=port, baudrate=baudrate, timeout=1) as ino_serial:
last_write = time.time();
while True:
if ino_serial.isOpen():
now = str(time.time_ns() // 1_000)
values = ino_serial.readline().decode('utf-8').strip()
vals = [f"{i}\t{v}" for i,v in enumerate(values.split(','), 1)]
with open(f"glove.log", "a") as mvmtfile:
for val in vals:
mvmtfile.write(f"{now}\t{val}\n")
ino_serial.flush()
if time.time() - last_write > 5:
last_write = time.time()
print(".", flush=True, end="")
print("Finished")