diff options
author | Pacien TRAN-GIRARD | 2015-05-30 20:51:07 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-05-30 20:51:07 +0200 |
commit | e1dad3dc1f70f5e6d360e5516a799481ea29ff2d (patch) | |
tree | ae4145aadea367dc07561b69ac6e5c50deb611d3 /visualizer | |
parent | f26c24f83eba172804efbdf506cf18d19f48b176 (diff) | |
download | museduino-e1dad3dc1f70f5e6d360e5516a799481ea29ff2d.tar.gz |
Import the second version of the spectrum visualization utility
Diffstat (limited to 'visualizer')
-rwxr-xr-x[-rw-r--r--] | visualizer/visualizer.py | 101 |
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 | |
2 | BINS = 128 | ||
3 | |||
4 | 2 | ||
5 | import serial | 3 | import serial |
6 | 4 | import matplotlib | |
7 | s = serial.Serial("/dev/ttyACM0", 115200) | ||
8 | |||
9 | |||
10 | def 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 | |||
30 | import matplotlib.pyplot as plt | 5 | import matplotlib.pyplot as plt |
31 | import numpy as np | 6 | import matplotlib.animation as animation |
32 | 7 | from collections import OrderedDict | |
33 | 8 | ||
34 | fig = plt.figure() | 9 | nb_bins = 128 |
35 | ax = fig.add_subplot(111) | 10 | baud_rate = 115200 |
36 | 11 | port = "/dev/ttyACM0" | |
37 | li, = ax.plot(range(BINS)) | 12 | |
38 | plt.axis([0, BINS, 0, 250]) | 13 | bin_separator = ' ' |
39 | 14 | val_separator = ':' | |
40 | fig.canvas.draw() | 15 | |
41 | plt.show(block=False) | 16 | |
42 | 17 | def 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 | |||
46 | if __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 | ||
44 | while 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 | ||