#![no_std]
#![no_main]
use cst226_rs::{Cst226Driver, Gesture, ResetInterface, CST226_DEVICE_ADDRESS};
use esp_alloc as _;
use esp_backtrace as _;
use esp_bootloader_esp_idf::esp_app_desc;
use esp_hal::{
delay::Delay,
gpio::{Io, Level, Output, OutputConfig},
i2c::master::{Config as I2cConfig, Error as I2cError, I2c},
main,
time::Rate,
};
use esp_println::{print, println};
esp_app_desc!();
pub struct ResetDriver<OUT> {
output: OUT,
}
impl<OUT> ResetDriver<OUT> {
pub fn new(output: OUT) -> Self {
ResetDriver { output }
}
}
impl<OUT> ResetInterface for ResetDriver<OUT>
where
OUT: embedded_hal::digital::OutputPin,
{
type Error = OUT::Error;
fn reset(&mut self) -> Result<(), Self::Error> {
let delay = Delay::new();
self.output.set_low()?;
delay.delay_millis(20);
self.output.set_high()?;
delay.delay_millis(150);
Ok(())
}
}
#[main]
fn main() -> ! {
let peripherals = esp_hal::init(esp_hal::Config::default());
let mut delay = Delay::new();
let touch_i2c = I2c::new(
peripherals.I2C0,
I2cConfig::default().with_frequency(Rate::from_khz(400)),
)
.unwrap()
.with_sda(peripherals.GPIO6)
.with_scl(peripherals.GPIO7);
let output = Output::new(peripherals.GPIO17, Level::High, OutputConfig::default());
let reset = ResetDriver::new(output);
println!("Initializing CST226 Touch Driver...");
let mut touch = Cst226Driver::new(touch_i2c, CST226_DEVICE_ADDRESS, reset, delay);
touch
.initialize()
.expect("Failed to initialize touch driver");
println!("Touch driver initialized. Reading points...");
loop {
match touch.get_touches() {
Ok(touches) => {
if !touches.is_empty() {
print!("Touches detected: {} | ", touches.len());
for (i, point) in touches.iter().enumerate() {
print!("P{}: ({}, {}) ", i, point.x, point.y);
}
println!();
}
}
Err(e) => {
println!("Error reading touches: {:?}", e);
}
}
match touch.get_gesture() {
Ok(gesture) => {
match gesture {
Gesture::SwipeUp => println!("Swipe Up detected"),
Gesture::SwipeDown => println!("Swipe Down detected"),
Gesture::SwipeLeft => println!("Swipe Left detected"),
Gesture::SwipeRight => println!("Swipe Right detected"),
Gesture::None => {} }
}
Err(e) => {
println!("Error reading gestures: {:?}", e);
}
}
delay.delay_millis(100);
}
}