Skip to content

InputConfig

Configuration structure for the InputManager.

Description

InputConfig defines the mapping between logical inputs and physical pins (ESP32) or keyboard keys (Native/SDL2). It uses variadic arguments to allow flexible configuration of any number of inputs.

The configuration is platform-specific: ESP32 uses GPIO pin numbers, while Native uses SDL keyboard scancodes.

Namespace

namespace pixelroot32::input {
    struct InputConfig {
        // ...
    };
}

Structure

int count

Total number of configured inputs.

Type: int

Access: Read-write

Default: 0

Notes: - Must match the number of arguments provided to constructor - Determines the size of the internal button array

int* inputPins (ESP32 only)

Array of GPIO pin numbers for ESP32.

Type: int*

Access: Read-write

Default: nullptr

Notes: - Only available on ESP32 platform - Array size equals count - Pin numbers correspond to ESP32 GPIO pins - Use nullptr if count is 0

Example:

// ESP32: 6 buttons on pins 0, 2, 4, 5, 18, 19
pixelroot32::input::InputConfig config(6, 0, 2, 4, 5, 18, 19);
// config.inputPins[0] = 0  (Up)
// config.inputPins[1] = 2  (Down)
// config.inputPins[2] = 4  (Left)
// config.inputPins[3] = 5  (Right)
// config.inputPins[4] = 18 (Button A)
// config.inputPins[5] = 19 (Button B)

uint8_t* buttonNames (Native only)

Array of button mappings (scancodes) for Native.

Type: uint8_t*

Access: Read-write

Default: nullptr

Notes: - Only available on Native platform - Array size equals count - Values are SDL keyboard scancodes - Use nullptr if count is 0

Example:

// Native: Map to keyboard keys
#include <SDL2/SDL.h>

pixelroot32::input::InputConfig config(6,
    SDL_SCANCODE_UP,    // Index 0
    SDL_SCANCODE_DOWN,  // Index 1
    SDL_SCANCODE_LEFT,  // Index 2
    SDL_SCANCODE_RIGHT, // Index 3
    SDL_SCANCODE_X,     // Index 4 (Button A)
    SDL_SCANCODE_Z      // Index 5 (Button B)
);

Constructors

InputConfig(int count, ...)

Constructs a new InputConfig with variadic arguments.

Parameters: - count (int): Number of inputs to configure - ... (variadic): Variable arguments list of pins (ESP32) or scancodes (Native)

Notes: - If count <= 0, configuration is empty (nullptr arrays) - Allocates arrays dynamically based on count - Arguments must match count in number - Platform-specific: ESP32 expects int (GPIO pins), Native expects int (SDL scancodes)

ESP32 Example:

// Configure 4 directional buttons
pixelroot32::input::InputConfig config(4, 0, 2, 4, 5);
// Pin 0 = Up, Pin 2 = Down, Pin 4 = Left, Pin 5 = Right

// Configure 6 buttons (4 directions + 2 action buttons)
pixelroot32::input::InputConfig config(6, 0, 2, 4, 5, 18, 19);

Native Example:

#include <SDL2/SDL.h>

// Configure 4 directional buttons
pixelroot32::input::InputConfig config(4,
    SDL_SCANCODE_UP,
    SDL_SCANCODE_DOWN,
    SDL_SCANCODE_LEFT,
    SDL_SCANCODE_RIGHT
);

// Configure 6 buttons (4 directions + 2 action buttons)
pixelroot32::input::InputConfig config(6,
    SDL_SCANCODE_UP,
    SDL_SCANCODE_DOWN,
    SDL_SCANCODE_LEFT,
    SDL_SCANCODE_RIGHT,
    SDL_SCANCODE_X,  // Button A
    SDL_SCANCODE_Z   // Button B
);

Usage Example

ESP32 Configuration

#include "input/InputConfig.h"
#include "input/InputManager.h"
#include "core/Engine.h"

void setup() {
    // Configure input: 6 buttons
    // Pins: Up=0, Down=2, Left=4, Right=5, A=18, B=19
    pixelroot32::input::InputConfig inputConfig(6, 0, 2, 4, 5, 18, 19);

    // Create input manager
    pixelroot32::input::InputManager inputManager(inputConfig);
    inputManager.init();

    // Or use with Engine
    pixelroot32::graphics::DisplayConfig displayConfig;
    pixelroot32::core::Engine engine(displayConfig, inputConfig);
    engine.init();
    engine.run();
}

