-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
73 lines (59 loc) · 1.84 KB
/
record.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""Reads data from the serial port and writes them to a file.
"""
import argparse
import csv
import errno
import os
import time
from collections import deque
import sensors
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file', nargs='?', default=None,
help="Name of the file to save")
args = parser.parse_args()
filename = args.file
if filename is None:
i = 1
while os.path.exists("data/data%d.csv" % i):
i += 1
filename = "data/data%d.csv" % i
# Create directory if it doesn't exist
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
# Start reading from serial port
s = sensors.Sensors()
activity = 0
print("Waiting for signal...")
while s.read() is None:
pass
print("")
while True:
prev_activity = activity
activity = input("Select activity to record or Q to finish [current: %d]: " % activity)\
or activity
if activity in ('q', 'Q'):
break
try:
activity = int(activity)
except ValueError:
print("Invalid option.")
activity = prev_activity
continue
print("\n ACTIVITY %d:" % (activity))
data = None
while data is None:
data = s.read()
save_data = list(data.clf_data())
with open(filename, "a", newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow([activity] + save_data)
print(" Saved %s " % save_data, end='\n\n')
print("Finished.")
if __name__ == "__main__":
main()