The rotary_encoder component provides a common interface for receiving rotary encoder events.
Two rotary encoder implementations are provided:
Both implementations use the RotaryEncoder interface and report rotation and button events using callbacks.
The RotaryEncoder interface reports the following events:
Rotation callbacks receive the number of encoder steps.
Positive values indicate clockwise rotation.
Negative values indicate counterclockwise rotation.
Example:
The reported direction can be reversed when creating an encoder.
The GpioRotaryEncoder reads a rotary encoder connected directly to Raspberry Pi GPIO pins.
The encoder uses two GPIO inputs for rotation:
An optional GPIO input can be used for the encoder push button.
GpioRotaryEncoderPins uses physical Raspberry Pi header pin numbers.
For example:
In this example:
Physical header pin numbers are converted to BCM GPIO numbers using the GPIO header mapping.
The poll() method dispatches accumulated rotation events and should be called periodically. tick() remains as a backward-compatible alias.
The SeesawRotaryEncoder reads a rotary encoder through an Adafruit Seesaw I2C device.
Multiple Seesaw rotary encoders can share the same I2C bus.
Each Seesaw device must use a unique I2C address.
The default address is:
Additional encoders can be configured using different addresses.
For example:
The I2C address is configured using the address jumpers on the Seesaw board.
The Seesaw encoder monitors rotation and button events in a background thread.
The Seesaw implementation inherits the no-op poll() behavior because it monitors events in its own background thread.
A single I2C bus can be shared between multiple encoders.
Each encoder can use its own callbacks.
The GPIO implementation requires the Raspberry Pi GPIO library.
On Raspberry Pi OS:
The Python package can also be installed using:
The Seesaw implementation requires the Adafruit Blinka and Seesaw libraries.
Install the required Python packages using:
I2C must be enabled on the Raspberry Pi when using Seesaw rotary encoders.
I2C can be enabled using:
Select:
The connected I2C devices can be displayed using i2cdetect.
Install the I2C tools:
Scan the I2C bus:
Example output with three Seesaw encoders:
The addresses 0x36, 0x37, and 0x38 indicate three detected Seesaw devices.
Example CLI applications are provided in the component_test directory.
Run the GPIO rotary encoder component test from the project root:
The pin arguments use physical Raspberry Pi header pin numbers.
Example output:
Use the --reverse option to reverse the reported rotation direction:
Press Ctrl+C to stop the component test.
The Seesaw component test can monitor one or more rotary encoders on a shared I2C bus. Pass the addresses of all connected encoders to --addresses. Each address must be unique.
When --addresses is omitted, the test uses three encoders at:
Run the component test from the project root:
To test a single encoder:
To test two encoders:
Additional addresses can be supplied in the same way to test any number of encoders:
Encoders are named encoder-1 through encoder-N in the order their addresses appear on the command line.
Example output:
Use the --reverse option to reverse the reported rotation direction for all encoders:
Each Seesaw position counter is zeroed when its encoder is initialized. Position changes are then read from the device's incremental-delta register instead of being calculated from its signed 32-bit absolute position. Invalid half-range deltas are ignored, so a normal encoder step is not reported as a large -2147483648 change.
Press Ctrl+C to stop the component test.
The Car UI uses encoder 0x36 as the system-volume encoder by default. Clockwise and counterclockwise steps from that encoder are routed to global volume up and volume down operations regardless of which panel is displayed. Pressing that encoder toggles system mute.
All configured encoders are wrapped by RotaryEncoderInputAdapter, which publishes normalized InputEvent values through the frontend-thread queue. InputMapper assigns semantic UiAction values, and CarUiFrontend offers those actions to the active ScreenUiIf before applying shell-level behavior. Screens never receive physical encoder addresses, pins, callbacks, or device objects. The volume encoder remains globally mapped to volume and mute actions.
The configured encoder list and volume role are set in config/runtime.toml:
The driver may be seesaw or gpio. Seesaw devices require a unique 7-bit address; GPIO devices require unique physical header pins. volume_index selects one entry in device order and is independent of the hardware driver.
The Car UI input runtime receives only RotaryEncoderIf instances and logical device identifiers. Seesaw addresses and GPIO pins remain confined to configuration and runtime hardware construction.
Run the configuration-driven volume test from the project root:
The test loads config/runtime.toml, constructs and starts only the device selected by volume_index through RotaryEncoderIf, and routes it to the real PipeWire system-volume controller. Contextual encoders are intentionally not started, so a missing panel encoder cannot block this test. Each detected step prints the resulting volume:
The test changes the actual default audio sink volume. It requires wpctl and the configured encoder hardware. Use --config for another runtime TOML, --volume-steps to change the reported range, or --step-percent to change the PipeWire adjustment made per encoder step. The default is 20 reported levels because the default 5% PipeWire increment divides the full range into 20 steps. The Car UI's eight-bar volume indicator is a separate visual scale. Positive adjustments are limited to 100% to prevent PipeWire amplification. In the full application, VolumeManager maps audio levels 0..20 proportionally onto indicator bars 0..8. When muted, all eight bars use the red muted color while preserving the underlying level for unmute.
The rotary encoder interface reports rotation and button events using callbacks.
Hardware-specific behavior is contained within each rotary encoder implementation.
The GPIO implementation uses Raspberry Pi GPIO interrupts and dispatches accumulated rotation through the interface's poll() method.
The Seesaw implementation reads encoder state over I2C and monitors the devices using background threads.