Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
0
def SPIsend(opcode, addr, data):
    GPIO.setup(SPI_CS0, GPIO.OUT)
    GPIO.output(SPI_CS0, GPIO.LOW)
    SPIsendValue(opcode | SPI_SLAVE_WRITE)
    SPIsendValue(addr)
    SPIsendValue(data)
    GPIO.output(SPI_CS0, GPIO.HIGH)
Esempio n. 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()
Esempio n. 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()
Esempio n. 6
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)
Esempio n. 7
0
	def reset(self):
		GPIO.output(self.pin1,GPIO.LOW)
		GPIO.output(self.pin2,GPIO.LOW)
		GPIO.output(self.pin3,GPIO.LOW)
		GPIO.output(self.pin4,GPIO.LOW)
		self.moving = False
		return	
Esempio n. 8
0
	def lock(self):
		GPIO.output(self.pin1,GPIO.HIGH)
		GPIO.output(self.pin2,GPIO.LOW)
		GPIO.output(self.pin3,GPIO.LOW)
		GPIO.output(self.pin4,GPIO.HIGH)
		self.moving = False
		return	
Esempio n. 9
0
def SPIsendValue(value):
    GPIO.setup(SPI_MOSI, GPIO.OUT)
    GPIO.setup(SPI_SCLK, GPIO.OUT)
    for i in range(8):
        if (value & 0x80):
            GPIO.output(SPI_MOSI, GPIO.HIGH)
        else:
            GPIO.output(SPI_MOSI, GPIO.LOW)
        GPIO.output(SPI_SCLK, GPIO.HIGH)
        GPIO.output(SPI_SCLK, GPIO.LOW)
        value <<= 1
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)')

        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()
Esempio n. 11
0
def LNdigital():
    try:
        GPIO.setup(SPI_SCLK, GPIO.OUT)
        GPIO.setup(SPI_MOSI, GPIO.OUT)
        GPIO.setup(SPI_MISO, GPIO.IN)
        GPIO.setup(SPI_CS0, GPIO.OUT)

        GPIO.output(SPI_CS0, GPIO.HIGH)       
        GPIO.output(SPI_SCLK, GPIO.LOW)

        SPIsend(SPI_SLAVE_ADDR, SPI_IODIRA, 0x00)#outputs
        SPIsend(SPI_SLAVE_ADDR, SPI_IODIRB, 0xFF)#inputs
        SPIsend(SPI_SLAVE_ADDR, SPI_GPIOA, 0x00)
        SPIsend(SPI_SLAVE_ADDR, SPI_GPIOB, 0xFF)
        Test = SPIread(SPI_SLAVE_ADDR, SPI_GPIOA)
        if Test == 0x00:
            Addons["LNdigital"] = True
        else:
            Addons["LNdigital"] = False
    except:
        Addons["LNdigital"] = False
Esempio n. 12
0
def SPIread(opcode, addr):
    GPIO.setup(SPI_MISO, GPIO.IN)
    GPIO.output(SPI_CS0, GPIO.LOW)
    SPIsendValue(opcode | SPI_SLAVE_READ)
    SPIsendValue(addr)
    value = 0
    for i in range(8):
        value <<= 1
        if(GPIO.input(SPI_MISO)):
            value |= 0x01
        GPIO.output(SPI_SCLK, GPIO.HIGH)
        GPIO.output(SPI_SCLK, GPIO.LOW)
    GPIO.output(SPI_CS0, GPIO.HIGH)
    return value
Esempio n. 13
0
def testOutput(mode, pins):
	if mode == "BOARD":
		print "Start to test the mode: %s" %(mode)
		GPIO.setmode(GPIO.BOARD)
	elif mode == "BCM":
		print "Start to test the mode: %s" %(mode)
		GPIO.setmode(GPIO.BCM)
	else:
		print "Invalid test mode: %s" %(mode)

	for i in pins:
		try:
			GPIO.setup(i, GPIO.OUT)
		except:
			print("Failed to setup GPIO %d", i)
			continue

		GPIO.output(i, True)
		time.sleep(0.5)
		GPIO.output(i, False)
		time.sleep(0.5)

	GPIO.cleanup()
Esempio n. 14
0
def testPwm(mode, pins):
    if mode == "BOARD":
        print "Start to test the mode: %s" % (mode)
        GPIO.setmode(GPIO.BOARD)
    elif mode == "BCM":
        print "Start to test the mode: %s" % (mode)
        GPIO.setmode(GPIO.BCM)
    else:
        print "Invalid test mode: %s" % (mode)

    for i in pins:
        GPIO.setup(i, GPIO.OUT)
        p = GPIO.PWM(i, 100)  #set freq: 100HZ
        p.start(10)  #duty cycle: 10%
        time.sleep(1)

        p.start(100)  #duty cycle: 100%
        time.sleep(1)

        GPIO.output(i, False)
        p.stop()

    GPIO.cleanup()
Esempio n. 15
0
def testPwm(mode, pins):
	if mode == "BOARD":
		print "Start to test the mode: %s" %(mode)
		GPIO.setmode(GPIO.BOARD)
	elif mode == "BCM":
		print "Start to test the mode: %s" %(mode)
		GPIO.setmode(GPIO.BCM)
	else:
		print "Invalid test mode: %s" %(mode)

	for i in pins: 
		GPIO.setup(i,GPIO.OUT)
		p = GPIO.PWM(i,100)   #set freq: 100HZ
		p.start(10)           #duty cycle: 10%
		time.sleep(1)
		
		p.start(100)          #duty cycle: 100%
		time.sleep(1)

		GPIO.output(i, False)		
		p.stop()

	GPIO.cleanup()
