Skip to content

Commit c3139fc

Browse files
committed
#177 clean up touch support
1 parent 746042f commit c3139fc

File tree

9 files changed

+102
-42
lines changed

9 files changed

+102
-42
lines changed

examples/arduino32/piPicoTftEncoder/piPicoTftEncoder.emf renamed to examples/arduino32/piPicoTftTouch/piPicoTftEncoder.emf

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@
335335
"codeOptions": {
336336
"embeddedPlatform": "ARDUINO32",
337337
"lastDisplayUuid": "e6342680-6fab-4d1b-ac08-ce8dbfc0974c",
338-
"lastInputUuid": "6dd87e7d-e751-4912-a70d-91793b1d3d87",
338+
"lastInputUuid": "89cd7f70-0457-4884-97c2-0db904ccb0ba",
339339
"lastRemoteUuid": "2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb",
340340
"lastRemoteUuids": [
341341
"2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb"
@@ -365,69 +365,64 @@
365365
"subsystem": "DISPLAY"
366366
},
367367
{
368-
"name": "INCLUDE_TOUCH_INBUILT",
369-
"latestValue": "false",
368+
"name": "TOUCH_SEPARATOR",
369+
"latestValue": "",
370370
"subsystem": "DISPLAY"
371371
},
372372
{
373-
"name": "TOUCH_ROTATION",
374-
"latestValue": "PORTRAIT",
373+
"name": "TFT_TOUCH_SCREEN_REQUIRED",
374+
"latestValue": "true",
375375
"subsystem": "DISPLAY"
376376
},
377377
{
378378
"name": "TOUCH_CALIB_WIDTH",
379-
"latestValue": "320",
379+
"latestValue": "4096",
380380
"subsystem": "DISPLAY"
381381
},
382382
{
383383
"name": "TOUCH_CALIB_HEIGHT",
384-
"latestValue": "240",
384+
"latestValue": "4096",
385385
"subsystem": "DISPLAY"
386386
},
387387
{
388-
"name": "SW_POLLING_MODE",
389-
"latestValue": "SWITCHES_POLL_EVERYTHING",
390-
"subsystem": "INPUT"
391-
},
392-
{
393-
"name": "SWITCH_IODEVICE",
394-
"latestValue": "devicePins",
395-
"subsystem": "INPUT"
388+
"name": "TOUCH_USE_RAW_TFT_FUNCTION",
389+
"latestValue": "true",
390+
"subsystem": "DISPLAY"
396391
},
397392
{
398-
"name": "ENCODER_PIN_A",
399-
"latestValue": "16",
400-
"subsystem": "INPUT"
393+
"name": "ORIENTATION_SEPARATOR",
394+
"latestValue": "",
395+
"subsystem": "DISPLAY"
401396
},
402397
{
403-
"name": "ENCODER_PIN_B",
404-
"latestValue": "17",
405-
"subsystem": "INPUT"
398+
"name": "TFT_TOUCH_SWAP_XY",
399+
"latestValue": "true",
400+
"subsystem": "DISPLAY"
406401
},
407402
{
408-
"name": "ENCODER_PIN_OK",
409-
"latestValue": "21",
410-
"subsystem": "INPUT"
403+
"name": "TFT_TOUCH_X_INVERTED",
404+
"latestValue": "false",
405+
"subsystem": "DISPLAY"
411406
},
412407
{
413-
"name": "ENCODER_BACK_PIN",
414-
"latestValue": "-1",
415-
"subsystem": "INPUT"
408+
"name": "TFT_TOUCH_Y_INVERTED",
409+
"latestValue": "false",
410+
"subsystem": "DISPLAY"
416411
},
417412
{
418-
"name": "ENCODER_NEXT_PIN",
419-
"latestValue": "-1",
420-
"subsystem": "INPUT"
413+
"name": "CALIB_SEPARATOR",
414+
"latestValue": "",
415+
"subsystem": "DISPLAY"
421416
},
422417
{
423-
"name": "PULLUP_LOGIC",
418+
"name": "TFT_TOUCH_SCREEN_CALIBRATE",
424419
"latestValue": "true",
425-
"subsystem": "INPUT"
420+
"subsystem": "DISPLAY"
426421
},
427422
{
428-
"name": "ENCODER_IS_QUARTER_CYCLE",
429-
"latestValue": "false",
430-
"subsystem": "INPUT"
423+
"name": "TFT_TOUCH_CALIBRATION_STORAGE_LOC",
424+
"latestValue": "400",
425+
"subsystem": "DISPLAY"
431426
},
432427
{
433428
"name": "ITEM_FONT",

examples/arduino32/piPicoTftEncoder/piPicoTftEncoder.ino renamed to examples/arduino32/piPicoTftTouch/piPicoTftTouch.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Getting started: https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/tcmenu-overview-quick-start/
66
*/
7-
#include "piPicoTftEncoder_menu.h"
7+
#include "piPicoTftTouch_menu.h"
88
#include <PlatformDetermination.h>
99
#include <IoLogging.h>
1010
#include <TaskManagerIO.h>
@@ -226,4 +226,4 @@ void CALLBACK_FUNCTION onRestart(int /*id*/) {
226226
serdebugF("Restart selected");
227227
strncpy(minfoStatusRestart.name, "Restarting", NAME_SIZE_T);
228228
menuStatusRestart.setChanged(true);
229-
}
229+
}

examples/arduino32/piPicoTftEncoder/piPicoTftEncoder_menu.cpp renamed to examples/arduino32/piPicoTftTouch/piPicoTftTouch_menu.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include <tcMenu.h>
12-
#include "piPicoTftEncoder_menu.h"
12+
#include "piPicoTftTouch_menu.h"
1313
#include "ThemeCoolBlueModern.h"
1414
#include <Fonts/OpenSansCyrillicLatin18.h>
1515
#include <Fonts/RobotoMedium24.h>
@@ -20,6 +20,10 @@ const ConnectorLocalInfo applicationInfo = { "RPiPico Tft Encoder", "0c6972c4-a
2020
TFT_eSPI gfx;
2121
TfteSpiDrawable gfxDrawable(&gfx, 45);
2222
GraphicsDeviceRenderer renderer(30, applicationInfo.name, &gfxDrawable);
23+
iotouch::TftSpiTouchInterrogator touchInterrogator(&gfx, 4096, 4096, true);
24+
iotouch::TouchOrientationSettings touchOrientation(true, false, false);
25+
MenuTouchScreenManager touchScreen(&touchInterrogator, &renderer, touchOrientation);
26+
tcextras::IoaTouchScreenCalibrator touchCalibrator(&touchScreen, &renderer, 400);
2327

2428
// Global Menu Item declarations
2529
ListRuntimeMenuItem menuRootList(17, 0, fnRootListRtCall, NULL);
@@ -80,8 +84,8 @@ void setupMenu() {
8084
gfx.begin();
8185
gfx.setRotation(1);
8286
renderer.setUpdatesPerSecond(15);
83-
switches.init(internalDigitalIo(), SWITCHES_POLL_EVERYTHING, true);
84-
menuMgr.initForEncoder(&renderer, &menuVolume, 16, 17, 21);
87+
touchScreen.start();
88+
menuMgr.initWithoutInput(&renderer, &menuVolume);
8589
renderer.setTitleMode(BaseGraphicalRenderer::TITLE_FIRST_ROW);
8690
renderer.setUseSliderForAnalog(true);
8791
renderer.enableTcUnicode();

examples/arduino32/piPicoTftEncoder/piPicoTftEncoder_menu.h renamed to examples/arduino32/piPicoTftTouch/piPicoTftTouch_menu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <tcUnicodeHelper.h>
1717
#include "tcMenuTfteSpi.h"
1818
#include <graphics/MenuTouchScreenEncoder.h>
19+
#include <extras/DrawableTouchCalibrator.h>
1920
#include <RuntimeMenuItem.h>
2021
#include <EditableLargeNumberMenuItem.h>
2122
#include <ScrollChoiceMenuItem.h>
@@ -27,6 +28,9 @@ extern const PROGMEM ConnectorLocalInfo applicationInfo;
2728
extern TFT_eSPI gfx;
2829
extern TfteSpiDrawable gfxDrawable;
2930
extern GraphicsDeviceRenderer renderer;
31+
extern iotouch::TftSpiTouchInterrogator touchInterrogator;
32+
extern MenuTouchScreenManager touchScreen;
33+
extern tcextras::IoaTouchScreenCalibrator touchCalibrator;
3034
extern const UnicodeFont OpenSansCyrillicLatin18[];
3135
extern const GFXfont RobotoMedium24;
3236

examples/arduino32/piPicoTftEncoder/tcMenuTfteSpi.cpp renamed to examples/arduino32/piPicoTftTouch/tcMenuTfteSpi.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "tcUnicodeHelper.h"
1717
#include <TFT_eSPI.h>
1818

19+
using namespace iotouch;
20+
1921
TfteSpiDrawable::TfteSpiDrawable(TFT_eSPI *tft, int spriteHeight) : tft(tft), spriteWithConfig(nullptr), spriteHeight(spriteHeight) {}
2022

2123
DeviceDrawable *TfteSpiDrawable::getSubDeviceFor(const Coord &where, const Coord& size, const color_t *palette, int paletteSize) {
@@ -163,3 +165,27 @@ void TftSpriteAndConfig::transaction(bool isStarting, bool redrawNeeded) {
163165
sprite.pushSprite(where.x, where.y, 0, 0, currentSize.x, currentSize.y);
164166
}
165167
}
168+
169+
#if TC_TFT_ESPI_NEEDS_TOUCH == true
170+
171+
TouchState TftSpiTouchInterrogator::internalProcessTouch(float *ptrX, float *ptrY, const TouchOrientationSettings& rotation, const iotouch::CalibrationHandler& calib) {
172+
173+
uint16_t touchX=0, touchY=0;
174+
bool pressed;
175+
if(usingRawTouch) {
176+
tft->getTouchRaw(&touchX, &touchY);
177+
pressed = tft->getTouchRawZ() > 600;
178+
*ptrX = calib.calibrateX(float(touchX) / XPT_2046_MAX, rotation.isXInverted());
179+
*ptrY = calib.calibrateY(float(touchY) / XPT_2046_MAX, rotation.isYInverted());
180+
} else {
181+
pressed = tft->getTouch(&touchX, &touchY);
182+
*ptrX = calib.calibrateX(float(touchX) / maxWidthDim, rotation.isXInverted());
183+
*ptrY = calib.calibrateY(float(touchY) / maxHeightDim, rotation.isYInverted());
184+
}
185+
186+
if(!pressed) return iotouch::NOT_TOUCHED;
187+
//serdebugF3("point at ", touchX, touchY);
188+
189+
return iotouch::TOUCHED;
190+
}
191+
#endif // TC_TFT_ESPI_NEEDS_TOUCH == true

examples/arduino32/piPicoTftEncoder/tcMenuTfteSpi.h renamed to examples/arduino32/piPicoTftTouch/tcMenuTfteSpi.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <graphics/GraphicsDeviceRenderer.h>
1717
#include <TFT_eSPI.h>
1818
#include <SPI.h>
19+
#include <ResistiveTouchScreen.h>
1920

2021
#define TFT_SPRITE_BITS 4
2122
#define SPRITE_PALETTE_SIZE (1 << TFT_SPRITE_BITS)
@@ -76,4 +77,34 @@ class TftSpriteAndConfig : public TfteSpiDrawable {
7677
color_t getUnderlyingColor(color_t col) override;
7778
};
7879

80+
#define TC_TFT_ESPI_NEEDS_TOUCH true
81+
#if TC_TFT_ESPI_NEEDS_TOUCH == true
82+
83+
#define XPT_2046_MAX 4096
84+
85+
namespace iotouch {
86+
87+
/**
88+
* Implements the touch interrogator class, this purely gets the current reading from the device when requested. This
89+
* implementation works with the TFT_eSPI touch functions providing tcMenu support for it. It is your responsibility
90+
* to have run whatever calibration is needed before calling this function. The X and Y values to the constructor
91+
* are the maximum values that can be returned by calling getTouch.
92+
*/
93+
class TftSpiTouchInterrogator : public iotouch::TouchInterrogator {
94+
private:
95+
TFT_eSPI* tft;
96+
float maxWidthDim;
97+
float maxHeightDim;
98+
bool usingRawTouch;
99+
public:
100+
TftSpiTouchInterrogator(TFT_eSPI* tft, uint16_t xMax, uint16_t yMax, bool rawTouch)
101+
: tft(tft), maxWidthDim(xMax), maxHeightDim(yMax), usingRawTouch(rawTouch) { }
102+
103+
iotouch::TouchState internalProcessTouch(float *ptrX, float *ptrY, const TouchOrientationSettings& touchOrientationSettings,
104+
const iotouch::CalibrationHandler& calib) override;
105+
};
106+
107+
}
108+
#endif // TC_TFT_ESPI_NEEDS_TOUCH == true
109+
79110
#endif //TCMENU_PLUGIN_TCMENUTFTESPI_H

src/graphics/MenuTouchScreenEncoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace tcgfx {
1313

1414
DialogMultiPartEditor dialogMultiPartEditor;
1515

16-
MenuTouchScreenManager::MenuTouchScreenManager(TouchInterrogator* interrogator, BaseGraphicalRenderer* renderer, iotouch::TouchInterrogator::TouchRotation rotation)
16+
MenuTouchScreenManager::MenuTouchScreenManager(TouchInterrogator* interrogator, BaseGraphicalRenderer* renderer, const TouchOrientationSettings& rotation)
1717
: TouchScreenManager(interrogator, rotation),
1818
currentlySelected(nullptr), localStart(0,0), localSize(0,0), encoder(renderer), renderer(renderer),
1919
observer(&encoder), lastX(0.0F), lastY(0.0F), currentState(NOT_TOUCHED) {

src/graphics/MenuTouchScreenEncoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace tcgfx {
105105
* @param rotation the rotation of the touch interface
106106
*/
107107
MenuTouchScreenManager(iotouch::TouchInterrogator* interrogator, BaseGraphicalRenderer* renderer,
108-
iotouch::TouchInterrogator::TouchRotation rotation);
108+
const iotouch::TouchOrientationSettings& rotation);
109109

110110
void sendEvent(float locationX, float locationY, float touchPressure, iotouch::TouchState touched) override;
111111

0 commit comments

Comments
 (0)