### Configure Pin as Output and Set High Source: https://docs.rs/pca9535/2.0.0/pca9535 Demonstrates using the `StandardExpanderInterface` to configure a specific pin as an output and then set its state to high. Ensure the pin is correctly configured before use. ```rust use rppal::i2c::I2c; use pca9535::GPIOBank; use pca9535::StandardExpanderInterface; use pca9535::Pca9535Immediate; let i2c = I2c::new().unwrap(); let address = 32; let mut expander = Pca9535Immediate::new(i2c, address); //Either Immediate or Cached expander expander.pin_into_output(GPIOBank::Bank0, 3).unwrap(); expander.pin_set_high(GPIOBank::Bank0, 3).unwrap(); // and so on... ``` -------------------------------- ### Get Type ID Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Returns the `TypeId` of the current type. This is part of the `Any` trait implementation. ```rust fn type_id(&self) -> TypeId> ``` -------------------------------- ### Get Cached Register Value Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/cached.rs.html Retrieves the cached value for a specified PCA9535 register. This method avoids I2C transactions by returning the value stored in the struct's fields. ```rust fn get_cached(&self, register: Register) -> u8 { match register { Register::InputPort0 => self.input_port_0, Register::InputPort1 => self.input_port_1, Register::OutputPort0 => self.output_port_0, Register::OutputPort1 => self.output_port_1, Register::PolarityInversionPort0 => self.polarity_inversion_port_0, Register::PolarityInversionPort1 => self.polarity_inversion_port_1, Register::ConfigurationPort0 => self.configuration_port_0, Register::ConfigurationPort1 => self.configuration_port_1, } } ``` -------------------------------- ### Module: immediate Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/index.html Contains the implementation of the Immediate Expander interface. ```APIDOC ## Module immediate ### Description Contains the implementation of the Immediate Expander interface. ``` -------------------------------- ### ExpanderOutputPin::new Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderOutputPin.html Creates a new `ExpanderOutputPin` instance. ```APIDOC ## Function ExpanderOutputPin::new ### Description Create a new output pin. ### Method `pub fn new(expander: &'a Io, bank: GPIOBank, pin: u8, state: PinState) -> Result>` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None #### Arguments * `expander` (&'a Io): A reference to the `SyncExpander` instance. * `bank` (GPIOBank): The GPIO bank the pin belongs to (e.g., 0 or 1). * `pin` (u8): The pin number within the bank (0-7). * `state` (PinState): The initial state of the pin (e.g., `PinState::High` or `PinState::Low`). ### Returns A `Result` containing the new `ExpanderOutputPin` instance on success, or an `ExpanderError` on failure. ### Panics The function will panic if the provided pin is not in the allowed range of 0-7. ``` -------------------------------- ### Module: io Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/index.html Contains the implementation to make an `Expander` Sync. ```APIDOC ## Module io ### Description Contains the implementation to make an `Expander` Sync. ``` -------------------------------- ### Create Immediate PCA9535 Instance Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/immediate.rs.html Initializes a new PCA9535 immediate expander instance. Panics if the device hardware address is outside the range of 32-39. ```rust pub fn new(i2c: I2C, address: u8) -> Self { assert!(address > 31 && address < 40); Self { address, i2c } } ``` -------------------------------- ### Initialize IO Expander with Mutex Wrapper Source: https://docs.rs/pca9535/2.0.0/pca9535 Initializes an I2C connection and wraps a PCA9535 expander in an `IoExpander` with a `Mutex` for thread-safe access in a standard environment. Ensure the 'std' feature is enabled for this implementation. ```rust use std::sync::Mutex; use rppal::i2c::I2c; use pca9535::IoExpander; use pca9535::Pca9535Immediate; let i2c = I2c::new().unwrap(); let address = 32; let mut expander = Pca9535Immediate::new(i2c, address); //Either Immediate or Cached expander let io_expander: IoExpander<_, _, Mutex<_>> = IoExpander::new(expander); // Wrapped expander in std environment using Mutex as ExpanderMutex ``` -------------------------------- ### Create and Use Expander HAL Pins Source: https://docs.rs/pca9535/2.0.0/pca9535 Demonstrates creating input and output pins from a wrapped IO expander. Requires the 'std' feature to be enabled. Pins implement standard HAL traits for GPIO manipulation. ```rust use std::sync::Mutex; use rppal::i2c::I2c; use hal::digital::{InputPin, OutputPin}; use pca9535::IoExpander; use pca9535::Pca9535Immediate; use pca9535::{ExpanderInputPin, ExpanderOutputPin}; use pca9535::GPIOBank::{Bank0, Bank1}; use pca9535::PinState; let i2c = I2c::new().unwrap(); let address = 32; let mut expander = Pca9535Immediate::new(i2c, address); let io_expander: IoExpander<_, _, Mutex<_>> = IoExpander::new(expander); let mut expander_pin_1_5 = ExpanderInputPin::new(&io_expander, Bank1, 5).unwrap(); let mut expander_pin_0_2 = ExpanderOutputPin::new(&io_expander, Bank0, 2, PinState::Low).unwrap(); expander_pin_0_2.set_high(); let is_high = expander_pin_1_5.is_high(); // and so on... ``` -------------------------------- ### IoExpander::new Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/io/struct.IoExpander.html Creates a new IoExpander instance from an existing Expander. ```APIDOC ## IoExpander::new ### Description Creates a new IoExpander instance out of an Expander. ### Method `pub fn new(expander: Ex) -> IoExpander` ### Parameters - **expander** (Ex): The Expander instance to wrap. ### Returns - IoExpander: A new IoExpander instance. ### Example ```rust // let expander: SomeExpanderType = ...; // let io_expander = IoExpander::<_ , _ , _>::new(expander); ``` ``` -------------------------------- ### ExpanderInputPin Methods Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderInputPin.html Methods available for creating and configuring an ExpanderInputPin. ```APIDOC ## impl<'a, I2C, E, Io> ExpanderInputPin<'a, I2C, Io> ### pub fn new(expander: &'a Io, bank: GPIOBank, pin: u8) -> Result> #### Description Create a new input pin. #### Panics The function will panic if the provided pin is not in the allowed range of 0-7. ### pub fn set_polarity(&mut self, polarity: Polarity) -> Result<(), ExpanderError> #### Description Sets the polarity of the input pin. The input pins have normal polarity by default on device startup. If the polarity is `Polarity::Normal` a logic `high` voltage level on the input is detected as `high` in the software. If the polarity is `Polarity::Inverse` a logic `high` voltage level on the input is detected as `low` by the software. ``` -------------------------------- ### Configure Pin as Output Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Configures a specified GPIO pin to operate as an output. ```rust fn pin_into_output( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> ``` -------------------------------- ### Create New Pca9535Cached Instance Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/cached/struct.Pca9535Cached.html Initializes a new Pca9535Cached instance. Set `init_defaults` to true if no prior transactions have been made with the device to assume default register values. Otherwise, set to false to read existing register states. ```rust pub fn new( i2c: I2C, address: u8, interrupt_pin: IP, init_defaults: bool, ) -> Result> ``` -------------------------------- ### Create New PCA9535Cached Instance Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/cached.rs.html Constructs a new PCA9535Cached instance. The `init_defaults` flag determines whether to initialize the cache by reading device registers or assume default values. Panics if the address is out of range. ```rust pub fn new( i2c: I2C, address: u8, interrupt_pin: IP, init_defaults: bool, ) -> Result> { assert!(address > 31 && address < 40); let mut expander = Self { address, i2c, interrupt_pin, input_port_0: 0x00, input_port_1: 0x00, output_port_0: 0xFF, output_port_1: 0xFF, polarity_inversion_port_0: 0x00, polarity_inversion_port_1: 0x00, configuration_port_0: 0xFF, configuration_port_1: 0xFF, }; if !init_defaults { Self::init_cache(&mut expander)?; } Ok(expander) } ``` -------------------------------- ### Configure Pin as Input Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Configures a specified GPIO pin to operate as an input. ```rust fn pin_into_input( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> ``` -------------------------------- ### Create PCA9535 Input Pin Source: https://docs.rs/pca9535/2.0.0/src/pca9535/pin.rs.html Initializes a new input pin for the PCA9535 expander. Panics if the pin number is out of range (0-7). ```rust pub fn new(expander: &'a Io, bank: GPIOBank, pin: u8) -> Result> { assert!(pin < 8); let register = match bank { GPIOBank::Bank0 => Register::ConfigurationPort0, GPIOBank::Bank1 => Register::ConfigurationPort1, }; let mut reg_val: u8 = 0x00; expander.read_byte(register, &mut reg_val)?; expander.write_byte(register, reg_val | (0x01 << pin))?; Ok(Self { expander, bank, pin, phantom_data: PhantomData, }) } ``` -------------------------------- ### Pin Configuration and State Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/cached/struct.Pca9535Cached.html APIs for configuring pin direction, checking pin state, and setting polarity. ```APIDOC ## POST /websites/rs_pca9535_2_0_0/pin_is_low ### Description Checks if the input state of the given pin is `low`. This function works with pins configured as inputs as well as outputs. ### Method POST ### Endpoint /websites/rs_pca9535_2_0_0/pin_is_low ### Parameters #### Path Parameters - **bank** (GPIOBank) - Required - The GPIO bank to operate on. - **pin** (u8) - Required - The pin number. ### Request Body ```json { "bank": "A", "pin": 0 } ``` ### Response #### Success Response (200) - **result** (bool) - True if the pin is low, false otherwise. #### Response Example ```json { "result": true } ``` ``` ```APIDOC ## POST /websites/rs_pca9535_2_0_0/pin_into_input ### Description Configures the given pin as input. ### Method POST ### Endpoint /websites/rs_pca9535_2_0_0/pin_into_input ### Parameters #### Path Parameters - **bank** (GPIOBank) - Required - The GPIO bank to operate on. - **pin** (u8) - Required - The pin number. ### Request Body ```json { "bank": "A", "pin": 0 } ``` ### Response #### Success Response (200) - **result** (string) - Indicates success or failure. #### Response Example ```json { "result": "success" } ``` ``` ```APIDOC ## POST /websites/rs_pca9535_2_0_0/pin_into_output ### Description Configures the given pin as output. ### Method POST ### Endpoint /websites/rs_pca9535_2_0_0/pin_into_output ### Parameters #### Path Parameters - **bank** (GPIOBank) - Required - The GPIO bank to operate on. - **pin** (u8) - Required - The pin number. ### Request Body ```json { "bank": "A", "pin": 0 } ``` ### Response #### Success Response (200) - **result** (string) - Indicates success or failure. #### Response Example ```json { "result": "success" } ``` ``` ```APIDOC ## POST /websites/rs_pca9535_2_0_0/pin_inverse_polarity ### Description Sets the input polarity of the given pin to inverted. ### Method POST ### Endpoint /websites/rs_pca9535_2_0_0/pin_inverse_polarity ### Parameters #### Path Parameters - **bank** (GPIOBank) - Required - The GPIO bank to operate on. - **pin** (u8) - Required - The pin number. ### Request Body ```json { "bank": "A", "pin": 0 } ``` ### Response #### Success Response (200) - **result** (string) - Indicates success or failure. #### Response Example ```json { "result": "success" } ``` ``` ```APIDOC ## POST /websites/rs_pca9535_2_0_0/pin_normal_polarity ### Description Sets the input polarity of the given pin to normal. ### Method POST ### Endpoint /websites/rs_pca9535_2_0_0/pin_normal_polarity ### Parameters #### Path Parameters - **bank** (GPIOBank) - Required - The GPIO bank to operate on. - **pin** (u8) - Required - The pin number. ### Request Body ```json { "bank": "A", "pin": 0 } ``` ### Response #### Success Response (200) - **result** (string) - Indicates success or failure. #### Response Example ```json { "result": "success" } ``` ``` -------------------------------- ### PinState Blanket Implementations Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.PinState.html Details blanket implementations for PinState, covering traits like Any, Borrow, BorrowMut, CloneToUninit, From, Into, and TryFrom/TryInto. ```APIDOC ## Blanket Implementations ### impl Any for T #### fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. ### impl Borrow for T #### fn borrow(&self) -> &T Immutably borrows from an owned value. ### impl BorrowMut for T #### fn borrow_mut(&mut self) -> &mut T Mutably borrows from an owned value. ### impl CloneToUninit for T #### unsafe fn clone_to_uninit(&self, dest: *mut u8) Performs copy-assignment from `self` to `dest`. ### impl From for T #### fn from(t: T) -> T Returns the argument unchanged. ### impl Into for T #### fn into(self) -> U Calls `U::from(self)`. ### impl TryFrom for T #### type Error = Infallible #### fn try_from(value: U) -> Result>::Error> Performs the conversion. ### impl TryInto for T #### type Error = >::Error #### fn try_into(self) -> Result>::Error> Performs the conversion. ``` -------------------------------- ### Register Helper Methods Source: https://docs.rs/pca9535/2.0.0/src/pca9535/lib.rs.html Methods to identify register types for input and polarity inversion. ```rust /// Returns true if register is an input register fn is_input(&self) -> bool { matches!(self, Self::InputPort0 | Self::InputPort1) } /// Returns true if register is a polarity inversion register fn is_polarity_inversion(&self) -> bool { matches!( self, Self::PolarityInversionPort0 | Self::PolarityInversionPort1 ) } ``` -------------------------------- ### Write Halfword to Output Port 1 and Read Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.Register.html Illustrates writing a halfword to the second output port register and subsequently reading byte values from both output port registers. This shows how writing to one register of a pair impacts the other. ```rust expander.write_halfword(Register::OutputPort1, 0x4A07 as u16).unwrap(); let mut output_bank0: u8 = 0x00; let mut output_bank1: u8 = 0x00; expander.read_byte(Register::OutputPort0, &mut output_bank0).unwrap(); expander.read_byte(Register::OutputPort1, &mut output_bank1).unwrap(); assert_eq!(output_bank0, 0x07 as u8); assert_eq!(output_bank1, 0x4A as u8); ``` -------------------------------- ### Implement OutputPin for ExpanderOutputPin Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderOutputPin.html Provides implementations for driving the pin low, high, or setting its state based on a PinState enum. These methods return a Result indicating success or an ExpanderError. ```rust fn set_low(&mut self) -> Result<(), Self::Error> ``` ```rust fn set_high(&mut self) -> Result<(), Self::Error> ``` ```rust fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> ``` -------------------------------- ### Create ExpanderOutputPin Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderOutputPin.html Creates a new output pin instance. Panics if the provided pin number is outside the valid range of 0-7. ```rust pub fn new( expander: &'a Io, bank: GPIOBank, pin: u8, state: PinState, ) -> Result> ``` -------------------------------- ### Generic `CloneToUninit::clone_to_uninit` Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.PinState.html Nightly-only experimental implementation of `CloneToUninit::clone_to_uninit` for generic types `T` that implement `Clone`. This performs copy-assignment to an uninitialized memory location. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Create New Pca9535Immediate Instance Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Initializes a new Pca9535Immediate instance. Panics if the provided I2C address is out of the valid range (32-39). ```rust pub fn new(i2c: I2C, address: u8) -> Self> ``` -------------------------------- ### Create PCA9535 Output Pin Source: https://docs.rs/pca9535/2.0.0/src/pca9535/pin.rs.html Initializes a new output pin for the PCA9535 expander with a specified initial state. Panics if the pin number is out of range (0-7). ```rust pub fn new( expander: &'a Io, bank: GPIOBank, pin: u8, state: PinState, ) -> Result> { assert!(pin < 8); let cp_register = match bank { GPIOBank::Bank0 => Register::ConfigurationPort0, GPIOBank::Bank1 => Register::ConfigurationPort1, }; let op_register = match bank { GPIOBank::Bank0 => Register::OutputPort0, GPIOBank::Bank1 => Register::OutputPort1, }; let mut reg_val: u8 = 0x00; expander.read_byte(op_register, &mut reg_val)?; if let PinState::High = state { expander.write_byte(op_register, reg_val | (0x01 << pin))?; } else { expander.write_byte(op_register, reg_val & !(0x01 << pin))?; } expander.read_byte(cp_register, &mut reg_val)?; expander.write_byte(cp_register, reg_val & !(0x01 << pin))?; Ok(Self { expander, bank, pin, phantom_data: PhantomData, }) } ``` -------------------------------- ### Create ExpanderInputPin Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderInputPin.html Creates a new input pin instance. Panics if the pin number is outside the valid range of 0-7. Requires a reference to the expander, the bank, and the pin number. ```rust pub fn new( expander: &'a Io, bank: GPIOBank, pin: u8, ) -> Result> ``` -------------------------------- ### StandardExpanderInterface Methods Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/standard/trait.StandardExpanderInterface.html This section details the methods available in the StandardExpanderInterface trait for interacting with the PCA9535 expander. ```APIDOC ## StandardExpanderInterface ### Description Standard expander interface not using `hal`. This interface does not track the state of the pins! Therefore, the user needs to ensure the pins are in input or output configuration before proceeding to call functions related to input or output pins. Otherwise, the results of those functions might not cause the expected behavior of the device. ### Methods #### `pin_set_high(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError>` Sets the specified pin to a high state. #### `pin_set_low(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError>` Drives the specified pin low. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `pin_is_high(&mut self, bank: GPIOBank, pin: u8) -> Result>` Checks if the input state of the given pin is `high`. This function works with pins configured as inputs as well as outputs. The function result does not necessarily represent the logic level of the applied voltage at the given pin but the value inside the input register of the device. Which is `1` or `0` Depending on the current polarity inversion configuration of the pin. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `pin_is_low(&mut self, bank: GPIOBank, pin: u8) -> Result>` Checks if the input state of the given pin is `low`. This function works with pins configured as inputs as well as outputs. The function result does not necessarily represent the logic level of the applied voltage at the given pin but the value inside the input register of the device. Which is `1` or `0` Depending on the current polarity inversion configuration of the pin. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `pin_into_input(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError>` Configures the specified pin as input. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `pin_into_output(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError>` Configures the specified pin as output. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `pin_inverse_polarity(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError>` Sets the input polarity of the specified pin to inverted. A logic high voltage applied at this input pin results in a `0` written to the device's input register and thus being registered as `low` by the driver. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `pin_normal_polarity(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError>` Sets the input polarity of the specified pin to normal. A logic high voltage applied at an input pin results in a `1` written to the device's input register, thus being registered as `high` by the driver. ##### Panics The function will panic if the provided pin is not in the allowed range of 0-7. #### `inverse_polarity(&mut self) -> Result<(), ExpanderError>` Sets the input polarity of all pins to inverted. A logic high voltage applied at an input pin results in a `0` written to the device's input register, thus being registered as `low` by the driver. #### `normal_polarity(&mut self) -> Result<(), ExpanderError>` Sets the input polarity of all pins to normal. A logic high voltage applied at an input pin results in a `1` written to the device's input register, thus being registered as `high` by the driver. ``` -------------------------------- ### Module: standard Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/index.html Implements the standard interface for all types implementing the `Expander` trait. ```APIDOC ## Module standard ### Description Implements the standard interface for all types implementing the `Expander` trait. ``` -------------------------------- ### Initialize Cache for PCA9535 Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/cached.rs.html Reads all necessary device registers (Configuration, Input, Output, Polarity Inversion) from the PCA9535 and populates the internal cache. Handles potential I2C write-read errors. ```rust fn init_cache(expander: &mut Self) -> Result<(), ExpanderError> { let mut buf: [u8; 2] = [0x00, 0x00]; expander .i2c .write_read( expander.address, &[Register::ConfigurationPort0 as u8], &mut buf, ) .map_err(ExpanderError::WriteReadError)?; expander.configuration_port_0 = buf[0]; expander.configuration_port_1 = buf[1]; expander .i2c .write_read(expander.address, &[Register::InputPort0 as u8], &mut buf) .map_err(ExpanderError::WriteReadError)?; expander.input_port_0 = buf[0]; expander.input_port_1 = buf[1]; expander .i2c .write_read(expander.address, &[Register::OutputPort0 as u8], &mut buf) .map_err(ExpanderError::WriteReadError)?; expander.output_port_0 = buf[0]; expander.output_port_1 = buf[1]; expander .i2c .write_read( expander.address, &[Register::PolarityInversionPort0 as u8], &mut buf, ) .map_err(ExpanderError::WriteReadError)?; expander.polarity_inversion_port_0 = buf[0]; expander.polarity_inversion_port_1 = buf[1]; Ok(()) } ``` -------------------------------- ### From Trait Implementation Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Allows creating an instance of a type from another type. ```APIDOC ## POST /websites/rs_pca9535_2_0_0/from ### Description Creates an instance of a type from another type. This implementation returns the argument unchanged. ### Method POST ### Endpoint /websites/rs_pca9535_2_0_0/from ### Parameters #### Request Body - **t** (T) - Required - The value to convert from. ### Response #### Success Response (200) - **T** (T) - The created instance, which is the same as the input value. ``` -------------------------------- ### Write and Read Halfword to Output Ports Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.Register.html Demonstrates writing a halfword to one output port register and then reading individual byte values from both output port registers. Note that writing to one register of a pair affects the other. ```rust expander.write_halfword(Register::OutputPort0, 0x4A07 as u16).unwrap(); let mut output_bank0: u8 = 0x00; let mut output_bank1: u8 = 0x00; expander.read_byte(Register::OutputPort0, &mut output_bank0).unwrap(); expander.read_byte(Register::OutputPort1, &mut output_bank1).unwrap(); assert_eq!(output_bank0, 0x4A as u8); assert_eq!(output_bank1, 0x07 as u8); ``` -------------------------------- ### Configure Pin as Output - PCA9535 Expander Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/standard.rs.html Configures a specified pin as an output. This modifies the device's configuration register. Panics if the pin number is out of the 0-7 range. ```rust fn pin_into_output(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError> { assert!(pin < 8); let register = match bank { GPIOBank::Bank0 => Register::ConfigurationPort0, ``` -------------------------------- ### Initialize Immediate PCA9535 Expander Source: https://docs.rs/pca9535/2.0.0/pca9535 Instantiates the `Pca9535Immediate` expander. This interface issues an I2C transaction on each function call and does not use the interrupt pin. ```rust use rppal::i2c::I2c; use pca9535::Pca9535Immediate; let i2c = I2c::new().unwrap(); let address = 32; let expander = Pca9535Immediate::new(i2c, address); ``` -------------------------------- ### Implement Copy for GPIOBank Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.GPIOBank.html Indicates that GPIOBank can be copied implicitly. This is suitable for simple enums where values are cheap to copy. ```rust impl Copy for GPIOBank ``` -------------------------------- ### PCA9535 Immediate Expander API Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/immediate.rs.html This section details the PCA9535 immediate expander interface, which allows for direct interaction with the device's registers. ```APIDOC ## Pca9535Immediate ### Description Represents an immediate PCA9535 expander instance, providing direct register access. ### Methods #### `new(i2c: I2C, address: u8) -> Self` Creates a new immediate PCA9535 instance. **Parameters:** - `i2c` (I2C): The I2C peripheral to use. - `address` (u8): The hardware address of the PCA9535 device. Must be in the range of 32-39. **Panics:** If the given device hardware address is outside the permittable range of `32-39`. #### `destroy(self) -> I2C` Destroys the expander struct, returning the contained I2C peripheral. ### Trait Implementations #### `Expander` ##### `write_byte(&mut self, register: Register, data: u8) -> Result<(), ExpanderError>` Writes one byte to the given register. Use with caution, as simpler methods are usually available. **Parameters:** - `register` (Register): The target register. - `data` (u8): The byte data to write. **Returns:** `Ok(())` on success, or `Err(ExpanderError::WriteError)` on failure. ##### `read_byte(&mut self, register: Register, buffer: &mut u8) -> Result<(), ExpanderError>` Reads one byte from the given register. Use with caution, as simpler methods are usually available. **Parameters:** - `register` (Register): The register to read from. - `buffer` (&mut u8): A mutable reference to store the read byte. **Returns:** `Ok(())` on success, or `Err(ExpanderError::WriteReadError)` on failure. ##### `write_halfword(&mut self, register: Register, data: u16) -> Result<(), ExpanderError>` Writes one halfword (16 bits) to the given register. Use with caution, as simpler methods are usually available. **Parameters:** - `register` (Register): The target register. Note: Register pairs might affect behavior. - `data` (u16): The halfword data to write. **Returns:** `Ok(())` on success, or `Err(ExpanderError::WriteError)` on failure. ##### `read_halfword(&mut self, register: Register, buffer: &mut u16) -> Result<(), ExpanderError>` Reads one halfword (16 bits) from the given register. Use with caution, as simpler methods are usually available. **Parameters:** - `register` (Register): The register to read from. Note: Register pairs might affect behavior. - `buffer` (&mut u16): A mutable reference to store the read halfword. **Returns:** `Ok(())` on success, or `Err(ExpanderError::WriteReadError)` on failure. #### `StandardExpanderInterface` This trait is implemented but has no specific methods documented here, implying it reuses or extends functionality from the `Expander` trait. ``` -------------------------------- ### Pca9535Cached::new Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/cached/struct.Pca9535Cached.html Creates a new cached PCA9535 instance. Handles initialization of device registers based on the `init_defaults` flag. ```APIDOC ## pub fn new( i2c: I2C, address: u8, interrupt_pin: IP, init_defaults: bool, ) -> Result> ### Description Creates a new cached PCA9535 instance. ### Parameters * `i2c`: The I2C peripheral to use for communication. * `address`: The I2C address of the PCA9535 device. * `interrupt_pin`: The interrupt pin connected to the PCA9535. * `init_defaults`: If `true`, assumes default register values and avoids initial bus transaction. If `false`, reads all registers to cache their current state. ### Returns A `Result` containing the new `Pca9535Cached` instance or an `ExpanderError`. ### Panics If the given device hardware address is outside the permittable range of `32-39`. ``` -------------------------------- ### Configure Pin as Input - PCA9535 Expander Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/standard.rs.html Configures a specified pin as an input. This modifies the device's configuration register. Panics if the pin number is out of the 0-7 range. ```rust fn pin_into_input(&mut self, bank: GPIOBank, pin: u8) -> Result<(), ExpanderError> { assert!(pin < 8); let register = match bank { GPIOBank::Bank0 => Register::ConfigurationPort0, GPIOBank::Bank1 => Register::ConfigurationPort1, }; let mut reg_val: u8 = 0x00; self.read_byte(register, &mut reg_val)?; self.write_byte(register, reg_val | (0x01 << pin)) } ``` -------------------------------- ### OutputPin Trait Implementation for ExpanderOutputPin Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderOutputPin.html Provides methods to control the state of the output pin. ```APIDOC ## Trait Implementation: OutputPin for ExpanderOutputPin ### Description This implementation allows the `ExpanderOutputPin` to be used with standard hardware abstraction layer (HAL) components that expect an `OutputPin`. ### Methods #### `set_low()` ##### Description Drives the pin low. ##### Method Signature `fn set_low(&mut self) -> Result<(), Self::Error>` #### `set_high()` ##### Description Drives the pin high. ##### Method Signature `fn set_high(&mut self) -> Result<(), Self::Error>` #### `set_state(state: PinState)` ##### Description Drives the pin high or low depending on the provided value. ##### Method Signature `fn set_state(&mut self, state: PinState) -> Result<(), Self::Error>` ### Error Type `type Error = ExpanderError` where `E` is the error type associated with the underlying I2C communication. ``` -------------------------------- ### Pca9535Immediate::new Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Creates a new PCA9535Immediate instance. Panics if the address is out of the valid range. ```APIDOC ## pub fn new(i2c: I2C, address: u8) -> Self ### Description Creates a new immediate PCA9535 instance. ### Panics If the given device hardware address is outside the permittable range of `32-39`. ``` -------------------------------- ### Register Configuration and States Source: https://docs.rs/pca9535/2.0.0/src/pca9535/lib.rs.html This section details the possible register configurations, input states, and polarity inversion states for the PCA9535 device. ```APIDOC ## Register Mapping and States ### Description This section describes the mapping of register states and the logic for determining input and polarity inversion registers. ### Register States - **PolarityInversionPort0** maps to **PolarityInversionPort1** - **PolarityInversionPort1** maps to **PolarityInversionPort0** - **ConfigurationPort0** maps to **ConfigurationPort1** - **ConfigurationPort1** maps to **ConfigurationPort0** ### Methods - **is_input()**: Returns `true` if the register is an input register (InputPort0 or InputPort1). - **is_polarity_inversion()**: Returns `true` if the register is a polarity inversion register (PolarityInversionPort0 or PolarityInversionPort1). ### Enums #### `GPIOBank` Represents the GPIO banks of the device. - `Bank0` (value: 0) - `Bank1` (value: 1) #### `Polarity` Represents the possible polarity states of inputs and outputs. - `Normal` (value: 0) - `Inverse` (value: 1) ``` -------------------------------- ### PinState::clone_from Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.PinState.html Implementation of `clone_from` for `PinState`, enabling efficient copy-assignment. ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Read Input Pin State (Low) Source: https://docs.rs/pca9535/2.0.0/src/pca9535/pin.rs.html Checks if an input pin on the expander is currently set to a low state. Requires the pin and bank to be configured. ```rust fn is_low(&mut self) -> Result { let register = match self.bank { GPIOBank::Bank0 => Register::InputPort0, GPIOBank::Bank1 => Register::InputPort1, }; let mut reg_val: u8 = 0x00; self.expander.read_byte(register, &mut reg_val)?; match (reg_val >> self.pin) & 1 { 1 => Ok(false), _ => Ok(true), } } ``` -------------------------------- ### Pca9535Immediate Struct Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Represents an immediate mode PCA9535 I/O expander instance. It requires an I2C peripheral and a device address. ```APIDOC ## Struct Pca9535Immediate ```rust pub struct Pca9535Immediate where I2C: I2c, { /* private fields */ } ``` ### Description Represents an immediate mode PCA9535 I/O expander instance. It requires an I2C peripheral and a device address. ``` -------------------------------- ### Input Pin Reading Source: https://docs.rs/pca9535/2.0.0/src/pca9535/pin.rs.html Provides methods to read the current state of input pins on the PCA9535 I/O expander. ```APIDOC ## Input Pin Reading for ExpanderOutputPin ### Description This section details the methods for reading the state of input pins using the `ExpanderOutputPin` struct, which interacts with the PCA9535 I/O expander. ### Method - `is_high()`: Reads the state of the specified input pin and returns `true` if it is high. - `is_low()`: Reads the state of the specified input pin and returns `true` if it is low. ### Endpoint N/A (This is a library implementation, not a direct API endpoint) ### Parameters - `self` (ExpanderOutputPin): A mutable reference to the `ExpanderOutputPin` instance. ### Request Body N/A ### Response #### Success Response (200) - `Ok(bool)`: Returns `true` if the pin is high (for `is_high`) or `false` if the pin is high (for `is_low`), and vice-versa. #### Error Response - `Err(ExpanderError)`: Indicates an error occurred during the I2C communication or pin reading, where `E` is the underlying I2C error type. ### Request Example ```rust // Assuming `pin` is an instance of ExpanderOutputPin configured as input if pin.is_high().expect("Failed to read pin state") { println!("Pin is high"); } else { println!("Pin is low"); } ``` ### Response Example ```rust // Pin is high: Ok(true) // Pin is low: Ok(false) // Error: Err(ExpanderError::IoExpanderError(I2cError::BusError)) ``` ``` -------------------------------- ### Initialize Cached PCA9535 Expander Source: https://docs.rs/pca9535/2.0.0/pca9535 Instantiates the `Pca9535Cached` expander. This interface minimizes I2C traffic by maintaining an internal state cache and requires the interrupt pin to be connected. ```rust use rppal::i2c::I2c; use rppal::gpio::Gpio; use pca9535::Pca9535Cached; let gpio = Gpio::new().unwrap(); //A HAL GPIO Input pin which is connected to the interrupt pin of the IO Expander let expander_interrupt_pin = gpio.get(0).unwrap().into_input(); let i2c = I2c::new().unwrap(); let address = 32; let expander = Pca9535Cached::new(i2c, address, expander_interrupt_pin, true); // create cached expander and initialize cache to defaults ``` -------------------------------- ### ExpanderInputPin Trait Implementations Source: https://docs.rs/pca9535/2.0.0/pca9535/pin/struct.ExpanderInputPin.html Trait implementations for ExpanderInputPin, including Debug, ErrorType, and InputPin. ```APIDOC ## Trait Implementations ### impl<'a, I2C, Io> Debug for ExpanderInputPin<'a, I2C, Io> #### fn fmt(&self, f: &mut Formatter<'_>) -> Result Formats the value using the given formatter. ### impl<'a, I2C, E, Io> ErrorType for ExpanderInputPin<'a, I2C, Io> #### type Error = ExpanderError Error type. ### impl<'a, I2C, E, Io> InputPin for ExpanderInputPin<'a, I2C, Io> #### fn is_high(&mut self) -> Result Is the input pin high? #### fn is_low(&mut self) -> Result Is the input pin low? ``` -------------------------------- ### StandardExpanderInterface Trait Implementations Source: https://docs.rs/pca9535/2.0.0/pca9535/expander/immediate/struct.Pca9535Immediate.html Provides a higher-level interface for controlling GPIO pins, including setting direction, state, and polarity. ```APIDOC ### impl StandardExpanderInterface for Pca9535Immediate #### fn pin_set_high( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> Sets the specified pin to a high logic level. #### fn pin_set_low( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> Drives the specified pin low. #### fn pin_is_high( &mut self, bank: GPIOBank, pin: u8, ) -> Result> Checks if the input state of the given pin is high. Works for both input and output configured pins. #### fn pin_is_low( &mut self, bank: GPIOBank, pin: u8, ) -> Result> Checks if the input state of the given pin is low. Works for both input and output configured pins. #### fn pin_into_input( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> Configures the specified pin as an input. #### fn pin_into_output( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> Configures the specified pin as an output. #### fn pin_inverse_polarity( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> Sets the input polarity of the specified pin to inverted. #### fn pin_normal_polarity( &mut self, bank: GPIOBank, pin: u8, ) -> Result<(), ExpanderError> Sets the input polarity of the specified pin to normal. #### fn inverse_polarity(&mut self) -> Result<(), ExpanderError> Sets the input polarity of all pins to inverted. #### fn normal_polarity(&mut self) -> Result<(), ExpanderError> Sets the input polarity of all pins to normal. ``` -------------------------------- ### Polarity Configuration Methods Source: https://docs.rs/pca9535/2.0.0/src/pca9535/expander/standard.rs.html Methods to set input polarity for specific pins or all pins on the PCA9535 device. ```APIDOC ## pin_inverse_polarity ### Description Sets the input polarity of the given pin to inverted. A logic high voltage applied at this input pin results in a 0 written to the devices input register. ### Parameters - **bank** (GPIOBank) - Required - The GPIO bank (Bank0 or Bank1). - **pin** (u8) - Required - The pin index (0-7). ## pin_normal_polarity ### Description Sets the input polarity of the given pin to normal. A logic high voltage applied at an input pin results in a 1 written to the device's input register. ### Parameters - **bank** (GPIOBank) - Required - The GPIO bank (Bank0 or Bank1). - **pin** (u8) - Required - The pin index (0-7). ## inverse_polarity ### Description Sets the input polarity of all pins to inverted. ## normal_polarity ### Description Sets the input polarity of all pins to normal. ``` -------------------------------- ### PinState Auto Trait Implementations Source: https://docs.rs/pca9535/2.0.0/pca9535/enum.PinState.html Lists the auto trait implementations for PinState, including Freeze, RefUnwindSafe, Send, Sync, Unpin, and UnwindSafe. ```APIDOC ## Auto Trait Implementations ### impl Freeze for PinState ### impl RefUnwindSafe for PinState ### impl Send for PinState ### impl Sync for PinState ### impl Unpin for PinState ### impl UnwindSafe for PinState ```