def main(): maxlen = 500 data_x = RingBuffer(maxlen, datetime.datetime.utcnow(), dtype=datetime.datetime) data_y = RingBuffer(maxlen) y = 100 # initial value fig, ax = plt.subplots() line, = ax.plot(data_x.all[::-1], data_y.all[::-1], linestyle='-', marker='+', color='r', markeredgecolor='b') delta_y = 20 ax.set_ylim([y - delta_y, y + delta_y]) while True: x = datetime.datetime.utcnow() y = y + np.random.uniform(-1, 1) data_x.append(x) data_y.append(y) line.set_xdata(data_x.all[::-1]) xmin, xmax = data_x.min(), data_x.max() if xmax > xmin: ax.set_xlim([xmin, xmax]) line.set_ydata(data_y.all[::-1]) ymin, ymax = data_y.min(), data_y.max() if ymax > ymin: ax.set_ylim([ymin, ymax]) plt.pause(0.001)
def test_min_max(): N = 5 ring = RingBuffer(size_max=N, default_value=-1) ring.append(2) ring.append(1) ring.append(4) ring.append(3) assert ring.min() == 1 assert ring.min(all=True) == -1 assert ring.max() == 4
def main(device, baudrate): logging.basicConfig(level=logging.INFO) sensors00 = SensorsArduino(device=device, baudrate=baudrate, adc_channels_number=2) print("capabilities: %s" % sensors00.capabilities) sensors00.connect() sensors00.ADC[0].calibrate(lambda value: linear_function_with_limit(value, 520.0, 603.0, 0.0, 100.0)) maxlen = 100 data_x = RingBuffer(maxlen, datetime.datetime.utcnow(), dtype=datetime.datetime) data_y = RingBuffer(maxlen) y = 100 # initial value fig, ax = plt.subplots() line, = ax.plot(data_x.all[::-1], data_y.all[::-1], linestyle='-', marker='+', color='r', markeredgecolor='b') #ax.set_ylim([0, 1023]) ax.set_ylim([0, 100]) t_last = datetime.datetime.utcnow() while True: t = datetime.datetime.utcnow() try: if sensors00.update() and sensors00.ADC[0].has_new_data: print(sensors00._bin_msg._data) y = sensors00.ADC[0].value #y = limit(y, 1800.0, 24000.0, 0.0, 100.0) logger.info("%s %s %s" % (t, y, t - t_last)) data_x.append(t) data_y.append(y) line.set_xdata(data_x.all[::-1]) xmin, xmax = data_x.min(), data_x.max() if xmax > xmin: ax.set_xlim([xmin, xmax]) line.set_ydata(data_y.all[::-1]) #ymin, ymax = data_y.min(), data_y.max() #if ymax > ymin: # ax.set_ylim([ymin, ymax]) plt.pause(0.001) except Exception as e: logger.error(traceback.format_exc()) #raise e t_last = t