diff --git a/firmware/application/touch.cpp b/firmware/application/touch.cpp index 0aae8a9b6..13efb236b 100644 --- a/firmware/application/touch.cpp +++ b/firmware/application/touch.cpp @@ -60,6 +60,20 @@ Metrics calculate_metrics(const Frame& frame) { }; } +ui::Point Calibration::translate(const DigitizerPoint& p) const { + static constexpr range_t x_range { 0, 240 - 1 }; + static constexpr range_t y_range { 0, 320 - 1 }; + + const int32_t x = (a * p.x + b * p.y + c) / k; + const int32_t y = (d * p.x + e * p.y + f) / k; + const auto x_clipped = x_range.clip(x); + const auto y_clipped = y_range.clip(y); + return { + static_cast(x_clipped), + static_cast(y_clipped) + }; +} + void Manager::feed(const Frame& frame) { // touch_debounce.feed(touch_raw); const auto touch_raw = frame.touch; diff --git a/firmware/application/touch.hpp b/firmware/application/touch.hpp index 31c1bd55f..e4b0f3639 100644 --- a/firmware/application/touch.hpp +++ b/firmware/application/touch.hpp @@ -114,6 +114,41 @@ struct Metrics { Metrics calculate_metrics(const Frame& frame); +struct DigitizerPoint { + int32_t x; + int32_t y; +}; + +struct Calibration { + /* Touch screen calibration matrix, based on article by Carlos E. Vidales: + * http://www.embedded.com/design/system-integration/4023968/How-To-Calibrate-Touch-Screens + */ + + constexpr Calibration( + const std::array& s, + const std::array& d + ) : k { (s[0].x - s[2].x) * (s[1].y - s[2].y) - (s[1].x - s[2].x) * (s[0].y - s[2].y) }, + a { (d[0].x - d[2].x) * (s[1].y - s[2].y) - (d[1].x - d[2].x) * (s[0].y - s[2].y) }, + b { (s[0].x - s[2].x) * (d[1].x - d[2].x) - (d[0].x - d[2].x) * (s[1].x - s[2].x) }, + c { s[0].y * (s[2].x * d[1].x - s[1].x * d[2].x) + s[1].y * (s[0].x * d[2].x - s[2].x * d[0].x) + s[2].y * (s[1].x * d[0].x - s[0].x * d[1].x) }, + d { (d[0].y - d[2].y) * (s[1].y - s[2].y) - (d[1].y - d[2].y) * (s[0].y - s[2].y) }, + e { (s[0].x - s[2].x) * (d[1].y - d[2].y) - (d[0].y - d[2].y) * (s[1].x - s[2].x) }, + f { s[0].y * (s[2].x * d[1].y - s[1].x * d[2].y) + s[1].y * (s[0].x * d[2].y - s[2].x * d[0].y) + s[2].y * (s[1].x * d[0].y - s[0].x * d[1].y) } + { + } + + ui::Point translate(const DigitizerPoint& p) const; + +private: + int32_t k; + int32_t a; + int32_t b; + int32_t c; + int32_t d; + int32_t e; + int32_t f; +}; + template class Filter { public: