Example #1
0
 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)
Example #2
0
 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)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
    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)
Example #6
0
#!/usr/bin/env python
import RaPi.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)