Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
advrspidc.py
Go to the documentation of this file.
1 # Copyright 2016 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 2016-01-30 Initial release.
18 #
19 # Config parameters
20 #
21 # - chip Integer Value of the SPI CS address, valid values
22 # are 0 and 1. Default value is 0.
23 #
24 # - vref Float Value of the analog reference voltage. Inherited
25 # from ADC abstraction but has no real meaning here.
26 # Used default value is 5.0.
27 #
28 # - chips Integer Number of chips in the daisy chain, valid values
29 # are 1 and above. Default value is 1.
30 #
31 #
32 # Usage remarks
33 #
34 # - The specified parameter "chips" must exactly match the number of chips
35 # in the chain.
36 #
37 # - This driver supports only identical type chips in a single chain
38 #
39 # Implementation remarks
40 #
41 # - This driver supports only the SPI interface of the chips.
42 #
43 # - This driver only works for "daisy-chained" SPI chips. See the chips spec
44 # how the wiring should look like.
45 #
46 # - As the SPI interface of all ADxxxx chips is write-only and does not allow to
47 # to read back the register values, those are cached in the driver to still
48 # allow reading them.
49 #
50 #
51 
52 from webiopi.utils.types import toint
53 from webiopi.devices.spi import SPI
54 from webiopi.devices.analog import DAC
55 from webiopi.utils.logger import debug, printBytes
56 
57 class ADVRSPIDC(DAC, SPI):
58 
59 #---------- Class initialisation ----------
60 
61  def __init__(self, chip, vref, channelCount, resolution, name):
62  SPI.__init__(self, toint(chip), 0, 8, 10000000)
63  DAC.__init__(self, toint(channelCount), toint(resolution), float(vref))
64  self.name = name
65  self.values = [0 for i in range(toint(channelCount))]
66 
67 #---------- Abstraction framework contracts ----------
68 
69  def __str__(self):
70  return "%s(chip=%d)(chips=%d)(slice=%d)" % (self.name, self.chip, self.chips, self.slice)
71 
72 #---------- ADC abstraction related methods ----------
73 
74  def __analogRead__(self, channel, diff=False):
75  return self.values[channel]
76 
77 
79 
80 #---------- Class initialisation ----------
81 
82  def __init__(self, chip, vref, channelCount, resolution, name, chipsCount):
83  ADVRSPIDC.__init__(self, chip, vref, channelCount*chipsCount, resolution, name)
84  self.chips = chipsCount
85  self.slice = channelCount
86 
87 #---------- DAC abstraction related methods ----------
88 
89  def __analogWrite__(self, channel, value):
90  #printBytes(self.values)
91 
92  self.values[channel] = value & 0xFF
93  #printBytes(self.values)
94 
95  chipAddr = channel%self.slice
96  addressString = (bin(chipAddr)[2:]).rjust(self.ADDRESS_BITS,'0')
97  debug("Address=%s" % addressString)
98 
99  slotValues = self.values[chipAddr::self.slice]
100  #printBytes(slotValues)
101  slotValues.reverse()
102  printBytes(slotValues)
103 
104  unpaddedNumberBits = (self.ADDRESS_BITS + 8) * self.chips
105  debug("Unpadlength=%s" % unpaddedNumberBits)
106 
107  lastBit = (unpaddedNumberBits % 8)
108  if lastBit > 0:
109  padLength = 8 - lastBit
110  else:
111  padLength = 0
112  debug("Padlength=%s" % padLength)
113 
114  padString = "".rjust(padLength,'0')
115  debug("Padding=%s" % padString)
116 
117  for i in range(len(slotValues)):
118  slotValues[i] = (bin(slotValues[i])[2:]).rjust(8,'0')
119  bitSequence = ""
120  for valueString in slotValues:
121  bitSequence = bitSequence + addressString + valueString
122  bitSequence = padString + bitSequence
123  debug("Bitsequence=%s" % bitSequence)
124 
125  data = []
126  for s in range (0, len(bitSequence), 8):
127  data.append(int(bitSequence[s:s+8], 2))
128  printBytes(data)
129 
130  self.writeBytes(bytearray(data))
131 
132 
134 
135 #---------- Constants and definitons ----------
136 
137  ADDRESS_BITS = 3
138 
139 #---------- Class initialisation ----------
140 
141  def __init__(self, chip=0, vref=5.0, chips=1):
142  ADVRSPIDCMULTI.__init__(self, chip, vref, 4, 8, "AD5204DC", toint(chips))
143 
144 
146 
147 #---------- Constants and definitons ----------
148 
149  ADDRESS_BITS = 2
150 
151 #---------- Class initialisation ----------
152 
153  def __init__(self, chip=0, vref=5.0, chips=1):
154  ADVRSPIDCMULTI.__init__(self, chip, vref, 4, 8, "AD5263DC", toint(chips))
155 
156 
158 
159 #---------- Constants and definitons ----------
160 
161  ADDRESS_BITS = 2
162 
163 #---------- Class initialisation ----------
164 
165  def __init__(self, chip=0, vref=5.0, chips=1):
166  ADVRSPIDCMULTI.__init__(self, chip, vref, 4, 8, "AD8403DC", toint(chips))
167 
168 
170 
171 #---------- Class initialisation ----------
172 
173  def __init__(self, chip, vref, chips, resolution, name):
174  ADVRSPIDC.__init__(self, chip, vref, chips, resolution, name)
175  self.chips = chips
176  self.slice = 1
177 
178 #---------- DAC abstraction related methods ----------
179 
180  def __analogWrite__(self, channel, value):
181  self.values[channel] = value & 0xFF
182  data = bytearray(self.values)
183  #printBytes(data)
184  data.reverse()
185  #printBytes(data)
186  self.writeBytes(data)
187 
188 
190 
191 #---------- Class initialisation ----------
192 
193  def __init__(self, chip=0, vref=5.0, chips=1):
194  ADVRSPIDCSINGLE.__init__(self, chip, vref, toint(chips), 8, "AD5161DC")
195 
196 
198 
199 #---------- Class initialisation ----------
200 
201  def __init__(self, chip=0, vref=5.0, chips=1):
202  ADVRSPIDCSINGLE.__init__(self, chip, vref, toint(chips), 8, "AD5290DC")
203 
204 
205 
206 
207