- Create a script called log_adc.py:
#!/usr/bin/python3 #log_adc.c import time import datetime import data_adc as dataDevice DEBUG=True FILE=True VAL0=0;VAL1=1;VAL2=2;VAL3=3 #Set data order FORMATHEADER = "t%st%st%st%st%s" FORMATBODY = "%dt%st%ft%ft%ft%f" if(FILE):f = open("data.log",'w') def timestamp(): ts = time.time() return datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H:%M:%S') def main(): counter=0 myData = dataDevice.device() myDataNames = myData.getName() header = (FORMATHEADER%("Time", myDataNames[VAL0],myDataNames[VAL1], myDataNames[VAL2],myDataNames[VAL3])) if(DEBUG):print (header) if(FILE):f.write(header+"n") while(1): data = myData.getNew() counter+=1 body = (FORMATBODY%(counter,timestamp(), data[0],data[1],data[2],data[3])) if(DEBUG):print (body) if(FILE):f.write(body+"n") time.sleep(0.1) try: main() finally: f.close() #End
- Create a second script called log_graph.py, as follows:
#!/usr/bin/python3 #log_graph.py import numpy as np import matplotlib.pyplot as plt filename = "data.log" OFFSET=2 with open(filename) as f: header = f.readline().split('t') data = np.genfromtxt(filename, delimiter='t', skip_header=1, names=['sample', 'date', 'DATA0', 'DATA1', 'DATA2', 'DATA3']) fig = plt.figure(1) ax1 = fig.add_subplot(211)#numrows, numcols, fignum ax2 = fig.add_subplot(212) ax1.plot(data['sample'],data['DATA0'],'r', label=header[OFFSET+0]) ax2.plot(data['sample'],data['DATA1'],'b', label=header[OFFSET+1]) ax1.set_title("ADC Samples") ax1.set_xlabel('Samples') ax1.set_ylabel('Reading') ax2.set_xlabel('Samples') ax2.set_ylabel('Reading') leg1 = ax1.legend() leg2 = ax2.legend() plt.show() #End