3-phase PV router
Loading...
Searching...
No Matches
utils_pins.h
Go to the documentation of this file.
1
12
13#ifndef UTILS_PINS_H
14#define UTILS_PINS_H
15
16#include <Arduino.h>
17
18#include "utils_bits.h"
19
20#if defined(__DOXYGEN__)
21inline constexpr void togglePin(uint8_t pin);
22
23inline constexpr void setPinON(uint8_t pin);
24
25inline constexpr void setPinOFF(uint8_t pin);
26template< uint16_t usedPins >
27inline void setPinsState(uint16_t pinsON, uint16_t pinsOFF);
28
29inline constexpr bool getPinState(uint8_t pin);
30
31template< uint16_t pins >
32inline void setPinsAsOutput();
33template< uint16_t pins >
34inline void setPinsAsInputPullup();
35#else
36inline constexpr void togglePin(uint8_t pin) __attribute__((always_inline));
37
38inline constexpr void setPinON(uint8_t pin) __attribute__((always_inline));
39
40inline constexpr void setPinOFF(uint8_t pin) __attribute__((always_inline));
41template< uint16_t usedPins >
42inline void setPinsState(uint16_t pinsON, uint16_t pinsOFF) __attribute__((always_inline));
43
44inline constexpr bool getPinState(uint8_t pin) __attribute__((always_inline));
45
46template< uint16_t pins >
47inline void setPinsAsOutput() __attribute__((always_inline));
48template< uint16_t pins >
49inline void setPinsAsInputPullup() __attribute__((always_inline));
50#endif
51
56void constexpr togglePin(const uint8_t pin)
57{
58 if (pin < 8)
59 {
60 bit_set(PIND, pin);
61 }
62 else if (pin < 14)
63 {
64 bit_set(PINB, pin - 8);
65 }
66 else
67 {
68 bit_set(PINC, pin - 14);
69 }
70}
71
84inline void setPinState(const uint8_t pin, const bool bState)
85{
86 const volatile uint8_t &port{ (pin < 8) ? PORTD : ((pin < 14) ? PORTB : PORTC) };
87 volatile uint8_t &toggle{ (pin < 8) ? PIND : ((pin < 14) ? PINB : PINC) };
88 const uint8_t mask{ static_cast< uint8_t >(1U << ((pin < 8) ? pin : ((pin < 14) ? pin - 8 : pin - 14))) };
89
90 if (static_cast< bool >(port & mask) != bState)
91 {
92 toggle = mask;
93 }
94}
95
104inline constexpr void setPinON(const uint8_t pin)
105{
106 if (pin < 8)
107 {
108 bit_set(PORTD, pin);
109 }
110 else if (pin < 14)
111 {
112 bit_set(PORTB, pin - 8);
113 }
114 else
115 {
116 bit_set(PORTC, pin - 14);
117 }
118}
119
128inline constexpr void setPinOFF(const uint8_t pin)
129{
130 if (pin < 8)
131 {
132 bit_clear(PORTD, pin);
133 }
134 else if (pin < 14)
135 {
136 bit_clear(PORTB, pin - 8);
137 }
138 else
139 {
140 bit_clear(PORTC, pin - 14);
141 }
142}
143
159template< uint16_t usedPins >
160inline void setPinsState(const uint16_t pinsON, const uint16_t pinsOFF)
161{
162 if constexpr (lowByte(usedPins) != 0)
163 {
164 PORTD = static_cast< uint8_t >((PORTD & ~lowByte(pinsOFF)) | lowByte(pinsON));
165 }
166 if constexpr (highByte(usedPins) != 0)
167 {
168 PORTB = static_cast< uint8_t >((PORTB & ~highByte(pinsOFF)) | highByte(pinsON));
169 }
170}
171
179inline constexpr bool getPinState(const uint8_t pin)
180{
181 if (pin < 8)
182 {
183 return bit_read(PIND, pin);
184 }
185 else if (pin < 14)
186 {
187 return bit_read(PINB, pin - 8);
188 }
189 else
190 {
191 return bit_read(PINC, pin - 14);
192 }
193}
194
203template< uint16_t pins >
204inline void setPinsAsOutput()
205{
206 if constexpr (lowByte(pins) != 0)
207 {
208 DDRD |= lowByte(pins);
209 }
210 if constexpr (highByte(pins) != 0)
211 {
212 DDRB |= highByte(pins);
213 }
214}
215
223template< uint16_t pins >
225{
226 if constexpr (lowByte(pins) != 0)
227 {
228 DDRD &= static_cast< uint8_t >(~lowByte(pins)); // input
229 PORTD |= lowByte(pins); // pull-up on
230 }
231 if constexpr (highByte(pins) != 0)
232 {
233 DDRB &= static_cast< uint8_t >(~highByte(pins));
234 PORTB |= highByte(pins);
235 }
236}
237
238#endif /* UTILS_PINS_H */
Bit manipulation utility functions.
constexpr void bit_set(T &_dest, const uint8_t bit)
Set the specified bit to 1.
Definition utils_bits.h:30
constexpr uint8_t bit_read(const T &_src, const uint8_t bit)
Read the specified bit.
Definition utils_bits.h:44
constexpr uint8_t bit_clear(T &_dest, const uint8_t bit)
Clear the specified bit.
Definition utils_bits.h:58
void setPinsState(uint16_t pinsON, uint16_t pinsOFF)
Switch pins ON and OFF, with one write per port.
Definition utils_pins.h:160
constexpr bool getPinState(uint8_t pin)
Get the Pin State.
Definition utils_pins.h:179
void setPinState(const uint8_t pin, const bool bState)
Set the Pin state for the specified pin.
Definition utils_pins.h:84
constexpr void togglePin(uint8_t pin)
Toggle the specified pin.
Definition utils_pins.h:56
constexpr void setPinON(uint8_t pin)
Set the Pin state to ON for the specified pin.
Definition utils_pins.h:104
constexpr void setPinOFF(uint8_t pin)
Set the Pin state to OFF for the specified pin.
Definition utils_pins.h:128
void setPinsAsInputPullup()
Set the pins as INPUT_PULLUP.
Definition utils_pins.h:224
void setPinsAsOutput()
Set the pins as OUTPUT.
Definition utils_pins.h:204