def testWaitForEventSwitchbounce(self): self.finished = False def bounce(): GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.01) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.2) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) GPIO.output(LOOP_OUT, GPIO.LOW) time.sleep(0.01) GPIO.output(LOOP_OUT, GPIO.HIGH) time.sleep(0.01) GPIO.output(LOOP_OUT, GPIO.LOW) self.finished = True GPIO.output(LOOP_OUT, GPIO.LOW) t1 = Timer(0.1, bounce) t1.start() starttime = time.time() GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=100) GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=100) finishtime = time.time() self.assertGreater(finishtime - starttime, 0.2) while not self.finished: time.sleep(0.1)
def testWaitForFalling(self): def makelow(): GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.output(LOOP_OUT, GPIO.HIGH) t = Timer(0.1, makelow) t.start() GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
def testWaitForRising(self): def makehigh(): GPIO.output(LOOP_OUT, GPIO.HIGH) GPIO.output(LOOP_OUT, GPIO.LOW) t = Timer(0.1, makehigh) t.start() GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)
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 testAlternateWaitForEdge(self): def makehigh(): GPIO.output(LOOP_OUT, GPIO.HIGH) def makelow(): GPIO.output(LOOP_OUT, GPIO.LOW) GPIO.output(LOOP_OUT, GPIO.LOW) t = Timer(2.0, makehigh) t2 = Timer(2.0, makelow) t.start() t2.start() GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
def testWaitForEdgeInLoop(self): def makelow(): GPIO.output(LOOP_OUT, GPIO.LOW) count = 0 timestart = time.time() GPIO.output(LOOP_OUT, GPIO.HIGH) while True: t = Timer(0.1, makelow) t.start() GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING) GPIO.output(LOOP_OUT, GPIO.HIGH) count += 1 if time.time() - timestart > 5 or count > 150: break
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)
timer = TimeUntilDisplayOff call(['xhost', '+']) os.environ["DISPLAY"] = ":0.0" try: while True: if GPIO.input(MotionSensor): timer = TimeUntilDisplayOff print("Setting timer to " + str(timer) + "s.") if timer > 0: timer -= 1 print("Timer: " + str(timer) + "s") else: print("Timer is 0 -> turning display off.") call(['xrandr', '--output', 'HDMI-1', '--off']) GPIO.wait_for_edge(MotionSensor, GPIO.RISING) print("Motion detected -> turning display on") call( ['xrandr', '--output', 'HDMI-1', '--auto', '--rotate', 'left']) timer = TimeUntilDisplayOff sleep(1) except KeyboardInterrupt: GPIO.cleanup()