Example #1
0
    def testShellCmdWithWaitForEdge(self):
        self.finished = False

        def shellcmd():
            for i in range(50):
                os.system('sleep 0')
                subprocess.call('sleep 0', shell=True)
            self.finished = True

        def makehigh():
            GPIO.output(LOOP_OUT, GPIO.HIGH)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t1 = Timer(0.1, shellcmd)
        t2 = Timer(0.5, makehigh)
        t1.start()
        t2.start()
        starttime = time.time()
        channel = GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout=1000)
        endtime = time.time()
        self.assertGreater(endtime - starttime, 0.5)
        self.assertLess(endtime - starttime, 0.6)
        self.assertEqual(channel, LOOP_IN)

        # make sure tasks in this test have finished before continuing
        while not self.finished:
            time.sleep(0.1)
Example #2
0
 def test_loopback(self):
     """Test output loops back to another input"""
     GPIO.setup(LOOP_IN, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
     GPIO.setup(LOOP_OUT, GPIO.OUT, initial=GPIO.LOW)
     self.assertEqual(GPIO.input(LOOP_IN), GPIO.LOW)
     GPIO.output(LOOP_OUT, GPIO.HIGH)
     self.assertEqual(GPIO.input(LOOP_IN), GPIO.HIGH)
Example #3
0
 def test_outputread(self):
     """Test that an output() can be input()"""
     GPIO.setup(LED_PIN, GPIO.OUT)
     GPIO.output(LED_PIN, GPIO.HIGH)
     self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH)
     GPIO.output(LED_PIN, GPIO.LOW)
     self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
def issue_154():
    # fails with led off at around 400
    count = 0
    pinRef = GPIO.PWM(LED_PIN, 50)  # create new PWM instance
    while True:
        pinRef.start(10)  # update PWM value
        time.sleep(0.05)
        pinRef.stop()
        GPIO.output(LED_PIN, 0)
        time.sleep(0.05)
        count = count + 1
        print count
Example #8
0
    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(0.1, makehigh)
        t2 = Timer(0.15, makelow)
        t.start()
        t2.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)
        GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
Example #9
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.01)
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        time.sleep(0.01)
        self.assertEqual(self.run_cb, True)
        GPIO.remove_event_detect(LOOP_IN)
Example #10
0
    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)
Example #11
0
    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()
            starttime = time.time()
            channel = GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING, timeout=200)
            endtime = time.time()
            self.assertLess(endtime - starttime, 0.12)
            self.assertEqual(channel, LOOP_IN)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            count += 1
            if time.time() - timestart > 5 or count > 150:
                break
Example #12
0
    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)
Example #13
0
 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)
