在我的项目中,我正在使用树莓派(带有python 3)。我试图通过按钮引入一个中断,我可以停止或暂停主要功能并处理另一个功能,完成该功能后应释放主要功能。我正在使用这个:
GPIO.add_event_detect(channel, GPIO.FALLING, callback=my_callback, bouncetime=200)
但是我面临一个问题,当按下按钮时,“ main”和“ my_callback”将同时执行!
然后我尝试在线程上工作,将main和my_callback设为线程,但是结果相同。
任何人都可以帮助我。 这是我的代码。
import os
import MySQLdb
import RPi.GPIO as GPIO
from time import sleep
from RPLCD.gpio import CharLCD
GPIO.setmode(GPIO.BOARD)
GreenRight = 3
RedRight = 5
GreenUp = 11
RedUp = 13
PushButton = 15
YellowRight = 40
GPIO.setwarnings(False)
GPIO.setup(GreenRight, GPIO.OUT)
GPIO.setup(RedRight, GPIO.OUT)
GPIO.setup(GreenUp, GPIO.OUT)
GPIO.setup(RedUp, GPIO.OUT)
GPIO.setup(YellowRight, GPIO.OUT)
GPIO.setup(PushButton, GPIO.IN, pull_up_down= GPIO.PUD_UP)
db = MySQLdb.connect(host = "***.***.*.***", port=3306, user = "****", passwd = "***", db = "mydb")
cur = db.cursor()
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],numbering_mode=GPIO.BOARD)
lcd.clear()
def TurnOnGreenHorizental(time):
sql1 = "UPDATE `test` SET `Green Light For Horizental`='ON' WHERE `Street`='StreetRight'"
cur.execute(sql1)
sql2 = "UPDATE `test` SET `Red Light For Horizental`='OFF' WHERE `Street`='StreetLeft'"
cur.execute(sql1)
i=time
GPIO.output(GreenRight, True)
GPIO.output(RedRight, False)
GPIO.output(GreenUp, False)
GPIO.output(RedUp, True)
GPIO.output(YellowRight, False)
while i > 0:
lcd.clear()
lcd.write_string("HS Green Lights ON for "+str(i)+ " s")
sleep(1)
i=i-1
lcd.clear()
GPIO.output(GreenRight, False)
GPIO.output(RedRight, False)
GPIO.output(RedUp, False)
GPIO.output(GreenUp, False)
GPIO.output(YellowRight, False)
sleep(4)
def TurnOnVertical(time):
sql1 = "UPDATE `test` SET `Green Light For Horizental`='OFF' WHERE `Street`='StreetRight'"
cur.execute(sql1)
sql2 = "UPDATE `test` SET `Red Light For Horizental`='ON' WHERE `Street`='StreetLeft'"
cur.execute(sql1)
i = time
GPIO.output(GreenRight, True)
GPIO.output(RedRight, False)
GPIO.output(GreenUp, False)
GPIO.output(RedUp, True)
GPIO.output(YellowRight, False)
while i > 0:
lcd.clear()
lcd.write_string("HS Green Lights ON for "+str(i)+ " s")
sleep(1)
i=i-1
lcd.clear()
GPIO.output(GreenRight, False)
GPIO.output(RedRight, False)
GPIO.output(RedUp, False)
GPIO.output(GreenUp, False)
GPIO.output(YellowRight, False)
sleep(4)
def my_callback(channel):
c=12
print("button is pressed" )
GPIO.output(YellowRight, True)
GPIO.output(GreenUp,False)
GPIO.output(RedUp,False)
GPIO.output(RedRight,False)
GPIO.output(GreenRight,False)
while c >0:
lcd.clear()
lcd.write_string("Passenger light is ON for "+str(c)+ " s")
sleep(1)
c=c-1
QUERY= "SELECT `Action` FROM `test`"
cur.execute(QUERY)
myresult = cur.fetchall()
def Array():
array= []
for x in myresult:
y = str(x[0])
array.append(y)
return array
Action = Array()
def main():
while True:
if ((Action[0]=='1' and Action[1]=='0') or (Action[0]=='0' and Action[1]=='1')):
QUERY= "SELECT `Time` FROM `test` WHERE `Street`='StreetRight' "
cur.execute(QUERY)
myresult = cur.fetchall()
t=Array()
time=t[0]
TurnOnGreenHorizental(int(time))
elif ((Action[2]=='1' and Action[3]=='0') or (Action[2]=='0' and Action[3]=='1')):
QUERY= "SELECT `Time` FROM `test` WHERE `Street`='StreetUp' "
cur.execute(QUERY)
myresult = cur.fetchall()
t=Array()
time=t[0]
print(time)
TurnOnGreenVertical(int(time))
db.commit()
GPIO.add_event_detect(PushButton, GPIO.FALLING,callback=my_callback,bouncetime=12000)
main()