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