Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
gpio.py
Go to the documentation of this file.
1 # Copyright 2012-2013 Eric Ptak - trouch.com
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from webiopi.utils.types import M_JSON
16 from webiopi.utils.logger import debug
17 from webiopi.devices.digital import GPIOPort
18 from webiopi.decorators.rest import request, response
19 try:
20  import _webiopi.GPIO as GPIO
21 except:
22  pass
23 
24 EXPORT = []
25 
27  def __init__(self):
28  GPIOPort.__init__(self, 54)
29  self.export = range(54)
30  self.post_value = True
31  self.post_function = True
32  self.gpio_setup = []
33  self.gpio_reset = []
34 
35  def __str__(self):
36  return "GPIO"
37 
38  def addGPIO(self, lst, gpio, params):
39  gpio = int(gpio)
40  params = params.split(" ")
41  func = params[0].lower()
42  if func == "in":
43  func = GPIO.IN
44  elif func == "out":
45  func = GPIO.OUT
46  else:
47  raise Exception("Unknown function")
48 
49  value = -1
50  if len(params) > 1:
51  value = int(params[1])
52  lst.append({"gpio": gpio, "func": func, "value": value})
53 
54  def addGPIOSetup(self, gpio, params):
55  self.addGPIO(self.gpio_setup, gpio, params)
56 
57  def addGPIOReset(self, gpio, params):
58  self.addGPIO(self.gpio_reset, gpio, params)
59 
60  def addSetups(self, gpios):
61  for (gpio, params) in gpios:
62  self.addGPIOSetup(gpio, params)
63 
64  def addResets(self, gpios):
65  for (gpio, params) in gpios:
66  self.addGPIOReset(gpio, params)
67 
68  def setup(self):
69  for g in self.gpio_setup:
70  gpio = g["gpio"]
71  debug("Setup GPIO %d" % gpio)
72  GPIO.setFunction(gpio, g["func"])
73  if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
74  GPIO.digitalWrite(gpio, g["value"])
75 
76  def close(self):
77  for g in self.gpio_reset:
78  gpio = g["gpio"]
79  debug("Reset GPIO %d" % gpio)
80  GPIO.setFunction(gpio, g["func"])
81  if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
82  GPIO.digitalWrite(gpio, g["value"])
83 
84  def checkDigitalChannelExported(self, channel):
85  if not channel in self.export:
86  raise GPIO.InvalidChannelException("Channel %d is not allowed" % channel)
87 
89  if not self.post_function:
90  raise ValueError("POSTing function to native GPIO not allowed")
91 
93  if not self.post_value:
94  raise ValueError("POSTing value to native GPIO not allowed")
95 
96  def __digitalRead__(self, channel):
97  self.checkDigitalChannelExported(channel)
98  return GPIO.digitalRead(channel)
99 
100  def __digitalWrite__(self, channel, value):
101  self.checkDigitalChannelExported(channel)
103  GPIO.digitalWrite(channel, value)
104 
105  def __getFunction__(self, channel):
106  self.checkDigitalChannelExported(channel)
107  return GPIO.getFunction(channel)
108 
109  def __setFunction__(self, channel, value):
110  self.checkDigitalChannelExported(channel)
112  GPIO.setFunction(channel, value)
113 
114  def __portRead__(self):
115  value = 0
116  for i in self.export:
117  value |= GPIO.digitalRead(i) << i
118  return value
119 
120  def __portWrite__(self, value):
121  if len(self.export) < 54:
122  for i in self.export:
123  if GPIO.getFunction(i) == GPIO.OUT:
124  GPIO.digitalWrite(i, (value >> i) & 1)
125  else:
126  raise Exception("Please limit exported GPIO to write integers")
127 
128  @request("GET", "*")
129  @response(contentType=M_JSON)
130  def wildcard(self, compact=False):
131  if compact:
132  f = "f"
133  v = "v"
134  else:
135  f = "function"
136  v = "value"
137 
138  values = {}
139  print(self.export)
140  for i in self.export:
141  if compact:
142  func = GPIO.getFunction(i)
143  else:
144  func = GPIO.getFunctionString(i)
145  values[i] = {f: func, v: int(GPIO.digitalRead(i))}
146  return values
147 
148 
149  @request("GET", "%(channel)d/pulse", "%s")
150  def getPulse(self, channel):
151  self.checkDigitalChannelExported(channel)
152  self.checkDigitalChannel(channel)
153  return GPIO.getPulse(channel)
154 
155  @request("POST", "%(channel)d/sequence/%(args)s")
156  @response("%d")
157  def outputSequence(self, channel, args):
158  self.checkDigitalChannelExported(channel)
160  self.checkDigitalChannel(channel)
161  (period, sequence) = args.split(",")
162  period = int(period)
163  GPIO.outputSequence(channel, period, sequence)
164  return int(sequence[-1])
165 
166  @request("POST", "%(channel)d/pulse/")
167  def pulse(self, channel):
168  self.checkDigitalChannelExported(channel)
170  self.checkDigitalChannel(channel)
171  GPIO.pulse(channel)
172  return "OK"
173 
174  @request("POST", "%(channel)d/pulseRatio/%(value)f")
175  def pulseRatio(self, channel, value):
176  self.checkDigitalChannelExported(channel)
178  self.checkDigitalChannel(channel)
179  GPIO.pulseRatio(channel, value)
180  return GPIO.getPulse(channel)
181 
182  @request("POST", "%(channel)d/pulseAngle/%(value)f")
183  def pulseAngle(self, channel, value):
184  self.checkDigitalChannelExported(channel)
186  self.checkDigitalChannel(channel)
187  GPIO.pulseAngle(channel, value)
188  return GPIO.getPulse(channel)
189 
tuple response
Definition: coap-client.py:9