Example #14
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.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)
Example #15
0
    def runTest(self):
        # Test mode not set (BOARD or BCM) exception
        with self.assertRaises(RuntimeError) as e:
            GPIO.setup(LED_PIN, GPIO.OUT)
        self.assertEqual(
            str(e.exception),
            'Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)'
        )

        # Test trying to change mode after it has been set
        GPIO.setmode(GPIO.BCM)
        with self.assertRaises(ValueError) as e:
            GPIO.setmode(GPIO.BOARD)
        GPIO.setup(LED_PIN_BCM, GPIO.IN)
        GPIO.cleanup()

        # Test setting an invalid mode
        with self.assertRaises(ValueError):
            GPIO.setmode(666)

        # Test getmode()
        self.assertEqual(GPIO.getmode(), None)
        GPIO.setmode(GPIO.BCM)
        self.assertEqual(GPIO.getmode(), GPIO.BCM)
        GPIO.setup(LED_PIN_BCM, GPIO.IN)
        GPIO.cleanup()
        GPIO.setmode(GPIO.BOARD)
        self.assertEqual(GPIO.getmode(), GPIO.BOARD)

        # Test not set as OUTPUT message
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(RuntimeError) as e:
            GPIO.output(LED_PIN, GPIO.HIGH)
        self.assertEqual(str(e.exception),
                         'The GPIO channel has not been set up as an OUTPUT')

        # Test setup(..., pull_up_down=GPIO.HIGH) raises exception
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(ValueError):
            GPIO.setup(LED_PIN, GPIO.IN, pull_up_down=GPIO.HIGH)

        # Test not valid on a raspi exception
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(ValueError) as e:
            GPIO.setup(GND_PIN, GPIO.OUT)
        self.assertEqual(str(e.exception),
                         'The channel sent is invalid on a Raspberry Pi')

        # Test 'already in use' warning
        GPIO.setmode(GPIO.BOARD)
        with open('/sys/class/gpio/export', 'wb') as f:
            f.write(str(LED_PIN_BCM).encode())
        time.sleep(0.2)  # wait for udev to set permissions
        with open('/sys/class/gpio/gpio%s/direction' % LED_PIN_BCM, 'wb') as f:
            f.write(b'out')
        time.sleep(0.2)
        with warnings.catch_warnings(record=True) as w:
            GPIO.setup(LED_PIN, GPIO.OUT)  # generate 'already in use' warning
            self.assertEqual(w[0].category, RuntimeWarning)
        with open('/sys/class/gpio/unexport', 'wb') as f:
            f.write(str(LED_PIN_BCM).encode())
        GPIO.cleanup()

        # test initial value of high reads back as high
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.HIGH)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH)
        GPIO.cleanup()

        # test initial value of low reads back as low
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW)
        GPIO.cleanup()

        # test pull up/down works
        GPIO.setmode(GPIO.BOARD)
        for i in range(1000):
            GPIO.setup(NC_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            time.sleep(0.001)
            self.assertEqual(GPIO.input(NC_PIN), GPIO.LOW)
            GPIO.setup(NC_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
            time.sleep(0.001)
            self.assertEqual(GPIO.input(NC_PIN), GPIO.HIGH)
        GPIO.cleanup()

        # test setup of a list of channels
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup([LED_PIN, LOOP_OUT], GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT)
        GPIO.cleanup()
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(ValueError) as e:
            GPIO.setup([LED_PIN, GND_PIN], GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
        self.assertEqual(str(e.exception),
                         'The channel sent is invalid on a Raspberry Pi')
        GPIO.cleanup()

        # test setup of a tuple of channels
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup((LED_PIN, LOOP_OUT), GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT)
        GPIO.cleanup()

        # test warning when using pull up/down on i2c channels
        GPIO.setmode(GPIO.BOARD)
        if GPIO.RPI_INFO['P1_REVISION'] == 0:  # compute module
            pass  # test not vailid
        else:  # revision 1, 2 or A+/B+
            with warnings.catch_warnings(record=True) as w:
                GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
                self.assertEqual(w[0].category, RuntimeWarning)
            with warnings.catch_warnings(record=True) as w:
                GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
                self.assertEqual(w[0].category, RuntimeWarning)
            GPIO.cleanup()

        # test non integer channel
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(ValueError):
            GPIO.setup('d', GPIO.OUT)
        with self.assertRaises(ValueError):
            GPIO.setup(('d', LED_PIN), GPIO.OUT)

        # test setting pull_up_down on an output
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(ValueError):
            GPIO.setup(LOOP_OUT, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)

        # test setting initial on an input
        GPIO.setmode(GPIO.BOARD)
        with self.assertRaises(ValueError):
            GPIO.setup(LOOP_IN, GPIO.IN, initial=GPIO.LOW)
Example #16
0
 def test_output_on_input(self):
     """Test output() can not be done on input"""
     GPIO.setup(SWITCH_PIN, GPIO.IN)
     with self.assertRaises(RuntimeError):
         GPIO.output(SWITCH_PIN, GPIO.LOW)
Example #17
0
    def test_output_list(self):
        """Test output() using lists"""
        GPIO.setup(LOOP_OUT, GPIO.OUT)
        GPIO.setup(LED_PIN, GPIO.OUT)

        GPIO.output([LOOP_OUT, LED_PIN], GPIO.HIGH)
        self.assertEqual(GPIO.input(LOOP_OUT), GPIO.HIGH)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH)

        GPIO.output((LOOP_OUT, LED_PIN), GPIO.LOW)
        self.assertEqual(GPIO.input(LOOP_OUT), GPIO.LOW)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW)

        GPIO.output([LOOP_OUT, LED_PIN], [GPIO.HIGH, GPIO.LOW])
        self.assertEqual(GPIO.input(LOOP_OUT), GPIO.HIGH)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW)

        GPIO.output((LOOP_OUT, LED_PIN), (GPIO.LOW, GPIO.HIGH))
        self.assertEqual(GPIO.input(LOOP_OUT), GPIO.LOW)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH)

        with self.assertRaises(RuntimeError):
            GPIO.output([LOOP_OUT, LED_PIN], [0, 0, 0])

        with self.assertRaises(RuntimeError):
            GPIO.output([LOOP_OUT, LED_PIN], (0, ))

        with self.assertRaises(RuntimeError):
            GPIO.output(LOOP_OUT, (0, 0))

        with self.assertRaises(ValueError):
            GPIO.output([LOOP_OUT, 'x'], (0, 0))

        with self.assertRaises(ValueError):
            GPIO.output([LOOP_OUT, LED_PIN], (0, 'x'))

        with self.assertRaises(ValueError):
            GPIO.output([LOOP_OUT, GND_PIN], (0, 0))

        with self.assertRaises(RuntimeError):
            GPIO.output([LOOP_OUT, LOOP_IN], (0, 0))
Example #18
0
 def makehigh():
     GPIO.output(LOOP_OUT, GPIO.HIGH)
Example #19
0
 def makelow():
     GPIO.output(LOOP_OUT, GPIO.LOW)
Example #20
0
 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