Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: pinMode & initializer lists

  1. #1
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,219

    pinMode & initializer lists

    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.
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,219
    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...
    }

  3. #3
    Senior Member brtaylor's Avatar
    Join Date
    Mar 2016
    Location
    Portland, OR
    Posts
    583
    Quote Originally Posted by luni View Post
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •