#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

#1797 in Hardware support

GPL-3.0 license

1MB
179 lines

Contains (WOFF font, 190KB) docs/FiraSans-Medium.woff, (WOFF font, 185KB) docs/FiraSans-Regular.woff, (WOFF font, 94KB) docs/SourceSerifPro-Bold.ttf.woff, (WOFF font, 89KB) docs/SourceSerifPro-Regular.ttf.woff, (WOFF font, 56KB) docs/SourceCodePro-Regular.woff, (WOFF font, 56KB) docs/SourceCodePro-Semibold.woff and 1 more.

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
~59K SLoC