3-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 < 8)
92 {
93 bit_set(PIND, pin);
94 }
95 else if (pin < 14)
96 {
97 bit_set(PINB, pin - 8);
98 }
99 else
100 {
101 bit_set(PINC, pin - 14);
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 < 8)
131 {
132 bit_set(PORTD, pin);
133 }
134 else if (pin < 14)
135 {
136 bit_set(PORTB, pin - 8);
137 }
138 else
139 {
140 bit_set(PORTC, pin - 14);
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 < 8)
163 {
164 bit_clear(PORTD, pin);
165 }
166 else if (pin < 14)
167 {
168 bit_clear(PORTB, pin - 8);
169 }
170 else
171 {
172 bit_clear(PORTC, pin - 14);
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 return (pin < 8) ? bit_read(PIND, pin) : (pin < 14) ? bit_read(PINB, pin - 8)
197 : bit_read(PINC, pin - 14);
198}
199
205inline void setPinsAsOutput(const uint16_t pins)
206{
207 DDRD |= lowByte(pins);
208 DDRB |= highByte(pins);
209}
210
216inline void setPinsAsInputPullup(const uint16_t pins)
217{
218 // Set pins as input
219 DDRD &= ~lowByte(pins);
220 DDRB &= ~highByte(pins);
221
222 // Enable pull-up resistors
223 PORTD |= lowByte(pins);
224 PORTB |= highByte(pins);
225}
226
227#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:216
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:205
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