1-phase PV router
Loading...
Searching...
No Matches
utils_pins.h
Go to the documentation of this file.
1
11
12#ifndef UTILS_PINS_H
13#define UTILS_PINS_H
14
15#include <Arduino.h>
16
17inline constexpr uint8_t unused_pin{ 0xff };
18
19#if defined(__DOXYGEN__)
20inline constexpr void togglePin(uint8_t pin);
21
22inline constexpr void setPinON(uint8_t pin);
23inline void setPinsON(uint16_t pins);
24
25inline constexpr void setPinOFF(uint8_t pin);
26inline void setPinsOFF(uint16_t pins);
27
28inline constexpr bool getPinState(uint8_t pin);
29
30inline void setPinsAsOutput(uint16_t pins);
31inline void setPinsAsInputPullup(uint16_t pins);
32#else
33inline constexpr void togglePin(uint8_t pin) __attribute__((always_inline));
34
35inline constexpr void setPinON(uint8_t pin) __attribute__((always_inline));
36inline void setPinsON(uint16_t pins) __attribute__((always_inline));
37
38inline constexpr void setPinOFF(uint8_t pin) __attribute__((always_inline));
39inline void setPinsOFF(uint16_t pins) __attribute__((always_inline));
40
41inline constexpr bool getPinState(uint8_t pin) __attribute__((always_inline));
42
43inline void setPinsAsOutput(uint16_t pins) __attribute__((always_inline));
44inline void setPinsAsInputPullup(uint16_t pins) __attribute__((always_inline));
45#endif
46
54template< typename T > constexpr void bit_set(T& _dest, const uint8_t bit)
55{
56 _dest |= (T)0x01 << bit;
57}
58
67template< typename T > constexpr uint8_t bit_read(const T& _src, const uint8_t bit)
68{
69 return (_src >> bit) & (T)0x01;
70}
71
80template< typename T > constexpr uint8_t bit_clear(T& _dest, const uint8_t bit)
81{
82 return _dest &= ~((T)0x01 << bit);
83}
84
89void constexpr togglePin(const uint8_t pin)
90{
91 if (pin < 8u)
92 {
93 bit_set(PIND, pin);
94 }
95 else if (pin < 14u)
96 {
97 bit_set(PINB, pin - 8u);
98 }
99 else
100 {
101 bit_set(PINC, pin - 14u);
102 }
103}
104
111inline constexpr void setPinState(const uint8_t pin, const bool bState)
112{
113 if (bState)
114 {
115 setPinON(pin);
116 }
117 else
118 {
119 setPinOFF(pin);
120 }
121}
122
128inline constexpr void setPinON(const uint8_t pin)
129{
130 if (pin < 8u)
131 {
132 bit_set(PORTD, pin);
133 }
134 else if (pin < 14u)
135 {
136 bit_set(PORTB, pin - 8u);
137 }
138 else
139 {
140 bit_set(PORTC, pin - 14u);
141 }
142}
143
149inline void setPinsON(const uint16_t pins)
150{
151 PORTD |= lowByte(pins);
152 PORTB |= highByte(pins);
153}
154
160inline constexpr void setPinOFF(const uint8_t pin)
161{
162 if (pin < 8u)
163 {
164 bit_clear(PORTD, pin);
165 }
166 else if (pin < 14u)
167 {
168 bit_clear(PORTB, pin - 8u);
169 }
170 else
171 {
172 bit_clear(PORTC, pin - 14u);
173 }
174}
175
181inline void setPinsOFF(const uint16_t pins)
182{
183 PORTD &= ~lowByte(pins);
184 PORTB &= ~highByte(pins);
185}
186
194inline constexpr bool getPinState(const uint8_t pin)
195{
196 if (pin < 8u)
197 {
198 return bit_read(PIND, pin);
199 }
200 else if (pin < 14u)
201 {
202 return bit_read(PINB, pin - 8u);
203 }
204 else
205 {
206 return bit_read(PINC, pin - 14u);
207 }
208}
209
215inline void setPinsAsOutput(const uint16_t pins)
216{
217 DDRD |= lowByte(pins);
218 DDRB |= highByte(pins);
219}
220
226inline void setPinsAsInputPullup(const uint16_t pins)
227{
228 DDRD &= ~lowByte(pins); // Set pins as input
229 PORTD |= lowByte(pins); // Enable pull-up resistors
230
231 DDRB &= ~highByte(pins); // Set pins as input
232 PORTB |= highByte(pins); // Enable pull-up resistors
233}
234
235#endif /* UTILS_PINS_H */
constexpr void bit_set(T &_dest, const uint8_t bit)
Set the specified bit to 1.
Definition utils_pins.h:54
constexpr bool getPinState(uint8_t pin)
Get the Pin State.
Definition utils_pins.h:194
void setPinsOFF(uint16_t pins)
Set the Pins state to OFF.
Definition utils_pins.h:181
constexpr void setPinState(const uint8_t pin, const bool bState)
Set the Pin state for the specified pin.
Definition utils_pins.h:111
constexpr void togglePin(uint8_t pin)
Toggle the specified pin.
Definition utils_pins.h:89
constexpr uint8_t bit_read(const T &_src, const uint8_t bit)
Read the specified bit.
Definition utils_pins.h:67
void setPinsAsInputPullup(uint16_t pins)
Set the pins as INPUT_PULLUP.
Definition utils_pins.h:226
constexpr void setPinON(uint8_t pin)
Set the Pin state to ON for the specified pin.
Definition utils_pins.h:128
void setPinsAsOutput(uint16_t pins)
Set the pins as OUTPUT.
Definition utils_pins.h:215
constexpr void setPinOFF(uint8_t pin)
Set the Pin state to OFF for the specified pin.
Definition utils_pins.h:160
void setPinsON(uint16_t pins)
Set the Pins state to ON.
Definition utils_pins.h:149
constexpr uint8_t bit_clear(T &_dest, const uint8_t bit)
Clear the specified bit.
Definition utils_pins.h:80
constexpr uint8_t unused_pin
Definition utils_pins.h:17