`switch-hal`
FreeAn "on"/"off" abstraction for input and output switches - [](https://crates.io/crates/switch-hal)
FreeFree tier
About `switch-hal`
Switch-HAL is a no_std embedded Rust library that provides a zero-cost abstraction over digital input and output switches, including buttons, LEDs, transistors, and relays. It implements the embedded-hal digital traits, simplifying firmware development by hiding active-high/active-low polarity details. With a simple on/off API (e.g., led.on(), led.off()), it reduces cognitive load and maintenance burden when hardware configurations change.
Key Features
Zero-cost abstraction for input/output switches
Supports active-high and active-low configuration
Implements embedded-hal digital traits
No_std compatible for embedded systems
Reduces cognitive load and maintenance burden
Simple on/off API (e.g., led.on(), led.off())
Pros & Cons
Pros
- Zero runtime overhead abstraction
- Simplifies code by hiding active-high/active-low details
- Reduces maintenance when hardware changes (only one place to update)
- Works with any embedded-hal compatible platform
- No_std, suitable for bare-metal microcontrollers
Cons
- Limited to embedded Rust ecosystem (requires embedded-hal)
- Not for high-level application development
- Documentation primarily in Rust docs (docs.rs)
Best For
Controlling LEDs in embedded projectsReading button presses in microcontroller applicationsDriving transistors or relaysAbstracting GPIO logic in firmware
FAQ
Why use Switch-HAL instead of raw GPIO?
Raw GPIO requires you to remember pin states and active polarity everywhere. Switch-HAL abstracts this, letting you call led.on() or led.off() without worrying about active-high or active-low. If hardware changes, you only update the initialization, not every usage.