Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
advrspi.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-11-11 Initial release. Works only for AD5263.
18 # 1.0 2015-11-20 Added many other ADVR SPI interface chips.
19 #
20 # Config parameters
21 #
22 # - chip Integer Value of the SPI CS address, valid values
23 # are 0 and 1.
24 #
25 # - vref Float Value of the analog reference voltage. Inherited
26 # from ADC abstraction but has no real meaning here.
27 #
28 # Usage remarks
29 #
30 # - Some chips have a selectable I2C and SPI interface. To use the SPI
31 # interface, their class name has a "...S" at the end.
32 #
33 # Implementation remarks
34 #
35 # - This driver supports only the SPI interface of the chips.
36 #
37 # - As the SPI interface of all chips is write-only and does not allow to
38 # to read back the register values, those are cached in the driver to still
39 # allow reading them.
40 #
41 #
42 
43 from webiopi.utils.types import toint
44 from webiopi.devices.spi import SPI
45 from webiopi.devices.analog import DAC
46 
47 
48 class ADVRSPI(DAC, SPI):
49 
50 #---------- Abstraction framework contracts ----------
51 
52  def __str__(self):
53  return "%s(chip=%d)" % (self.name, self.chip)
54 
55 
57 
58 #---------- Class initialisation ----------
59 
60  def __init__(self, chip, vref, channelCount, resolution, name):
61  SPI.__init__(self, toint(chip), 0, 8, 10000000)
62  DAC.__init__(self, channelCount, resolution, float(vref))
63  self.name = name
64  self.values = [0 for i in range(channelCount)]
65 
66 #---------- ADC abstraction related methods ----------
67 
68  def __analogRead__(self, channel, diff=False):
69  return self.values[channel]
70 
71 #---------- DAC abstraction related methods ----------
72 
73  def __analogWrite__(self, channel, value):
74  d = bytearray(2)
75  d[0] = channel & self.CHANNEL_MASK
76  d[1] = value & 0xFF
77  self.writeBytes(d)
78  self.values[channel] = value
79 
80 
82 
83 #---------- Constants and definitons ----------
84 
85  CHANNEL_MASK = 0b00000001
86 
87 #---------- Class initialisation ----------
88 
89  def __init__(self, chip=0, vref=5.0):
90  ADVRSPIMULTI.__init__(self, chip, vref, 2, 8, "AD5162")
91 
92 
94 
95 #---------- Constants and definitons ----------
96 
97  CHANNEL_MASK = 0b00000111
98 
99 #---------- Class initialisation ----------
100 
101  def __init__(self, chip=0, vref=5.0):
102  ADVRSPIMULTI.__init__(self, chip, vref, 4, 8, "AD5204")
103 
104 
106 
107 #---------- Constants and definitons ----------
108 
109  CHANNEL_MASK = 0b00000111
110 
111 #---------- Class initialisation ----------
112 
113  def __init__(self, chip=0, vref=5.0):
114  ADVRSPIMULTI.__init__(self, chip, vref, 6, 8, "AD5206")
115 
116 
118 
119 #---------- Constants and definitons ----------
120 
121  CHANNEL_MASK = 0b00000011
122 
123 #---------- Class initialisation ----------
124 
125  def __init__(self, chip=0, vref=5.0):
126  ADVRSPIMULTI.__init__(self, chip, vref, 4, 8, "AD5263S")
127 
128 
130 # Special case, is a single channel chip but still does expect 2 address bits.
131 
132 #---------- Constants and definitons ----------
133 
134  CHANNEL_MASK = 0b00000011
135 
136 #---------- Class initialisation ----------
137 
138  def __init__(self, chip=0, vref=5.0):
139  ADVRSPIMULTI.__init__(self, chip, vref, 1, 8, "AD8400")
140 
141 
143 # Special case, is a dual channel chip but still does expect 2 address bits.
144 
145 #---------- Constants and definitons ----------
146 
147  CHANNEL_MASK = 0b00000011
148 
149 #---------- Class initialisation ----------
150 
151  def __init__(self, chip=0, vref=5.0):
152  ADVRSPIMULTI.__init__(self, chip, vref, 2, 8, "AD8402")
153 
154 
156 
157 #---------- Constants and definitons ----------
158 
159  CHANNEL_MASK = 0b00000011
160 
161 #---------- Class initialisation ----------
162 
163  def __init__(self, chip=0, vref=5.0):
164  ADVRSPIMULTI.__init__(self, chip, vref, 4, 8, "AD8403")
165 
166 
168 
169 #---------- Class initialisation ----------
170 
171  def __init__(self, chip, vref, resolution, name):
172  SPI.__init__(self, toint(chip), 0, 8, 10000000)
173  DAC.__init__(self, 1, resolution, float(vref))
174  self.name = name
175  self.value = 0
176 
177 #---------- ADC abstraction related methods ----------
178 
179  def __analogRead__(self, channel, diff=False):
180  return self.value
181 
182 #---------- DAC abstraction related methods ----------
183 
184  def __analogWrite__(self, channel, value):
185  self.writeByte(value & 0xFF)
186  self.value = value
187 
188 
190 
191 #---------- Class initialisation ----------
192 
193  def __init__(self, chip=0, vref=5.0):
194  ADVRSPISINGLE.__init__(self, chip, vref, 8, "AD5160")
195 
196 
198 
199 #---------- Class initialisation ----------
200 
201  def __init__(self, chip=0, vref=5.0):
202  ADVRSPISINGLE.__init__(self, chip, vref, 8, "AD5161S")
203 
204 
206 
207 #---------- Class initialisation ----------
208 
209  def __init__(self, chip=0, vref=5.0):
210  ADVRSPISINGLE.__init__(self, chip, vref, 8, "AD5165")
211 
212 
214 
215 #---------- Class initialisation ----------
216 
217  def __init__(self, chip=0, vref=5.0):
218  ADVRSPISINGLE.__init__(self, chip, vref, 8, "AD5200")
219 
220 
222 
223 #---------- Class initialisation ----------
224 
225  def __init__(self, chip=0, vref=5.0):
226  ADVRSPISINGLE.__init__(self, chip, vref, 6, "AD5201")
227 
228 
230 
231 #---------- Class initialisation ----------
232 
233  def __init__(self, chip=0, vref=5.0):
234  ADVRSPISINGLE.__init__(self, chip, vref, 8, "AD5290")
235 
236 
237 
238 
239