Skip to content

Commit

Permalink
updated for embedded-hal v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
qsc-mattmahin committed Aug 22, 2024
1 parent 2bbb722 commit 14fdef3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = "https://github.com/VersBinarii/xpt2046"
name = "xpt2046"

[dependencies]
embedded-hal = "=1.0.0-alpha.7"
embedded-hal = "^1.0.0"
embedded-graphics = "0.7.1"
embedded-graphics-core = "0.3.3"
defmt = { version = "0.3.0", optional = true }
Expand Down
17 changes: 2 additions & 15 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
#[cfg(feature = "with_defmt")]
use defmt::{write, Format, Formatter};

#[cfg_attr(feature = "with_defmt", derive(Format))]
#[derive(Debug)]
pub enum BusError<SPIError, CSError> {
Spi(SPIError),
Pin(CSError),
}

#[cfg_attr(feature = "with_defmt", derive(Format))]
#[derive(Debug)]
pub enum CalibrationError {
Expand All @@ -19,21 +12,15 @@ pub enum CalibrationError {
}

#[derive(Debug)]
pub enum Error<E> {
pub enum Error<SpiError> {
/// SPI bus error
Bus(E),
Spi(SpiError),
/// Error when calculating new calibration values
Calibration(CalibrationError),
/// Delay error
Delay,
}

impl<SPIError, CSError> From<CSError> for Error<BusError<SPIError, CSError>> {
fn from(e: CSError) -> Self {
Self::Bus(BusError::Pin(e))
}
}

#[cfg(feature = "with_defmt")]
impl<E> Format for Error<E> {
fn format(&self, fmt: Formatter) {
Expand Down
68 changes: 27 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use crate::calibration::{calculate_calibration, calibration_draw_point};
pub use crate::{
calibration::CalibrationPoint,
error::{BusError, Error},
error::Error,
exti_pin::Xpt2046Exti,
};
use core::{fmt::Debug, ops::RemAssign};
Expand All @@ -32,7 +32,8 @@ use embedded_graphics_core::{
pixelcolor::{Rgb565, RgbColor},
};
use embedded_hal::{
delay::blocking::DelayUs, digital::blocking::OutputPin, spi::blocking::Transfer,
delay::DelayNs,
spi::SpiDevice,
};

#[cfg(feature = "with_defmt")]
Expand Down Expand Up @@ -182,11 +183,9 @@ impl TouchSamples {
}

#[derive(Debug)]
pub struct Xpt2046<SPI, CS, PinIRQ> {
/// THe SPI interface
pub struct Xpt2046<SPI, PinIRQ> {
/// The SPI interface
spi: SPI,
/// Control pin
cs: CS,
/// Interrupt control pin
irq: PinIRQ,
/// Internall buffers tx
Expand All @@ -204,16 +203,14 @@ pub struct Xpt2046<SPI, CS, PinIRQ> {
calibration_point: CalibrationPoint,
}

impl<SPI, CS, PinIRQ> Xpt2046<SPI, CS, PinIRQ>
impl<SPI, PinIRQ> Xpt2046<SPI, PinIRQ>
where
SPI: Transfer<u8>,
CS: OutputPin,
SPI: SpiDevice,
PinIRQ: Xpt2046Exti,
{
pub fn new(spi: SPI, cs: CS, irq: PinIRQ, orientation: Orientation) -> Self {
pub fn new(spi: SPI, irq: PinIRQ, orientation: Orientation) -> Self {
Self {
spi,
cs,
irq,
tx_buff: [0; TX_BUFF_LEN],
rx_buff: [0; TX_BUFF_LEN],
Expand All @@ -224,40 +221,30 @@ where
calibration_point: orientation.calibration_point(),
}
}
}

impl<SPI, CS, PinIRQ, SPIError, CSError> Xpt2046<SPI, CS, PinIRQ>
where
SPI: Transfer<u8, Error = SPIError>,
CS: OutputPin<Error = CSError>,
PinIRQ: Xpt2046Exti,
SPIError: Debug,
CSError: Debug,
{
fn spi_read(&mut self) -> Result<(), Error<BusError<SPIError, CSError>>> {
self.cs
.set_low()
.map_err(|e| Error::Bus(BusError::Pin(e)))?;
fn spi_read(&mut self, buf: &mut [u8]) -> Result<(), Error<SPI::Error>> {
self.spi
.transfer(&mut self.rx_buff, &self.tx_buff)
.map_err(|e| Error::Bus(BusError::Spi(e)))?;
self.cs
.set_high()
.map_err(|e| Error::Bus(BusError::Pin(e)))?;
Ok(())
.transfer_in_place(buf)
.map_err(|e| Error::Spi(e))
}

/// Read raw values from the XPT2046 driver
fn read_xy(&mut self) -> Result<Point, Error<BusError<SPIError, CSError>>> {
self.spi_read()?;
fn read_xy(&mut self) -> Result<Point, Error<SPI::Error>> {
self.spi_read(&mut [
CHANNEL_SETTING_X >> 3,
CHANNEL_SETTING_X << 5,
CHANNEL_SETTING_Y >> 3,
CHANNEL_SETTING_Y << 5,
0,
])?;

let x = (self.rx_buff[1] as i32) << 8 | self.rx_buff[2] as i32;
let y = (self.rx_buff[3] as i32) << 8 | self.rx_buff[4] as i32;
Ok(Point::new(x, y))
}

/// Read the calibrated point of touch from XPT2046
fn read_touch_point(&mut self) -> Result<Point, Error<BusError<SPIError, CSError>>> {
fn read_touch_point(&mut self) -> Result<Point, Error<SPI::Error>> {
let raw_point = self.read_xy()?;

let (x, y) = match self.operation_mode {
Expand Down Expand Up @@ -297,14 +284,13 @@ where
}

/// Reset the driver and preload tx buffer with register data.
pub fn init<D: DelayUs>(
pub fn init<D: DelayNs>(
&mut self,
delay: &mut D,
) -> Result<(), Error<BusError<SPIError, CSError>>> {
) -> Result<(), Error<SPI::Error>> {
self.tx_buff[0] = 0x80;
self.cs.set_high()?;
self.spi_read()?;
delay.delay_ms(1).map_err(|_| Error::Delay)?;
self.spi_read(& mut [0x80, 0, 0, 0, 0])?;
delay.delay_ms(1);

/*
* Load the tx_buffer with the channels config
Expand All @@ -328,7 +314,7 @@ where
pub fn run(
&mut self,
exti: &mut PinIRQ::Exti,
) -> Result<(), Error<BusError<SPIError, CSError>>> {
) -> Result<(), Error<SPI::Error>> {
match self.screen_state {
TouchScreenState::IDLE => {
if self.operation_mode == TouchScreenOperationMode::CALIBRATION && self.irq.is_low()
Expand Down Expand Up @@ -403,10 +389,10 @@ where
dt: &mut DT,
delay: &mut DELAY,
exti: &mut PinIRQ::Exti,
) -> Result<(), Error<BusError<SPIError, CSError>>>
) -> Result<(), Error<SPI::Error>>
where
DT: DrawTarget<Color = Rgb565>,
DELAY: DelayUs,
DELAY: DelayNs,
{
let mut calibration_count = 0;
let mut retry = 3;
Expand Down

0 comments on commit 14fdef3

Please sign in to comment.