pub struct GpioConfig { /* private fields */ }
Implementations§
Source§impl GpioConfig
impl GpioConfig
Sourcepub fn set_direction(&mut self, pin: usize, direction: Direction)
pub fn set_direction(&mut self, pin: usize, direction: Direction)
Sets a GPIO pin to input or output.
Examples found in repository?
examples/scan_bus.rs (line 27)
14fn run() -> mcp2221::Result<()> {
15 let mut config = mcp2221::Config::default();
16 config.i2c_speed_hz = 400_000;
17 // For talking to a peripheral we might want a higher timeout, but for
18 // scanning the bus, a short timeout is good since it allows us to scan all
19 // addresses more quickly.
20 config.timeout = Duration::from_millis(10);
21 let mut dev = mcp2221::Handle::open_first(&config)?;
22
23 // Set GPIO pin 0 high. This is useful if your I2C bus goes through a level
24 // shifter and you need to enable that level shifter in order to use the I2C
25 // bus. It also serves as an example of using GPIO.
26 let mut gpio_config = mcp2221::GpioConfig::default();
27 gpio_config.set_direction(0, mcp2221::Direction::Output);
28 gpio_config.set_value(0, true);
29 dev.configure_gpio(&gpio_config)?;
30
31 // Before we start, SDA and SCL should be high. If they're not, then either
32 // the pull-up resistors are missing, the bus isn't properly connected or
33 // something on the bus is holding them low. In any case, we won't be able
34 // to operate.
35 dev.check_bus()?;
36
37 println!("{}", dev.get_device_info()?);
38
39 for base_address in (0..=127).step_by(16) {
40 for offset in 0..=15 {
41 let address = base_address + offset;
42 match dev.read(address, &mut [0u8]) {
43 Ok(_) => print!("0x{:02x}", address),
44 Err(_) => print!(" -- "),
45 }
46 }
47 println!();
48 }
49
50 Ok(())
51}
Sourcepub fn set_value(&mut self, pin: usize, value: bool)
pub fn set_value(&mut self, pin: usize, value: bool)
Sets the value of an output pin.
Examples found in repository?
examples/scan_bus.rs (line 28)
14fn run() -> mcp2221::Result<()> {
15 let mut config = mcp2221::Config::default();
16 config.i2c_speed_hz = 400_000;
17 // For talking to a peripheral we might want a higher timeout, but for
18 // scanning the bus, a short timeout is good since it allows us to scan all
19 // addresses more quickly.
20 config.timeout = Duration::from_millis(10);
21 let mut dev = mcp2221::Handle::open_first(&config)?;
22
23 // Set GPIO pin 0 high. This is useful if your I2C bus goes through a level
24 // shifter and you need to enable that level shifter in order to use the I2C
25 // bus. It also serves as an example of using GPIO.
26 let mut gpio_config = mcp2221::GpioConfig::default();
27 gpio_config.set_direction(0, mcp2221::Direction::Output);
28 gpio_config.set_value(0, true);
29 dev.configure_gpio(&gpio_config)?;
30
31 // Before we start, SDA and SCL should be high. If they're not, then either
32 // the pull-up resistors are missing, the bus isn't properly connected or
33 // something on the bus is holding them low. In any case, we won't be able
34 // to operate.
35 dev.check_bus()?;
36
37 println!("{}", dev.get_device_info()?);
38
39 for base_address in (0..=127).step_by(16) {
40 for offset in 0..=15 {
41 let address = base_address + offset;
42 match dev.read(address, &mut [0u8]) {
43 Ok(_) => print!("0x{:02x}", address),
44 Err(_) => print!(" -- "),
45 }
46 }
47 println!();
48 }
49
50 Ok(())
51}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GpioConfig
impl RefUnwindSafe for GpioConfig
impl Send for GpioConfig
impl Sync for GpioConfig
impl Unpin for GpioConfig
impl UnwindSafe for GpioConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more