LCOV - code coverage report
Current view: top level - src/Hardware/ExtendedInputOutput - ExtendedInputOutput.cpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 35 52 67.3 %
Date: 2019-11-24 14:50:27 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "ExtendedInputOutput.hpp"
       2             : #include "ExtendedIOElement.hpp"
       3             : #include <Arduino.h>
       4             : #include <Helpers/Error.hpp>
       5             : 
       6             : BEGIN_CS_NAMESPACE
       7             : 
       8             : namespace ExtIO {
       9             : 
      10             : template <class T>
      11          24 : bool inRange(T target, T start, T end) {
      12          24 :     return target >= start && target < end;
      13             : }
      14             : 
      15          20 : ExtendedIOElement &getIOElementOfPin(pin_t pin) {
      16          26 :     for (ExtendedIOElement &el : ExtendedIOElement::getAll())
      17          25 :         if (pin < el.getStart())
      18           1 :             break;
      19          24 :         else if (inRange(pin, el.getStart(), el.getEnd()))
      20          18 :             return el;
      21             : 
      22           2 :     FATAL_ERROR(
      23             :         F("The given pin does not correspond to an Extended IO element."),
      24             :         0x8888);
      25             : 
      26             :     // TODO: why doesn't this give a compilation error?
      27             :     // No return statement. On desktop, FATAL_ERROR throws an exception, so
      28             :     // I get why that works, but on Arduino, it just calls fatalErrorExit, which
      29             :     // is marked 'noreturn'. However, if I remove the 'noreturn' attribute, and
      30             :     // have it return immediately, it still compiles, without returning a valid
      31             :     // reference.
      32           2 : }
      33             : 
      34         160 : void pinMode(pin_t pin, uint8_t mode) {
      35             :     // DEBUGFN(DEBUGVAR(pin) << '\t' << DEBUGVAR(mode));
      36         160 :     if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      37         159 :         ::pinMode(pin, mode);
      38         159 :     } else {
      39           1 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      40           1 :         el.pinMode(pin - el.getStart(), mode);
      41             :     }
      42         160 : }
      43           0 : void pinMode(int pin, uint8_t mode) { pinMode((pin_t)pin, mode); }
      44             : 
      45          70 : void digitalWrite(pin_t pin, uint8_t val) {
      46             :     // DEBUGFN(DEBUGVAR(pin) << '\t' << DEBUGVAR(val));
      47          70 :     if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      48          70 :         ::digitalWrite(pin, val);
      49          70 :     } else {
      50           0 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      51           0 :         el.digitalWrite(pin - el.getStart(), val);
      52             :     }
      53          70 : }
      54           0 : void digitalWrite(int pin, uint8_t val) { digitalWrite((pin_t)pin, val); }
      55             : 
      56         252 : int digitalRead(pin_t pin) {
      57         252 :     if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      58         240 :         return ::digitalRead(pin);
      59             :     } else {
      60          12 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      61          12 :         return el.digitalRead(pin - el.getStart());
      62             :     }
      63             :     return 0;
      64         250 : }
      65           5 : int digitalRead(int pin) { return digitalRead((pin_t)pin); }
      66             : 
      67           0 : void shiftOut(pin_t dataPin, pin_t clockPin, uint8_t bitOrder, uint8_t val) {
      68             :     uint8_t i;
      69             : 
      70           0 :     for (i = 0; i < 8; i++) {
      71           0 :         if (bitOrder == LSBFIRST)
      72           0 :             digitalWrite(dataPin, !!(val & (1 << i)));
      73             :         else
      74           0 :             digitalWrite(dataPin, !!(val & (1 << (7 - i))));
      75             : 
      76           0 :         digitalWrite(clockPin, HIGH);
      77           0 :         digitalWrite(clockPin, LOW);
      78           0 :     }
      79           0 : }
      80           0 : void shiftOut(int dataPin, int clockPin, uint8_t bitOrder, uint8_t val) {
      81           0 :     shiftOut((pin_t)dataPin, (pin_t)clockPin, bitOrder, val);
      82           0 : }
      83             : 
      84          39 : analog_t analogRead(pin_t pin) {
      85          39 :     if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      86          32 :         return ::analogRead(pin);
      87             :     } else {
      88           7 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      89           7 :         return el.analogRead(pin - el.getStart());
      90             :     }
      91             :     return 0;
      92          39 : }
      93           0 : analog_t analogRead(int pin) { return analogRead((pin_t)pin); }
      94             : 
      95             : } // namespace ExtIO
      96             : 
      97             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6