问题描述
def main():
# Import Libraries
import RPi.GPIO as GPIO
from time import sleep
import sys
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate',150) # Speed percent
engine.setProperty('volume',0.9) # Volume 0-1
# Configure GPIO of Rpi
GPIO.cleanup()
GPIO.setwarnings(False) # Ignore warning for Now
GPIO.setmode(GPIO.BCM) # Use GPIO pin numbering
button1 = 25 # For WhatsApp
button2 = 17 # For Careem
# Set up GPIO for Whatsapp
GPIO.setup(button1,GPIO.IN,pull_up_down = GPIO.PUD_DOWN) # Set GPIO 25 (pin 22) to be an input pin for WhatsApp
# Set up GPIO for Careem
GPIO.setup(button2,pull_up_down = GPIO.PUD_DOWN) # Set GPIO 17 (pin 11) to be an input pin for UBER
# Call_back function for WhatsApp
def x1(button1):
sleep(0.005) # edge debounce of 5mSec
# only deal with valid edges
if GPIO.input(button1) == 1:
WhatsApp(lat_o,long_o) # ---------> Function which contains engine.say() commands
GPIO.remove_event_detect(button1)
GPIO.remove_event_detect(button2)
GPIO.cleanup([button1])
GPIO.cleanup([button2])
sys.exit("Stopping due to WhatsApp Interrupt!")
return
# Call_back function for Careem
def x2(button2):
sleep(0.005) # edge debounce of 5mSec
# only deal with valid edges
if GPIO.input(button2) == 1:
careem() # ---------> Function which contains engine.say() commands
GPIO.remove_event_detect(button1)
GPIO.remove_event_detect(button2)
GPIO.cleanup([button1])
GPIO.cleanup([button2])
sys.exit("Stopping due to Careem Interrupt!")
return
# Setting up Interrupt for WhatsApp
GPIO.add_event_detect(button1,GPIO.RISING,callback = x1,bouncetime = 5)
# Setting up Interrupt for Careem
GPIO.add_event_detect(button2,callback = x2,bouncetime = 5)
# Voice Recognition
lat_d,long_d = VR_GEO() # ----> Function which contains engine.say() commands
while True:
# GOOGLE_DIRECTIONS_API
directions() # --------> Function which contains engine.say() commands
pass
return None
在对上述代码进行一些研究和检查后,我发现 GPIO.add_event_detect 在与 main() 线程不同的线程中运行,因此我想要一种方法来完成/终止主线程,当 button1 或 button2 为按下并按计划调用相应的回调函数。
因为main()里面的VR_GEO()函数有一些engine.say()命令,call_back函数里面的WhatsApp()和Careem()函数也有一些engine.say()命令,所以当button1被按下时,由于 WhatsApp() 正在执行时的 engine.runAndWait() 命令,VR_GEO() 的 engine.say() 命令首先完成它们的执行,这导致我的蓝牙耳机出现一些未知错误,它断开连接并使我的 RaspBerry Pi极慢。
所以,我想要一种有效的方法来使用标志(我猜)从另一个线程(即正在运行回调的线程)中杀死包含 VR_GEO() 的 main() 线程,以便我的中断可以按计划执行。
请帮忙!
另外,VR_GEO() 的代码行数太多,无法贴在这里。但我相信我们这里不需要它,问题只是杀死 main() 线程。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)