Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
ads1x1x.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 time import sleep
16 from webiopi.utils.types import toint, signInteger
17 from webiopi.devices.i2c import I2C
18 from webiopi.devices.analog import ADC
19 
20 
21 class ADS1X1X(ADC, I2C):
22  VALUE = 0x00
23  CONFIG = 0x01
24  LO_THRESH = 0x02
25  HI_THRESH = 0x03
26 
27  CONFIG_STATUS_MASK = 0x80
28  CONFIG_CHANNEL_MASK = 0x70
29  CONFIG_GAIN_MASK = 0x0E
30  CONFIG_MODE_MASK = 0x01
31 
32  def __init__(self, slave, channelCount, resolution, name):
33  I2C.__init__(self, toint(slave))
34  ADC.__init__(self, channelCount, resolution, 4.096)
35  self._analogMax = 2**(resolution-1)
36  self.name = name
37 
38  config = self.readRegisters(self.CONFIG, 2)
39 
40  mode = 0 # continuous
41  config[0] &= ~self.CONFIG_MODE_MASK
42  config[0] |= mode
43 
44  gain = 0x1 # FS = +/- 4.096V
45  config[0] &= ~self.CONFIG_GAIN_MASK
46  config[0] |= gain << 1
47 
48  self.writeRegisters(self.CONFIG, config)
49 
50  def __str__(self):
51  return "%s(slave=0x%02X)" % (self.name, self.slave)
52 
53  def __analogRead__(self, channel, diff=False):
54  config = self.readRegisters(self.CONFIG, 2)
55  config[0] &= ~self.CONFIG_CHANNEL_MASK
56  if diff:
57  config[0] |= channel << 4
58  else:
59  config[0] |= (channel + 4) << 4
60  self.writeRegisters(self.CONFIG, config)
61  sleep(0.001)
62  d = self.readRegisters(self.VALUE, 2)
63  value = (d[0] << 8 | d[1]) >> (16-self._analogResolution)
64  return signInteger(value, self._analogResolution)
65 
66 
68  def __init__(self, slave=0x48):
69  ADS1X1X.__init__(self, slave, 1, 12, "ADS1014")
70 
72  def __init__(self, slave=0x48):
73  ADS1X1X.__init__(self, slave, 4, 12, "ADS1015")
74 
76  def __init__(self, slave=0x48):
77  ADS1X1X.__init__(self, slave, 1, 16, "ADS1114")
78 
80  def __init__(self, slave=0x48):
81  ADS1X1X.__init__(self, slave, 4, 16, "ADS1115")
82