Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
pcf8591.py
Go to the documentation of this file.
1 # Copyright 2014 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 toint
16 from webiopi.devices.i2c import I2C
17 from webiopi.devices.analog import DAC, ADC
18 
19 
20 class PCF8591(DAC, I2C):
21  def __init__(self, slave=0x48, vref=3.3):
22  I2C.__init__(self, toint(slave))
23  DAC.__init__(self, 5, 8, float(vref))
24  self.daValue = 0
25 
26 
27  def __str__(self):
28  return "PCF8591(slave=0x%02X)" % self.slave
29 
30  def __family__(self):
31  return [DAC.__family__(self), ADC.__family__(self)]
32 
33  def __command__(self, adChannel=0):
34  d = bytearray(2)
35  d[0] = 0x40 # enable output
36  d[0] |= adChannel & 0x03
37  d[1] = self.daValue
38  return d
39 
40 
41  def __analogRead__(self, channel, diff=False):
42  if (channel == 0):
43  return self.daValue
44  else:
45  self.writeBytes(self.__command__(channel-1))
46  return self.readBytes(3)[2]
47 
48 
49  def __analogWrite__(self, channel, value):
50  if (channel == 0):
51  self.daValue = value
52  self.writeBytes(self.__command__())