|
| 1 | +# SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +:py:class:`~adafruit_mcp3xxx.mcp3204.MCP3204` |
| 7 | +================================================ |
| 8 | +MCP3204 4-channel, 12-bit, analog-to-digital |
| 9 | +converter instance. |
| 10 | +
|
| 11 | +* Author(s): Brent Rubell, Kevin J. Walters |
| 12 | +
|
| 13 | +For proper wiring, please refer to `Package Types diagram |
| 14 | +<https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/21298e.pdf>`_ |
| 15 | +and `Pin Description section |
| 16 | +<https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/21298e.pdf#G1.1041174>`_ |
| 17 | +of the MCP3204/MCP3208 datasheet. |
| 18 | +""" |
| 19 | + |
| 20 | +from .mcp3xxx import MCP3xxx |
| 21 | + |
| 22 | +# MCP3204 Pin Mapping |
| 23 | +P0 = 0 |
| 24 | +P1 = 1 |
| 25 | +P2 = 2 |
| 26 | +P3 = 3 |
| 27 | + |
| 28 | + |
| 29 | +class MCP3204(MCP3xxx): |
| 30 | + """ |
| 31 | + MCP3204 Differential channel mapping. The following list of available differential readings |
| 32 | + takes the form ``(positive_pin, negative_pin) = (channel A) - (channel B)``. |
| 33 | +
|
| 34 | + - (P0, P1) = CH0 - CH1 |
| 35 | + - (P1, P0) = CH1 - CH0 |
| 36 | + - (P2, P3) = CH2 - CH3 |
| 37 | + - (P3, P2) = CH3 - CH2 |
| 38 | +
|
| 39 | + See also the warning in the `AnalogIn`_ class API. |
| 40 | + """ |
| 41 | + |
| 42 | + BITS = 12 |
| 43 | + DIFF_PINS = {(0, 1): P0, (1, 0): P1, (2, 3): P2, (3, 2): P3} |
| 44 | + |
| 45 | + def read(self, pin: int, is_differential: bool = False) -> int: |
| 46 | + """SPI Interface for MCP3xxx-based ADCs reads. Due to 10-bit accuracy, the returned |
| 47 | + value ranges [0, 1023]. |
| 48 | +
|
| 49 | + :param int pin: individual or differential pin. |
| 50 | + :param bool is_differential: single-ended or differential read. |
| 51 | +
|
| 52 | + .. note:: This library offers a helper class called `AnalogIn`_ for both single-ended |
| 53 | + and differential reads. If you opt to not implement `AnalogIn`_ during differential |
| 54 | + reads, then the ``pin`` parameter should be the first of the two pins associated with |
| 55 | + the desired differential channel mapping. |
| 56 | + """ |
| 57 | + self._out_buf[0] = 0x04 | ((not is_differential) << 1) | (pin >> 2) |
| 58 | + self._out_buf[1] = (pin & 0x03) << 6 |
| 59 | + with self._spi_device as spi: |
| 60 | + # pylint: disable=no-member |
| 61 | + spi.write_readinto(self._out_buf, self._in_buf) |
| 62 | + return ((self._in_buf[1] & 0x0F) << 8) | self._in_buf[2] |
0 commit comments