3-phase PV router
Loading...
Searching...
No Matches
utils_pins.h
Go to the documentation of this file.
1
12#ifndef UTILS_PINS_H
13#define UTILS_PINS_H
14
15#include <Arduino.h>
16
17#if defined(__DOXYGEN__)
18inline constexpr void togglePin(const uint8_t pin);
19
20inline constexpr void setPinON(const uint8_t pin);
21inline void setPinsON(const uint16_t pins);
22
23inline constexpr void setPinOFF(const uint8_t pin);
24inline void setPinsOFF(const uint16_t pins);
25
26inline bool getPinState(const uint8_t pin);
27#else
28inline constexpr void togglePin(const uint8_t pin) __attribute__((always_inline));
29
30inline constexpr void setPinON(const uint8_t pin) __attribute__((always_inline));
31inline void setPinsON(const uint16_t pins) __attribute__((always_inline));
32
33inline constexpr void setPinOFF(const uint8_t pin) __attribute__((always_inline));
34inline void setPinsOFF(const uint16_t pins) __attribute__((always_inline));
35
36inline bool getPinState(const uint8_t pin) __attribute__((always_inline));
37#endif
38
46template< typename T > constexpr void bit_set(T& _dest, const uint8_t bit)
47{
48 _dest |= (T)0x01 << bit;
49}
50
59template< typename T > constexpr uint8_t bit_read(const T& _src, const uint8_t bit)
60{
61 return (_src >> bit) & (T)0x01;
62}
63
72template< typename T > constexpr uint8_t bit_clear(T& _dest, const uint8_t bit)
73{
74 return _dest &= ~((T)0x01 << bit);
75}
76
81void constexpr togglePin(const uint8_t pin)
82{
83 if (pin < 8)
84 {
85 bit_set(PIND, pin);
86 }
87 else
88 {
89 bit_set(PINB, pin - 8);
90 }
91}
92
99inline constexpr void setPinState(const uint8_t pin, const bool bState)
100{
101 if (bState)
102 {
103 setPinON(pin);
104 }
105 else
106 {
107 setPinOFF(pin);
108 }
109}
110
116inline constexpr void setPinON(const uint8_t pin)
117{
118 if (pin < 8)
119 {
120 bit_set(PORTD, pin);
121 }
122 else
123 {
124 bit_set(PORTB, pin - 8);
125 }
126}
127
133inline void setPinsON(const uint16_t pins)
134{
135 PORTD |= lowByte(pins);
136 PORTB |= highByte(pins);
137}
138
144inline constexpr void setPinOFF(const uint8_t pin)
145{
146 if (pin < 8)
147 {
148 bit_clear(PORTD, pin);
149 }
150 else
151 {
152 bit_clear(PORTB, pin - 8);
153 }
154}
155
161inline void setPinsOFF(const uint16_t pins)
162{
163 PORTD &= ~lowByte(pins);
164 PORTB &= ~highByte(pins);
165}
166
174inline bool getPinState(const uint8_t pin)
175{
176 return (pin < 8) ? bitRead(PIND, pin) : bitRead(PINB, pin - 8);
177}
178
179#endif // UTILS_PINS_H
constexpr uint16_t
Definition: config_system.h:32
constexpr void bit_set(T &_dest, const uint8_t bit)
Set the specified bit to 1.
Definition: utils_pins.h:46
void setPinsOFF(const uint16_t pins)
Set the Pins state to OFF.
Definition: utils_pins.h:161
constexpr void setPinState(const uint8_t pin, const bool bState)
Set the Pin state for the specified pin.
Definition: utils_pins.h:99
constexpr void togglePin(const uint8_t pin)
Toggle the specified pin.
Definition: utils_pins.h:81
constexpr uint8_t bit_read(const T &_src, const uint8_t bit)
Read the specified bit.
Definition: utils_pins.h:59
constexpr void setPinON(const uint8_t pin)
Set the Pin state to ON for the specified pin.
Definition: utils_pins.h:116
bool getPinState(const uint8_t pin)
Get the Pin State.
Definition: utils_pins.h:174
constexpr void setPinOFF(const uint8_t pin)
Set the Pin state to OFF for the specified pin.
Definition: utils_pins.h:144
void setPinsON(const uint16_t pins)
Set the Pins state to ON.
Definition: utils_pins.h:133
constexpr uint8_t bit_clear(T &_dest, const uint8_t bit)
Clear the specified bit.
Definition: utils_pins.h:72