Skip to content

Commit

Permalink
Update to embedded-hal=1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyTimeTraveler committed May 14, 2024
1 parent c056214 commit 8d8c12f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/target/
/.idea/
*.iml
44 changes: 17 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ repository = "https://github.com/VersBinarii/xpt2046"
name = "xpt2046"

[dependencies]
embedded-hal = "=1.0.0-rc.1"
embedded-hal = "=1.0.0"
embedded-graphics = "0.8.1"
embedded-graphics-core = "0.4.0"
defmt = { version = "0.3.0", optional = true }
defmt = { version = "0.3.7", optional = true }

[dev-dependencies]
cortex-m-rtic = "1.0.0"
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
defmt-rtt = "0.4.0"
cortex-m-rt = "0.7.4"
defmt-rtt = "0.4.1"
panic-semihosting = "0.6"
ili9341 = "0.5.0"
ili9341 = "0.6.0"

[features]
with_defmt = ["defmt"]
Expand Down
13 changes: 0 additions & 13 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 @@ -28,12 +21,6 @@ pub enum Error<E> {
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
53 changes: 17 additions & 36 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,7 @@ use embedded_graphics_core::{
pixelcolor::{Rgb565, RgbColor},
};
use embedded_hal::{
delay::DelayUs, digital::OutputPin,spi::SpiDevice
delay::DelayNs, spi::SpiDevice
};

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

#[derive(Debug)]
pub struct Xpt2046<SPI, CS, PinIRQ> {
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 +202,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: SpiDevice<u8>,
CS: OutputPin,
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 @@ -226,38 +222,24 @@ where
}
}

impl<SPI, CS, PinIRQ, SPIError, CSError> Xpt2046<SPI, CS, PinIRQ>
impl<SPI, PinIRQ> Xpt2046<SPI, PinIRQ>
where
SPI: SpiDevice<u8, Error = SPIError>,
CS: OutputPin<Error = CSError>,
SPI: SpiDevice<u8>,
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)))?;
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(())
}

/// 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.transfer(&mut self.rx_buff, &self.tx_buff)
.map_err(|e|Error::Bus(e))?;

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,13 +279,12 @@ 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<(), SPI::Error> {
self.tx_buff[0] = 0x80;
self.cs.set_high()?;
self.spi_read()?;
self.spi.transfer(&mut self.rx_buff, &self.tx_buff)?;
delay.delay_ms(1);

/*
Expand All @@ -328,7 +309,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 +384,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 8d8c12f

Please sign in to comment.