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
17#include "utils_bits.h"
18
19inline constexpr uint8_t unused_pin{ 0xff };
20
21#if defined(__DOXYGEN__)
22inline constexpr void togglePin(uint8_t pin);
23
24inline constexpr void setPinON(uint8_t pin);
25inline void setPinsON(uint16_t pins);
26
27inline constexpr void setPinOFF(uint8_t pin);
28inline void setPinsOFF(uint16_t pins);
29
30inline constexpr bool getPinState(uint8_t pin);
31
32inline void setPinsAsOutput(uint16_t pins);
33inline void setPinsAsInputPullup(uint16_t pins);
34#else
35inline constexpr void togglePin(uint8_t pin) __attribute__((always_inline));
36
37inline constexpr void setPinON(uint8_t pin) __attribute__((always_inline));
38inline void setPinsON(uint16_t pins) __attribute__((always_inline));
39
40inline constexpr void setPinOFF(uint8_t pin) __attribute__((always_inline));
41inline void setPinsOFF(uint16_t pins) __attribute__((always_inline));
42
43inline constexpr bool getPinState(uint8_t pin) __attribute__((always_inline));
44
45inline void setPinsAsOutput(uint16_t pins) __attribute__((always_inline));
46inline void setPinsAsInputPullup(uint16_t pins) __attribute__((always_inline));
47#endif
48
53void constexpr togglePin(const uint8_t pin)
54{
55 if (pin < 8)
56 {
57 bit_set(PIND, pin);
58 }
59 else if (pin < 14)
60 {
61 bit_set(PINB, pin - 8);
62 }
63 else
64 {
65 bit_set(PINC, pin - 14);
66 }
67}
68
75inline constexpr void setPinState(const uint8_t pin, const bool bState)
76{
77 if (bState)
78 {
79 setPinON(pin);
80 }
81 else
82 {
83 setPinOFF(pin);
84 }
85}
86
92inline constexpr void setPinON(const uint8_t pin)
93{
94 if (pin < 8)
95 {
96 bit_set(PORTD, pin);
97 }
98 else if (pin < 14)
99 {
100 bit_set(PORTB, pin - 8);
101 }
102 else
103 {
104 bit_set(PORTC, pin - 14);
105 }
106}
107
113inline void setPinsON(const uint16_t pins)
114{
115 PORTD |= lowByte(pins);
116 PORTB |= highByte(pins);
117}
118
124inline constexpr void setPinOFF(const uint8_t pin)
125{
126 if (pin < 8)
127 {
128 bit_clear(PORTD, pin);
129 }
130 else if (pin < 14)
131 {
132 bit_clear(PORTB, pin - 8);
133 }
134 else
135 {
136 bit_clear(PORTC, pin - 14);
137 }
138}
139
145inline void setPinsOFF(const uint16_t pins)
146{
147 PORTD &= ~lowByte(pins);
148 PORTB &= ~highByte(pins);
149}
150
158inline constexpr bool getPinState(const uint8_t pin)
159{
160 if (pin < 8)
161 {
162 return bit_read(PIND, pin);
163 }
164 else if (pin < 14)
165 {
166 return bit_read(PINB, pin - 8);
167 }
168 else
169 {
170 return bit_read(PINC, pin - 14);
171 }
172}
173
179inline void setPinsAsOutput(const uint16_t pins)
180{
181 DDRD |= lowByte(pins);
182 DDRB |= highByte(pins);
183}
184
190inline void setPinsAsInputPullup(const uint16_t pins)
191{
192 // Set pins as input
193 DDRD &= ~lowByte(pins);
194 DDRB &= ~highByte(pins);
195
196 // Enable pull-up resistors
197 PORTD |= lowByte(pins);
198 PORTB |= highByte(pins);
199}
200
201#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:27
constexpr uint8_t bit_read(const T &_src, const uint8_t bit)
Read the specified bit.
Definition utils_bits.h:41
constexpr uint8_t bit_clear(T &_dest, const uint8_t bit)
Clear the specified bit.
Definition utils_bits.h:55
constexpr bool getPinState(uint8_t pin)
Get the Pin State.
Definition utils_pins.h:158
void setPinsOFF(uint16_t pins)
Set the Pins state to OFF.
Definition utils_pins.h:145
constexpr void setPinState(const uint8_t pin, const bool bState)
Set the Pin state for the specified pin.
Definition utils_pins.h:75
constexpr void togglePin(uint8_t pin)
Toggle the specified pin.
Definition utils_pins.h:53
void setPinsAsInputPullup(uint16_t pins)
Set the pins as INPUT_PULLUP.
Definition utils_pins.h:190
constexpr void setPinON(uint8_t pin)
Set the Pin state to ON for the specified pin.
Definition utils_pins.h:92
void setPinsAsOutput(uint16_t pins)
Set the pins as OUTPUT.
Definition utils_pins.h:179
constexpr void setPinOFF(uint8_t pin)
Set the Pin state to OFF for the specified pin.
Definition utils_pins.h:124
void setPinsON(uint16_t pins)
Set the Pins state to ON.
Definition utils_pins.h:113
constexpr uint8_t unused_pin
Definition utils_pins.h:19