/ adafruit_mcp3xxx / mcp3008.py
mcp3008.py
 1  # The MIT License (MIT)
 2  #
 3  # Copyright (c) 2018 Brent Rubell for Adafruit
 4  #
 5  # Permission is hereby granted, free of charge, to any person obtaining a copy
 6  # of this software and associated documentation files (the "Software"), to deal
 7  # in the Software without restriction, including without limitation the rights
 8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9  # copies of the Software, and to permit persons to whom the Software is
10  # furnished to do so, subject to the following conditions:
11  #
12  # The above copyright notice and this permission notice shall be included in
13  # all copies or substantial portions of the Software.
14  #
15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  # THE SOFTWARE.
22  """
23  :py:class:`~adafruit_mcp3xxx.mcp3008.MCP3008`
24  =============================================================
25  MCP3008 8-channel, 10-bit, analog-to-digital
26  converter instance.
27  
28  * Author(s): Brent Rubell
29  
30  For proper wiring, please refer to the `Package Types diagram
31  <https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf#page=1>`_ and `Pin Description section
32  <https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf#G1.1035093>`_ of the MCP3004/MCP3008
33  datasheet.
34  """
35  
36  from .mcp3xxx import MCP3xxx
37  
38  # MCP3008 Pin Mapping
39  P0 = 0
40  P1 = 1
41  P2 = 2
42  P3 = 3
43  P4 = 4
44  P5 = 5
45  P6 = 6
46  P7 = 7
47  
48  
49  class MCP3008(MCP3xxx):
50      """
51      MCP3008 Differential channel mapping. The following list of available differential readings
52      takes the form ``(positive_pin, negative_pin) = (channel A) - (channel B)``.
53  
54      - (P0, P1) = CH0 - CH1
55      - (P1, P0) = CH1 - CH0
56      - (P2, P3) = CH2 - CH3
57      - (P3, P2) = CH3 - CH2
58      - (P4, P5) = CH4 - CH5
59      - (P5, P4) = CH5 - CH4
60      - (P6, P7) = CH6 - CH7
61      - (P7, P6) = CH7 - CH6
62  
63      See also the warning in the `AnalogIn`_ class API.
64      """
65  
66      DIFF_PINS = {
67          (0, 1): P0,
68          (1, 0): P1,
69          (2, 3): P2,
70          (3, 2): P3,
71          (4, 5): P4,
72          (5, 4): P5,
73          (6, 7): P6,
74          (7, 6): P7,
75      }
76  
77      def __init__(self, spi_bus, cs, ref_voltage=3.3):
78          super(MCP3008, self).__init__(spi_bus, cs, ref_voltage=ref_voltage)
79          self._out_buf[0] = 0x01