Native Configuration

#include "input/InputConfig.h"
#include <SDL2/SDL.h>

void setup() {
    // Configure input: 6 buttons mapped to keyboard
    pixelroot32::input::InputConfig inputConfig(6,
        SDL_SCANCODE_UP,    // Index 0: Up
        SDL_SCANCODE_DOWN,  // Index 1: Down
        SDL_SCANCODE_LEFT,  // Index 2: Left
        SDL_SCANCODE_RIGHT, // Index 3: Right
        SDL_SCANCODE_X,     // Index 4: Button A
        SDL_SCANCODE_Z      // Index 5: Button B
    );

    // Use with Engine
    pixelroot32::graphics::DisplayConfig displayConfig;
    pixelroot32::core::Engine engine(displayConfig, inputConfig);
    engine.init();
    engine.run();
}

Platform-Agnostic Configuration

#ifdef PLATFORM_ESP32
    // ESP32: Use GPIO pins
    pixelroot32::input::InputConfig inputConfig(6, 0, 2, 4, 5, 18, 19);
#elif PLATFORM_NATIVE
    // Native: Use SDL scancodes
    #include <SDL2/SDL.h>
    pixelroot32::input::InputConfig inputConfig(6,
        SDL_SCANCODE_UP,
        SDL_SCANCODE_DOWN,
        SDL_SCANCODE_LEFT,
        SDL_SCANCODE_RIGHT,
        SDL_SCANCODE_X,
        SDL_SCANCODE_Z
    );
#endif

Button Index Mapping

Button indices are determined by the order in the constructor:

Typical Convention: - Index 0: Up / Primary action - Index 1: Down / Secondary action - Index 2: Left - Index 3: Right - Index 4+: Additional buttons

Example:

// 4-button D-pad
InputConfig config(4, UP_PIN, DOWN_PIN, LEFT_PIN, RIGHT_PIN);
// Index 0 = Up, Index 1 = Down, Index 2 = Left, Index 3 = Right

// 6-button setup (D-pad + 2 action buttons)
InputConfig config(6, UP_PIN, DOWN_PIN, LEFT_PIN, RIGHT_PIN, A_PIN, B_PIN);
// Index 0-3 = D-pad, Index 4 = A, Index 5 = B

ESP32 Pin Considerations

  • GPIO pins: Use any available GPIO pin
  • Pull-up/pull-down: Configure resistors appropriately
  • Input mode: Pins are automatically configured as inputs
  • Restrictions: Some pins have special functions (check ESP32 datasheet)

Common Pin Choices: - GPIO 0, 2, 4, 5: Safe for buttons (watch for boot mode pins) - GPIO 18, 19: Good for additional buttons - Avoid: GPIO 6-11 (flash), GPIO 34-39 (input only, no pull-up)

Native SDL Scancode Reference

Common SDL scancodes:

  • SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT: Arrow keys
  • SDL_SCANCODE_W, SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_D: WASD
  • SDL_SCANCODE_X, SDL_SCANCODE_Z: Common action buttons
  • SDL_SCANCODE_SPACE: Spacebar
  • SDL_SCANCODE_RETURN: Enter key

Example:

// WASD + Space + Enter
pixelroot32::input::InputConfig config(6,
    SDL_SCANCODE_W,        // Up
    SDL_SCANCODE_S,        // Down
    SDL_SCANCODE_A,        // Left
    SDL_SCANCODE_D,         // Right
    SDL_SCANCODE_SPACE,    // Jump
    SDL_SCANCODE_RETURN    // Action
);

Performance Considerations

  • Memory: Arrays are allocated dynamically (small overhead)
  • Configuration: Done once at startup, no runtime cost
  • Access: Button indices are fast (array access)

ESP32 Considerations

  • Pin configuration: Ensure pins are not used by other peripherals
  • Debouncing: Hardware debouncing recommended for reliable input
  • Power: Buttons should use pull-up resistors to avoid floating pins

See Also