Skip to content

A platform-independent driver for interacting with the products GPIO Port Expander (Troyka Module), Troyka HAT and Slot Expander Expansion Board

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

amperka/gpio-expander-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gpio-expander

Version Docs

This is a crate that provides a general abstraction for an I²C port expander used in the GPIO Port Expander (Troyka Module), Troyka HAT and Slot Expander Expansion Board products. This abstraction is not necessarily the most performant, but it allows pins to be used in the same way as direct GPIOs. Because pin types also implement embedded-hal digital I/O characteristics, they can also be passed to subsequent drivers (for example, as a reset or chip select pin).

Example

use std::error::Error;
use std::thread;
use std::time::Duration;

use gpio_expander::{prelude::*, GpioExpander};
use rppal::i2c::I2c;

fn main() -> Result<(), Box<dyn Error>> {
    // Initializing an I²C Peripheral from HAL
    let i2c = I2c::with_bus(1)?;

    // Initializing GpioExpander with default I²C address
    let mut expander = GpioExpander::new(i2c, None);
    let expander_pins = expander.pins();

    // Pin 0 into output mode
    let mut led = expander_pins.p00.into_output()?;

    loop {
        led.set_high()?;
        thread::sleep(Duration::from_secs(1));
        led.set_low()?;
        thread::sleep(Duration::from_secs(1));
    }
}

More examples in the examples folder.

Documentation

The documentation can be found at docs.rs.

Use in multithread

gpio-expander uses the BusMutex from shared-bus under the hood. This means you can also make the pins shareable across task/thread boundaries, given that you provide an appropriate mutex type:

// Initializing GpioExpander with default I²C address, and alternative Mutex
let mut expander: GpioExpander<std::sync::Mutex<_>> = GpioExpander::new(i2c, None);

The default configuration allows pins to be used in single-threaded mode, for multi-threaded mode, use the example above (Mutex implementation depends on the platform you are using).

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

A platform-independent driver for interacting with the products GPIO Port Expander (Troyka Module), Troyka HAT and Slot Expander Expansion Board

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages