55
66#include " DrawableTouchCalibrator.h"
77#include < BaseDialog.h>
8+ #include " ../lang/language_select.h"
89
910using namespace tcextras ;
1011using namespace tcgfx ;
12+ using namespace iotouch ;
1113
1214#define TOUCH_BLACK RGB (0 ,0 ,0 )
1315#define TOUCH_ORANGE RGB (255 , 69 , 0 )
1416#define TOUCH_YELLOW RGB (255 , 255 , 0 )
1517
16- void TouchScreenCalibrator ::started(BaseMenuRenderer *currentRenderer) {
18+ void IoaTouchScreenCalibrator ::started(BaseMenuRenderer *currentRenderer) {
1719 if (currentRenderer->getRendererType () != RENDER_TYPE_CONFIGURABLE) {
1820 giveItBack ();
1921 }
@@ -22,48 +24,118 @@ void TouchScreenCalibrator::started(BaseMenuRenderer *currentRenderer) {
2224 drawable->setDrawColor (TOUCH_BLACK);
2325 drawable->drawBox (Coord (0 , 0 ), drawable->getDisplayDimensions (), true );
2426 drawable->endDraw ();
25- takeOverCount = 0 ;
2627}
2728
28- void TouchScreenCalibrator ::renderLoop (unsigned int currentValue, RenderPressMode userClick) {
29+ void IoaTouchScreenCalibrator ::renderLoop (unsigned int currentValue, RenderPressMode userClick) {
2930
30- // If the dialog is in use then we leave this loop immediately to give it priority.
31- // As of 2.1 onwards this behaviour is up to you, you can choose to have higher priority than dialogs.
32- if (renderer->getDialog () != nullptr && renderer->getDialog ()->isInUse ()) {
33- giveItBack ();
34- return ;
35- }
31+ iotouch::TouchState touchState = touchScreen->getLastTouchState ();
32+ if (touchState != iotouch::TOUCHED && touchState != iotouch::HELD) return ;
3633
3734 drawable->startDraw ();
3835 drawable->setDrawColor (TOUCH_BLACK);
3936 drawable->drawCircle (Coord (oldX, oldY), 10 , true );
4037
41- drawable->drawBox (Coord (0 , 0 ), Coord (drawable->getDisplayDimensions ().x , 45 ), true );
38+ Coord dims = drawable->getDisplayDimensions ();
39+ drawable->drawBox (Coord (0 , 50 ), Coord (dims.x , 45 ), true );
4240 drawable->setColors (TOUCH_ORANGE, TOUCH_BLACK);
43- drawable->drawText (Coord (0 , 2 ), nullptr , 0 , " Calibrate screen" );
44-
4541 char sz[40 ];
42+ strncpy_P (sz, TXT_TOUCH_CONFIGURE, sizeof (sz));
43+ auto titleConfig = renderer->getGraphicsPropertiesFactory ().configFor (nullptr , tcgfx::ItemDisplayProperties::COMPTYPE_TITLE);
44+ drawable->drawText (Coord (0 , 45 ), titleConfig->getFont (), titleConfig->getFontMagnification (), sz);
45+
4646 strcpy (sz, " x: " );
4747 fastftoa (sz, touchScreen->getLastX (), 3 , sizeof sz);
4848 strcat (sz, " , y: " );
4949 fastftoa (sz, touchScreen->getLastY (), 3 , sizeof sz);
5050 strcat (sz, " , z: " );
51- fastltoa (sz, touchScreen->getLastTouchState (), 1 , NOT_PADDED, sizeof sz);
51+ fastltoa (sz, touchState, 1 , NOT_PADDED, sizeof sz);
52+
53+ int buttonSize = 40 ;
54+ int16_t rightCorner = dims.x - buttonSize;
55+
56+ drawable->setDrawColor (TOUCH_YELLOW);
57+ drawable->drawBox (Coord (0 , 0 ), Coord (buttonSize, buttonSize), true );
58+ drawable->drawBox (Coord (dims.x - buttonSize, 0 ), Coord (buttonSize, buttonSize), true );
59+ drawable->drawBox (Coord (dims.x - buttonSize, rightCorner), Coord (buttonSize, buttonSize), true );
60+ drawable->setDrawColor (TOUCH_ORANGE);
61+ drawable->drawBox (Coord (0 , rightCorner), Coord (buttonSize, buttonSize), true );
62+ drawable->drawText (Coord (0 , dims.y - 5 ), titleConfig->getFont (), titleConfig->getFontMagnification (), " [X]" );
5263
5364 drawable->setColors (TOUCH_YELLOW, TOUCH_BLACK);
5465 drawable->drawText (Coord (0 , 22 ), nullptr , 0 , sz);
5566
56- oldX = int (touchScreen->getLastX () * (float ) drawable-> getDisplayDimensions () .x );
57- oldY = int (touchScreen->getLastY () * (float ) drawable-> getDisplayDimensions () .y );
67+ oldX = int (touchScreen->getLastX () * (float ) dims .x );
68+ oldY = int (touchScreen->getLastY () * (float ) dims .y );
5869 drawable->setDrawColor (TOUCH_YELLOW);
5970 drawable->drawCircle (Coord (oldX, oldY), 10 , true );
71+ drawable->endDraw ();
6072
61- if (oldX < 40 && oldY < 40 && touchScreen->getLastTouchState () == iotouch::HELD) {
62- giveItBack ();
73+ if (oldY < buttonSize) {
74+ // top
75+ if (oldX < buttonSize) {
76+ calibrationHandler.setCalibrationValues (touchScreen->getLastX (), calibrationHandler.getMaxX (),
77+ touchScreen->getLastY (), calibrationHandler.getMaxY ());
78+ } else if (oldX > rightCorner) {
79+ calibrationHandler.setCalibrationValues (calibrationHandler.getMinX (), touchScreen->getLastX (),
80+ touchScreen->getLastY (), calibrationHandler.getMaxY ());
81+ }
82+
83+ } else if (oldY > (dims.y - buttonSize)) {
84+ // bottom
85+ if (oldX < buttonSize) {
86+ // bottom left - exit
87+ saveCalibration ();
88+ giveItBack ();
89+ }
90+ else if (oldX > rightCorner) {
91+ calibrationHandler.setCalibrationValues (calibrationHandler.getMinX (), touchScreen->getLastX (),
92+ calibrationHandler.getMinY (), touchScreen->getLastY ());
93+ }
6394 }
95+
6496}
6597
66- void TouchScreenCalibrator ::giveItBack () {
98+ void IoaTouchScreenCalibrator ::giveItBack () {
6799 renderer->giveBackDisplay ();
68- if (calibrationCompletedHandler) calibrationCompletedHandler ();
100+ renderer->setCustomDrawingHandler (lastDrawing);
101+ lastDrawing = nullptr ;
102+ if (screenPrep != nullptr ) screenPrep (true );
103+ }
104+
105+ void IoaTouchScreenCalibrator::reCalibrateNow () {
106+ if (screenPrep != nullptr ) screenPrep (true );
107+ lastDrawing = renderer->getCurrentCustomDrawing ();
108+ renderer->setCustomDrawingHandler (this );
109+ renderer->takeOverDisplay ();
110+ }
111+
112+ float fltFromU32 (uint32_t val) {
113+ return float (val) / 100000 .0F ;
114+ }
115+
116+ uint32_t fltToU32 (float val) {
117+ return uint32_t (val * 100000 .0F );
118+ }
119+
120+ void IoaTouchScreenCalibrator::saveCalibration () {
121+ serlogF2 (SER_TCMENU_INFO, " Saving IOA touch calibration at " , romPos);
122+ EepromAbstraction *rom = menuMgr.getEepromAbstraction ();
123+ rom->write16 (romPos, calibrationMagicIoa);
124+ rom->write32 (romPos + 2 , fltToU32 (calibrationHandler.getMinX ()));
125+ rom->write32 (romPos + 6 , fltToU32 (calibrationHandler.getMaxX ()));
126+ rom->write32 (romPos + 10 , fltToU32 (calibrationHandler.getMinY ()));
127+ rom->write32 (romPos + 14 , fltToU32 (calibrationHandler.getMaxY ()));
128+ serlogF2 (SER_TCMENU_INFO, " Saved touch data " , rom->hasErrorOccurred ());
129+ }
130+
131+ bool IoaTouchScreenCalibrator::loadCalibration () {
132+ serlogF2 (SER_TCMENU_INFO, " Loading IOA touch calibration at " , romPos);
133+ EepromAbstraction *rom = menuMgr.getEepromAbstraction ();
134+ if (rom->read16 (romPos) != calibrationMagicIoa) return false ;
135+ calibrationHandler.setCalibrationValues (
136+ fltFromU32 (rom->read32 (romPos + 2 )), fltFromU32 (rom->read32 (romPos + 6 )),
137+ fltFromU32 (rom->read32 (romPos + 10 )), fltFromU32 (rom->read32 (romPos + 14 )));
138+ if (rom->hasErrorOccurred ()) return false ;
139+
140+ serlogF (SER_TCMENU_INFO, " Loaded calibration OK" );
69141}
0 commit comments