mcp/
mcp.rs

1//! MCP23017 GPIO expander test:
2//! connect A7 to any B pin, touch B0 to ground, watch that other B pin follow
3//!
4//! See https://github.com/lucazulian/mcp23017 for a full driver
5
6const IODIRA: u8 = 0x00;
7const IODIRB: u8 = 0x01;
8const GPIOA: u8 = 0x12;
9const GPIOB: u8 = 0x13;
10const GPPUB: u8 = 0x0d;
11
12fn main() {
13    let adr = 0x20;
14    use embedded_hal::i2c::blocking::*;
15    let mut iic = freebsd_embedded_hal::I2cBus::from_unit(1).unwrap();
16    iic.write(adr, &[IODIRA, 0]).unwrap(); // A* are outputs
17    iic.write(adr, &[IODIRB, 0xff]).unwrap(); // B* are inputs
18    iic.write(adr, &[GPPUB, 0xff]).unwrap(); // B* are pulled up
19    loop {
20        let mut bank_b: [u8; 1] = [0x69];
21        iic.write_read(adr, &[GPIOB], &mut bank_b).unwrap();
22        eprintln!("GPIOB: {:x} ({:b})", bank_b[0], bank_b[0]);
23        std::thread::sleep(std::time::Duration::from_millis(10));
24        iic.write(adr, &[GPIOA, (bank_b[0] & 1) << 7]).unwrap();
25        std::thread::sleep(std::time::Duration::from_millis(10));
26    }
27}