blink1rs
Control the blink(1) USB RGB LED from Rust.
A small synchronous API over hidapi, aimed at wiring a blink(1) into
alerting: red when the build breaks, green when it passes, and dark when
the thing that was watching stops running.
use ;
use Duration;
let mut b = open?;
b.fade?;
Why this crate
- hidapi, not libusb. No driver swapping on Windows, no
rusbcontext to thread around. Works on Linux, macOS and Windows. - The watchdog is first class. The device can change its own colour when your process stops talking to it. That is the thing a desktop notification cannot do.
- Colours are sent as given. Gamma correction is off by default, so
#ff8000is the byte triple the device receives, and reading it back returns exactly that. - Real error types, no stringly-typed failures, and
hidapistays out of the public API.
Install
Library only, without the CLI and its dependencies:
[]
= { = "0.2", = false }
The watchdog
Arm it, then tickle it from your health-check loop. If the loop dies, the device acts by itself.
use ;
use Duration;
let mut b = open?;
// 'D' can only go dark, stay lit, or play a pattern, so "turn red" means
// parking red in pattern line 0 and pointing the watchdog at it.
b.write_pattern_line?;
let fire = PlayPattern ;
b.watchdog_enable?;
b.set?;
loop
Firmware 204 tops out near 62 seconds no matter what you ask for, so tickle well inside the timeout.
Command line
The crate also installs a blink1rs binary.
Several devices
Blink1::list() sorts by serial number, the same order blink1-tool -d N
uses, so indices agree between the two.
for info in list?
Hardware support
| mk1 | mk2 | mk3 | mk4 | |
|---|---|---|---|---|
| set / fade | yes | yes | yes | yes |
| read colour back | no | yes | yes | yes |
| per-LED addressing | no | yes | yes | yes |
| pattern lines | 16 | 16 | 32 | 32 |
save_patterns needed |
no | yes | yes | yes |
| watchdog | yes | yes | yes | yes |
Generation is read from the serial number prefix, the same way blink1-lib
does it. Per-LED addressing and per-LED pattern lines need firmware 204 or
later; startup parameters need firmware 206 or later, or mk3 and up. This
crate documents those requirements but does not enforce them, matching the C
library; older firmware ignores the command rather than failing.
Linux
A blink(1) with no read permission reports no serial number and is skipped during enumeration, so it looks like no device is attached rather than like a permission error. Install the udev rule:
&&
Then replug the device.
Building needs libudev-dev and pkg-config, which hidapi's default
hidraw backend links against. To use a different hidapi backend, depend on
hidapi directly and configure it there; blink1rs does not re-export its
backend features, because Cargo would unify them in ways that silently
undo the choice.
Differences from blink1-tool
- Gamma is off by default.
blink1-toolapplies a correction table, so the same hex value looks different between the two. Callset_gamma(true)to match it. save_patterns()returnsOkwithout confirmation. The device stalls the USB transfer while it writes flash, so there is nothing to confirm with;blink1-libdoes the same. Read a pattern line back if you need to be sure.- Durations are
Duration. The wire format counts 10ms ticks, so values under 10ms jump instantly and anything over 655.35s is clamped.
Threads
Blink1 is Send but not Sync: move it into a thread, or share one behind
a Mutex.
Testing
The unit tests assert every report against byte vectors transcribed from
blink1-lib.c, with the source function cited in each test, so they catch a
wire-format regression without a device attached. The hardware tests must run
single-threaded: there is one HID handle per device.
License
MIT