启动了VNC服务,利用远程桌面,在BBB上运行python程序,以控制硬件
__author__ = '[email protected]'
import tkinter
from tkinter import ttk
import Adafruit_BBIO.GPIO as GPIO # for BBB platform
import Adafruit_BBIO.ADC as ADC
import Adafruit_BBIO.PWM as PWM
import time
import threading
class BBB_PWM():
def __init__(self,root,origin,IO_number):
self.IO_number = IO_number
self.label0 = tkinter.Label(root, text = IO_number+':')
self.label0.place(x = origin[0],y = origin[1])
self.label1 = tkinter.Label(root, text = 'cycle:')
self.label1.place(x = origin[0]+50,y = origin[1])
self.label2 = tkinter.Label(root, text = 'freq:')
self.label2.place(x = origin[0]+130,y = origin[1])
self.text0 = tkinter.Text(root,width = 3,height = 1,wrap = tkinter.WORD)
self.text0.place(x = origin[0]+95,y = origin[1])
self.text1 = tkinter.Text(root,width = 4,height = 1,wrap = tkinter.WORD)
self.text1.place(x = origin[0]+170,y = origin[1])
self.button0 = tkinter.Button(root,text = 'start',command = self.start)
self.button0.place(x = origin[0]+210,y = origin[1])
self.button1 = tkinter.Button(root,text = 'stop',command = self.stop)
self.button1.place(x = origin[0]+264,y = origin[1])
#-------------------BBB function start ------------------
def start(self):
value0 = self.text0.get(0.0,tkinter.END)
value1 = self.text1.get(0.0,tkinter.END)
PWM.start(self.IO_number,int(value0),int(value1))
def stop(self):
PWM.stop(self.IO_number)
#-------------------BBB function end ------------------
class PWM_Planner():
def __init__(self,root,origin):
self.label = tkinter.Label(root, text = "PWM_Plan:",bg='#81A9E1')
self.label.place(x = origin[0]+30,y = origin[1]+30)
self.PWM0 = BBB_PWM(root,(origin[0],origin[1]+60),"P9_14")
self.PWM1 = BBB_PWM(root,(origin[0],origin[1]+90),"P9_16")
self.PWM2 = BBB_PWM(root,(origin[0],origin[1]+120),"P9_21")
self.PWM3 = BBB_PWM(root,(origin[0],origin[1]+150),"P9_22")
self.PWM4 = BBB_PWM(root,(origin[0],origin[1]+180),"P9_42")
self.PWM5 = BBB_PWM(root,(origin[0],origin[1]+210),"P8_13")
self.PWM6 = BBB_PWM(root,(origin[0],origin[1]+240),"P8_19")
class BBB_ADC():
def __init__(self,root,origin,IO_number):
self.IO_number = IO_number
self.label = tkinter.Label(root, text = IO_number+'_ADC:')
self.label.place(x = origin[0],y = origin[1])
self.text = tkinter.Text(root,width = 8,height = 1,wrap = tkinter.WORD)
self.text.place(x = origin[0]+90,y = origin[1])
class ADC_Planner():
def __init__(self,root,origin):
self.label = tkinter.Label(root, text = "ADC_Plan:",bg='#81A9E1')
self.label.place(x = origin[0]+20,y = origin[1]+20)
self.button = tkinter.Button(root,text = 'set ',command = self.set)
self.button.place(x = origin[0]+100,y = origin[1]+20)
self.ain0 = BBB_ADC(root,(origin[0],origin[1]+60),"P9_39")
self.ain1 = BBB_ADC(root,(origin[0],origin[1]+90),"P9_40")
self.ain2 = BBB_ADC(root,(origin[0],origin[1]+120),"P9_37")
self.ain3 = BBB_ADC(root,(origin[0],origin[1]+150),"P9_38")
self.ain4 = BBB_ADC(root,(origin[0],origin[1]+180),"P9_33")
self.ain5 = BBB_ADC(root,(origin[0],origin[1]+210),"P9_36")
self.ain6 = BBB_ADC(root,(origin[0],origin[1]+240),"P9_35")
self.read_flg = 0
#-------------------BBB function start ------------------
def set(self):
self.read_flg = 1
ADC.setup();
def read(self):
while 1:
if(self.read_flg):
number0 = ADC.read("P9_39")
number1 = ADC.read("P9_40")
number2 = ADC.read("P9_37")
number3 = ADC.read("P9_38")
number4 = ADC.read("P9_33")
number5 = ADC.read("P9_36")
number6 = ADC.read("P9_35")
self.ain0.text.delete(0.0,tkinter.END)
self.ain1.text.delete(0.0,tkinter.END)
self.ain2.text.delete(0.0,tkinter.END)
self.ain3.text.delete(0.0,tkinter.END)
self.ain4.text.delete(0.0,tkinter.END)
self.ain5.text.delete(0.0,tkinter.END)
self.ain6.text.delete(0.0,tkinter.END)
self.ain0.text.insert(0.0,number0)
self.ain1.text.insert(0.0,number1)
self.ain2.text.insert(0.0,number2)
self.ain3.text.insert(0.0,number3)
self.ain4.text.insert(0.0,number4)
self.ain5.text.insert(0.0,number5)
self.ain6.text.insert(0.0,number6)
time.sleep(1)
#-------------------BBB function end ------------------
class BBB_digital_IO():
def __init__(self,root,origin,IO_number):
self.IO_number = IO_number
self.label = tkinter.Label(root, text = IO_number+':')
self.label.place(x = origin[0],y = origin[1])
self.boxValue = tkinter.StringVar()
self.boxChoice =ttk.Combobox(root,textvariable = self.boxValue,state = 'readonly',width = 6)
self.boxChoice['value'] = ('Input','Output')
self.boxChoice.current()
self.boxChoice.bind('<>',self.Choice) # set for choice function
self.boxChoice.place(x = origin[0]+50,y = origin[1])
self.text = tkinter.Text(root,width = 3,height = 1,wrap = tkinter.WORD, state = tkinter.DISABLED)
self.text.place(x = origin[0]+130,y = origin[1])
self.button = tkinter.Button(root,text = 'read',state = tkinter.DISABLED)
self.button.place(x = origin[0]+170,y = origin[1])
def Choice(self,event):
context = self.boxValue.get()
print(context)
if(context == 'Input'):
self.text.delete(0.0,tkinter.END)
self.text['state'] = tkinter.DISABLED
self.button['text'] = 'read'
self.button['state'] = tkinter.NORMAL
self.button['command'] = self.read
self.set_IO(0)
print('set read done')
else:
self.text['state'] = tkinter.NORMAL
self.text.delete(0.0,tkinter.END)
self.button['text'] = 'write'
self.button['state'] = tkinter.NORMAL
self.button['command'] = self.write
self.set_IO(1)
print('set write done')
def read(self):
self.text['state'] = tkinter.NORMAL
self.text.delete(0.0,tkinter.END)
if(self.input_IO()):
self.text.insert(0.0,'1')
else:
self.text.insert(0.0,'0')
self.text['state'] = tkinter.DISABLED # after write it, disable it
print(self.input_IO())
def write(self):
value = self.text.get(0.0,tkinter.END)
if(value[0] == '1'):
self.output_IO(1)
print("output:" + value[0])
elif(value[0] == '0'):
self.output_IO(0)
print("output:" + value[0])
#-------------------BBB function start ------------------
def set_IO(self,value):
if(value):
GPIO.setup(self.IO_number,GPIO.OUT)
print("set to output")
else:
GPIO.setup(self.IO_number,GPIO.IN)
print("set to input")
def output_IO(self,value):
if(value):
GPIO.output(self.IO_number,GPIO.HIGH)
else:
GPIO.output(self.IO_number,GPIO.LOW)
def input_IO(self):
if GPIO.input(self.IO_number):
return 1
else:
return 0
#-------------------BBB function end ------------------
class IoPlanner():
def __init__(self,root,origin,P8orP9):
self.label0 = tkinter.Label(root, text = P8orP9 +"_IO_Plan:",bg='#81A9E1')
self.label0.place(x = origin[0]+100,y = origin[1]+20)
self.label1 = tkinter.Label(root, text = "IO_Number:",bg='#81A9E1')
self.label1.place(x = origin[0]+10,y = origin[1]+50)
self.text0 = tkinter.Text(root,width = 3,height = 1,wrap = tkinter.WORD)
self.text0.place(x = origin[0]+100,y = origin[1]+50)
self.button0 = tkinter.Button(root,text = 'ADD', command = self.add)
self.button0.place(x = origin[0]+150,y = origin[1]+50)
self.origin = origin
self.lastX = origin[0]+10
self.lastY = origin[1]+90
self.P8orP9 = P8orP9
def add(self):
value = self.text0.get(0.0,tkinter.END)
number_str = "0123456789"
number_P9not = (1,2,3,4,5,6,7,8,9,10,32,33,34,35,36,37,38,39,40,43,44,45,46)
number_P8not = (1,2,3,4,5,6)
if((value[0] in number_str) and (value[1] in number_str)):
number_IO = int(value[0:2])
if (number_IO> 0 and number_IO < 47 ):
if(self.P8orP9 == "P8" and (number_IO not in number_P8not)):
origin = (self.lastX,self.lastY)
self.lastY = self.lastY + 40
BBB_digital_IO(root,origin,self.P8orP9 +'_'+ value[0:2] )
elif(self.P8orP9 == "P9" and (number_IO not in number_P9not)):
origin = (self.lastX,self.lastY)
self.lastY = self.lastY + 40
BBB_digital_IO(root,origin,self.P8orP9 +'_'+ value[0:2] )
else:
print ("input invalid1")
else:
print ("input invalid2")
else:
print ("input invalid3")
class MyTk(tkinter.Tk):
def quit(self):
#StartClock.looping = False # when click the close button, the some class can stop
self.quit()
class GUI(tkinter.Frame):
def __init__(self,root):
frame = tkinter.Frame(root)
frame.pack()
self.P8IO = IoPlanner(root,(0,0),"P8")
self.P9IO = IoPlanner(root,(240,0),"P9")
self.adc = ADC_Planner(root,(500,0))
self.PWM = PWM_Planner(root,(485,260))
if __name__ == '__main__':
root = MyTk()
cvns = tkinter.Canvas(root,width=805,height=600,bg='#4169E1')
cvns.pack()
app = GUI(root)
t = threading.Thread(target=app.adc.read) # set a thread
t.setDaemon(True) # defend this thread
t.start() # thread start
root.title("BBB_hardware")
root.resizable(False,False)
root.mainloop()