LCOV - code coverage report
Current view: top level - src/AH/Hardware/ExtendedInputOutput - ExtendedInputOutput.cpp (source / functions) Hit Total Coverage
Test: e224b347cd670555e44f06608ac41bd1ace9d9d8 Lines: 133 133 100.0 %
Date: 2020-09-08 17:44:46 Functions: 28 28 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include <AH/Settings/Warnings.hpp>
       2             : AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
       3             : 
       4             : #include "ExtendedIOElement.hpp"
       5             : #include "ExtendedInputOutput.hpp"
       6             : #include <AH/Error/Error.hpp>
       7             : 
       8             : BEGIN_AH_NAMESPACE
       9             : 
      10             : namespace ExtIO {
      11             : 
      12             : template <class T>
      13         185 : bool inRange(T target, T start, T end) {
      14         185 :     return target >= start && target < end;
      15             : }
      16             : 
      17         164 : ExtendedIOElement &getIOElementOfPin(pin_t pin) {
      18         350 :     for (auto &el : ExtendedIOElement::getAll())
      19         186 :         if (pin < el.getStart())
      20           1 :             break;
      21         185 :         else if (inRange(pin, el.getStart(), el.getEnd()))
      22         186 :             return el;
      23             : 
      24           2 :     FATAL_ERROR(
      25             :         F("The given pin does not correspond to an Extended IO element."),
      26             :         0x8888);
      27             : 
      28             :     // TODO: why doesn't this give a compilation error?
      29             :     // No return statement. On desktop, FATAL_ERROR throws an exception, so
      30             :     // I get why that works, but on Arduino, it just calls fatalErrorExit, which
      31             :     // is marked 'noreturn'. However, if I remove the 'noreturn' attribute, and
      32             :     // have it return immediately, it still compiles, without returning a valid
      33             :     // reference.
      34         164 : }
      35             : 
      36         201 : void pinMode(pin_t pin, PinMode_t mode) {
      37         201 :     if (pin == NO_PIN)
      38             :         return; // LCOV_EXCL_LINE
      39         201 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      40         191 :         ::pinMode(pin, mode);
      41         191 :     } else {
      42          10 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      43          10 :         el.pinMode(pin - el.getStart(), mode);
      44          10 :     }
      45         201 : }
      46           5 : void pinMode(int pin, PinMode_t mode) { pinMode((pin_t)pin, mode); }
      47             : 
      48         225 : void digitalWrite(pin_t pin, PinStatus_t val) {
      49         225 :     if (pin == NO_PIN)
      50             :         return; // LCOV_EXCL_LINE
      51         225 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      52         120 :         ::digitalWrite(pin, val);
      53         120 :     } else {
      54         105 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      55         105 :         el.digitalWrite(pin - el.getStart(), val);
      56         105 :     }
      57         225 : }
      58           5 : void digitalWrite(int pin, PinStatus_t val) { digitalWrite((pin_t)pin, val); }
      59             : 
      60         307 : int digitalRead(pin_t pin) {
      61         307 :     if (pin == NO_PIN)
      62             :         return 0; // LCOV_EXCL_LINE
      63         307 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      64         294 :         return ::digitalRead(pin);
      65             :     } else {
      66          13 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      67          13 :         return el.digitalRead(pin - el.getStart());
      68          13 :     }
      69             :     return 0;
      70         305 : }
      71           5 : int digitalRead(int pin) { return digitalRead((pin_t)pin); }
      72             : 
      73          75 : analog_t analogRead(pin_t pin) {
      74          75 :     if (pin == NO_PIN)
      75             :         return 0; // LCOV_EXCL_LINE
      76          75 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      77          59 :         return ::analogRead(pin);
      78             :     } else {
      79          16 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      80          16 :         return el.analogRead(pin - el.getStart());
      81          16 :     }
      82             :     return 0;
      83          75 : }
      84           5 : analog_t analogRead(int pin) { return analogRead((pin_t)pin); }
      85             : 
      86          16 : void analogWrite(pin_t pin, analog_t val) {
      87          16 :     if (pin == NO_PIN)
      88             :         return; // LCOV_EXCL_LINE
      89          16 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
      90             : #ifndef ESP32
      91           5 :         ::analogWrite(pin, val);
      92             : #endif
      93           5 :     } else {
      94          11 :         ExtendedIOElement &el = getIOElementOfPin(pin);
      95          11 :         el.analogWrite(pin - el.getStart(), val);
      96          11 :     }
      97          16 : }
      98           2 : void analogWrite(int pin, analog_t val) { analogWrite((pin_t)pin, val); }
      99           5 : void analogWrite(int pin, int val) { analogWrite((pin_t)pin, (analog_t)val); }
     100           9 : void analogWrite(pin_t pin, int val) { analogWrite(pin, (analog_t)val); }
     101             : 
     102           3 : void pinModeBuffered(pin_t pin, PinMode_t mode) {
     103           3 :     if (pin == NO_PIN)
     104             :         return; // LCOV_EXCL_LINE
     105           3 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
     106           1 :         ::pinMode(pin, mode);
     107           1 :     } else {
     108           2 :         ExtendedIOElement &el = getIOElementOfPin(pin);
     109           2 :         el.pinModeBuffered(pin - el.getStart(), mode);
     110           2 :     }
     111           3 : }
     112           1 : void pinModeBuffered(int pin, PinMode_t mode) {
     113           1 :     pinModeBuffered((pin_t)pin, mode);
     114           1 : }
     115             : 
     116           2 : void digitalWriteBuffered(pin_t pin, PinStatus_t val) {
     117           2 :     if (pin == NO_PIN)
     118             :         return; // LCOV_EXCL_LINE
     119           2 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
     120           1 :         ::digitalWrite(pin, val);
     121           1 :     } else {
     122           1 :         ExtendedIOElement &el = getIOElementOfPin(pin);
     123           1 :         el.digitalWriteBuffered(pin - el.getStart(), val);
     124           1 :     }
     125           2 : }
     126           1 : void digitalWriteBuffered(int pin, PinStatus_t val) {
     127           1 :     digitalWriteBuffered((pin_t)pin, val);
     128           1 : }
     129             : 
     130           3 : int digitalReadBuffered(pin_t pin) {
     131           3 :     if (pin == NO_PIN)
     132             :         return 0; // LCOV_EXCL_LINE
     133           3 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
     134           1 :         return ::digitalRead(pin);
     135             :     } else {
     136           2 :         ExtendedIOElement &el = getIOElementOfPin(pin);
     137           2 :         return el.digitalReadBuffered(pin - el.getStart());
     138           2 :     }
     139             :     return 0;
     140           3 : }
     141           1 : int digitalReadBuffered(int pin) { return digitalReadBuffered((pin_t)pin); }
     142             : 
     143           3 : analog_t analogReadBuffered(pin_t pin) {
     144           3 :     if (pin == NO_PIN)
     145             :         return 0; // LCOV_EXCL_LINE
     146           3 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
     147           1 :         return ::analogRead(pin);
     148             :     } else {
     149           2 :         ExtendedIOElement &el = getIOElementOfPin(pin);
     150           2 :         return el.analogReadBuffered(pin - el.getStart());
     151           2 :     }
     152             :     return 0;
     153           3 : }
     154           1 : analog_t analogReadBuffered(int pin) { return analogReadBuffered((pin_t)pin); }
     155             : 
     156           4 : void analogWriteBuffered(pin_t pin, analog_t val) {
     157           4 :     if (pin == NO_PIN)
     158             :         return; // LCOV_EXCL_LINE
     159           4 :     else if (pin < NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) {
     160             : #ifndef ESP32
     161           2 :         ::analogWrite(pin, val);
     162             : #endif
     163           2 :     } else {
     164           2 :         ExtendedIOElement &el = getIOElementOfPin(pin);
     165           2 :         el.analogWriteBuffered(pin - el.getStart(), val);
     166           2 :     }
     167           4 : }
     168           1 : void analogWriteBuffered(int pin, analog_t val) {
     169           1 :     analogWriteBuffered((pin_t)pin, val);
     170           1 : }
     171           1 : void analogWriteBuffered(int pin, int val) {
     172           1 :     analogWriteBuffered((pin_t)pin, (analog_t)val);
     173           1 : }
     174           1 : void analogWriteBuffered(pin_t pin, int val) {
     175           1 :     analogWriteBuffered(pin, (analog_t)val);
     176           1 : }
     177             : 
     178           4 : void shiftOut(pin_t dataPin, pin_t clockPin, BitOrder_t bitOrder, uint8_t val) {
     179           4 :     uint8_t i;
     180             : 
     181          36 :     for (i = 0; i < 8; i++) {
     182          32 :         if (bitOrder == LSBFIRST)
     183          16 :             digitalWrite(dataPin, (val & (1 << i)) ? HIGH : LOW);
     184             :         else
     185          16 :             digitalWrite(dataPin, (val & (1 << (7 - i))) ? HIGH : LOW);
     186             : 
     187          32 :         digitalWrite(clockPin, HIGH);
     188          32 :         digitalWrite(clockPin, LOW);
     189          32 :     }
     190           4 : }
     191           2 : void shiftOut(int dataPin, int clockPin, BitOrder_t bitOrder, uint8_t val) {
     192           2 :     shiftOut((pin_t)dataPin, (pin_t)clockPin, bitOrder, val);
     193           2 : }
     194             : 
     195             : } // namespace ExtIO
     196             : 
     197             : END_AH_NAMESPACE
     198             : 
     199             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.14-6-g40580cd