Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
ds2408.py
Go to the documentation of this file.
1 # Copyright 2013 Stuart Marsden
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.devices.onewire import OneWire
16 from webiopi.devices.digital import GPIOPort
17 
19  FUNCTIONS = [GPIOPort.IN for i in range(8)]
20 
21  def __init__(self, slave=None):
22  OneWire.__init__(self, slave, 0x29, "2408")
23  GPIOPort.__init__(self, 8)
24  self.portWrite(0x00)
25 
26  def __str__(self):
27  return "DS2408(slave=%s)" % self.slave
28 
29  def __getFunction__(self, channel):
30  return self.FUNCTIONS[channel]
31 
32  def __setFunction__(self, channel, value):
33  if not value in [self.IN, self.OUT]:
34  raise ValueError("Requested function not supported")
35  self.FUNCTIONS[channel] = value
36  if value == self.IN:
37  self.__output__(channel, 0)
38 
39  def __digitalRead__(self, channel):
40  mask = 1 << channel
41  d = self.readState()
42  if d != None:
43  return (d & mask) == mask
44 
45 
46  def __digitalWrite__(self, channel, value):
47  mask = 1 << channel
48  b = self.readByte()
49  if value:
50  b |= mask
51  else:
52  b &= ~mask
53  self.writeByte(b)
54 
55  def __portWrite__(self, value):
56  self.writeByte(value)
57 
58  def __portRead__(self):
59  return self.readByte()
60 
61  def readState(self):
62  try:
63  with open("/sys/bus/w1/devices/%s/state" % self.slave, "rb") as f:
64  data = f.read(1)
65  return ord(data)
66  except IOError:
67  return -1
68 
69  def readByte(self):
70  try:
71  with open("/sys/bus/w1/devices/%s/output" % self.slave, "rb") as f:
72  data = f.read(1)
73  return bytearray(data)[0]
74  except IOError:
75  return -1
76 
77  def writeByte(self, value):
78  try:
79  with open("/sys/bus/w1/devices/%s/output" % self.slave, "wb") as f:
80  f.write(bytearray([value]))
81  except IOError:
82  pass
83 
84