diff options
Diffstat (limited to 'pytools/gpioutil')
-rw-r--r-- | pytools/gpioutil | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/pytools/gpioutil b/pytools/gpioutil new file mode 100644 index 0000000..ab44b4d --- /dev/null +++ b/pytools/gpioutil @@ -0,0 +1,162 @@ +#!/usr/bin/env python + +import sys +import os +import getopt + + +def printUsage(): + print '\nUsage:' + print 'gpioutil SYSTEM_NAME -n GPIO_NAME [-v value]' + print 'gpioutil SYSTEM_NAME -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]' + print 'gpioutil SYSTEM_NAME -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]' + print 'gpioutil SYSTEM_NAME -l PIN_NAME (lookup pin name only)' + exit(1) + + + +if (len(sys.argv) < 3): + printUsage() + +sys.argv.pop(0) +libr = sys.argv.pop(0) +System = __import__(libr) + +GPIO_SYSFS = '/sys/class/gpio/' + +class Gpio: + def __init__(self,gpio_num): + self.gpio_num = str(gpio_num) + self.direction = '' + self.interrupt = '' + self.exported = False + + def getPath(self,name): + return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name + + def export(self): + if (os.path.exists(GPIO_SYSFS+'export') == False): + raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?") + if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False): + self.write(GPIO_SYSFS+'export',self.gpio_num) + + self.exported = True + + def setDirection(self,dir): + if (self.exported == False): + raise Exception("ERROR - Not exported: "+self.getPath()) + + self.direction = '' + self.interrupt = '' + if (dir == 'in' or dir == 'out'): + self.direction = dir + elif (dir == 'rising' or + dir == 'falling' or + dir == 'both'): + self.direction = 'in' + self.interrupt = dir + self.write(self.getPath('edge'),self.interrupt) + else: + raise Exception("ERROR - Invalid Direction: "+dir) + + + self.write(self.getPath('direction'),self.direction) + + def setValue(self,value): + if (value == '0'): + self.write(self.getPath('value'),'0') + elif (value == '1'): + self.write(self.getPath('value'),'1') + else: + raise Exception("ERROR - Invalid value: "+value) + + def getValue(self): + return self.read(self.getPath('value')) + + def write(self,path,data): + f = open(path,'w') + f.write(data) + f.close() + + + def read(self,path): + f = open(path,'r') + data = f.readline() + f.close() + return data + + + +if __name__ == '__main__': + + try: + opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:") + except getopt.GetoptError: + printUsage() + + + + lookup = False + gpio_name = "" + value = "" + direction = "" + for opt, arg in opts: + if opt == '-h': + printUsage() + + elif opt in ("-n"): + gpio_name = arg + lookup = True + elif opt in ("-i"): + gpio_name = arg + elif opt in ("-d"): + direction = arg + elif opt in ("-v"): + value = arg + elif opt in ("-p"): + gpio_name = System.convertGpio(arg) + elif opt in ("-l"): + gpio_name = System.convertGpio(arg) + print gpio_name + exit(0) + + gpio_info = {} + if (lookup == True): + if (System.GPIO_CONFIG.has_key(gpio_name) == False): + print "ERROR - GPIO Name doesn't exist" + print "Valid names: " + for n in System.GPIO_CONFIG: + print "\t"+n + exit(0) + gpio_info = System.GPIO_CONFIG[gpio_name] + direction = gpio_info['direction'] + if (gpio_info.has_key('gpio_num')): + gpio_name = str(gpio_info['gpio_num']) + else: + gpio_name = str(System.convertGpio(gpio_info['gpio_pin'])) + print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction + + + ## Rules + if (gpio_name == ""): + print "ERROR - Gpio not specified" + printUsage() + + if (direction == "in" and value != ""): + print "ERROR - Value cannot be specified when direction = in" + printUsage() + + gpio = Gpio(gpio_name) + try: + gpio.export() + if (direction != ""): + gpio.setDirection(direction) + + if (value == ""): + print gpio.getValue() + else: + gpio.setValue(value) + + except Exception as e: + print e + |