summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-05-30 20:51:07 +0200
committerPacien TRAN-GIRARD2015-05-30 20:51:07 +0200
commite1dad3dc1f70f5e6d360e5516a799481ea29ff2d (patch)
treeae4145aadea367dc07561b69ac6e5c50deb611d3
parentf26c24f83eba172804efbdf506cf18d19f48b176 (diff)
downloadmuseduino-e1dad3dc1f70f5e6d360e5516a799481ea29ff2d.tar.gz
Import the second version of the spectrum visualization utility
-rwxr-xr-x[-rw-r--r--]visualizer/visualizer.py101
1 files changed, 54 insertions, 47 deletions
diff --git a/visualizer/visualizer.py b/visualizer/visualizer.py
index ed65384..872dd9d 100644..100755
--- a/visualizer/visualizer.py
+++ b/visualizer/visualizer.py
@@ -1,53 +1,60 @@
1 1#!/usr/bin/python3
2BINS = 128
3
4 2
5import serial 3import serial
6 4import matplotlib
7s = serial.Serial("/dev/ttyACM0", 115200)
8
9
10def acquire_data():
11
12 nb_val = 0
13 while nb_val != BINS+1:
14 l = s.readline()
15 d = l.decode("utf8")
16 v = d.split(' ')
17 nb_val = len(v)
18
19 n = [0] * BINS
20 for i in range(BINS):
21 try:
22 n[i] = int(v[i])
23
24 except ValueError:
25 n[i] = 0
26
27 return n
28
29
30import matplotlib.pyplot as plt 5import matplotlib.pyplot as plt
31import numpy as np 6import matplotlib.animation as animation
32 7from collections import OrderedDict
33 8
34fig = plt.figure() 9nb_bins = 128
35ax = fig.add_subplot(111) 10baud_rate = 115200
36 11port = "/dev/ttyACM0"
37li, = ax.plot(range(BINS)) 12
38plt.axis([0, BINS, 0, 250]) 13bin_separator = ' '
39 14val_separator = ':'
40fig.canvas.draw() 15
41plt.show(block=False) 16
42 17def recv(link, nb_val):
18 valid = False
19 while not valid:
20 line = link.readline().rstrip()
21
22 try:
23 line = line.decode("utf-8")
24 except TypeError:
25 valid = False
26 continue
27
28 raw_bins = line.split(bin_separator)
29 valid = len(raw_bins) == nb_bins
30
31 if not valid:
32 continue
33
34 bins = OrderedDict()
35 for b in raw_bins:
36 values = b.split(val_separator)
37 valid = len(values) == 2
38 if not valid:
39 continue
40
41 bins[int(values[0])] = int(values[1])
42
43 return bins
44
45
46if __name__ == '__main__':
47
48 link = serial.Serial(port, baud_rate)
49
50 while 1:
51 data = recv(link, nb_bins)
52 x, y = list(data.keys()), list(data.values())
53
54 plt.cla()
55 plt.hist(y, bins=x)
56 plt.draw()
57 plt.show(block=False)
43 58
44while True:
45 try:
46 y = acquire_data()
47 print(y)
48 li.set_ydata(y)
49 fig.canvas.draw()
50 59
51 except KeyboardInterrupt:
52 break
53 60