LCOV - code coverage report
Current view: top level - src/Hardware/ExtendedInputOutput - AnalogMultiplex.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 41 46 89.1 %
Date: 2019-11-24 14:50:27 Functions: 16 26 61.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* ✔ */
       2             : 
       3             : #pragma once
       4             : 
       5             : #include "ExtendedInputOutput.hpp"
       6             : #include "StaticSizeExtendedIOElement.hpp"
       7             : #include <Helpers/Array.hpp>
       8             : #include <stdlib.h>
       9             : 
      10             : BEGIN_CS_NAMESPACE
      11             : 
      12             : /**
      13             :  * @brief   A class for reading multiplexed analog inputs.
      14             :  *          Supports 74HC4067, 74HC4051, etc.
      15             :  * 
      16             :  * You can use many multiplexers on the same address lines if each of the 
      17             :  * multiplexers has a different enable line.
      18             :  * 
      19             :  * @tparam  N 
      20             :  *          The number of address lines.
      21             :  * 
      22             :  * @ingroup ExtIO
      23             :  */
      24             : template <uint8_t N>
      25           6 : class AnalogMultiplex : public StaticSizeExtendedIOElement<1 << N> {
      26             :   public:
      27             :     /**
      28             :      * @brief   Create a new AnalogMultiplex object on the given pins.
      29             :      * 
      30             :      * @param   analogPin
      31             :      *          The analog input pin connected to the output of the multiplexer.
      32             :      * @param   addressPins
      33             :      *          An array of the pins connected to the address lines of the
      34             :      *          multiplexer. (Labeled S0, S1, S2 ... in the datasheet.)
      35             :      * @param   enablePin
      36             :      *          The digital output pin connected to the enable pin of the
      37             :      *          multiplexer. (Labeled Ē in the datasheet.)  
      38             :      *          If you don't need the enable pin, you can use NO_PIN, which is 
      39             :      *          the default.
      40             :      */
      41           6 :     AnalogMultiplex(pin_t analogPin, const Array<pin_t, N> &addressPins,
      42             :                     pin_t enablePin = NO_PIN)
      43          12 :         : analogPin(analogPin), addressPins(addressPins), enablePin(enablePin) {
      44           6 :     }
      45             : 
      46             :     /**
      47             :      * @brief   Set the pin mode of the analog input pin.  
      48             :      *          This allows you to enable the internal pull-up resistor, for
      49             :      *          use with buttons or open-collector outputs.
      50             :      * 
      51             :      * @note    This applies to all pins of this multiplexer.  
      52             :      *          This affects all pins of the multiplexer, because it has only
      53             :      *          a single common pin.  
      54             :      * 
      55             :      * @param   pin
      56             :      *          (Unused)
      57             :      * @param   mode
      58             :      *          The new mode of the input pin: 
      59             :      *          either INPUT or INPUT_PULLUP.
      60             :      */
      61             :     void pinMode(UNUSED_PARAM pin_t pin, uint8_t mode) override;
      62             : 
      63             :     /**
      64             :      * @brief   The digitalWrite function is not implemented because writing an
      65             :      *          output to a multiplexer is not useful.
      66             :      */
      67           0 :     void digitalWrite(UNUSED_PARAM pin_t pin, UNUSED_PARAM uint8_t val) override
      68           0 :         __attribute__((deprecated)) {}
      69             : 
      70             :     /**
      71             :      * @brief   Read the digital state of the given input.
      72             :      * 
      73             :      * @param   pin
      74             :      *          The multiplexer's pin number to read from.
      75             :      */
      76             :     int digitalRead(pin_t pin) override;
      77             : 
      78             :     /**
      79             :      * @brief   Read the analog value of the given input.
      80             :      * 
      81             :      * @param   pin
      82             :      *          The multiplexer's pin number to read from.
      83             :      */
      84             :     analog_t analogRead(pin_t pin) override;
      85             : 
      86             :     /**
      87             :      * @brief   The analogWrite function is not implemented because writing an
      88             :      *          output to a multiplexer is not useful.
      89             :      */
      90           0 :     void analogWrite(UNUSED_PARAM pin_t pin, UNUSED_PARAM analog_t val) override
      91           0 :         __attribute__((deprecated)) {}
      92             : 
      93             :     /**
      94             :      * @brief   Initialize the multiplexer: set the pin mode of the address pins
      95             :      *          and the enable pin to output mode.
      96             :      */
      97             :     void begin() override;
      98             : 
      99             :     /**
     100             :      * @brief   No periodic updating of the state is necessary, all actions are 
     101             :      *          carried out when the user calls analogRead or digitalRead.
     102             :      */
     103           0 :     void update() override {}
     104             : 
     105             :   private:
     106             :     const pin_t analogPin;
     107             :     const Array<pin_t, N> addressPins;
     108             :     const pin_t enablePin;
     109             : 
     110             :     /**
     111             :      * @brief   Write the pin number/address to the address pins of the 
     112             :      *          multiplexer.
     113             :      * 
     114             :      * @param   address
     115             :      *          The address to select.
     116             :      */
     117             :     void setMuxAddress(uint8_t address);
     118             : 
     119             :     /**
     120             :      * @brief   Select the correct address and enable the multiplexer.
     121             :      * 
     122             :      * @param   address
     123             :      *          The address to select.
     124             :      */
     125             :     void prepareReading(uint8_t address);
     126             : 
     127             :     /**
     128             :      * @brief   Disable the multiplexer.
     129             :      */
     130             :     void afterReading();
     131             : 
     132             :     // The enable pin is active low.
     133             :     constexpr static uint8_t MUX_ENABLED = LOW;
     134             :     constexpr static uint8_t MUX_DISABLED = HIGH;
     135             : };
     136             : 
     137             : /**
     138             :  * @brief   An alias for AnalogMultiplex<4> to use with CD74HC4067 analog 
     139             :  *          multiplexers.
     140             :  * 
     141             :  * @ingroup ExtIO
     142             :  */
     143             : using CD74HC4067 = AnalogMultiplex<4>;
     144             : 
     145             : /**
     146             :  * @brief   An alias for AnalogMultiplex<3> to use with CD74HC4051 analog 
     147             :  *          multiplexers.
     148             :  * 
     149             :  * @ingroup ExtIO
     150             :  */
     151             : using CD74HC4051 = AnalogMultiplex<3>;
     152             : 
     153             : // -------------------------------------------------------------------------- //
     154             : 
     155             : template <uint8_t N>
     156           1 : void AnalogMultiplex<N>::pinMode(pin_t, uint8_t mode) {
     157           1 :     ExtIO::pinMode(analogPin, mode);
     158           1 : }
     159             : 
     160             : template <uint8_t N>
     161           2 : int AnalogMultiplex<N>::digitalRead(pin_t pin) {
     162           2 :     prepareReading(pin);
     163           2 :     int result = ExtIO::digitalRead(analogPin);
     164           2 :     afterReading();
     165           2 :     return result;
     166             : }
     167             : 
     168             : template <uint8_t N>
     169           7 : analog_t AnalogMultiplex<N>::analogRead(pin_t pin) {
     170           7 :     prepareReading(pin);
     171           7 :     ExtIO::analogRead(analogPin); // Discard first reading
     172           7 :     analog_t result = ExtIO::analogRead(analogPin);
     173           7 :     afterReading();
     174           7 :     return result;
     175             : }
     176             : 
     177             : template <uint8_t N>
     178           6 : void AnalogMultiplex<N>::begin() {
     179          29 :     for (const pin_t &addressPin : addressPins)
     180          23 :         ExtIO::pinMode(addressPin, OUTPUT);
     181           6 :     if (enablePin != NO_PIN) {
     182           4 :         ExtIO::pinMode(enablePin, OUTPUT);
     183           4 :         ExtIO::digitalWrite(enablePin, MUX_DISABLED);
     184           4 :     }
     185           6 : }
     186             : 
     187             : template <uint8_t N>
     188           9 : void AnalogMultiplex<N>::setMuxAddress(uint8_t address) {
     189           9 :     uint8_t mask = 1;
     190          42 :     for (const pin_t &addressPin : addressPins) {
     191          33 :         ExtIO::digitalWrite(addressPin, (address & mask) != 0);
     192          33 :         mask <<= 1;
     193             :     }
     194             : #if !defined(__AVR__) && !defined(__x86_64__)
     195             :     delayMicroseconds(5);
     196             : #endif
     197           9 : }
     198             : 
     199             : template <uint8_t N>
     200           9 : void AnalogMultiplex<N>::prepareReading(uint8_t address) {
     201           9 :     setMuxAddress(address);
     202           9 :     if (enablePin != NO_PIN)
     203           5 :         ExtIO::digitalWrite(enablePin, MUX_ENABLED);
     204           9 : }
     205             : 
     206             : template <uint8_t N>
     207           9 : void AnalogMultiplex<N>::afterReading() {
     208           9 :     if (enablePin != NO_PIN)
     209           5 :         ExtIO::digitalWrite(enablePin, MUX_DISABLED);
     210           9 : }
     211             : 
     212             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6