Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
piface.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.devices.digital.mcp23XXX import MCP23S17
17 from webiopi.decorators.rest import request, response
18 
19 
20 class PiFaceDigital():
21  def __init__(self, board=0):
22  mcp = MCP23S17(0, 0x20+board)
23  mcp.writeRegister(mcp.getAddress(mcp.IODIR, 0), 0x00) # Port A as output
24  mcp.writeRegister(mcp.getAddress(mcp.IODIR, 8), 0xFF) # Port B as input
25  mcp.writeRegister(mcp.getAddress(mcp.GPPU, 0), 0x00) # Port A PU OFF
26  mcp.writeRegister(mcp.getAddress(mcp.GPPU, 8), 0xFF) # Port B PU ON
27  self.mcp = mcp
28  self.board = board
29 
30  def __str__(self):
31  return "PiFaceDigital(%d)" % self.board
32 
33  def __family__(self):
34  return "PiFaceDigital"
35 
36  def checkChannel(self, channel):
37  if not channel in range(8):
38  raise ValueError("Channel %d invalid" % channel)
39 
40  @request("GET", "digital/input/%(channel)d")
41  @response("%d")
42  def digitalRead(self, channel):
43  self.checkChannel(channel)
44  return not self.mcp.digitalRead(channel+8)
45 
46  @request("POST", "digital/output/%(channel)d/%(value)d")
47  @response("%d")
48  def digitalWrite(self, channel, value):
49  self.checkChannel(channel)
50  return self.mcp.digitalWrite(channel, value)
51 
52  @request("GET", "digital/output/%(channel)d")
53  @response("%d")
54  def digitalReadOutput(self, channel):
55  self.checkChannel(channel)
56  return self.mcp.digitalRead(channel)
57 
58  @request("GET", "digital/*")
59  @response(contentType=M_JSON)
60  def readAll(self):
61  inputs = {}
62  outputs = {}
63  for i in range(8):
64  inputs[i] = self.digitalRead(i)
65  outputs[i] = self.digitalReadOutput(i)
66  return {"input": inputs, "output": outputs}
tuple response
Definition: coap-client.py:9