pinMode & initializer lists

luni

Well-known member
Here a (IMHO) nice supplement for the pinMode function utilizing 'modern' c++ features

file: pinModeEx.h (attached)
Code:
#include <initializer_list>
#include "core_pins.h"

inline void pinMode(std::initializer_list<uint8_t> pins, uint8_t mode)
{
    for (uint8_t pin : pins)
    {
        pinMode(pin, mode);
    }
}

You can use it like this:

Code:
const int pinA = 3, pinB = 17, switch1 = 3, switch2 = 4;

void setup()
{
    pinMode({pinA, pinB, 17, LED_BUILTIN}, OUTPUT);  // set a bunch of pins to mode OUTPUT...
    pinMode({switch1, switch2}, INPUT_PULLUP);       // others to INPUT
}

It of course doesn't touch the original pinMode function. Might be nice to have it in the core.
 

Attachments

  • pinModeEx.h
    222 bytes · Views: 50
Without any additional header you can also do:

Code:
for(int pin : {1,2,17})
{
    pinMode(pin, OUTPUT);
    // or of course any other function requiring the pins...
}
 
Here a (IMHO) nice supplement for the pinMode function utilizing 'modern' c++ features

file: pinModeEx.h (attached)
Code:
#include <initializer_list>
#include "core_pins.h"

inline void pinMode(std::initializer_list<uint8_t> pins, uint8_t mode)
{
    for (uint8_t pin : pins)
    {
        pinMode(pin, mode);
    }
}

You can use it like this:

Code:
const int pinA = 3, pinB = 17, switch1 = 3, switch2 = 4;

void setup()
{
    pinMode({pinA, pinB, 17, LED_BUILTIN}, OUTPUT);  // set a bunch of pins to mode OUTPUT...
    pinMode({switch1, switch2}, INPUT_PULLUP);       // others to INPUT
}

It of course doesn't touch the original pinMode function. Might be nice to have it in the core.

That's really convenient!
 
Back
Top