|
44 | 44 |
|
45 | 45 | OUT = const(0)
|
46 | 46 |
|
| 47 | + |
47 | 48 | class PCF8591:
|
48 | 49 | """Driver for the PCF8591 DAC & ADC Combo breakout.
|
49 | 50 |
|
@@ -72,38 +73,31 @@ def reference_voltage(self):
|
72 | 73 | An ADC value of 65535 will equal `reference_voltage`"""
|
73 | 74 | return self._reference_voltage
|
74 | 75 |
|
75 |
| - def read(self, channel): |
76 |
| - """Read an analog value from one of the four ADC inputs |
77 |
| -
|
78 |
| - param: :adcnum The single-ended ADC to read from, 0 thru 3 |
79 |
| - """ |
| 76 | + def _half_read(self, channel): |
80 | 77 | if self._dac_enabled:
|
81 | 78 | self._buffer[0] = _PCF8591_ENABLE_DAC
|
82 | 79 | self._buffer[1] = self._dacval
|
83 | 80 | else:
|
84 | 81 | self._buffer[0] = 0
|
85 | 82 | self._buffer[1] = 0
|
86 | 83 |
|
87 |
| - if channel < 0 or channel > 3: |
88 |
| - raise ValueError("channel must be from 0-3") |
89 |
| - |
90 | 84 | self._buffer[0] |= channel & 0x3
|
91 | 85 |
|
92 | 86 | with self.i2c_device as i2c:
|
93 | 87 | i2c.write_then_readinto(self._buffer, self._buffer)
|
94 | 88 |
|
95 |
| - if self._dac_enabled: |
96 |
| - self._buffer[0] = _PCF8591_ENABLE_DAC |
97 |
| - self._buffer[1] = self._dacval |
98 |
| - else: |
99 |
| - self._buffer[0] = 0 |
100 |
| - self._buffer[1] = 0 |
101 |
| - self._buffer[0] |= channel & 0x3 |
102 |
| - |
103 |
| - # final read before render |
| 89 | + def read(self, channel): |
| 90 | + """Read an analog value from one of the four ADC inputs |
104 | 91 |
|
105 |
| - with self.i2c_device as i2c: |
106 |
| - i2c.write_then_readinto(self._buffer, self._buffer) |
| 92 | + param: :channel The single-ended ADC channel to read from, 0 thru 3 |
| 93 | + """ |
| 94 | + if channel < 0 or channel > 3: |
| 95 | + raise ValueError("channel must be from 0-3") |
| 96 | + # reads are started on the ACK of the WRITE to the 'register' and |
| 97 | + # not returned until the read after the _next_ WRITE so we have to |
| 98 | + # do it twice to get the actual value |
| 99 | + self._half_read(channel) |
| 100 | + self._half_read(channel) |
107 | 101 |
|
108 | 102 | return unpack_from(">B", self._buffer[1:])[0]
|
109 | 103 |
|
|
0 commit comments