Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
mcp4725.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 toint
16 from webiopi.devices.i2c import I2C
17 from webiopi.devices.analog import DAC
18 
19 
20 class MCP4725(DAC, I2C):
21  def __init__(self, slave=0x60, vref=3.3):
22  I2C.__init__(self, toint(slave))
23  DAC.__init__(self, 1, 12, float(vref))
24 
25  def __str__(self):
26  return "MCP4725(slave=0x%02X)" % self.slave
27 
28  def __analogRead__(self, channel, diff=False):
29  d = self.readBytes(3)
30  value = (d[1] << 8 | d[2]) >> 4
31  return value
32 
33 
34  def __analogWrite__(self, channel, value):
35  d = bytearray(2)
36  d[0] = (value >> 8) & 0x0F
37  d[1] = value & 0xFF
38  self.writeBytes(d)