From c424bf08f3cc48048fcc57a966b95e414b9e5cfb Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 27 Jul 2016 15:30:43 -0700 Subject: [PATCH] Touch: Migrate touch calibration to persistent memory. --- firmware/application/touch.cpp | 27 +++++++------------ firmware/application/touch.hpp | 4 +-- firmware/application/ui_touch_calibration.cpp | 5 +++- .../common/portapack_persistent_memory.cpp | 16 +++++++++++ .../common/portapack_persistent_memory.hpp | 4 +++ 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/firmware/application/touch.cpp b/firmware/application/touch.cpp index 07c558bfe..ac48c0539 100644 --- a/firmware/application/touch.cpp +++ b/firmware/application/touch.cpp @@ -21,6 +21,9 @@ #include "touch.hpp" +#include "portapack_persistent_memory.hpp" +using namespace portapack; + #include "utility.hpp" namespace touch { @@ -76,26 +79,14 @@ ui::Point Calibration::translate(const DigitizerPoint& p) const { }; } -static const Calibration _default_calibration { +const Calibration default_calibration() { /* Values derived from one PortaPack H1 unit. */ - { { { 256, 731 }, { 880, 432 }, { 568, 146 } } }, - { { { 32, 48 }, { 208, 168 }, { 120, 288 } } } + return { + { { { 256, 731 }, { 880, 432 }, { 568, 146 } } }, + { { { 32, 48 }, { 208, 168 }, { 120, 288 } } } + }; }; -static Calibration _calibration = _default_calibration; - -void set_calibration(const Calibration& value) { - _calibration = value; -} - -const Calibration& calibration() { - return _calibration; -} - -const Calibration& default_calibration() { - return _default_calibration; -} - void Manager::feed(const Frame& frame) { // touch_debounce.feed(touch_raw); const auto touch_raw = frame.touch; @@ -145,7 +136,7 @@ void Manager::feed(const Frame& frame) { } ui::Point Manager::filtered_point() const { - return calibration().translate({ filter_x.value(), filter_y.value() }); + return persistent_memory::touch_calibration().translate({ filter_x.value(), filter_y.value() }); } } /* namespace touch */ diff --git a/firmware/application/touch.hpp b/firmware/application/touch.hpp index 3507c0af3..f862cf188 100644 --- a/firmware/application/touch.hpp +++ b/firmware/application/touch.hpp @@ -149,9 +149,7 @@ private: int32_t f; }; -void set_calibration(const Calibration& calibration); -const Calibration& calibration(); -const Calibration& default_calibration(); +const Calibration default_calibration(); template class Filter { diff --git a/firmware/application/ui_touch_calibration.cpp b/firmware/application/ui_touch_calibration.cpp index 6e58b6c3a..fe11d8901 100644 --- a/firmware/application/ui_touch_calibration.cpp +++ b/firmware/application/ui_touch_calibration.cpp @@ -23,6 +23,9 @@ #include "irq_controls.hpp" +#include "portapack_persistent_memory.hpp" +using namespace portapack; + namespace ui { TouchCalibrationView::TouchCalibrationView( @@ -151,7 +154,7 @@ void TouchCalibrationView::touch_complete() { void TouchCalibrationView::on_ok() { if( phase == Phase::Success ) { - touch::set_calibration(calibration); + persistent_memory::set_touch_calibration(calibration); nav.pop(); } if( phase == Phase::Failure ) { diff --git a/firmware/common/portapack_persistent_memory.cpp b/firmware/common/portapack_persistent_memory.cpp index 2f4c758c8..8ee50f420 100644 --- a/firmware/common/portapack_persistent_memory.cpp +++ b/firmware/common/portapack_persistent_memory.cpp @@ -46,6 +46,8 @@ constexpr ppb_t ppb_reset_value { 0 }; struct data_t { int64_t tuned_frequency; int32_t correction_ppb; + uint32_t touch_calibration_magic; + touch::Calibration touch_calibration; }; static_assert(sizeof(data_t) <= backup_ram.size(), "Persistent memory structure too large for VBAT-maintained region"); @@ -72,5 +74,19 @@ void set_correction_ppb(const ppb_t new_value) { portapack::clock_manager.set_reference_ppb(clipped_value); } +static constexpr uint32_t touch_calibration_magic = 0x074af82f; + +void set_touch_calibration(const touch::Calibration& new_value) { + data->touch_calibration = new_value; + data->touch_calibration_magic = touch_calibration_magic; +} + +const touch::Calibration& touch_calibration() { + if( data->touch_calibration_magic != touch_calibration_magic ) { + set_touch_calibration(touch::default_calibration()); + } + return data->touch_calibration; +} + } /* namespace persistent_memory */ } /* namespace portapack */ diff --git a/firmware/common/portapack_persistent_memory.hpp b/firmware/common/portapack_persistent_memory.hpp index 3e1a6c146..2ae7f023b 100644 --- a/firmware/common/portapack_persistent_memory.hpp +++ b/firmware/common/portapack_persistent_memory.hpp @@ -25,6 +25,7 @@ #include #include "rf_path.hpp" +#include "touch.hpp" namespace portapack { namespace persistent_memory { @@ -37,6 +38,9 @@ void set_tuned_frequency(const rf::Frequency new_value); ppb_t correction_ppb(); void set_correction_ppb(const ppb_t new_value); +void set_touch_calibration(const touch::Calibration& new_value); +const touch::Calibration& touch_calibration(); + } /* namespace persistent_memory */ } /* namespace portapack */