#!/usr/bin/python #////////////////////////////////////// # analogInContinuous.py # Read analog data via IIO continuous mode and plots it. #////////////////////////////////////// # From: https://stackoverflow.com/questions/20295646/python-ascii-plots-in-terminal # https://github.com/dkogan/gnuplotlib # https://github.com/dkogan/gnuplotlib/blob/master/guide/guide.org # sudo apt install gnuplot (10 minute to install) # sudo apt install libatlas-base-dev # pip3 install gnuplotlib # This uses X11, so when connecting to the bone from the host use: ssh -X bone # See https://elinux.org/index.php?title=EBC_Exercise_10a_Analog_In#Analog_in_-_Continuous.2C_Change_the_sample_rate # for instructions on changing the sampling rate. Can go up to 200KHz. fd = open(IIODEV, "r") import numpy as np import gnuplotlib as gp import time # import struct IIOPATH='/sys/bus/iio/devices/iio:device0' IIODEV='/dev/iio:device0' LEN = 100 SAMPLERATE=8000 AIN='2' # Setup IIO for Continous reading # Enable AIN try: file1 = open(IIOPATH+'/scan_elements/in_voltage'+AIN+'_en', 'w') file1.write('1') file1.close() except: # carry on if it's already enabled pass # Set buffer length file1 = open(IIOPATH+'/buffer/length', 'w') file1.write(str(2*LEN)) # I think LEN is in 16-bit values, but here we pass bytes file1.close() # Enable continuous file1 = open(IIOPATH+'/buffer/enable', 'w') file1.write('1') file1.close() x = np.linspace(0, 1000*LEN/SAMPLERATE, LEN) # Do a dummy plot to give time of the fonts to load. gp.plot(x, x) print("Waiting for fonts to load") time.sleep(10) print('Hit ^C to stop') fd = open(IIODEV, "r") try: while True: y = np.fromfile(fd, dtype='uint16', count=LEN)*1.8/4096 # print(y) gp.plot(x, y, xlabel = 't (ms)', ylabel = 'volts', _yrange = [0, 2], title = 'analogInContinuous', legend = np.array( ("P9.39", ), ), # ascii=1, # terminal="xterm", # legend = np.array( ("P9.40", "P9.38"), ), # _with = 'lines' ) except KeyboardInterrupt: print("Turning off input.") # Disable continuous file1 = open(IIOPATH+'/buffer/enable', 'w') file1.write('0') file1.close() file1 = open(IIOPATH+'/scan_elements/in_voltage'+AIN+'_en', 'w') file1.write('0') file1.close() # // Bone | Pocket | AIN # // ----- | ------ | --- # // P9_39 | P1_19 | 0 # // P9_40 | P1_21 | 1 # // P9_37 | P1_23 | 2 # // P9_38 | P1_25 | 3 # // P9_33 | P1_27 | 4 # // P9_36 | P2_35 | 5 # // P9_35 | P1_02 | 6