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()
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()
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()
#!/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()
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()
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)
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))
#!/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()
#!/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)
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"
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()
#!/usr/bin/env python import RaPi.GPIO as GPIO import time PIN_0 = 3 PIN_1 = 5 #GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN_0,GPIO.OUT) GPIO.setup(PIN_1,GPIO.IN) while True: GPIO.output(PIN_0,True) print "\n value = %d\n" %(GPIO.input(PIN_1)) time.sleep(1) GPIO.output(PIN_0,False) print "\n value = %d\n" %(GPIO.input(PIN_1)) time.sleep(1) GPIO.cleanup()