Esempio n. 1
0
    def test_alreadyinuse(self):
        """Test 'already in use' warning"""
        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(False)
        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')
        with warnings.catch_warnings(record=True) as w:
            GPIO.setup(LED_PIN, GPIO.OUT)  # generate 'already in use' warning
            self.assertEqual(len(w), 0)  # should be no warnings
        with open('/sys/class/gpio/unexport', 'wb') as f:
            f.write(str(LED_PIN_BCM).encode())
        GPIO.cleanup()

        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(True)
        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')
        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()
Esempio n. 2
0
 def runTest(self):
     GPIO.setmode(GPIO.BOARD)
     GPIO.setup(LED_PIN, GPIO.OUT)
     pwm = GPIO.PWM(LED_PIN, 50)
     with self.assertRaises(RuntimeError):
         pwm2 = GPIO.PWM(LED_PIN, 49)
     GPIO.cleanup()
Esempio n. 3
0
 def test_cleanall(self):
     GPIO.setup(LOOP_OUT, GPIO.OUT)
     GPIO.setup(LED_PIN, GPIO.OUT)
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
     GPIO.cleanup()
     GPIO.setmode(GPIO.BOARD)
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN)
Esempio n. 4
0
    def runTest(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(LED_PIN_BCM, GPIO.IN)
        self.assertEqual(GPIO.gpio_function(LED_PIN_BCM), GPIO.IN)
        GPIO.setup(LED_PIN_BCM, GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LED_PIN_BCM), GPIO.OUT)
        GPIO.cleanup()

        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(LED_PIN, GPIO.IN)
        self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN)
        GPIO.setup(LED_PIN, GPIO.OUT)
        self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
Esempio n. 5
0
 def test_cleanone(self):
     GPIO.setup(LOOP_OUT, GPIO.OUT)
     GPIO.setup(LED_PIN, GPIO.OUT)
     GPIO.setup(SWITCH_PIN, GPIO.IN)
     GPIO.setup(LOOP_IN, GPIO.IN)
     GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING)
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     time.sleep(0.2)  # wait for udev to set permissions
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
     self.assertEqual(GPIO.gpio_function(SWITCH_PIN), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LOOP_IN), GPIO.IN)
     self.assertTrue(
         os.path.exists('/sys/class/gpio/gpio%s' % SWITCH_PIN_BCM))
     self.assertTrue(os.path.exists('/sys/class/gpio/gpio%s' % LOOP_IN_BCM))
     GPIO.cleanup(SWITCH_PIN)
     time.sleep(0.2)  # wait for udev to set permissions
     self.assertFalse(
         os.path.exists('/sys/class/gpio/gpio%s' % SWITCH_PIN_BCM))
     self.assertTrue(os.path.exists('/sys/class/gpio/gpio%s' % LOOP_IN_BCM))
     GPIO.cleanup(LOOP_IN)
     time.sleep(0.2)  # wait for udev to set permissions
     self.assertFalse(
         os.path.exists('/sys/class/gpio/gpio%s' % SWITCH_PIN_BCM))
     self.assertFalse(os.path.exists('/sys/class/gpio/gpio%s' %
                                     LOOP_IN_BCM))
     GPIO.cleanup(LOOP_OUT)
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
     GPIO.cleanup(LED_PIN)
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN)
Esempio n. 6
0
 def runTest(self):
     GPIO.setmode(GPIO.BOARD)
     GPIO.setup(LED_PIN, GPIO.OUT)
     pwm = GPIO.PWM(LED_PIN, 50)
     pwm.start(100)
     print("\nPWM tests")
     response = input('Is the LED on (y/n) ? ').upper()
     self.assertEqual(response, 'Y')
     pwm.start(0)
     response = input('Is the LED off (y/n) ? ').upper()
     self.assertEqual(response, 'Y')
     print("LED Brighten/fade test...")
     for i in range(0, 3):
         for x in range(0, 101, 5):
             pwm.ChangeDutyCycle(x)
             time.sleep(0.1)
         for x in range(100, -1, -5):
             pwm.ChangeDutyCycle(x)
             time.sleep(0.1)
     pwm.stop()
     response = input('Did it work (y/n) ? ').upper()
     self.assertEqual(response, 'Y')
     GPIO.cleanup()
Esempio n. 7
0
    def test_cleanupwarning(self):
        """Test initial GPIO.cleanup() produces warning"""
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(SWITCH_PIN, GPIO.IN)
        with warnings.catch_warnings(record=True) as w:
            GPIO.cleanup()
            self.assertEqual(len(w), 0)  # no warnings
            GPIO.cleanup()
            self.assertEqual(len(w), 0)  # no warnings

        GPIO.setwarnings(True)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(SWITCH_PIN, GPIO.IN)
        with warnings.catch_warnings(record=True) as w:
            GPIO.cleanup()
            self.assertEqual(len(w), 0)  # no warnings
            GPIO.cleanup()
            self.assertEqual(w[0].category, RuntimeWarning)  # a warning
Esempio n. 8
0
 def test_cleanlist(self):
     GPIO.setup(LOOP_OUT, GPIO.OUT)
     GPIO.setup(LED_PIN, GPIO.OUT)
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
     GPIO.cleanup([LOOP_OUT])
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT)
     GPIO.cleanup([LED_PIN])
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN)
     GPIO.setup(LOOP_OUT, GPIO.OUT)
     GPIO.setup(LED_PIN, GPIO.OUT)
     GPIO.cleanup([LOOP_OUT, LED_PIN])
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN)
Esempio n. 9
0
 def tearDown(self):
     GPIO.cleanup()
Esempio n. 10
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)
Esempio n. 11
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


def issue_94(cycles):
    # led flickers.  Bug = LED stays off at around cycle 400
    pwm = GPIO.PWM(LED_PIN, 1)
    for i in xrange(cycles):
        print(i)
        pwm.ChangeFrequency(25)
        pwm.start(50)
        time.sleep(1)
        pwm.stop()


if __name__ == '__main__':
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(LED_PIN, GPIO.OUT)
    try:
        #        issue_94(1000)
        issue_154()
    finally:
        GPIO.cleanup()