Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
__init__.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.decorators.rest import request, response
16 from webiopi.utils.types import M_JSON
17 
18 class GPIOPort():
19  IN = 0
20  OUT = 1
21 
22  LOW = False
23  HIGH = True
24 
25  def __init__(self, channelCount, bankCount=0):
26  self.digitalChannelCount = channelCount
27  self.checkDigitalBanks(bankCount)
28  self.digitalBanksCount = bankCount
29 
30  def checkDigitalChannel(self, channel):
31  if not 0 <= channel < self.digitalChannelCount:
32  raise ValueError("Channel %d out of range [%d..%d]" % (channel, 0, self.digitalChannelCount-1))
33 
34  def checkDigitalValue(self, value):
35  if not (value == 0 or value == 1):
36  raise ValueError("Value %d not in {0, 1}")
37 
38  def checkDigitalBanks(self, banks):
39  if not 0 <= banks < self.digitalChannelCount:
40  raise ValueError("Banks %d out of range [%d..%d]" % (banks, 0, self.digitalChannelCount))
41 
42  @request("GET", "count")
43  @response("%d")
44  def digitalCount(self):
45  return self.digitalChannelCount
46 
47  @request("GET", "banks")
48  @response("%d")
49  def bankCount(self):
50  return self.digitalBanksCount
51 
52  def __family__(self):
53  return "GPIOPort"
54 
55  def __getFunction__(self, channel):
56  raise NotImplementedError
57 
58  def __setFunction__(self, channel, func):
59  raise NotImplementedError
60 
61  def __digitalRead__(self, chanel):
62  raise NotImplementedError
63 
64  def __portRead__(self):
65  raise NotImplementedError
66 
67  def __digitalWrite__(self, chanel, value):
68  raise NotImplementedError
69 
70  def __portWrite__(self, value):
71  raise NotImplementedError
72 
73  def getFunction(self, channel):
74  self.checkDigitalChannel(channel)
75  return self.__getFunction__(channel)
76 
77  @request("GET", "%(channel)d/function")
78  def getFunctionString(self, channel):
79  func = self.getFunction(channel)
80  if func == self.IN:
81  return "IN"
82  elif func == self.OUT:
83  return "OUT"
84 # elif func == GPIO.PWM:
85 # return "PWM"
86  else:
87  return "UNKNOWN"
88 
89  def setFunction(self, channel, value):
90  self.checkDigitalChannel(channel)
91  self.__setFunction__(channel, value)
92  return self.getFunction(channel)
93 
94  @request("POST", "%(channel)d/function/%(value)s")
95  def setFunctionString(self, channel, value):
96  value = value.lower()
97  if value == "in":
98  self.setFunction(channel, self.IN)
99  elif value == "out":
100  self.setFunction(channel, self.OUT)
101 # elif value == "pwm":
102 # self.setFunction(channel, GPIO.PWM)
103  else:
104  raise ValueError("Bad Function")
105  return self.getFunctionString(channel)
106 
107  @request("GET", "%(channel)d/value")
108  @response("%d")
109  def digitalRead(self, channel):
110  self.checkDigitalChannel(channel)
111  return self.__digitalRead__(channel)
112 
113  @request("GET", "*")
114  @response(contentType=M_JSON)
115  def wildcard(self, compact=False):
116  if compact:
117  f = "f"
118  v = "v"
119  else:
120  f = "function"
121  v = "value"
122 
123  values = {}
124  for i in range(self.digitalChannelCount):
125  if compact:
126  func = self.getFunction(i)
127  else:
128  func = self.getFunctionString(i)
129  values[i] = {f: func, v: int(self.digitalRead(i))}
130  return values
131 
132  @request("GET", "*/integer")
133  @response("%d")
134  def portRead(self):
135  return self.__portRead__()
136 
137  @request("POST", "%(channel)d/value/%(value)d")
138  @response("%d")
139  def digitalWrite(self, channel, value):
140  self.checkDigitalChannel(channel)
141  self.checkDigitalValue(value)
142  self.__digitalWrite__(channel, value)
143  return self.digitalRead(channel)
144 
145  @request("POST", "*/integer/%(value)d")
146  @response("%d")
147  def portWrite(self, value):
148  self.__portWrite__(value)
149  return self.portRead()
150 
151 DRIVERS = {}
152 DRIVERS["mcp23XXX"] = ["MCP23008", "MCP23009", "MCP23017", "MCP23018", "MCP23S08", "MCP23S09", "MCP23S17", "MCP23S18"]
153 DRIVERS["pcf8574" ] = ["PCF8574", "PCF8574A"]
154 DRIVERS["ds2408" ] = ["DS2408"]
155 DRIVERS["pca9555" ] = ["PCA9555", "PCA9535"]
tuple response
Definition: coap-client.py:9