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