1*40a360c2SBrad Bishop#!/usr/bin/env python 2*40a360c2SBrad Bishop 3*40a360c2SBrad Bishopimport sys 4*40a360c2SBrad Bishopimport os 5*40a360c2SBrad Bishopimport getopt 6*40a360c2SBrad Bishop 7*40a360c2SBrad Bishop 8*40a360c2SBrad Bishopdef printUsage(): 9*40a360c2SBrad Bishop print '\nUsage:' 10*40a360c2SBrad Bishop print 'gpioutil SYSTEM_NAME -n GPIO_NAME [-v value]' 11*40a360c2SBrad Bishop print 'gpioutil SYSTEM_NAME -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]' 12*40a360c2SBrad Bishop print 'gpioutil SYSTEM_NAME -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]' 13*40a360c2SBrad Bishop print 'gpioutil SYSTEM_NAME -l PIN_NAME (lookup pin name only)' 14*40a360c2SBrad Bishop exit(1) 15*40a360c2SBrad Bishop 16*40a360c2SBrad Bishop 17*40a360c2SBrad Bishop 18*40a360c2SBrad Bishopif (len(sys.argv) < 3): 19*40a360c2SBrad Bishop printUsage() 20*40a360c2SBrad Bishop 21*40a360c2SBrad Bishopsys.argv.pop(0) 22*40a360c2SBrad Bishoplibr = sys.argv.pop(0) 23*40a360c2SBrad BishopSystem = __import__(libr) 24*40a360c2SBrad Bishop 25*40a360c2SBrad BishopGPIO_SYSFS = '/sys/class/gpio/' 26*40a360c2SBrad Bishop 27*40a360c2SBrad Bishopclass Gpio: 28*40a360c2SBrad Bishop def __init__(self,gpio_num): 29*40a360c2SBrad Bishop self.gpio_num = str(gpio_num) 30*40a360c2SBrad Bishop self.direction = '' 31*40a360c2SBrad Bishop self.interrupt = '' 32*40a360c2SBrad Bishop self.exported = False 33*40a360c2SBrad Bishop 34*40a360c2SBrad Bishop def getPath(self,name): 35*40a360c2SBrad Bishop return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name 36*40a360c2SBrad Bishop 37*40a360c2SBrad Bishop def export(self): 38*40a360c2SBrad Bishop if (os.path.exists(GPIO_SYSFS+'export') == False): 39*40a360c2SBrad Bishop raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?") 40*40a360c2SBrad Bishop if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False): 41*40a360c2SBrad Bishop self.write(GPIO_SYSFS+'export',self.gpio_num) 42*40a360c2SBrad Bishop 43*40a360c2SBrad Bishop self.exported = True 44*40a360c2SBrad Bishop 45*40a360c2SBrad Bishop def setDirection(self,dir): 46*40a360c2SBrad Bishop if (self.exported == False): 47*40a360c2SBrad Bishop raise Exception("ERROR - Not exported: "+self.getPath()) 48*40a360c2SBrad Bishop 49*40a360c2SBrad Bishop self.direction = '' 50*40a360c2SBrad Bishop self.interrupt = '' 51*40a360c2SBrad Bishop if (dir == 'in' or dir == 'out'): 52*40a360c2SBrad Bishop self.direction = dir 53*40a360c2SBrad Bishop elif (dir == 'rising' or 54*40a360c2SBrad Bishop dir == 'falling' or 55*40a360c2SBrad Bishop dir == 'both'): 56*40a360c2SBrad Bishop self.direction = 'in' 57*40a360c2SBrad Bishop self.interrupt = dir 58*40a360c2SBrad Bishop self.write(self.getPath('edge'),self.interrupt) 59*40a360c2SBrad Bishop else: 60*40a360c2SBrad Bishop raise Exception("ERROR - Invalid Direction: "+dir) 61*40a360c2SBrad Bishop 62*40a360c2SBrad Bishop 63*40a360c2SBrad Bishop self.write(self.getPath('direction'),self.direction) 64*40a360c2SBrad Bishop 65*40a360c2SBrad Bishop def setValue(self,value): 66*40a360c2SBrad Bishop if (value == '0'): 67*40a360c2SBrad Bishop self.write(self.getPath('value'),'0') 68*40a360c2SBrad Bishop elif (value == '1'): 69*40a360c2SBrad Bishop self.write(self.getPath('value'),'1') 70*40a360c2SBrad Bishop else: 71*40a360c2SBrad Bishop raise Exception("ERROR - Invalid value: "+value) 72*40a360c2SBrad Bishop 73*40a360c2SBrad Bishop def getValue(self): 74*40a360c2SBrad Bishop return self.read(self.getPath('value')) 75*40a360c2SBrad Bishop 76*40a360c2SBrad Bishop def write(self,path,data): 77*40a360c2SBrad Bishop f = open(path,'w') 78*40a360c2SBrad Bishop f.write(data) 79*40a360c2SBrad Bishop f.close() 80*40a360c2SBrad Bishop 81*40a360c2SBrad Bishop 82*40a360c2SBrad Bishop def read(self,path): 83*40a360c2SBrad Bishop f = open(path,'r') 84*40a360c2SBrad Bishop data = f.readline() 85*40a360c2SBrad Bishop f.close() 86*40a360c2SBrad Bishop return data 87*40a360c2SBrad Bishop 88*40a360c2SBrad Bishop 89*40a360c2SBrad Bishop 90*40a360c2SBrad Bishopif __name__ == '__main__': 91*40a360c2SBrad Bishop 92*40a360c2SBrad Bishop try: 93*40a360c2SBrad Bishop opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:") 94*40a360c2SBrad Bishop except getopt.GetoptError: 95*40a360c2SBrad Bishop printUsage() 96*40a360c2SBrad Bishop 97*40a360c2SBrad Bishop 98*40a360c2SBrad Bishop 99*40a360c2SBrad Bishop lookup = False 100*40a360c2SBrad Bishop gpio_name = "" 101*40a360c2SBrad Bishop value = "" 102*40a360c2SBrad Bishop direction = "" 103*40a360c2SBrad Bishop for opt, arg in opts: 104*40a360c2SBrad Bishop if opt == '-h': 105*40a360c2SBrad Bishop printUsage() 106*40a360c2SBrad Bishop 107*40a360c2SBrad Bishop elif opt in ("-n"): 108*40a360c2SBrad Bishop gpio_name = arg 109*40a360c2SBrad Bishop lookup = True 110*40a360c2SBrad Bishop elif opt in ("-i"): 111*40a360c2SBrad Bishop gpio_name = arg 112*40a360c2SBrad Bishop elif opt in ("-d"): 113*40a360c2SBrad Bishop direction = arg 114*40a360c2SBrad Bishop elif opt in ("-v"): 115*40a360c2SBrad Bishop value = arg 116*40a360c2SBrad Bishop elif opt in ("-p"): 117*40a360c2SBrad Bishop gpio_name = System.convertGpio(arg) 118*40a360c2SBrad Bishop elif opt in ("-l"): 119*40a360c2SBrad Bishop gpio_name = System.convertGpio(arg) 120*40a360c2SBrad Bishop print gpio_name 121*40a360c2SBrad Bishop exit(0) 122*40a360c2SBrad Bishop 123*40a360c2SBrad Bishop gpio_info = {} 124*40a360c2SBrad Bishop if (lookup == True): 125*40a360c2SBrad Bishop if (System.GPIO_CONFIG.has_key(gpio_name) == False): 126*40a360c2SBrad Bishop print "ERROR - GPIO Name doesn't exist" 127*40a360c2SBrad Bishop print "Valid names: " 128*40a360c2SBrad Bishop for n in System.GPIO_CONFIG: 129*40a360c2SBrad Bishop print "\t"+n 130*40a360c2SBrad Bishop exit(0) 131*40a360c2SBrad Bishop gpio_info = System.GPIO_CONFIG[gpio_name] 132*40a360c2SBrad Bishop direction = gpio_info['direction'] 133*40a360c2SBrad Bishop if (gpio_info.has_key('gpio_num')): 134*40a360c2SBrad Bishop gpio_name = str(gpio_info['gpio_num']) 135*40a360c2SBrad Bishop else: 136*40a360c2SBrad Bishop gpio_name = str(System.convertGpio(gpio_info['gpio_pin'])) 137*40a360c2SBrad Bishop print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction 138*40a360c2SBrad Bishop 139*40a360c2SBrad Bishop 140*40a360c2SBrad Bishop ## Rules 141*40a360c2SBrad Bishop if (gpio_name == ""): 142*40a360c2SBrad Bishop print "ERROR - Gpio not specified" 143*40a360c2SBrad Bishop printUsage() 144*40a360c2SBrad Bishop 145*40a360c2SBrad Bishop if (direction == "in" and value != ""): 146*40a360c2SBrad Bishop print "ERROR - Value cannot be specified when direction = in" 147*40a360c2SBrad Bishop printUsage() 148*40a360c2SBrad Bishop 149*40a360c2SBrad Bishop gpio = Gpio(gpio_name) 150*40a360c2SBrad Bishop try: 151*40a360c2SBrad Bishop gpio.export() 152*40a360c2SBrad Bishop if (direction != ""): 153*40a360c2SBrad Bishop gpio.setDirection(direction) 154*40a360c2SBrad Bishop 155*40a360c2SBrad Bishop if (value == ""): 156*40a360c2SBrad Bishop print gpio.getValue() 157*40a360c2SBrad Bishop else: 158*40a360c2SBrad Bishop gpio.setValue(value) 159*40a360c2SBrad Bishop 160*40a360c2SBrad Bishop except Exception as e: 161*40a360c2SBrad Bishop print e 162*40a360c2SBrad Bishop 163