Skip to content

Commit

Permalink
Allow posibility of calibration failing
Browse files Browse the repository at this point in the history
  • Loading branch information
VersBinarii committed Apr 21, 2022
1 parent c83a311 commit 9207c85
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xpt2046"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Embedded-hal based XPT2046 driver for TFT LCD displays"
authors = ["VersBInarii <versbinarii@gmail.com>"]
Expand Down
34 changes: 26 additions & 8 deletions src/calibration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::CalibrationData;
use crate::error::CalibrationError;
use embedded_graphics::{
primitives::{Line, Primitive, PrimitiveStyle},
Drawable,
Expand Down Expand Up @@ -53,7 +54,7 @@ pub(crate) fn calibration_draw_point<DT: DrawTarget<Color = Rgb565>>(dt: &mut DT
pub(crate) fn calculate_calibration(
old_cp: &CalibrationPoint,
new_cp: &CalibrationPoint,
) -> CalibrationData {
) -> Result<CalibrationData, CalibrationError> {
/*
* We need delta calculated from the new points
*/
Expand All @@ -65,37 +66,54 @@ pub(crate) fn calculate_calibration(
let alpha_x = ((old_cp.a[0] - old_cp.c[0]) * (new_cp.b[1] - new_cp.c[1])
- (old_cp.b[0] - old_cp.c[0]) * (new_cp.a[1] - new_cp.c[1])) as f32
/ delta;

/*
* We might end up reading garabge from SPI
* so we must test for the weird floats
*/
if !alpha_x.is_normal() {
return Err(CalibrationError::Alpha);
}
let beta_x = ((new_cp.a[0] - new_cp.c[0]) * (old_cp.b[0] - old_cp.c[0])
- (new_cp.b[0] - new_cp.c[0]) * (old_cp.a[0] - old_cp.c[0])) as f32
/ delta;

if !beta_x.is_normal() {
return Err(CalibrationError::Beta);
}
let delta_x = ((old_cp.a[0]) * (new_cp.b[0] * new_cp.c[1] - new_cp.c[0] * new_cp.b[1])
- (old_cp.b[0]) * (new_cp.a[0] * new_cp.c[1] - new_cp.c[0] * new_cp.a[1])
+ (old_cp.c[0]) * (new_cp.a[0] * new_cp.b[1] - new_cp.b[0] * new_cp.a[1]))
as f32
/ delta;

if !delta_x.is_normal() {
return Err(CalibrationError::Delta);
}
let alpha_y = ((old_cp.a[1] - old_cp.c[1]) * (new_cp.b[1] - new_cp.c[1])
- (old_cp.b[1] - old_cp.c[1]) * (new_cp.a[1] - new_cp.c[1])) as f32
/ delta;

if !alpha_y.is_normal() {
return Err(CalibrationError::Alpha);
}
let beta_y = ((new_cp.a[0] - new_cp.c[0]) * (old_cp.b[1] - old_cp.c[1])
- (new_cp.b[0] - new_cp.c[0]) * (old_cp.a[1] - old_cp.c[1])) as f32
/ delta;

if !beta_y.is_normal() {
return Err(CalibrationError::Beta);
}
let delta_y = ((old_cp.a[1]) * (new_cp.b[0] * new_cp.c[1] - new_cp.c[0] * new_cp.b[1])
- (old_cp.b[1]) * (new_cp.a[0] * new_cp.c[1] - new_cp.c[0] * new_cp.a[1])
+ (old_cp.c[1]) * (new_cp.a[0] * new_cp.b[1] - new_cp.b[0] * new_cp.a[1]))
as f32
/ delta;
if !delta_y.is_normal() {
return Err(CalibrationError::Delta);
}

CalibrationData {
Ok(CalibrationData {
alpha_x,
beta_x,
delta_x,
alpha_y,
beta_y,
delta_y,
}
})
}
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@
#[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 {
Alpha,
Beta,
Delta,
}

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

#[cfg(feature = "with_defmt")]
impl<E> Format for Error<E> {
fn format(&self, fmt: Formatter) {
match self {
Error::Bus(_) => write!(fmt, "Bus error"),
Error::Calibration(e) => write!(fmt, "Error when calculating calibration for: {}", e),
}
}
}
34 changes: 30 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use embedded_hal::{
};

#[cfg(feature = "with_defmt")]
use defmt::{write, Format, Formatter};
use defmt::Format;

pub mod calibration;
pub mod error;
Expand All @@ -48,6 +48,7 @@ const CHANNEL_SETTING_Y: u8 = 0b11010000;
const MAX_SAMPLES: usize = 128;
const TX_BUFF_LEN: usize = 5;

#[cfg_attr(feature = "with_defmt", derive(Format))]
#[derive(Debug)]
pub struct CalibrationData {
pub alpha_x: f32,
Expand Down Expand Up @@ -388,12 +389,18 @@ where
/// to work ok but if for some reason touch screen needs to be recalibrated
/// then look no further.
/// This should be run after init() method.
pub fn calibrate<DT, DELAY>(&mut self, dt: &mut DT, delay: &mut DELAY, exti: &mut PinIRQ::Exti)
pub fn calibrate<DT, DELAY>(
&mut self,
dt: &mut DT,
delay: &mut DELAY,
exti: &mut PinIRQ::Exti,
) -> Result<(), Error<SPIError>>
where
DT: DrawTarget<Color = Rgb565>,
DELAY: DelayUs,
{
let mut calibration_count = 0;
let mut retry = 3;
let mut new_a = Point::zero();
let mut new_b = Point::zero();
let mut new_c = Point::zero();
Expand Down Expand Up @@ -447,14 +454,33 @@ where
c: new_c,
};
// and then re-caculate calibration
self.calibration_data = calculate_calibration(&old_cp, &self.calibration_point);
calibration_count += 1;
match calculate_calibration(&old_cp, &self.calibration_point) {
Ok(new_calibration_data) => {
self.calibration_data = new_calibration_data;
calibration_count += 1;
}
Err(e) => {
// We have problem calculating new values
if retry == 0 {
return Err(Error::Calibration(e));
}
/*
* If out calculation failed lets retry
*/
retry -= 1;
calibration_count = 0;

let _ = dt.clear(Rgb565::BLACK);
}
}
}
_ => {}
}
}

let _ = dt.clear(Rgb565::WHITE);
self.operation_mode = TouchScreenOperationMode::NORMAL;

Ok(())
}
}

0 comments on commit 9207c85

Please sign in to comment.