Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
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
79#include <hardware/structs/sio.h>
83 return {&sio_hw->gpio_in, uint32_t(1) << pin};
84}
85
86// ARM mbed OS
87#elif defined(ARDUINO_ARCH_MBED)
88
90#include <mbed.h>
91// ↑ Must be first
92#include <drivers/DigitalIn.h>
94struct DirectPinRead {
95 mbed::DigitalIn pin;
96 bool read() { return pin.read(); }
97};
99 return {mbed::DigitalIn {digitalPinToPinName(pin), PullUp}};
100}
101
102// Arduino UNO R4
103#elif defined(ARDUINO_UNOR4_MINIMA) || defined(ARDUINO_UNOR4_WIFI)
104
105struct DirectPinRead {
106 bsp_io_port_pin_t pin;
107 bool read() const {
108 bsp_io_level_t ret;
109 R_IOPORT_PinRead(NULL, pin, &ret);
110 return ret == BSP_IO_LEVEL_LOW;
111 }
112};
114 return {g_pin_cfg[pin].pin};
115}
116
117#else
118#warning "Unknown board. Please define the direct_pin_read function."
119
122 bool read() const { return ::digitalRead(pin) == HIGH; }
123};
124inline DirectPinRead direct_pin_read(ArduinoPin_t pin) { return {pin}; }
125
126#endif
127
constexpr PinStatus_t HIGH
AH::function_traits< decltype(::digitalWrite)>::argument_t< 0 > ArduinoPin_t
DirectPinRead direct_pin_read(ArduinoPin_t pin)
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
bool read() const
std::remove_cv< RegisterType >::type bitmask
RegisterType * in_reg
ArduinoPin_t pin
bool read() const