Control Surface stm32
MIDI Control Surface library for Arduino
DirectPinRead.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <Settings/NamespaceSettings.hpp>
5
6#include <AH/STL/type_traits>
7#include <Def/Def.hpp>
8
9#ifdef ARDUINO_ARCH_NRF52840
10#include <pins_arduino.h>
11#endif
12
14
15template <class RegisterType = const volatile uint32_t>
17 RegisterType *in_reg;
18 typename std::remove_cv<RegisterType>::type bitmask;
19 bool read() const { return *in_reg & bitmask; }
20};
21
22// 8-bit AVR
23#if defined(__AVR__) || \
24 (defined(TEENSYDUINO) && (defined(KINETISK) || defined(KINETISL)))
25
28 return {portInputRegister(digitalPinToPort(pin)),
29 static_cast<uint8_t>(digitalPinToBitMask(pin))};
30}
31
32// 32-bit
33#elif defined(__SAM3X8E__) || defined(ESP32) || defined(ESP8266) || \
34 defined(__SAMD51__)
35
38 return {portInputRegister(digitalPinToPort(pin)), digitalPinToBitMask(pin)};
39}
40
41// Special cases
42#elif defined(__SAMD21G18A__) || defined(__SAMD21E18A__)
43
46 return {portModeRegister(digitalPinToPort(pin)) + 8,
47 static_cast<uint32_t>(digitalPinToBitMask(pin))};
48}
49
50// More special cases
51#elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
52
55 return {portOutputRegister(pin), digitalPinToBitMask(pin)};
56}
57
58// Nordic
59#elif defined(RBL_NRF51822) || defined(ARDUINO_ARCH_NRF52840)
60
61struct DirectPinRead {
62 uint32_t pin;
63 bool read() const { return nrf_gpio_pin_read(pin); }
64};
66#if defined(RBL_NRF51822)
67 return {static_cast<uint32_t>(pin)};
68#elif defined(ARDUINO_ARCH_NRF52840)
69 return {static_cast<uint32_t>(digitalPinToPinName(pin))};
70#else
71#error "Unknown Nordic board"
72#endif
73}
74
75// Raspberry Pi RP2040
76#elif defined(ARDUINO_ARCH_RP2040)
77
78#include <hardware/structs/sio.h>
81 return {&sio_hw->gpio_in, uint32_t(1) << pin};
82}
83
84// ARM mbed OS
85#elif defined(ARDUINO_ARCH_MBED)
86
87#include <drivers/DigitalIn.h>
88struct DirectPinRead {
89 mbed::DigitalIn pin;
90 bool read() const { return pin.read(); }
91};
93 return {mbed::DigitalIn {digitalPinToPinName(pin), PullUp}};
94}
95
96#else
97#warning "Unknown board. Please define the direct_pin_read function."
98
100 uint8_t pin;
101 bool read() const { return ::digitalRead(pin) == HIGH; }
102};
103inline DirectPinRead direct_pin_read(uint8_t pin) { return {pin}; }
104
105#endif
106
constexpr PinStatus_t HIGH
DirectPinRead direct_pin_read(uint8_t pin)
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
bool read() const
std::remove_cv< RegisterType >::type bitmask
RegisterType * in_reg
bool read() const