Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
mcp3x0x.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 ADC
18 
19 class MCP3X0X(SPI, ADC):
20  def __init__(self, chip, channelCount, resolution, vref, name):
21  SPI.__init__(self, toint(chip), 0, 8, 10000)
22  ADC.__init__(self, channelCount, resolution, float(vref))
23  self.name = name
24  self.MSB_MASK = 2**(resolution-8) - 1
25 
26  def __str__(self):
27  return "%s(chip=%d)" % (self.name, self.chip)
28 
29  def __analogRead__(self, channel, diff):
30  data = self.__command__(channel, diff)
31  r = self.xfer(data)
32  return ((r[1] & self.MSB_MASK) << 8) | r[2]
33 
35  def __init__(self, chip, channelCount, vref, name):
36  MCP3X0X.__init__(self, chip, channelCount, 10, vref, name)
37 
38  def __command__(self, channel, diff):
39  d = [0x00, 0x00, 0x00]
40  d[0] |= 1
41  d[1] |= (not diff) << 7
42  d[1] |= ((channel >> 2) & 0x01) << 6
43  d[1] |= ((channel >> 1) & 0x01) << 5
44  d[1] |= ((channel >> 0) & 0x01) << 4
45  return d
46 
48  def __init__(self, chip=0, vref=3.3):
49  MCP300X.__init__(self, chip, 2, vref, "MCP3002")
50 
51  def __analogRead__(self, channel, diff):
52  data = self.__command__(channel, diff)
53  r = self.xfer(data)
54 
55  # Format of return is
56  # 1 empty bit
57  # 1 null bit
58  # 10 ADC bits
59  return (r[0]<<14 | r[1]<<6 | r[2]>>2) & ((2**self._analogResolution)-1)
60 
61  def __command__(self, channel, diff):
62  d = [0x00, 0x00, 0x00]
63  d[0] |= 1 #start bit
64  d[1] |= (not diff) << 7 #single/differntial input
65  d[1] |= channel << 6 #channel select
66  return d
67 
69  def __init__(self, chip=0, vref=3.3):
70  MCP300X.__init__(self, chip, 4, vref, "MCP3004")
71 
73  def __init__(self, chip=0, vref=3.3):
74  MCP300X.__init__(self, chip, 8, vref, "MCP3008")
75 
77  def __init__(self, chip, channelCount, vref, name):
78  MCP3X0X.__init__(self, chip, channelCount, 12, vref, name)
79 
80  def __command__(self, channel, diff):
81  d = [0x00, 0x00, 0x00]
82  d[0] |= 1 << 2
83  d[0] |= (not diff) << 1
84  d[0] |= (channel >> 2) & 0x01
85  d[1] |= ((channel >> 1) & 0x01) << 7
86  d[1] |= ((channel >> 0) & 0x01) << 6
87  return d
88 
90  def __init__(self, chip=0, vref=3.3):
91  MCP320X.__init__(self, chip, 4, vref, "MCP3204")
92 
94  def __init__(self, chip=0, vref=3.3):
95  MCP320X.__init__(self, chip, 8, vref, "MCP3208")
96