Struct GpioConfig

Source
pub struct GpioConfig { /* private fields */ }

Implementations§

Source§

impl GpioConfig

Source

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}
Source

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§

Source§

impl Default for GpioConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.