Rotary Switch Helper
A Rust library for handling rotary encoders and switches on Raspberry Pi.
Overview
This library provides a clean, thread-safe interface for working with rotary encoders and switches on Raspberry Pi. It handles the debouncing, state management, and event callbacks, allowing you to focus on your application logic rather than hardware details.
Features
- Support for standalone rotary encoders
- Support for standalone switches with long press detection
- Support for rotary encoders with built-in switches (shifted mode)
- Thread-safe design using atomic operations
- Customizable callback functions for rotation and switch events
- Normal and "shifted" mode for rotary encoders with switches
- Comprehensive test suite with hardware mocking and hardware integration tests
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
Usage Examples
Recommended: Using the PiInput Wrapper
The recommended way to use this library is through the PiInput wrapper, which manages all your encoders and handles GPIO initialization:
use ;
// Callback for rotary encoders
// Callback for switches
Alternative: Direct Component Usage
While using the PiInput wrapper is recommended, you can also use the individual components directly if needed. Note that when using components directly, you'll need to manage the GPIO initialization yourself.
Basic Rotary Encoder
use ;
use Gpio;
Switch (with optional long press detection)
use switch_encoder;
use Gpio;
use Duration;
Rotary Encoder with Built-in Switch (Shifted Mode)
use rotary_encoder;
use Direction;
use Gpio;
How It Works
Rotary Encoder State Machine
The library implements a state machine to track the rotary encoder's state transitions. The encoder uses two pins (DT and CLK) which create a Gray code pattern during rotation:
State | CLK | DT
------|-----|----
0 | 0 | 0 (Resting position)
1 | 1 | 0 (First step clockwise)
3 | 1 | 1 (Second step)
2 | 0 | 1 (Third step)
0 | 0 | 0 (Back to resting - complete clockwise turn)
For counter-clockwise rotation, the sequence is reversed.
The library detects these state transitions and calls the provided callback function when a full rotation is detected.
Switch Handling
Switches are debounced and trigger callbacks on both press and release events. The library also supports long press detection - when configured with a time threshold and a long press name, the switch will trigger different callbacks for normal presses versus long presses (when the button is held down beyond the threshold).
Shifted Mode
When using a rotary encoder with a built-in switch, the library supports a "shifted" mode. When the switch is pressed, the rotary encoder enters shifted mode, allowing you to implement different behaviors for the same physical control.
Testing
The library includes a comprehensive test suite with both unit tests and hardware integration tests.
Unit Tests
Run the unit tests (which use mock objects to simulate hardware interactions) with:
Hardware Integration Tests
The library also includes hardware integration tests that require actual Raspberry Pi hardware with encoders connected. These tests verify end-to-end functionality including GPIO interrupts and callbacks.
Run hardware tests (requires manual interaction with physical hardware):
# Run all hardware tests
# Run a specific hardware test
See tests/hardware_integration_test.rs for hardware setup instructions.
Requirements
- Rust 1.56 or higher
- Raspberry Pi with GPIO pins
rppalcrate compatibility (most Raspberry Pi models)
License
This project is licensed under the MIT License.
Discalimer
Unittests and Readme are written by AI. You might want to prefer the actual code for a deeper/more correct understanding.