qwiic-button-led 0.1.1

A driver for the SparkFun Qwiic Button LED
Documentation
  • Coverage
  • 100%
    49 out of 49 items documented1 out of 37 items with examples
  • Size
  • Source code size: 28.69 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.41 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • anahata0108

This crate provides an interface to the SparkFun Qwiic Button LED over the I2C protocol. It supports manipulating the LED and responding to button presses. It builds on embedded-hal and thus supports any platform which implements that crate's traits, like popular microcontrollers and Raspberry Pi models.

An example for the Raspberry Pi:

use linux_embedded_hal as hal;
use qwiic_button_led::*;
use std::{thread, time};

// The rPi model 4 B has /dev/i2c-1 as its only I2C device
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
// The Qwiic Button LED's default address is 0x6F, but is user-configurable
let address = 0x6F;
let mut button = ButtonLED::init(i2c, address);
loop {
    let status = button.button_status().unwrap();
    if status.pressed {
        // if the button is pressed, turn the LED on
        button.set_led_brightness(255).unwrap()
    } else {
        // otherwise, turn it off
        button.set_led_brightness(0).unwrap();
    }
    // sleep a bit to not hammer the I2C bus
    thread::sleep(time::Duration::from_millis(10));
}

This example turns the LED on when the button is depressed and turns the LED off when the button is released.

The button LED supports both static brightness settings and dynamic pulsing. When setting the brightness statically, the LED stays on. The pulse cycle and pulse off time values configure LED pulsing. The pulse cycle time configures how long the LED is on for while pulsing, and the off time configures how long the LED is off while pulsing.

let mut button = ButtonLED(i2c, address);
// 300 ms on, 300 ms off, in a loop
button.set_led_pulse_cycle_time(300);
button.set_led_pulse_off_time(300);