SHARE
TWEET

scope-test.py

farry Jul 12th, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as P
  5. import matplotlib.widgets as W
  6. import os,sys,serial,time
  7.  
  8. for d in ("USB0", "USB1", "ACM0", "ACM1", "FAIL"):
  9.   if d == "FAIL": sys.exit("Can not find serial port")
  10.   dev = "/dev/tty" + d
  11.   if os.path.exists(dev): break
  12.  
  13. serial.Serial(dev, 9600, timeout=2).close()     # Bugfix Olimex CH340 chip
  14. ser = serial.Serial(dev, 500000, timeout=2)     # 115200 baud
  15. time.sleep(2)                                   # Arduino starts in ~1.6s
  16.  
  17.  
  18. def analyse(y):
  19.   m, s = 0, 0
  20.   for n in range(len(y)):
  21.     if   m == 0 and y[n] < 2.5: m, s = 1, 0
  22.     elif m == 1 and y[n] > 2.5: m, s = 2, n
  23.     elif m == 2 and y[n] < 2.5: m, s = 3, s
  24.     elif m == 3 and y[n] > 2.5: m, s = 4, n - s
  25.   period = (14.0 / afreq) + 4e-6
  26.   if m == 4:
  27.     freq =  int(1.0 / (period * s))
  28.     ax.set_title("Waveform freq: " + str(freq) + " Hz" )
  29.   return period
  30.  
  31. def showgraph(*args):
  32.   ser.flushInput()                              # flush input just in case
  33.   ser.write(b"w\n")                             # send string + newline
  34.   txt = ser.readline(5000).decode()             # read string + newline
  35.   ytxt=txt.split(',')                           # split into list of strings
  36.   del ytxt[-1]                                  # delete newline
  37.   ax.cla()
  38.   y = [ float(n) * 5 / 1024 for n in ytxt]
  39.   period = analyse(y)
  40.   x = [ n * period * 1000 for n in range(len(y))]
  41.   ax.set_xlabel("Time (ms)")
  42.   ax.set_ylabel("Voltage (V)")
  43.   ax.plot(x,y,lw=2)
  44.   P.draw()
  45.  
  46. fig, ax = P.subplots()
  47. P.subplots_adjust(left=0.2)         # leave gap for buttons
  48. axcolor = 'lightgoldenrodyellow'
  49.  
  50. def butfunc(butname):
  51.   global act
  52.   act = butname
  53.   if act in ["Single", "Repeat"]: timer.start()
  54.  
  55. butlist = ["Stop", "Repeat", "Single"]
  56. but = list(butlist)
  57. for b in range(len(butlist)):
  58.   butshape = P.axes([0.01, 0.1 + b/17.5, 0.12, 0.05])
  59.   but[b] = W.Button(butshape, butlist[b], color=axcolor)
  60.   but[b].on_clicked(lambda event, butname=butlist[b]: butfunc(butname))
  61.  
  62. def afunc(label):
  63.   global afreq
  64.   afreq = int(label) * 1000
  65.   ser.flushInput()
  66.   anumber = str(aname.index(label) + 2)
  67.   ser.write(("a" + anumber + "\n").encode())
  68.   discard = ser.readline(5000)
  69.  
  70. aname = ("4000", "2000", "1000", "500", "250", "125")
  71. ashape = P.axes([0.01, 0.65, 0.12, 0.2], facecolor=axcolor)
  72. ashape.set_title("Clock to\nADC (kHz)")
  73. aradio = W.RadioButtons(ashape, aname, active=2)
  74. aradio.on_clicked(afunc)
  75. afreq = 1000000
  76.  
  77. def fqfunc(label):
  78.   ser.flushInput()
  79.   fqnumber = str(fname.index(label) + 1)
  80.   ser.write(("s" + fqnumber + "\n").encode())
  81.   discard = ser.readline(5000)
  82.  
  83. fname = ("31250", "3906", "976", "488", "244", "122", "30.5")
  84. fshape = P.axes([0.01, 0.32, 0.12, 0.22], facecolor=axcolor)
  85. fshape.set_title("Signal\nout (Hz)")
  86. fradio = W.RadioButtons(fshape, fname, active=4)
  87. fradio.on_clicked(fqfunc)
  88.  
  89. def tfunc():
  90.   if act != "Stop":  showgraph()
  91.   if act == "Repeat": timer.start()
  92.  
  93. act = "Single"
  94. timer = fig.canvas.new_timer(interval=10)
  95. timer.add_callback(tfunc)
  96. timer.single_shot = True
  97. timer.start()
  98. P.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top