-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0385b54
Showing
7 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "pcf857x-simple" | ||
authors = ["Emeric Martineau <[email protected]>"] | ||
description = "A very simple manager of PCF8574 and PCF8575" | ||
homepage = "https://github.com/emeric-martineau/pcf857x-simple-rs" | ||
keywords = ["embedded", "PCF8574", "PCF8575", "Esp32"] | ||
license = "MIT" | ||
readme = "README.md" | ||
repository = "https://github.com/emeric-martineau/pcf857x-simple-rs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
||
[dependencies] | ||
embedded-hal = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Emeric MARTINEAU | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Manager of PFC8574 and PFC8575. | ||
|
||
This crate is very simple manager of hardware pcf8574 and pcf8575. | ||
|
||
Crate was tested on ESP32 WROOM-32. | ||
|
||
See rust documentation. | ||
|
||
## Compatibility | ||
|
||
0.1.x is compatible with embedded-hal v0.2.x | ||
|
||
## Example of use | ||
|
||
```rust | ||
use esp_idf_hal::units::KiloHertz; | ||
use esp_idf_sys as _; | ||
use std::thread; | ||
use std::time::Duration; | ||
use esp_idf_hal::peripherals::Peripherals; | ||
use esp_idf_hal::i2c::I2cDriver; | ||
use esp_idf_hal::i2c::config::Config | ||
use pcf857x_simple::pcf8574::Pcf8574; | ||
use pcf857x_simple::PCF857X_DEFAULT_ADDRESS; | ||
use pcf857x_simple::Pin; | ||
|
||
use esp_idf_hal::i2c; | ||
|
||
fn main() { | ||
esp_idf_sys::link_patches(); | ||
esp_idf_svc::log::EspLogger::initialize_default(); | ||
|
||
let peripherals = Peripherals::take().unwrap(); | ||
|
||
let scl = peripherals.pins.gpio22; | ||
let sda = peripherals.pins.gpio21; | ||
|
||
let i2c_config = Config::new() | ||
.baudrate(KiloHertz(100).into()) | ||
.scl_enable_pullup(true) | ||
.sda_enable_pullup(true); | ||
|
||
let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap(); | ||
|
||
let mut expander = Pcf8574::new(i2c_driver, PCF857X_DEFAULT_ADDRESS); | ||
|
||
loop { | ||
let _ expander.clear(); | ||
|
||
println!("All off"); | ||
thread::sleep(Duration::from_millis(1000)); | ||
|
||
let _ = expander.up_pins(Pin::P0).unwrap(); | ||
println!("P0 on"); | ||
|
||
thread::sleep(Duration::from_millis(1000)); | ||
|
||
let _ = expander.up_pins(Pin::P1).unwrap(); | ||
println!("P0, P1 on"); | ||
|
||
thread::sleep(Duration::from_millis(1000)); | ||
|
||
let _ = expander.up_pins(Pin::P2).unwrap(); | ||
println!("P0, P1, P2 on"); | ||
|
||
thread::sleep(Duration::from_millis(1000)); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
//! A Rust driver for the PCF857x I/O expanders, based on the [`embedded-hal`] traits. | ||
//! | ||
//! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal | ||
//! | ||
//! This driver is very simple. You can read/write all pins or individual pins. | ||
//! Driver use a cache to avoid read each time you want know pins status or write. | ||
//! | ||
//! ## Devices | ||
//! | ||
//! Devices consist of 8 or 16 bidirectional ports, I²C-bus interface, with three | ||
//! hardware address inputs and interrupt output. | ||
//! | ||
//! Datasheets: | ||
//! - [PCF8574x](https://www.ti.com/lit/gpn/PCF8574) | ||
//! - [PCF8575](https://www.ti.com/lit/gpn/PCF8575) | ||
//! | ||
//! ## How use-it (ESP32 example)? | ||
//! | ||
//! ### With the default address | ||
//! | ||
//! Import this crate and an `embedded_hal` implementation, then instantiate | ||
//! the device: | ||
//! | ||
//! ```no_run | ||
//! use esp_idf_hal::peripherals::Peripherals; | ||
//! use esp_idf_hal::i2c::I2cDriver; | ||
//! use esp_idf_hal::i2c::config::Config | ||
//! use pcf857x_simple::pcf8574::Pcf8574; | ||
//! use pcf857x_simple::PCF857X_DEFAULT_ADDRESS; | ||
//! | ||
//! esp_idf_sys::link_patches(); | ||
//! esp_idf_svc::log::EspLogger::initialize_default(); | ||
//! | ||
//! let peripherals = Peripherals::take().unwrap(); | ||
//! | ||
//! let scl = peripherals.pins.gpio22; | ||
//! let sda = peripherals.pins.gpio21; | ||
//! | ||
//! let i2c_config = Config::new() | ||
//! .baudrate(KiloHertz(100).into()) | ||
//! .scl_enable_pullup(true) | ||
//! .sda_enable_pullup(true); | ||
//! | ||
//! let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap(); | ||
//! | ||
//! let mut expander = Pcf8574::new(i2c_driver, PCF857X_DEFAULT_ADDRESS); | ||
//! ``` | ||
//! | ||
//! ### With helper address | ||
//! | ||
//! ```no_run | ||
//! use esp_idf_hal::peripherals::Peripherals; | ||
//! use esp_idf_hal::i2c::I2cDriver; | ||
//! use esp_idf_hal::i2c::config::Config | ||
//! use pcf857x_simple::pcf8574::Pcf8574; | ||
//! use pcf857x_simple::pcf857x_address; | ||
//! | ||
//! esp_idf_sys::link_patches(); | ||
//! esp_idf_svc::log::EspLogger::initialize_default(); | ||
//! | ||
//! let peripherals = Peripherals::take().unwrap(); | ||
//! | ||
//! let scl = peripherals.pins.gpio22; | ||
//! let sda = peripherals.pins.gpio21; | ||
//! | ||
//! let i2c_config = Config::new() | ||
//! .baudrate(KiloHertz(100).into()) | ||
//! .scl_enable_pullup(true) | ||
//! .sda_enable_pullup(true); | ||
//! | ||
//! let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap(); | ||
//! | ||
//! let mut expander = Pcf8574::new(i2c_driver, pcf857x_address(false, false, false)); | ||
//! ``` | ||
//! | ||
//! ### With direct address | ||
//! | ||
//! ```no_run | ||
//! use esp_idf_hal::peripherals::Peripherals; | ||
//! use esp_idf_hal::i2c::I2cDriver; | ||
//! use esp_idf_hal::i2c::config::Config | ||
//! use pcf857x_simple::pcf8574::Pcf8574; | ||
//! use pcf857x_simple::pcf857x_address; | ||
//! | ||
//! esp_idf_sys::link_patches(); | ||
//! esp_idf_svc::log::EspLogger::initialize_default(); | ||
//! | ||
//! let peripherals = Peripherals::take().unwrap(); | ||
//! | ||
//! let scl = peripherals.pins.gpio22; | ||
//! let sda = peripherals.pins.gpio21; | ||
//! | ||
//! let i2c_config = Config::new() | ||
//! .baudrate(KiloHertz(100).into()) | ||
//! .scl_enable_pullup(true) | ||
//! .sda_enable_pullup(true); | ||
//! | ||
//! let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap(); | ||
//! | ||
//! let mut expander = Pcf8574::new(i2c_driver, 0x24); | ||
//! ``` | ||
//! | ||
//! ### Set P0 and P7 high | ||
//! | ||
//! ```no_run | ||
//! use pcf857x_simple::Pin; | ||
//! ... | ||
//! let _ = expander.clear(); | ||
//! let _ = expander.up_pins(&[Pin::P0, Pin::P7]); | ||
//! ``` | ||
//! | ||
//! ### Read P0 and P7 high direct from device | ||
//! | ||
//! ```no_run | ||
//! use pcf857x_simple::Pin; | ||
//! ... | ||
//! let _ = expander.clear(); | ||
//! let _ = expander.read_pin(Pin::P0); | ||
//! let _ = expander.read_pin(Pin::P7); | ||
//! ``` | ||
//! | ||
//! ### Read P0 and P7 high direct from cache | ||
//! | ||
//! ```no_run | ||
//! use pcf857x_simple::Pin; | ||
//! ... | ||
//! let _ = expander.clear(); | ||
//! let _ = expander.get_pin_from_cache(Pin::P0); | ||
//! let _ = expander.get_pin_from_cache(Pin::P7); | ||
//! ``` | ||
#![no_std] | ||
|
||
pub mod pcf8574; | ||
pub mod pcf8575; | ||
|
||
/// Pin of Pcf857x | ||
#[derive(Copy, Clone)] | ||
pub enum Pin { | ||
/// Pin 0 | ||
P0 = 1, | ||
/// Pin 1 | ||
P1 = 2, | ||
/// Pin 2 | ||
P2 = 4, | ||
/// Pin 3 | ||
P3 = 8, | ||
/// Pin 4 | ||
P4 = 16, | ||
/// Pin 5 | ||
P5 = 32, | ||
/// Pin 6 | ||
P6 = 64, | ||
/// Pin 7 | ||
P7 = 128, | ||
/// Pin 10 (only PCF8575) | ||
P10 = 256, | ||
/// Pin 11 (only PCF8575) | ||
P11 = 512, | ||
/// Pin 12 (only PCF8575) | ||
P12 = 1024, | ||
/// Pin 13 (only PCF8575) | ||
P13 = 2048, | ||
/// Pin 14 (only PCF8575) | ||
P14 = 4096, | ||
/// Pin 15 (only PCF8575) | ||
P15 = 8192, | ||
/// Pin 16 (only PCF8575) | ||
P16 = 16384, | ||
/// Pin 17 (only PCF8575) | ||
P17 = 32768, | ||
} | ||
|
||
/// Defaut base address of device | ||
pub const PCF857X_DEFAULT_ADDRESS: u8 = 0x20; | ||
|
||
/// Helper to set address depending of address' pin's device | ||
pub fn pcf857x_address(a0: bool, a1: bool, a2: bool) -> u8 { | ||
let mut addr: u8 = PCF857X_DEFAULT_ADDRESS; | ||
|
||
if a0 { | ||
addr += 1; | ||
} | ||
|
||
if a1 { | ||
addr += 2; | ||
} | ||
|
||
if a2 { | ||
addr += 4; | ||
} | ||
|
||
addr | ||
} |
Oops, something went wrong.