Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial touch driver #346

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub mod sys;
pub mod task;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod timer;
#[cfg(all(any(esp32s2, esp32s3), not(feature = "riscv-ulp-hal")))]
pub mod touch;
keirlawson marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod uart;
#[cfg(all(
Expand Down
132 changes: 132 additions & 0 deletions src/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use core::borrow::{Borrow, BorrowMut};

use esp_idf_sys::{
esp, touch_fsm_mode_t, touch_fsm_mode_t_TOUCH_FSM_MODE_MAX, touch_fsm_mode_t_TOUCH_FSM_MODE_SW,
touch_fsm_mode_t_TOUCH_FSM_MODE_TIMER, touch_pad_config, touch_pad_fsm_start,
touch_pad_fsm_stop, touch_pad_init, touch_pad_read_raw_data, touch_pad_set_fsm_mode,
touch_pad_t, touch_pad_t_TOUCH_PAD_NUM0, touch_pad_t_TOUCH_PAD_NUM1,
touch_pad_t_TOUCH_PAD_NUM10, touch_pad_t_TOUCH_PAD_NUM11, touch_pad_t_TOUCH_PAD_NUM12,
touch_pad_t_TOUCH_PAD_NUM13, touch_pad_t_TOUCH_PAD_NUM14, touch_pad_t_TOUCH_PAD_NUM2,
touch_pad_t_TOUCH_PAD_NUM3, touch_pad_t_TOUCH_PAD_NUM4, touch_pad_t_TOUCH_PAD_NUM5,
touch_pad_t_TOUCH_PAD_NUM6, touch_pad_t_TOUCH_PAD_NUM7, touch_pad_t_TOUCH_PAD_NUM8,
touch_pad_t_TOUCH_PAD_NUM9, EspError,
};

#[derive(Copy, Clone)]
pub enum FsmMode {
Timer,
SW,
Max,
}

impl From<FsmMode> for touch_fsm_mode_t {
fn from(value: FsmMode) -> Self {
match value {
FsmMode::Timer => touch_fsm_mode_t_TOUCH_FSM_MODE_TIMER,
FsmMode::SW => touch_fsm_mode_t_TOUCH_FSM_MODE_SW,
FsmMode::Max => touch_fsm_mode_t_TOUCH_FSM_MODE_MAX,
}
}
}

#[derive(Copy, Clone)]
pub enum TouchPad {
Pad0,
Pad1,
Pad2,
Pad3,
Pad4,
Pad5,
Pad6,
Pad7,
Pad8,
Pad9,
Pad10,
Pad11,
Pad12,
Pad13,
Pad14,
}

impl From<TouchPad> for touch_pad_t {
fn from(value: TouchPad) -> Self {
match value {
TouchPad::Pad0 => touch_pad_t_TOUCH_PAD_NUM0,
TouchPad::Pad1 => touch_pad_t_TOUCH_PAD_NUM1,
TouchPad::Pad2 => touch_pad_t_TOUCH_PAD_NUM2,
TouchPad::Pad3 => touch_pad_t_TOUCH_PAD_NUM3,
TouchPad::Pad4 => touch_pad_t_TOUCH_PAD_NUM4,
TouchPad::Pad5 => touch_pad_t_TOUCH_PAD_NUM5,
TouchPad::Pad6 => touch_pad_t_TOUCH_PAD_NUM6,
TouchPad::Pad7 => touch_pad_t_TOUCH_PAD_NUM7,
TouchPad::Pad8 => touch_pad_t_TOUCH_PAD_NUM8,
TouchPad::Pad9 => touch_pad_t_TOUCH_PAD_NUM9,
TouchPad::Pad10 => touch_pad_t_TOUCH_PAD_NUM10,
TouchPad::Pad11 => touch_pad_t_TOUCH_PAD_NUM11,
TouchPad::Pad12 => touch_pad_t_TOUCH_PAD_NUM12,
TouchPad::Pad13 => touch_pad_t_TOUCH_PAD_NUM13,
TouchPad::Pad14 => touch_pad_t_TOUCH_PAD_NUM14,
}
}
}

pub struct TouchConfig {
fsm_mode: FsmMode,
}

pub struct TouchDriver {
config: TouchConfig,
}

pub struct TouchPadDriver<T>
where
T: BorrowMut<TouchDriver>,
{
_touch: T,
pad: TouchPad,
}

impl TouchPadDriver<TouchDriver> {
pub fn new_single_started(pad: TouchPad, config: TouchConfig) -> Result<Self, EspError> {
let mut touch = TouchDriver::new(config)?;
Copy link
Collaborator

@Vollbrecht Vollbrecht Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crating the TouchDriver inside the TouchPadDriver makes little sense, ( Sorry i hope i am not rude here ;D ) You can explain your thinking here.

My thinking: As in this case you wouldn't even need all the TouchDriver buisness in the first place. Its fine to create two different version of TouchPadDriver, one with new() and one with new_single(). Where the first one takes a reference to the TouchDriver that is created before so multiple TouchPadDrivers can be created, and in the later case it takes an owned value of TouchDriver, where one would only ever use on pin.

esp!(unsafe { touch_pad_config(pad.into()) })?;
touch.start()?;
Ok(Self { _touch: touch, pad })
}
}

impl<T> TouchPadDriver<T>
where
T: BorrowMut<TouchDriver>,
{
pub fn read_raw_data(&self) -> Result<u32, EspError> {
let mut raw: u32 = 0;
let result = esp!(unsafe { touch_pad_read_raw_data(self.borrow().pad.into(), &mut raw) });
result.map(|_| raw)
}
}

impl TouchDriver {
pub fn new(config: TouchConfig) -> Result<Self, EspError> {
unsafe {
esp!(touch_pad_init())?;
}

Ok(TouchDriver { config })
}

pub fn start(&mut self) -> Result<(), EspError> {
unsafe {
esp!(touch_pad_set_fsm_mode(self.config.fsm_mode.into()))?;
esp!(touch_pad_fsm_start())?;
}

Ok(())
}
}

impl Drop for TouchDriver {
fn drop(&mut self) {
esp!(unsafe { touch_pad_fsm_stop() }).unwrap()
}
}