Advertisement
Guest User

Untitled

a guest
Jan 30th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import threading
  2. import time
  3.  
  4. import RPi.GPIO as GPIO
  5.  
  6. aSetOfFunctionData =[
  7.     "some data to be sent to USB",
  8.     "some more data to be sent to USB",
  9.     "knock, knock",
  10.  
  11.     ]
  12.  
  13. def getNextFunctionData():
  14.     for functionData in aSetOfFunctionData:
  15.         yield functionData
  16.  
  17. START_PIN = 17
  18. STOP_PIN = 18
  19. NEXT_PIN = 19
  20.  
  21. def setupBoard():
  22.     GPIO.setmode(GPIO.BCM)
  23.     GPIO.setup(START_PIN, GPIO.IN) # START BUTTON
  24.     GPIO.setup(STOP_PIN, GPIO.IN) # STOP BUTTON
  25.     GPIO.setup(NEXT_PIN, GPIO.IN) # NEXT BUTTON
  26.  
  27. def sendFunctionDataToUSB(functionData):
  28.     # USB communication code
  29.     pass
  30.  
  31. class ButtonProcessor(threading.Thread):
  32.     def __init__(self):
  33.         threading.Thread.__init__(self)
  34.         self.stopEvent = threading.Event()
  35.  
  36.     def run(self):
  37.         startTime = 0
  38.         while not self.stopEvent.is_set():
  39.             startPressed = GPIO.input(START_PIN)
  40.             stopPressed = GPIO.input(STOP_PIN)
  41.             nextPressed = GPIO.input(NEXT_PIN)
  42.             if stopPressed:
  43.                 self.stop()
  44.             elif startPressed:
  45.                 startTime = time.time()
  46.                 sendFunctionDataToUSB(getNextFunctionData())
  47.             elif nextPressed:
  48.                 sendFunctionDataToUSB(getNextFunctionData())
  49.  
  50.             if startTime and time.time()-startTime >10:
  51.                 startTime = 0
  52.                 sendFunctionDataToUSB(getNextFunctionData())
  53.  
  54.             self.stopEvent.wait(0.05)
  55.  
  56.     def stop(self):
  57.         self.event.set()
  58.  
  59. setupBoard()
  60. processor = ButtonProcessor()
  61. processor.start()
  62.  
  63. time.sleep(120) # this simulation will self-destruct in 2 minutes.
  64.  
  65. processor.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement