Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import time
- import RPi.GPIO as GPIO
- aSetOfFunctionData =[
- "some data to be sent to USB",
- "some more data to be sent to USB",
- "knock, knock",
- ]
- def getNextFunctionData():
- for functionData in aSetOfFunctionData:
- yield functionData
- START_PIN = 17
- STOP_PIN = 18
- NEXT_PIN = 19
- def setupBoard():
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(START_PIN, GPIO.IN) # START BUTTON
- GPIO.setup(STOP_PIN, GPIO.IN) # STOP BUTTON
- GPIO.setup(NEXT_PIN, GPIO.IN) # NEXT BUTTON
- def sendFunctionDataToUSB(functionData):
- # USB communication code
- pass
- class ButtonProcessor(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.stopEvent = threading.Event()
- def run(self):
- startTime = 0
- while not self.stopEvent.is_set():
- startPressed = GPIO.input(START_PIN)
- stopPressed = GPIO.input(STOP_PIN)
- nextPressed = GPIO.input(NEXT_PIN)
- if stopPressed:
- self.stop()
- elif startPressed:
- startTime = time.time()
- sendFunctionDataToUSB(getNextFunctionData())
- elif nextPressed:
- sendFunctionDataToUSB(getNextFunctionData())
- if startTime and time.time()-startTime >10:
- startTime = 0
- sendFunctionDataToUSB(getNextFunctionData())
- self.stopEvent.wait(0.05)
- def stop(self):
- self.event.set()
- setupBoard()
- processor = ButtonProcessor()
- processor.start()
- time.sleep(120) # this simulation will self-destruct in 2 minutes.
- processor.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement