Example #1
0
    def test_alreadyinuse(self):
        """Test 'already in use' warning"""
        GPIO.setwarnings(False)
        with open('/sys/class/gpio/export','wb') as f:
            f.write(str(LED_PIN_BCM).encode())
        with open('/sys/class/gpio/gpio%s/direction'%LED_PIN_BCM,'wb') as f:
            f.write(b'out')
        with open('/sys/class/gpio/gpio%s/value'%LED_PIN_BCM,'wb') as f:
            f.write(b'1')
        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.setwarnings(True)
        with open('/sys/class/gpio/export','wb') as f:
            f.write(str(LED_PIN_BCM).encode())
        with open('/sys/class/gpio/gpio%s/direction'%LED_PIN_BCM,'wb') as f:
            f.write(b'out')
        with open('/sys/class/gpio/gpio%s/value'%LED_PIN_BCM,'wb') as f:
            f.write(b'1')
        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()
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)
     GPIO.cleanup()
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)
     GPIO.cleanup()
Example #4
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()
     self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN)
     self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN)
Example #5
0
 def runTest(self):
     GPIO.setup(LED_PIN, GPIO.OUT)
     pwm = GPIO.PWM(LED_PIN, 50)
     pwm.start(100)
     print "\nPWM tests"
     response = raw_input('Is the LED on (y/n) ? ').upper()
     self.assertEqual(response,'Y')
     pwm.start(0)
     response = raw_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 = raw_input('Did it work (y/n) ? ').upper()
     self.assertEqual(response,'Y')
     GPIO.cleanup()
Example #6
0
    def test_cleanupwarning(self):
        """Test initial GPIO.cleanup() produces warning"""
        GPIO.setwarnings(False)
        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.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
Example #7
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)')

        GPIO.setmode(GPIO.BOARD)

        # Test not set as OUTPUT message
        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')

        GPIO.setup(LED_PIN, GPIO.IN)

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

        # Test 'already in use' warning
        GPIO.cleanup()
        with open('/sys/class/gpio/export','wb') as f:
            f.write(str(LED_PIN_BCM).encode())
        with open('/sys/class/gpio/gpio%s/direction'%LED_PIN_BCM,'wb') as f:
            f.write(b'out')
        with open('/sys/class/gpio/gpio%s/value'%LED_PIN_BCM,'wb') as f:
            f.write(b'1')
        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.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.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
        self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW)
        GPIO.cleanup()
Example #8
0
import RaPi.GPIO as GPIO
import time

PIN_NUM = 12
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN_NUM,GPIO.OUT)

frequency = 50
p = GPIO.PWM(PIN_NUM,frequency)
p.start(0)

try:
    while True:
        for dutyCycle in range(0,100,5):
            p.ChangeDutyCycle(dutyCycle)
            time.sleep(0.1)
        for dutyCycle in range(100,0,-5):
            p.ChangeDutyCycle(dutyCycle)
            time.sleep(0.1)
except KeyboardInterrupt:
    pass

p.stop()
GPIO.cleanup()
Example #9
0
import RaPi.GPIO as GPIO
import time

#channel = 15

GPIO.setmode(GPIO.RAW)
GPIO.setup(GPIO.PI+17,GPIO.IN,GPIO.PUD_DOWN)
print " Now input is Low\n Our task is performed when it becomes High"

while True:
    if GPIO.input(GPIO.PI+17):
        print "Input was High,begin to perform"
	print "Count Down"
        for i in range(7,0,-1):
            print "%d" %i
            time.sleep(1)
        print "Performed!"
	exit()

GPIO.cleanup(GPIO.PI+17)
Example #10
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)
     GPIO.cleanup()
Example #11
0
 def tearDown(self):
     GPIO.cleanup()