Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include <AH/Settings/Warnings.hpp> 6 : AH_DIAGNOSTIC_WERROR() // Enable errors on warnings 7 : 8 : #include <AH/Containers/Array.hpp> 9 : #include <AH/PrintStream/PrintStream.hpp> 10 : #include <AH/STL/limits> 11 : #include <AH/Settings/NamespaceSettings.hpp> 12 : #include <stdint.h> // uint8_t 13 : 14 : BEGIN_AH_NAMESPACE 15 : 16 : /// The type returned from analogRead and similar functions. 17 : using analog_t = uint16_t; 18 : 19 : namespace ExtIO { 20 : /// Type for storing pin numbers of Extended Input/Output elements. 21 : struct pin_t { 22 : /// Default constructor (NO_PIN). 23 : constexpr pin_t() = default; 24 : /// Constructor from integer. 25 405 : constexpr pin_t(uint16_t pin) : pin(pin) {} 26 : 27 : /// The actual underlying pin number. 28 : uint16_t pin = (std::numeric_limits<decltype(pin)>::max() >> 1) + 1; 29 : 30 : static_assert(std::is_unsigned<decltype(pin)>::value, 31 : "Error: pin_t should be an unsigned integer type"); 32 : 33 : pin_t &operator+=(pin_t b) { 34 : this->pin += b.pin; 35 : return *this; 36 : } 37 : pin_t &operator++() { 38 : ++pin; 39 : return *this; 40 : } 41 : pin_t operator++(int) { 42 : pin_t t = *this; 43 : ++pin; 44 : return t; 45 : } 46 : 47 : pin_t &operator-=(pin_t b) { 48 : this->pin -= b.pin; 49 : return *this; 50 : } 51 : pin_t &operator--() { 52 : --pin; 53 : return *this; 54 : } 55 : pin_t operator--(int) { 56 : pin_t t = *this; 57 : --pin; 58 : return t; 59 : } 60 : }; 61 947 : constexpr inline bool operator==(pin_t a, pin_t b) { return a.pin == b.pin; } 62 181 : constexpr inline bool operator<(pin_t a, pin_t b) { return a.pin < b.pin; } 63 : constexpr inline bool operator<=(pin_t a, pin_t b) { return a.pin <= b.pin; } 64 48 : constexpr inline bool operator>(pin_t a, pin_t b) { return a.pin > b.pin; } 65 162 : constexpr inline bool operator>=(pin_t a, pin_t b) { return a.pin >= b.pin; } 66 75 : constexpr inline bool operator!=(pin_t a, pin_t b) { return !(a == b); } 67 78 : constexpr inline pin_t operator-(pin_t a, pin_t b) { return a.pin - b.pin; } 68 119 : constexpr inline pin_t operator+(pin_t a, pin_t b) { return a.pin + b.pin; } 69 : constexpr inline pin_t operator*(pin_t a, pin_t b) { return a.pin * b.pin; } 70 : inline Print &operator<<(Print &os, pin_t p) { 71 : using AH::operator<<; 72 : return os << +p.pin; 73 : } 74 : #ifndef ARDUINO 75 4 : inline std::ostream &operator<<(std::ostream &os, pin_t p) { 76 4 : return os << +p.pin; 77 : } 78 : #endif 79 : } // namespace ExtIO 80 : 81 : /// The type for Arduino pins (and ExtendedIOElement pins). 82 : using ExtIO::pin_t; 83 : 84 : #ifdef NO_PIN // Fix for FastLED: https://github.com/FastLED/FastLED/issues/893 85 : #undef NO_PIN 86 : #endif 87 : 88 : /// A special pin number that indicates an unused or invalid pin. 89 : constexpr pin_t NO_PIN{}; 90 : 91 : /// An easy alias for arrays of pins. 92 : template <size_t N> 93 : using PinList = Array<pin_t, N>; 94 : 95 : END_AH_NAMESPACE 96 : 97 : AH_DIAGNOSTIC_POP()