Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
advri2c.py
Go to the documentation of this file.
1 # Copyright 2015 Andreas Riegg - t-h-i-n-x.net
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 # Changelog
16 #
17 # 0.9 2015-10-08 Initial release.
18 # 1.0 2015-10-09 I2C default address fixed. Comments and arbitrary
19 # channel read added.
20 # 1.1 2015-10-16 Added more chips of same vendor with 1 and 2 channels.
21 # 1.2 2015-11-11 Renamed filename and classes. Chips that provide a dual
22 # interface have an "...I" at the end of the class name.
23 # 1.3 2015-20-11 Aligned the comments with advrspi.py
24 #
25 # Config parameters
26 #
27 # - slave 8 bit Value of the I2C slave address, valid values
28 # are in the range 0x2C to 0x2F (2 address bits) for most
29 # chips, exceptions apply.
30 #
31 # - vref Float Value of the analog reference voltage. Inherited
32 # from ADC abstraction but has no real meaning here.
33 #
34 # Usage remarks
35 #
36 # - Some chips have a selectable I2C and SPI interface. To use the I2C
37 # interface, their class name has a "...I" at the end.
38 #
39 # - Some chips have a fixed slave address. For them, the slave parameter
40 # is not available. For most others, it defaults to 0x2C.
41 #
42 # Implementation remarks
43 #
44 # - This driver supports only the I2C interface of the chips.
45 #
46 # - This driver does currently not support the digital output pins
47 # (O1, some have also O2) that are available in I2C mode for some chips.
48 #
49 # - This driver does currently not support the software reset (RS) and
50 # shutdown (SD) command bits that are available in I2C mode for some chips.
51 #
52 
53 
54 from webiopi.utils.types import toint
55 from webiopi.devices.i2c import I2C
56 from webiopi.devices.analog import DAC
57 
58 
59 class ADVRI2C(DAC, I2C):
60 
61 #---------- Abstraction framework contracts ----------
62 
63  def __str__(self):
64  return "%s(slave=0x%02X)" % (self.name, self.slave)
65 
66 
68 
69 #---------- Class initialisation ----------
70 
71  def __init__(self, slave, vref, channelCount, resolution, name):
72  I2C.__init__(self, toint(slave))
73  DAC.__init__(self, channelCount, resolution, float(vref))
74  self.name = name
75 
76 #---------- ADC abstraction related methods ----------
77 
78  def __analogRead__(self, channel, diff=False):
79  d = (channel << self.CHANNEL_OFFSET) & self.CHANNEL_MASK
80  self.writeByte(d)
81  return self.readByte()
82 
83 #---------- DAC abstraction related methods ----------
84 
85  def __analogWrite__(self, channel, value):
86  d = bytearray(2)
87  d[0] = (channel << self.CHANNEL_OFFSET) & self.CHANNEL_MASK
88  d[1] = value & 0xFF
89  self.writeBytes(d)
90 
91 
93 
94 #---------- Constants and definitons ----------
95 
96  CHANNEL_MASK = 0b10000000
97  CHANNEL_OFFSET = 7
98 
99 #---------- Class initialisation ----------
100 
101  def __init__(self, slave=0x2C, vref=5.0):
102  ADVRI2CMULTI.__init__(self, slave, vref, 2, 8, "AD5242")
103 
104 
106 
107 #---------- Constants and definitons ----------
108 
109  CHANNEL_MASK = 0b10000000
110  CHANNEL_OFFSET = 7
111 
112 #---------- Class initialisation ----------
113 
114  def __init__(self, vref=5.0):
115  ADVRI2CMULTI.__init__(self, 0x2F, vref, 2, 8, "AD5243")
116 
117 
119 
120 #---------- Constants and definitons ----------
121 
122  CHANNEL_MASK = 0b10000000
123  CHANNEL_OFFSET = 7
124 
125 #---------- Class initialisation ----------
126 
127  def __init__(self, slave=0x2C, vref=5.0):
128  ADVRI2CMULTI.__init__(self, slave, vref, 2, 8, "AD5248")
129 
130 
132 
133 #---------- Constants and definitons ----------
134 
135  CHANNEL_MASK = 0b01100000
136  CHANNEL_OFFSET = 5
137 
138 #---------- Class initialisation ----------
139 
140  def __init__(self, slave=0x2C, vref=5.0):
141  ADVRI2CMULTI.__init__(self, slave, vref, 4, 8, "AD5263I")
142 
143 
145 
146 #---------- Constants and definitons ----------
147 
148  CHANNEL_MASK = 0b10000000
149  CHANNEL_OFFSET = 7
150 
151 #---------- Class initialisation ----------
152 
153  def __init__(self, slave=0x2C, vref=5.0):
154  ADVRI2CMULTI.__init__(self, slave, vref, 2, 8, "AD5282")
155 
156 
158 
159 #---------- Class initialisation ----------
160 
161  def __init__(self, slave, vref, resolution, name):
162  I2C.__init__(self, toint(slave))
163  DAC.__init__(self, 1, resolution, float(vref))
164  self.name = name
165 
166 #---------- ADC abstraction related methods ----------
167 
168  def __analogRead__(self, channel, diff=False):
169  return self.readByte()
170 
171 #---------- DAC abstraction related methods ----------
172 
173  def __analogWrite__(self, channel, value):
174  d = bytearray(2)
175  d[0] = 0x00
176  d[1] = value & 0xFF
177  self.writeBytes(d)
178 
179 
181 
182 #---------- Class initialisation ----------
183 
184  def __init__(self, slave=0x2C, vref=5.0):
185  ADVRI2CSINGLE.__init__(self, slave, vref, 8, "AD5161I")
186 
187 
189 
190 #---------- Class initialisation ----------
191 
192  def __init__(self, slave=0x2C, vref=5.0):
193  ADVRI2CSINGLE.__init__(self, slave, vref, 8, "AD5241")
194 
195 
197 
198 #---------- Class initialisation ----------
199 
200  def __init__(self, slave=0x2C, vref=5.0):
201  ADVRI2CSINGLE.__init__(self, slave, vref, 8, "AD5245")
202 
203 
205 
206 #---------- Class initialisation ----------
207 
208  def __init__(self, slave=0x2C, vref=5.0):
209  ADVRI2CSINGLE.__init__(self, slave, vref, 8, "AD5280")
210 
211 
213 
214 #---------- Class initialisation ----------
215 
216  def __init__(self, slave, vref, resolution, name):
217  ADVRI2CSINGLE.__init__(self, slave, vref, resolution, name)
218 
219 #---------- DAC abstraction related methods ----------
220 
221  def __analogWrite__(self, channel, value):
222  self.writeByte(value & 0xFF)
223 
224 
226 
227 #---------- Class initialisation ----------
228 
229  def __init__(self, vref=5.0):
230  ADVRI2CSIMPLE.__init__(self, 0x2E, vref, 7, "AD5246")
231 
233 
234 #---------- Class initialisation ----------
235 
236  def __init__(self, slave=0x2E, vref=5.0):
237  ADVRI2CSIMPLE.__init__(self, slave, vref, 7, "AD5247")