Some utility functions for pins manipulation.
More...
#include <Arduino.h>
#include "utils_bits.h"
Go to the source code of this file.
|
| constexpr bool | getPinState (const uint8_t pin) |
| | Get the Pin State.
|
| |
| constexpr void | setPinOFF (const uint8_t pin) |
| | Set the Pin state to OFF for the specified pin.
|
| |
| constexpr void | setPinON (const uint8_t pin) |
| | Set the Pin state to ON for the specified pin.
|
| |
| template<uint16_t pins> |
| void | setPinsAsInputPullup () |
| | Set the pins as INPUT_PULLUP.
|
| |
| template<uint16_t pins> |
| void | setPinsAsOutput () |
| | Set the pins as OUTPUT.
|
| |
| template<uint16_t usedPins> |
| void | setPinsState (const uint16_t pinsON, const uint16_t pinsOFF) |
| | Switch pins ON and OFF, with one write per port.
|
| |
| void | setPinState (const uint8_t pin, const bool bState) |
| | Set the Pin state for the specified pin.
|
| |
| constexpr void | togglePin (const uint8_t pin) |
| | Toggle the specified pin.
|
| |
Some utility functions for pins manipulation.
- Author
- Frédéric Metrich (frede.nosp@m.ric..nosp@m.metri.nosp@m.ch@l.nosp@m.ive.f.nosp@m.r)
- Version
- 0.1
- Date
- 2026-11-24
- Copyright
- Copyright (c) 2023-2026 SPDX-License-Identifier: GPL-3.0-or-later
Definition in file utils_pins.h.
◆ getPinState()
| bool getPinState |
( |
const uint8_t | pin | ) |
|
|
inlineconstexpr |
Get the Pin State.
- Parameters
-
- Returns
- true if HIGH
-
false if LOW
Definition at line 179 of file utils_pins.h.
◆ setPinOFF()
| void setPinOFF |
( |
const uint8_t | pin | ) |
|
|
inlineconstexpr |
Set the Pin state to OFF for the specified pin.
- Parameters
-
- Warning
- Atomic (a single sbi/cbi) only for a pin known at compile time. For a pin known at run time it is a read-modify-write of the port: from loop(), use setPinState().
Definition at line 128 of file utils_pins.h.
◆ setPinON()
| void setPinON |
( |
const uint8_t | pin | ) |
|
|
inlineconstexpr |
Set the Pin state to ON for the specified pin.
- Parameters
-
- Warning
- Atomic (a single sbi/cbi) only for a pin known at compile time. For a pin known at run time it is a read-modify-write of the port: from loop(), use setPinState().
Definition at line 104 of file utils_pins.h.
◆ setPinsAsInputPullup()
template<uint16_t pins>
| void setPinsAsInputPullup |
( |
| ) |
|
|
inline |
Set the pins as INPUT_PULLUP.
- Template Parameters
-
| pins | The pins to set as INPUT_PULLUP, known at compile time |
A port none of pins is on is not touched at all (see setPinsAsOutput()).
Definition at line 224 of file utils_pins.h.
◆ setPinsAsOutput()
Set the pins as OUTPUT.
- Template Parameters
-
| pins | The pins to set as OUTPUT, known at compile time |
The registers are volatile: DDRB |= 0 is still a read and a write. A port none of pins is on is not touched at all.
Definition at line 204 of file utils_pins.h.
◆ setPinsState()
template<uint16_t usedPins>
| void setPinsState |
( |
const uint16_t | pinsON, |
|
|
const uint16_t | pinsOFF ) |
|
inline |
Switch pins ON and OFF, with one write per port.
- Template Parameters
-
| usedPins | All the pins this call may ever change, known at compile time |
- Parameters
-
| pinsON | The pins to switch ON |
| pinsOFF | The pins to switch OFF (none of them in pinsON) |
The ports are volatile: each PORTD |= … is its own read and write, even when another one follows or the mask is 0. So each port is read and written once, and a port none of usedPins is on (PORTB with every pin on 2..7) is not touched at all.
- Warning
- Read-modify-write of PORTD and PORTB: only safe from the ADC ISR (its sole caller), which cannot be interrupted. From loop(), use setPinState() or togglePin(): an ISR landing in the middle would have its load pin changes written back to the old state.
Definition at line 160 of file utils_pins.h.
◆ setPinState()
| void setPinState |
( |
const uint8_t | pin, |
|
|
const bool | bState ) |
|
inline |
Set the Pin state for the specified pin.
- Parameters
-
| pin | pin to change [2..13] |
| bState | state to be set |
For a pin only known at run time (a relay). The ADC ISR writes the load pins, possibly on the same port, so the port must not be read-modified-written here: the pin is toggled through PINx instead, where writing a 1 toggles that bit of PORTx and writing a 0 leaves the others alone. Only the caller changes this pin, so reading its state first is safe.
Definition at line 84 of file utils_pins.h.
◆ togglePin()
| void constexpr togglePin |
( |
uint8_t | pin | ) |
|
|
inlineconstexpr |