summaryrefslogtreecommitdiff
path: root/visualizer/visualizer.py
blob: 872dd9d5d8197201f40093742f8220ff03347f30 (plain)
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
#!/usr/bin/python3

import serial
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from collections import OrderedDict

nb_bins = 128
baud_rate = 115200
port = "/dev/ttyACM0"

bin_separator = ' '
val_separator = ':'


def recv(link, nb_val):
	valid = False
	while not valid:
		line = link.readline().rstrip()
		
		try:
			line = line.decode("utf-8")
		except TypeError:
			valid = False
			continue

		raw_bins = line.split(bin_separator)
		valid = len(raw_bins) == nb_bins
				
		if not valid:
			continue
		
		bins = OrderedDict()
		for b in raw_bins:
			values = b.split(val_separator)
			valid = len(values) == 2
			if not valid:
				continue
				
			bins[int(values[0])] = int(values[1])
		
	return bins
	

if __name__ == '__main__':
	
	link = serial.Serial(port, baud_rate)
	
	while 1:
		data = recv(link, nb_bins)
		x, y = list(data.keys()), list(data.values())
		
		plt.cla()
		plt.hist(y, bins=x)
		plt.draw()
		plt.show(block=False)