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