#i2c #pwm #raspberry-pi #chip #pca9685 #lib #address

pca9685_lib

A library for communicating to a PCA9685 Chip for Raspberry Pi

3 releases

0.2.2 Jun 23, 2020
0.2.1 Jun 20, 2020
0.2.0 Jun 20, 2020
0.1.1 Jun 18, 2020
0.1.0 Jun 18, 2020

#1670 in Hardware support

GPL-3.0 license

1MB
179 lines

pca9685_lib Version Badge

Raspberry Pi drivers for the PCA9685 Documentation

This uses tokio to allow for other tasks to be run while the program runs for an (admittedly short period) of time, 500us, but since rustaceans are writing in systems level code, I thought this might squeeze a little more performance.

Quickstart

use rppal::i2c::{I2c};
use pca9865_lib::PCA9685;
use tokio::time::delay_for;
use std::time::Duration;

#[tokio::main]
async fn main() {
    //Create a new device with address 0x40. Note mutability.
    let mut device = PCA9685::new(0x40, I2c::new().unwrap()).unwrap();

    //Set the refresh rate to 50Hz, (re)start the chip when complete
    if let Err(_e) = device.set_prescale_fr(50, true).await {
        panic!();
    }

    //Servo fun time
    loop {
        
        //The chip divides the refresh rate into 4096 blocks
        //The first tuple is which block to turn on the pulse
        //The second tuple is which block to turn the pulse off (in this case ~two milliseconds after);
        if let Err(_e) = device.set_channel(0, (0, 0), (0x01, 0x97)) {
            panic!();
        }
        delay_for(Duration::from_secs(2)).await;
        //Set Mid
        if let Err(_e) = device.set_channel(0, (0, 0), (0x01, 0x33)) {
            panic!();
        }
        delay_for(Duration::from_secs(2)).await;
        //Set Min
        if let Err(_e) = device.set_channel(0, (0, 0), (0x00, 0xCD)) {
            panic!();
        }
        delay_for(Duration::from_secs(2)).await;
        //Set Mid
        if let Err(_e) = device.set_channel(0, (0, 0), (0x01, 0x33)) {
            panic!();
        }
        delay_for(Duration::from_secs(2)).await;
    }

}

Dependencies

~4MB
~57K SLoC