Overview
InputSource is an abstract base class that defines the interface for reading physical input devices such as buttons, switches, and external controllers. Implementations of this class handle the hardware-specific details of reading inputs and updating the shared InputState structure.
Class Definition
Scan Speed Enumeration
InputScanSpeed enum allows input sources to declare their optimal scanning frequency. This enables the communication backend to selectively scan inputs based on performance requirements.
For infrequently changing inputs (e.g., configuration switches, mode selectors)
For inputs that change at moderate rates (e.g., analog sensors, external controllers)
For primary button inputs that require minimal latency (e.g., GPIO buttons, matrix switches)
Constructor & Destructor
InputSource()
~InputSource()
Pure Virtual Methods
These methods must be implemented by any concrete class derived fromInputSource.
ScanSpeed
InputScanSpeed enum value indicating how frequently this input source should be scanned.
Usage:
The communication backend uses this to implement selective scanning:
UpdateInputs
Reference to the shared input state structure to update with current button states
- Read the current hardware state
- Update the relevant fields in the
inputsstructure - Handle any necessary debouncing or filtering
- Should be fast and non-blocking
Multiple input sources can write to the same
InputState structure. Use bitwise OR operations to combine inputs without overwriting values from other sources.Built-in Implementations
HayBox includes several concrete implementations ofInputSource for common input devices:
GpioButtonInput
SwitchMatrixInput
Reads buttons arranged in a matrix configuration (rows and columns). More efficient for large numbers of buttons.DebouncedGpioButtonInput
ExtendsGpioButtonInput with software debouncing to filter mechanical switch noise.
DebouncedSwitchMatrixInput
Combines switch matrix scanning with debouncing.NunchukInput
Reads input from a Wii Nunchuk controller connected via I2C.GamecubeControllerInput
Reads input from a GameCube controller connected to a GPIO pin.Pca9671Input
Reads buttons from a PCA9671 I2C GPIO expander chip.Usage Example
Creating a Simple GPIO Input Source
Implementing a Custom Input Source
Combining Multiple Input Sources
Selective Scanning
The communication backend supports scanning input sources selectively based on their scan speed:Calling
backend.ScanInputs() without parameters scans all input sources regardless of their scan speed.Input State Structure
TheInputState structure contains all possible button states:
Best Practices
Implement
UpdateInputs() to be as fast as possible. Avoid blocking operations, delays, or excessive computation. This method is called every iteration of the main loop.Return
InputScanSpeed::FAST for primary button inputs that affect gameplay. Reserve SLOW for configuration inputs that rarely change.Debouncing Considerations
Mechanical switches require debouncing to filter bounce noise. You can implement debouncing in two ways:- Hardware debouncing: Use capacitors on button inputs
- Software debouncing: Use HayBox’s debounced input source classes
Related Classes
- CommunicationBackend - Manages input sources and scans inputs
- ControllerMode - Processes inputs into controller outputs
- KeyboardMode - Processes inputs into keyboard outputs
