-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_data.py
79 lines (64 loc) · 1.58 KB
/
visualize_data.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
74
75
76
77
78
79
import string
import sys
import os
import serial
import pickle
import heartpy as hp
from time import sleep
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import threading
arr = []
# def printit():
# threading.Timer(5.0, printit).start()
# global arr
# with open('data.txt', 'w') as outfile:
# outfile.write("\n".join(arr))
#
# printit()
# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
ser = serial.Serial()
ser.port = 'COM8'
ser.baudrate = 9600
# ser.port = sys.argv[1]
# ser.baudrate = sys.argv[2]
ser.bytesize = serial.EIGHTBITS
ser.partiy = serial.PARITY_NONE
ser.timeout = 2
ser.open()
count = 0
def get_read():
a = ''
while(1):
read_str = ser.read().decode("utf-8")
if(read_str == '\r'):
filter_str = filter(str.isdigit, a)
str_converted = "".join(filter_str)
return int(str_converted)
elif (len(read_str) == 1):
a = (str(a) + str(read_str))
def animate(i, xs, ys):
global count
read = get_read()
arr.append(read)
count = count + 1
# Add x and y to lists
xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
ys.append(read)
# Limit x and y lists to 20 items
xs = xs[-50:]
ys = ys[-50:]
# Draw x and y lists
ax.clear()
ax.plot(xs, ys)
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('Heart Monitor')
plt.ylabel('Heart sensor data')
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1)
plt.show()