diff options
Diffstat (limited to 'test_fw/plot.py')
| -rw-r--r-- | test_fw/plot.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/test_fw/plot.py b/test_fw/plot.py new file mode 100644 index 0000000..53f1bc2 --- /dev/null +++ b/test_fw/plot.py @@ -0,0 +1,98 @@ +from datetime import datetime +import matplotlib.pyplot as plt +import matplotlib.animation as animation +import serial +import numpy as np +import math + + +SER = serial.Serial('COM14') + +CHANNELS = [ + 'ax', + 'ay', + 'az', + 'gx', + 'gy', + 'gz', + 'temp', + 'hum', + 'pres', + 'gas', + 'lux', +] +CHANNEL_IDX = {name: i for (i, name) in enumerate(CHANNELS)} + +SAMPLE_COUNT = 500 + + +def main(): + fig = plt.figure() + + # subfigs = fig.subfigures(3, math.ceil(len(CHANNELS) / 3)) + + elem_data = {} + + + for chan in CHANNELS: + idx = CHANNEL_IDX[chan] + # subfig = subfigs[idx % 3][idx // 3] + + ax = fig.add_subplot(3, math.ceil(len(CHANNELS) / 3), idx + 1) + ax.set_title(chan) + + xs = np.arange(SAMPLE_COUNT), + ys = np.zeros(SAMPLE_COUNT), + + line = ax.plot(xs, ys)[0] + + elem_data[chan] = { + 'ys': ys, + # 'fig': subfig, + 'ax': ax, + 'line': line + } + + elem_data['temp']['ax'].set_ylim([15, 40]) + elem_data['hum']['ax'].set_ylim([0, 100]) + elem_data['pres']['ax'].set_ylim([0, 100000]) + + def animate(i): + now = datetime.now() + + if not SER.in_waiting: + return [] + + line = SER.readline().decode('utf-8') + + for elem in line.split(','): + elem = elem.strip() + + if len(elem) == 0: + continue + + split = elem.split(':') + + if len(split) != 2: + continue + + k = split[0].strip() + v = float(split[1].strip()) + + if k in elem_data: + dat = elem_data[k] + + dat['ys'] = np.roll(dat['ys'], -1) + dat['ys'][-1] = v + dat['line'].set_ydata(dat['ys']) + + yield from (item['line'] for item in elem_data.values()) + + + ani = animation.FuncAnimation(fig, animate, interval=50, blit=True) + plt.show() + + + +if __name__ == '__main__': + main() |
