Speeduino
Loading...
Searching...
No Matches
port_pin.h
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4
6// Automatic type deduction
7namespace type_detection_detail {
8 // These are used for automatic type detection *at compile time* and are never executed.
9
10 // Get the return type of a function call
11 template <typename R, typename... Args> R return_type_of(R(*)(Args...));
12
13 // portOutputRegister() and digitalPinToBitMask() on AVR is a GCC return expression, which needs to
14 // be wrapped in a function to be used in this context.
15 static inline auto detectPortRegisterType(void) { return portOutputRegister(digitalPinToPort(0)); };
16 static inline auto detectDigitalPinToBitMask(void) { return digitalPinToBitMask(0); };
17}
19
23{
24 port_pin_t() = default;
25
28
30 bool isValid(void) const {
31 return _port!=NULL_PORT;
32 }
33
34 // LCOV_EXCL_START
36 bool isPinHigh(void) const noexcept {
37 return isValid() ?
38#if defined(UNIT_TEST)
40#else
41 (*_port & _mask) != 0
42#endif
43 : false;
44 }
45 // LCOV_EXCL_STOP
46
48 bool isPinLow(void) const noexcept {
49 return !isPinHigh();
50 }
51
53 void setPinHigh(void) noexcept;
54
56 void setPinLow(void) noexcept;
57
58private:
59
62
65
66 static constexpr port_register_t NULL_PORT = {nullptr};
67
68 port_register_t _port = NULL_PORT;
69 pin_mask_t _mask = {0};
70#if defined(UNIT_TEST)
71 bool _pinState = LOW;
72#endif
73};
74
Definition array.h:14
constexpr array()=default
A structure to support direct port manipulation.
Definition port_pin.h:23
bool isValid(void) const
Is the port register initialised?
Definition port_pin.h:30
port_pin_t()=default
void setPinHigh(void) noexcept
Set the pin high.
Definition port_pin.cpp:17
bool isPinHigh(void) const noexcept
Check if the pin is set high.
Definition port_pin.h:36
bool isPinLow(void) const noexcept
Check if the pin is set low.
Definition port_pin.h:48
void setPinLow(void) noexcept
Set the pin low.
Definition port_pin.cpp:29