def test_switchbounce(self): self.switchcount = 0 print "\nSwitch bounce test. Press switch at least 10 times and count..." GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING, callback=self.cb, bouncetime=200) while self.switchcount < 10: time.sleep(1) GPIO.remove_event_detect(SWITCH_PIN)
def test_event_detected(self): self.switchcount = 0 print "\nGPIO.event_detected() switch bounce test. Press switch at least 10 times and count..." GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING, bouncetime=200) while self.switchcount < 10: if GPIO.event_detected(SWITCH_PIN): self.switchcount += 1 print 'Button press',self.switchcount GPIO.remove_event_detect(SWITCH_PIN)
def testRisingEventDetected(self): GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.add_event_detect(LOOP_IN, GPIO.RISING) time.sleep(0.001) self.assertEqual(GPIO.event_detected(LOOP_IN), False) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) self.assertEqual(GPIO.event_detected(LOOP_IN), True) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) self.assertEqual(GPIO.event_detected(LOOP_IN), False) GPIO.remove_event_detect(LOOP_IN)
def testEventDetect(pin, trigger): switchCnt = 0 GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) GPIO.add_event_detect(pin, trigger) while switchCnt < 2: if GPIO.event_detected(pin): switchCnt += 1 print 'The event has been detected' print "\n value_%d = %d\n" %(pin,GPIO.input(pin)) GPIO.remove_event_detect(pin)
def testExceptionInCallback(self): self.run_cb = False def cb(channel): with self.assertRaises(ZeroDivisionError): self.run_cb = True a = 1/0 GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) self.assertEqual(self.run_cb, True) GPIO.remove_event_detect(LOOP_IN)
def testAddEventCallback(self): def cb(channel): self.callback_count += 1 # falling test self.callback_count = 0 GPIO.output(LOOP_OUT, GPIO.HIGH) GPIO.add_event_detect(LOOP_IN, GPIO.FALLING) GPIO.add_event_callback(LOOP_IN, cb) time.sleep(0.001) for i in range(5): GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) self.assertEqual(self.callback_count, 5) GPIO.remove_event_detect(LOOP_IN) # rising test self.callback_count = 0 GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb) time.sleep(0.001) for i in range(5): GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) self.assertEqual(self.callback_count, 5) GPIO.remove_event_detect(LOOP_IN) # both test self.callback_count = 0 GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.add_event_detect(LOOP_IN, GPIO.BOTH, callback=cb) time.sleep(0.001) for i in range(5): GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) self.assertEqual(self.callback_count, 10) GPIO.remove_event_detect(LOOP_IN)
import LMK.GPIO as GPIO import time #PIN_NUM = 24 #GPIO.setmode(GPIO.BOARD) #GPIO.setup(PIN_NUM,GPIO.IN,GPIO.PUD_DOWN) GPIO.setmode(GPIO.RAW) GPIO.setup(GPIO.PI+10,GPIO.IN,GPIO.PUD_DOWN) print "The value of Pin %d is %d" %(GPIO.PI+10,GPIO.input(GPIO.PI+10)) def my_callback(channel): print "Callback trigger %d" %channel print "Now value of the Pin is %d" %(GPIO.input(GPIO.PI+10)) print "Click Ctr + C to exit" GPIO.add_event_detect(GPIO.PI+10,GPIO.RISING,callback = my_callback,bouncetime = 300) try: while True: time.sleep(0.1) except KeyboardInterrupt: pass GPIO.cleanup()
'''P8 P10 P12 P13 P15 P16 P22 P19 P23''' #The following pins support the external event detected on the 40Pin Header of the BananaPro '''P7 P8 P10 P11 P13 P15 P16 P18 P19 P21 P22 P23 P24 P26''' testPinOnGt = 8 #For LeMaker Guitar testPinOnBP = 18 #For BananaPro GPIO.setmode(GPIO.BOARD) GPIO.setup(testPinOnGt, GPIO.IN, GPIO.PUD_UP) def testCallback(channel): print "running callback function", channel GPIO.add_event_detect(testPinOnGt, GPIO.RISING, callback=testCallback, bouncetime=300) try: while True: time.sleep(1) except KeyboardInterrupt: pass GPIO.remove_event_detect(testPinOnGt) GPIO.cleanup()
#!/usr/bin/env python import LMK.GPIO as GPIO import time from threading import Timer SWITCH_PIN = 18 GPIO.setmode(GPIO.BOARD) GPIO.setup(SWITCH_PIN,GPIO.IN,GPIO.PUD_UP) print "\n value_%d = %d\n" %(SWITCH_PIN,GPIO.input(SWITCH_PIN)) GPIO.add_event_detect(SWITCH_PIN, GPIO.RISING,bouncetime=200) # add rising edge detection on a channel switchcount = 0 while switchcount < 2: if GPIO.event_detected(SWITCH_PIN): switchcount += 1 print 'Button pressed',switchcount print "\n value_%d = %d\n" %(SWITCH_PIN,GPIO.input(SWITCH_PIN)) GPIO.remove_event_detect(SWITCH_PIN)
else: print("I2C PCF8591 should read from channel [0 | 1 | 2 | 3]") if DA and Addons["PCF8591"]: out = int(DA[0]) IOboard.writePCF8591(out) outV = 3.3*out/255 print("I2C PCF8591: analog output: {0} voltage.".format(outV)) sendCmd("sensor-update writePCF8591 %.2f" % (outV)) if Sound: soundPin = int(Sound[0]) try: GPIO.setup(soundPin, GPIO.IN, GPIO.PUD_UP) print("Sound detect on GPIO %d is %d" %(soundPin, GPIO.input(soundPin))) GPIO.add_event_detect(soundPin,GPIO.RISING,callback = soundDetect,bouncetime = 300) except: print("Failed to setup the Sound sensor") if Touch: touchPin = int(Touch[0]) try: GPIO.setup(touchPin, GPIO.IN, GPIO.PUD_DOWN) print("Touch detect on GPIO %d is %d" %(touchPin, GPIO.input(touchPin))) Addons["TouchSensor"] = True except: print("Failed to setup the Touch sensor") if Tilt: tiltPin = int(Tilt[0])
def testEventOnOutput(self): with self.assertRaises(RuntimeError): GPIO.add_event_detect(LOOP_OUT, GPIO.FALLING)
def testHighLowEvent(self): with self.assertRaises(ValueError): GPIO.add_event_detect(LOOP_IN, GPIO.LOW) with self.assertRaises(ValueError): GPIO.add_event_detect(LOOP_IN, GPIO.HIGH)
import LMK.GPIO as GPIO import time #The following pins support the external event detected on the 40Pin Header of the LeMake Guitar '''P8 P10 P12 P13 P15 P16 P22 P19 P23''' #The following pins support the external event detected on the 40Pin Header of the BananaPro '''P7 P8 P10 P11 P13 P15 P16 P18 P19 P21 P22 P23 P24 P26''' testPinOnGt = 8 #For LeMaker Guitar testPinOnBP = 18 #For BananaPro GPIO.setmode(GPIO.BOARD) GPIO.setup(testPinOnGt, GPIO.IN, GPIO.PUD_UP) def testCallback(channel): print "running callback function", channel GPIO.add_event_detect(testPinOnGt, GPIO.RISING, callback=testCallback, bouncetime=300) try: while True: time.sleep(1) except KeyboardInterrupt: pass GPIO.remove_event_detect(testPinOnGt) GPIO.cleanup()
PIN_NUM = 24 GPIO.setmode(GPIO.BCM) #GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN_NUM,GPIO.IN,GPIO.PUD_UP) print "The value of Pin %d is %d" %(PIN_NUM,GPIO.input(PIN_NUM)) def my_callback(channel): print "The value of Pin %d is %d" %(PIN_NUM,GPIO.input(PIN_NUM)) print "Callback trigger %d" %channel print "Now value of the Pin is %d" %(GPIO.input(PIN_NUM)) print "Click Ctr + C to exit" GPIO.add_event_detect(PIN_NUM,GPIO.RISING,callback = my_callback,bouncetime = 300) print "The value of Pin %d is %d" %(PIN_NUM,GPIO.input(PIN_NUM)) try: while True: time.sleep(0.1) except KeyboardInterrupt: pass GPIO.cleanup()