Skip to content

Commit b375d5c

Browse files
author
dave
committed
#127 move the calibrator into the library for common use.
1 parent 82dfe83 commit b375d5c

File tree

4 files changed

+124
-86
lines changed

4 files changed

+124
-86
lines changed

examples/esp32Amplifier/TouchCalibrator.h

Lines changed: 0 additions & 84 deletions
This file was deleted.

examples/esp32Amplifier/esp32Amplifier_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include <tcMenuVersion.h>
2020
#include "AmplifierController.h"
2121
#include "app_icondata.h"
22-
#include "TouchCalibrator.h"
2322
#include "TestingDialogController.h"
23+
#include <extras/DrawableTouchCalibrator.h>
2424

2525
const char pgmVersionHeader[] PROGMEM = "tcMenu Version";
2626

@@ -56,7 +56,7 @@ void setup() {
5656
controller.initialise();
5757
touchScreen.calibrateMinMaxValues(0.250F, 0.890F, 0.09F, 0.88F);
5858

59-
renderer.setCustomDrawingHandler(new TouchScreenCalibrator(&touchScreen));
59+
renderer.setCustomDrawingHandler(new tcextras::TouchScreenCalibrator(&touchScreen, &renderer));
6060

6161
// first we get the graphics factory
6262
auto & factory = renderer.getGraphicsPropertiesFactory();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3+
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4+
*/
5+
6+
#include "DrawableTouchCalibrator.h"
7+
#include <BaseDialog.h>
8+
9+
using namespace tcextras;
10+
using namespace tcgfx;
11+
12+
#define TOUCH_BLACK RGB(0,0,0)
13+
#define TOUCH_ORANGE RGB(255, 69, 0)
14+
#define TOUCH_YELLOW RGB(255, 255, 0)
15+
16+
void TouchScreenCalibrator::started(BaseMenuRenderer *currentRenderer) {
17+
if (currentRenderer->getRendererType() != RENDER_TYPE_CONFIGURABLE) {
18+
giveItBack();
19+
}
20+
drawable = renderer->getDeviceDrawable();
21+
drawable->startDraw();
22+
drawable->setDrawColor(TOUCH_BLACK);
23+
drawable->drawBox(Coord(0, 0), drawable->getDisplayDimensions(), true);
24+
drawable->endDraw();
25+
takeOverCount = 0;
26+
}
27+
28+
void TouchScreenCalibrator::renderLoop(unsigned int currentValue, RenderPressMode userClick) {
29+
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+
}
36+
37+
drawable->startDraw();
38+
drawable->setDrawColor(TOUCH_BLACK);
39+
drawable->drawCircle(Coord(oldX, oldY), 10, true);
40+
41+
drawable->drawBox(Coord(0, 0), Coord(drawable->getDisplayDimensions().x, 45), true);
42+
drawable->setColors(TOUCH_ORANGE, TOUCH_BLACK);
43+
drawable->drawText(Coord(0, 2), nullptr, 0, "Calibrate screen");
44+
45+
char sz[40];
46+
strcpy(sz, "x: ");
47+
fastftoa(sz, touchScreen->getLastX(), 3, sizeof sz);
48+
strcat(sz, ", y: ");
49+
fastftoa(sz, touchScreen->getLastY(), 3, sizeof sz);
50+
strcat(sz, ", z: ");
51+
fastltoa(sz, touchScreen->getLastTouchState(), 1, NOT_PADDED, sizeof sz);
52+
53+
drawable->setColors(TOUCH_YELLOW, TOUCH_BLACK);
54+
drawable->drawText(Coord(0, 22), nullptr, 0, sz);
55+
56+
oldX = int(touchScreen->getLastX() * (float) drawable->getDisplayDimensions().x);
57+
oldY = int(touchScreen->getLastY() * (float) drawable->getDisplayDimensions().y);
58+
drawable->setDrawColor(TOUCH_YELLOW);
59+
drawable->drawCircle(Coord(oldX, oldY), 10, true);
60+
61+
if (oldX < 40 && oldY < 40 && touchScreen->getLastTouchState() == iotouch::HELD) {
62+
giveItBack();
63+
}
64+
}
65+
66+
void TouchScreenCalibrator::giveItBack() {
67+
renderer->giveBackDisplay();
68+
if(calibrationCompletedHandler) calibrationCompletedHandler();
69+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3+
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4+
*/
5+
6+
#ifndef TCMENU_DRAWABLETOUCHCALIBRATOR_H
7+
#define TCMENU_DRAWABLETOUCHCALIBRATOR_H
8+
9+
#include <PlatformDetermination.h>
10+
#include "../graphics/GraphicsDeviceRenderer.h"
11+
#include "../graphics/MenuTouchScreenEncoder.h"
12+
13+
namespace tcextras {
14+
15+
/**
16+
* This class implements custom drawing so that it can take over the display and render a basic touch screen
17+
* calibration UI. It presents the position of the cursor using the current rotation so that it is easy to
18+
* provide a touch calibrator object.
19+
*
20+
* To use this class:
21+
* 1. Either using new or globally create an instance of this class.
22+
* 2. renderer.setCustomDrawingHandler(touchCalibrator);
23+
* 3. renderer.takeOverDisplay();
24+
*/
25+
class TouchScreenCalibrator : public CustomDrawing {
26+
private:
27+
tcgfx::DeviceDrawable *drawable;
28+
tcgfx::MenuTouchScreenManager *touchScreen;
29+
tcgfx::GraphicsDeviceRenderer* renderer;
30+
int oldX = 0, oldY = 0;
31+
unsigned int takeOverCount = 0;
32+
TimerFn calibrationCompletedHandler;
33+
public:
34+
explicit TouchScreenCalibrator(tcgfx::MenuTouchScreenManager *touchScreen, tcgfx::GraphicsDeviceRenderer* renderer)
35+
: drawable(nullptr), touchScreen(touchScreen), renderer(renderer), calibrationCompletedHandler(nullptr) {}
36+
37+
void setCalibrationCompletedHandler(TimerFn calibrationFinished) {
38+
calibrationCompletedHandler = calibrationFinished;
39+
}
40+
41+
void reset() override {
42+
renderer->takeOverDisplay();
43+
}
44+
45+
void started(BaseMenuRenderer *currentRenderer) override;
46+
47+
void renderLoop(unsigned int currentValue, RenderPressMode userClick) override;
48+
49+
void giveItBack();
50+
};
51+
} // namespace tcextras
52+
53+
#endif // TCMENU_DRAWABLETOUCHCALIBRATOR_H

0 commit comments

Comments
 (0)