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 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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
0
def my_callback(channel):
	print "Callback trigger %d" %channel
	print "Now value of the Pin is %d" %(GPIO.input(GPIO.PI+10))
	print "Click Ctr + C to exit"
Example #11
0
import RaPi.GPIO as GPIO 
import time

#PIN_NUM = 24
#GPIO.setmode(GPIO.BOARD)
#GPIO.setup(PIN_NUM,GPIO.IN,GPIO.PUD_DOWN)

GPIO.setmode(GPIO.RAW)
GPIO.setup(GPIO.PI+10,GPIO.IN,GPIO.PUD_DOWN)

print "The value of Pin %d is %d" %(GPIO.PI+10,GPIO.input(GPIO.PI+10))


def my_callback(channel):
	print "Callback trigger %d" %channel
	print "Now value of the Pin is %d" %(GPIO.input(GPIO.PI+10))
	print "Click Ctr + C to exit"

GPIO.add_event_detect(GPIO.PI+10,GPIO.RISING,callback = my_callback,bouncetime = 300)

try:
    while True:
	time.sleep(0.1)
except KeyboardInterrupt:
    pass

GPIO.cleanup()

Example #12
0
 def setUp(self):
     GPIO.setup(LOOP_IN, GPIO.IN)
     GPIO.setup(LOOP_OUT, GPIO.OUT)
Example #13
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 #14
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 #15
0
 def testHighLowEvent(self):
     with self.assertRaises(ValueError):
         GPIO.add_event_detect(LOOP_IN, GPIO.LOW)
     with self.assertRaises(ValueError):
         GPIO.add_event_detect(LOOP_IN, GPIO.HIGH)
Example #16
0
import RaPi.GPIO as GPIO
import time

channel = 7

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

while True:
    if GPIO.input(channel):
        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()
Example #17
0
#!/usr/bin/env python
import RaPi.GPIO as GPIO
import time

#LED Mode BCM
#PINs = [2,3,4,14,15,17,18,27,22,23,24,10,9,25,11,8,7,0,1,5,6,12,13,19,16,26,20,21]
#[2,3,4,14,18,23,24,10,9,11,8,7,0,1,5,6,12,13,19,16,26,20,21]
#[15,17,27,22,25]
#GPIO.setmode(GPIO.BCM)

#LED Mode BOARD
#PINs = [3,5,7,8,10,11,12,13,15,16,18,19,21,22,23,24,26,27,28,29,31,32,33,35,36,37,38,40]
#[3,5,7,8,12,16,18,19,21,23,24,26,27,28,29,31,32,33,35,36,37,38,40]
#[10,11,13,15,22]
PINs = [10]
GPIO.setmode(GPIO.BOARD)

while True:
	for PIN_NUM in range(len(PINs)):
		try:
			GPIO.setup(PINs[PIN_NUM], GPIO.OUT)
		except:
			print("Failed to setup GPIO %d", PIN_NUM)

		GPIO.output(PINs[PIN_NUM], True)
		time.sleep(0.5)
		GPIO.output(PINs[PIN_NUM], False)
                time.sleep(0.5)
Example #18
0
def makehigh():
	print "\n value_%d = %d\n" %(channel,GPIO.input(channel))
	GPIO.output(PIN_NUM,False)
	print "\n value_%d = %d\n" %(PIN_NUM,GPIO.input(PIN_NUM))
Example #19
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)
Example #20
0
 def makelow():
     GPIO.output(LOOP_OUT, GPIO.LOW)
Example #21
0
 def testBothEventDetected(self):
     GPIO.output(LOOP_OUT, GPIO.LOW)
     GPIO.add_event_detect(LOOP_IN, GPIO.BOTH)
     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)
     self.assertEqual(GPIO.event_detected(LOOP_IN), False)
     GPIO.output(LOOP_OUT, GPIO.LOW)
     time.sleep(0.001)
     self.assertEqual(GPIO.event_detected(LOOP_IN), True)
     GPIO.remove_event_detect(LOOP_IN)
Example #22
0
 def makehigh():
     GPIO.output(LOOP_OUT, GPIO.HIGH)
Example #23
0
#!/usr/bin/env python
import RaPi.GPIO as GPIO
import time
from threading import Timer

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

GPIO.output(PIN_NUM,True)
print "\n value_%d = %d\n" %(PIN_NUM,GPIO.input(PIN_NUM))

GPIO.setup(channel,GPIO.IN,GPIO.PUD_DOWN)
print "\n value_%d = %d\n" %(channel,GPIO.input(channel))


def makehigh():
	print "\n value_%d = %d\n" %(channel,GPIO.input(channel))
	GPIO.output(PIN_NUM,False)
	print "\n value_%d = %d\n" %(PIN_NUM,GPIO.input(PIN_NUM))
	
	

	
GPIO.wait_for_edge(channel, GPIO.RISING)
t = Timer(1,makehigh)
t.start()
Example #24
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 #25
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 #26
0
 def testEventOnOutput(self):
     with self.assertRaises(RuntimeError):
         GPIO.add_event_detect(LOOP_OUT, GPIO.FALLING)
Example #27
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 #28
0
 def tearDown(self):
     GPIO.cleanup()
Example #29
0
#!/usr/bin/env python
import RaPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print "\n value_7 = %d\n" %(GPIO.input(7))	

GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print "\n value_8 = %d\n" %(GPIO.input(8))	

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print "\n value_12 = %d\n" %(GPIO.input(12))	
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print "\n value_12 = %d\n" %(GPIO.input(12))	

#GPIO.cleanup()
Example #30
0
 def setUp(self):
     GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)