3-phase PV router
Loading...
Searching...
No Matches
utils_pins.h File Reference

Some utility functions for pins manipulation. More...

#include <Arduino.h>
#include "utils_bits.h"
Include dependency graph for utils_pins.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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.
 

Detailed Description

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

Definition in file utils_pins.h.

Function Documentation

◆ getPinState()

bool getPinState ( const uint8_t pin)
inlineconstexpr

Get the Pin State.

Parameters
pinThe pin to read
Returns
true if HIGH
false if LOW

Definition at line 179 of file utils_pins.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPinOFF()

void setPinOFF ( const uint8_t pin)
inlineconstexpr

Set the Pin state to OFF for the specified pin.

Parameters
pinpin to change [2..13]
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPinON()

void setPinON ( const uint8_t pin)
inlineconstexpr

Set the Pin state to ON for the specified pin.

Parameters
pinpin to change [2..13]
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPinsAsInputPullup()

template<uint16_t pins>
void setPinsAsInputPullup ( )
inline

Set the pins as INPUT_PULLUP.

Template Parameters
pinsThe 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.

Here is the caller graph for this function:

◆ setPinsAsOutput()

template<uint16_t pins>
void setPinsAsOutput ( )
inline

Set the pins as OUTPUT.

Template Parameters
pinsThe 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.

Here is the caller graph for this function:

◆ 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
usedPinsAll the pins this call may ever change, known at compile time
Parameters
pinsONThe pins to switch ON
pinsOFFThe 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.

Here is the caller graph for this function:

◆ setPinState()

void setPinState ( const uint8_t pin,
const bool bState )
inline

Set the Pin state for the specified pin.

Parameters
pinpin to change [2..13]
bStatestate 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.

Here is the caller graph for this function:

◆ togglePin()

void constexpr togglePin ( uint8_t pin)
inlineconstexpr

Toggle the specified pin.

Definition at line 56 of file utils_pins.h.

Here is the call graph for this function:
Here is the caller graph for this function: