Raspberry_Pi_Education_Manual

# start thread running self.start() def pressed (self): if self._pressed: # clear the pressed flag now we have detected it self._pressed = False return True else : return False def run (self): previous = None while 1: # read gpio channel current = GPIO.input(self.channel) time.sleep(0.01) # wait 10 ms # detect change from 1 to 0 (a button press) if current == False and previous == True : self._pressed = True # wait for flag to be cleared while self._pressed: time.sleep(0.05) # wait 50 ms previous = current def onButtonPress (): print ( 'Button has been pressed!' ) # create a button thread for a button on pin 11 button = Button(11) while True : # ask for a name and say hello name = input ( 'Enter a name (or Q to quit): ' ) if name.upper() == ( 'Q' ): break print ( 'Hello' , name) # check if button has been pressed if button.pressed(): onButtonPress()

Notes:

Human-computer interfacing

133

Made with FlippingBook flipbook maker