Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import numpy as np
- import matplotlib.pyplot as P
- import matplotlib.widgets as W
- import os,sys,serial,time
- for d in ("USB0", "USB1", "ACM0", "ACM1", "FAIL"):
- if d == "FAIL": sys.exit("Can not find serial port")
- dev = "/dev/tty" + d
- if os.path.exists(dev): break
- serial.Serial(dev, 9600, timeout=2).close() # Bugfix Olimex CH340 chip
- ser = serial.Serial(dev, 500000, timeout=2) # 115200 baud
- time.sleep(2) # Arduino starts in ~1.6s
- def analyse(y):
- m, s = 0, 0
- for n in range(len(y)):
- if m == 0 and y[n] < 2.5: m, s = 1, 0
- elif m == 1 and y[n] > 2.5: m, s = 2, n
- elif m == 2 and y[n] < 2.5: m, s = 3, s
- elif m == 3 and y[n] > 2.5: m, s = 4, n - s
- period = (14.0 / afreq) + 4e-6
- if m == 4:
- freq = int(1.0 / (period * s))
- ax.set_title("Waveform freq: " + str(freq) + " Hz" )
- return period
- def showgraph(*args):
- ser.flushInput() # flush input just in case
- ser.write(b"w\n") # send string + newline
- txt = ser.readline(5000).decode() # read string + newline
- ytxt=txt.split(',') # split into list of strings
- del ytxt[-1] # delete newline
- ax.cla()
- y = [ float(n) * 5 / 1024 for n in ytxt]
- period = analyse(y)
- x = [ n * period * 1000 for n in range(len(y))]
- ax.set_xlabel("Time (ms)")
- ax.set_ylabel("Voltage (V)")
- ax.plot(x,y,lw=2)
- P.draw()
- fig, ax = P.subplots()
- P.subplots_adjust(left=0.2) # leave gap for buttons
- axcolor = 'lightgoldenrodyellow'
- def butfunc(butname):
- global act
- act = butname
- if act in ["Single", "Repeat"]: timer.start()
- butlist = ["Stop", "Repeat", "Single"]
- but = list(butlist)
- for b in range(len(butlist)):
- butshape = P.axes([0.01, 0.1 + b/17.5, 0.12, 0.05])
- but[b] = W.Button(butshape, butlist[b], color=axcolor)
- but[b].on_clicked(lambda event, butname=butlist[b]: butfunc(butname))
- def afunc(label):
- global afreq
- afreq = int(label) * 1000
- ser.flushInput()
- anumber = str(aname.index(label) + 2)
- ser.write(("a" + anumber + "\n").encode())
- discard = ser.readline(5000)
- aname = ("4000", "2000", "1000", "500", "250", "125")
- ashape = P.axes([0.01, 0.65, 0.12, 0.2], facecolor=axcolor)
- ashape.set_title("Clock to\nADC (kHz)")
- aradio = W.RadioButtons(ashape, aname, active=2)
- aradio.on_clicked(afunc)
- afreq = 1000000
- def fqfunc(label):
- ser.flushInput()
- fqnumber = str(fname.index(label) + 1)
- ser.write(("s" + fqnumber + "\n").encode())
- discard = ser.readline(5000)
- fname = ("31250", "3906", "976", "488", "244", "122", "30.5")
- fshape = P.axes([0.01, 0.32, 0.12, 0.22], facecolor=axcolor)
- fshape.set_title("Signal\nout (Hz)")
- fradio = W.RadioButtons(fshape, fname, active=4)
- fradio.on_clicked(fqfunc)
- def tfunc():
- if act != "Stop": showgraph()
- if act == "Repeat": timer.start()
- act = "Single"
- timer = fig.canvas.new_timer(interval=10)
- timer.add_callback(tfunc)
- timer.single_shot = True
- timer.start()
- P.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement