Skip to main content

Crate cst226_rs

Crate cst226_rs 

Source
Expand description

§CST226 Touch Controller Driver Crate

A no_std driver for the CST226 touch controller, providing functionality to read touch points and manage power modes.

This driver is based on publicly available C++ drivers, as official datasheets for the CST226 are not widely available. It is embedded-hal compatible and provides a generic interface for the device’s hardware reset functionality. This allows the reset pin to be controlled by a direct GPIO pin or an I2C I/O expander.

This driver reads the entire touch data block in a single I2C transaction to report all active touch points simultaneously.

Some drivers include an INT pin indicating touch events, but this driver does not use it. If interrupts need to be supported, the user can attach the pin output to an interrupt and implement their own callback.

§Usage

  1. Implement the ResetInterface trait for your specific reset mechanism.
  2. Create an instance of the Cst226Driver.
  3. Initialize the touch driver.
  4. In a loop, use the get_touches() method to read the current touch state.
// This is a conceptual example. `I2CInstance`, `PinInstance`, and `DelayInstance`
// would be your concrete implementations from a HAL crate.

// 1. Implement the ResetInterface for your hardware.
struct GpioReset<P: OutputPin> {
    pin: P,
}

impl<P: OutputPin> ResetInterface for GpioReset<P> {
    type Error = P::Error;
    fn reset(&mut self, delay: &mut impl DelayNs) -> Result<(), Self::Error> {
        self.pin.set_high()?;
        delay.delay_ms(5);
        self.pin.set_low()?;
        delay.delay_ms(5);
        self.pin.set_high()?;
        delay.delay_ms(30);
        Ok(())
    }
}

// 2. Create driver instance.
let reset_pin = GpioReset { pin: PinInstance };
let mut touch = Cst226Driver::new(
    I2CInstance,
    CST226_DEVICE_ADDRESS,
    reset_pin,
);

// 3. Initialize the driver.
touch.initialize(&mut DelayInstance).expect("Failed to initialize touch driver");

// 4. Read touches in a loop.
loop {
    let touches = touch.get_touches().unwrap();

    for (i, touch) in touches.iter().enumerate() {
        println!("Touch {}: x={}, y={}", i + 1, touch.x, touch.y);
    }
}

Notes:

  • If the reset pin is controlled via an I2C GPIO expander sharing the same bus with the touch driver, you should use a shared bus implementation like embedded_hal_bus to manage I2C access.

Structs§

Cst226Driver
CST226 touch driver
TouchPoint
Represents a single touch point with X and Y coordinates.

Enums§

DriverError
Driver Errors
Gesture
Single-finger swipe gestures.
PowerMode
Power modes for the touch device.

Constants§

CST226_DEVICE_ADDRESS

Traits§

ResetInterface
Trait for controlling the CST226 hardware reset pin.