Esempio n. 16
0
 def testRisingEventDetected(self):
     GPIO.output(LOOP_OUT, GPIO.LOW)
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     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)
     GPIO.output(LOOP_OUT, GPIO.LOW)
     time.sleep(0.001)
     self.assertEqual(GPIO.event_detected(LOOP_IN), False)
     GPIO.remove_event_detect(LOOP_IN)
Esempio n. 17
0
def calDistance(trigger, echo):
    stop = start = 0
    GPIO.setup(trigger, GPIO.OUT)
    GPIO.setup(echo, GPIO.IN)
    GPIO.output(trigger, 0)
    time.sleep(0.5)
    
    GPIO.output(trigger, 1)
    time.sleep(0.00001)
    GPIO.output(trigger, 0)
    start = time.time()
    while GPIO.input(echo) == 0:
        start = time.time()

    while GPIO.input(echo) == 1:
        stop = time.time()

    delta = stop - start
    print("UltraSonic time %.8f" % (delta))
    distance = delta * 34300
    distance = distance / 2.0
    GPIO.cleanup()
    return distance
Esempio n. 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))
Esempio n. 19
0
#!/usr/bin/env python
import LMK.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 = [8]
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)
Esempio n. 20
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()
Esempio n. 21
0
'''
We use the program to test input of the pins
'''
import LMK.GPIO as GPIO
import time

#----------P3  P5  P7  P8  P10  P11  P12  P13  P15  P16  P18  P19 P21  P22  P23  P24 P26 P27 P28 P29  P31  P32  P33  P35  P36  P37  P38  P40-----------#
phyPins = (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)
bcmPins = (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)

pinOut = 3
pinIn  = 5

GPIO.setmode(GPIO.BOARD)
GPIO.setup(pinOut,GPIO.OUT)
GPIO.setup(pinIn,GPIO.IN)

while True:
	GPIO.output(pinOut,True)
	print "value = %d\n" %(GPIO.input(pinIn)) 
	time.sleep(1)
	GPIO.output(pinOut,False)
	print "value = %d\n" %(GPIO.input(pinIn))
	time.sleep(1)

GPIO.cleanup()
Esempio n. 22
0
 def makehigh():
     GPIO.output(LOOP_OUT, GPIO.HIGH)
Esempio n. 23
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)
Esempio n. 24
0
#!/usr/bin/env python
import LMK.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()
Esempio n. 25
0
#!/usr/bin/env python
import LMK.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()
Esempio n. 26
0
                        Cmd = Data2[1].upper()
                        if not Pin in [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]:
                            break
                        if Cmd == "OUT":
                            GPIOset[Pin] = True
                            GPIOdir[Pin] = "OUT"
                            GPIO.setup(Pin, GPIO.OUT)
                            print(str(Pin) + " set to OUT")
                        if Cmd == "IN":
                            GPIOset[Pin] = True
                            GPIOdir[Pin] = "IN"
                            GPIO.setup(Pin, GPIO.IN)
                            print(str(Pin) + " set to IN")
                        if Cmd == "ON" and GPIOset[Pin]:
                            GPIO.setup(Pin, GPIO.OUT)
                            GPIO.output(Pin, 1)
                            print(str(Pin) + " set to ON")
                        if Cmd == "OFF" and GPIOset[Pin]:
                            GPIO.setup(Pin, GPIO.OUT)
                            GPIO.output(Pin, 0)
                            print(str(Pin) + " set to OFF")

                    if LNDI:                    
                        LNDI = LNDI[0]
                        Pin = int(LNDI[0])
                        Cmd = LNDI[1].upper()
                        if not Pin in range(0,8):
                            break
                        if Cmd == "OUT":
                            LNout[Pin] = True
                            print("Listen to OUTPUT " + str(Pin))
Esempio n. 27
0
 def makelow():
     GPIO.output(LOOP_OUT, GPIO.LOW)
Esempio n. 28
0
	def turn(self,steps,direction):
		global CLOCKWISE
		self.stop()
		self.moving = True
		self.steps = steps		
		mrange = len(out1)
		while self.steps > 0:
			if direction == self.CLOCKWISE:
				for pin in range(mrange):
					GPIO.output(self.pin1,out1[pin])
					GPIO.output(self.pin2,out2[pin])
					GPIO.output(self.pin3,out3[pin])
					GPIO.output(self.pin4,out4[pin])
					time.sleep(self.speed)
				self.incrementPosition()
			else:
				for pin in reversed(range(mrange)):
					GPIO.output(self.pin1,out1[pin])
					GPIO.output(self.pin2,out2[pin])
					GPIO.output(self.pin3,out3[pin])
					GPIO.output(self.pin4,out4[pin])
					time.sleep(self.speed)
				self.decrementPosition()
			self.steps -= 1
			if self.halt:
				break
		self.stop()
		return
Esempio n. 29
0
#!/usr/bin/env python
import LMK.GPIO as GPIO
import time
PIN_NUM = 7
PIN_IN = 10
val = 0
GPIO.setmode(GPIO.BOARD)
while 1:
    try:
        GPIO.setup(PIN_NUM, GPIO.OUT)
        GPIO.setup(PIN_IN, GPIO.IN)
    except:
        print("fail to setup GPIO %d", PIN_NUM)
    val = GPIO.input(PIN_IN)
    if val:
        GPIO.output(PIN_NUM, True)
    else:
        GPIO.output(PIN_NUM, False)