我正在设置一个脚本,以便在循环中以随机顺序触发433MHz设备。我必须对脚本进行排序,但是我希望有两个按钮,一个可以启动脚本,一个可以在出现任何问题时中断它。
我对python还是很陌生,希望您能提供任何帮助。
from time import sleep
import random
from rpi_rf import RFDevice
rfdevice = RFDevice(17)
for c in range(0,15):
#Randomly Choose 1st Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose 2nd Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose Interval Time for Reload
wait_for_next = random.uniform(7,9)
sleep(wait_for_next)
列出的脚本可以同时触发这两种设备30次,但是最后一个框没有键盘或鼠标,因此我希望使用一个按钮来启动脚本,而如果有任何按钮,请使用一个按钮来中断脚本问题出现了。
谢谢任何关注此问题的人,我期待任何提出的建议。
!!!编辑!!!
这是我更新的脚本
import RPi.GPIO as GPIO
import time
import os
import subprocess
from time import sleep
import random
from rpi_rf import RFDevice
rfdevice = RFDevice(17)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(23)
if input_state == False:
print('Execute')
for c in range(0,1):
#Randomly Choose Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose 2nd Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
time.sleep(0.2)
该脚本在按下按钮时执行,现在我需要在另一个按钮(GPIO 24)上中止该脚本。
!!!编辑2 !!!
我想我已经完成了,如果按住按钮,下面的脚本会起作用
import RPi.GPIO as GPIO
import time
import os
import subprocess
from time import sleep
import random
from rpi_rf import RFDevice
rfdevice = RFDevice(17)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(23)
if input_state == False:
print('Execute')
for c in range(0,1):
#Randomly Choose Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
sleep(wait_for_next)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose 2nd Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
sleep(wait_for_next)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
print('Complete')
time.sleep(0.2)
非常感谢@furas的时间和支持,非常感谢!