From e1dad3dc1f70f5e6d360e5516a799481ea29ff2d Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 30 May 2015 20:51:07 +0200 Subject: Import the second version of the spectrum visualization utility --- visualizer/visualizer.py | 101 +++++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 47 deletions(-) mode change 100644 => 100755 visualizer/visualizer.py (limited to 'visualizer') diff --git a/visualizer/visualizer.py b/visualizer/visualizer.py old mode 100644 new mode 100755 index ed65384..872dd9d --- a/visualizer/visualizer.py +++ b/visualizer/visualizer.py @@ -1,53 +1,60 @@ - -BINS = 128 - +#!/usr/bin/python3 import serial - -s = serial.Serial("/dev/ttyACM0", 115200) - - -def acquire_data(): - - nb_val = 0 - while nb_val != BINS+1: - l = s.readline() - d = l.decode("utf8") - v = d.split(' ') - nb_val = len(v) - - n = [0] * BINS - for i in range(BINS): - try: - n[i] = int(v[i]) - - except ValueError: - n[i] = 0 - - return n - - +import matplotlib import matplotlib.pyplot as plt -import numpy as np - - -fig = plt.figure() -ax = fig.add_subplot(111) - -li, = ax.plot(range(BINS)) -plt.axis([0, BINS, 0, 250]) - -fig.canvas.draw() -plt.show(block=False) - +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) -while True: - try: - y = acquire_data() - print(y) - li.set_ydata(y) - fig.canvas.draw() - except KeyboardInterrupt: - break -- cgit v1.2.3