def testInvalidBouncetime(self): with self.assertRaises(ValueError): GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=-1) with self.assertRaises(ValueError): GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=-1) GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=123) with self.assertRaises(RuntimeError): GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=321) GPIO.remove_event_detect(LOOP_IN)
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.01) self.assertEqual(GPIO.event_detected(LOOP_IN), False) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) self.assertEqual(GPIO.event_detected(LOOP_IN), True) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.01) self.assertEqual(GPIO.event_detected(LOOP_IN), False) GPIO.remove_event_detect(LOOP_IN)
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 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.01) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) self.assertEqual(self.run_cb, True) GPIO.remove_event_detect(LOOP_IN)
def testShellCmdWithEventCallback(self): self.run_cb = False def cb(channel): self.run_cb = True GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb) time.sleep(0.01) for i in range(50): os.system('sleep 0') subprocess.call('sleep 0', shell=True) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) GPIO.remove_event_detect(LOOP_IN) self.assertEqual(self.run_cb, True)
def testWaitForEdgeWithCallback(self): def cb(): raise Exception("Callback should not be called") def makehigh(): GPIO.output(LOOP_OUT, GPIO.HIGH) GPIO.output(LOOP_OUT, GPIO.LOW) t = Timer(0.1, makehigh) GPIO.add_event_detect(LOOP_IN, GPIO.RISING) t.start() GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.add_event_callback(LOOP_IN, callback=cb) with self.assertRaises(RuntimeError): # conflicting edge exception GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) 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.01) for i in range(2048): GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) GPIO.remove_event_detect(LOOP_IN) self.assertEqual(self.callback_count, 2048) # 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.01) for i in range(2048): GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) GPIO.remove_event_detect(LOOP_IN) self.assertEqual(self.callback_count, 2048) # 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.01) for i in range(2048): GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.001) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.001) GPIO.remove_event_detect(LOOP_IN) self.assertEqual(self.callback_count, 4096)
def testAlreadyAdded(self): GPIO.add_event_detect(LOOP_IN, GPIO.RISING) with self.assertRaises(RuntimeError): GPIO.add_event_detect(LOOP_IN, GPIO.RISING) GPIO.remove_event_detect(LOOP_IN)