Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
mcp492X.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.spi import SPI
17 from webiopi.devices.analog import DAC
18 
19 class MCP492X(SPI, DAC):
20  def __init__(self, chip, channelCount, vref):
21  SPI.__init__(self, toint(chip), 0, 8, 10000000)
22  DAC.__init__(self, channelCount, 12, float(vref))
23  self.buffered=False
24  self.gain=False
25  self.shutdown=False
26  self.values = [0 for i in range(channelCount)]
27 
28  def __str__(self):
29  return "MCP492%d(chip=%d)" % (self._analogCount, self.chip)
30 
31  def __analogRead__(self, channel, diff=False):
32  return self.values[channel]
33 
34  def __analogWrite__(self, channel, value):
35  d = bytearray(2)
36  d[0] = 0
37  d[0] |= (channel & 0x01) << 7
38  d[0] |= (self.buffered & 0x01) << 6
39  d[0] |= (not self.gain & 0x01) << 5
40  d[0] |= (not self.shutdown & 0x01) << 4
41  d[0] |= (value >> 8) & 0x0F
42  d[1] = value & 0xFF
43  self.writeBytes(d)
44  self.values[channel] = value
45 
47  def __init__(self, chip=0, vref=3.3):
48  MCP492X.__init__(self, chip, 1)
49 
51  def __init__(self, chip=0, vref=3.3):
52  MCP492X.__init__(self, chip, 2)
53