1+ #include " nvs.h"
2+ #include " nvs_handle.hpp"
13#include " powerSave.h"
24#include < Wire.h>
35#include < interface.h>
46
7+ namespace {
8+ std::unique_ptr<nvs::NVSHandle> openNamespace (const char *ns, nvs_open_mode_t mode, esp_err_t &err) {
9+ auto handle = nvs::open_nvs_handle (ns, mode, &err);
10+ if (err != ESP_OK) {
11+ log_i (" openNamespace(%s) failed: %d" , ns, err);
12+ return nullptr ;
13+ }
14+ return handle;
15+ }
16+ } // namespace
517#ifndef TFT_BRIGHT_CHANNEL
618#define TFT_BRIGHT_CHANNEL 0
719#define TFT_BRIGHT_FREQ 5000
820#define TFT_BRIGHT_Bits 8
921#define TFT_BL 27
1022#endif
1123
12- #if defined(HAS_CAPACITIVE_TOUCH) // CST816S
13- #include " CYD28_TouchscreenC.h"
14- #define CYD28_DISPLAY_HOR_RES_MAX 240
15- #define CYD28_DISPLAY_VER_RES_MAX 320
16- CYD28_TouchC touch (CYD28_DISPLAY_HOR_RES_MAX, CYD28_DISPLAY_VER_RES_MAX);
17-
18- #elif defined(TOUCH_GT911_I2C) || defined(TOUCH_CST816S_I2C)
19- #ifdef TOUCH_GT911_I2C
20- #define TOUCH_MODULES_GT911
21- #define TOUCH_SDA_PIN GT911_I2C_CONFIG_SDA_IO_NUM
22- #define TOUCH_SCL_PIN GT911_I2C_CONFIG_SCL_IO_NUM
23- #define TOUCH_RST_PIN GT911_TOUCH_CONFIG_RST_GPIO_NUM
24- #define TOUCH_INT_PIN GT911_TOUCH_CONFIG_INT_GPIO_NUM
25- #define TOUCH_ADDR GT911_SLAVE_ADDRESS1
26- #ifndef TOUCH_INVERTED
27- #define TOUCH_INVERTED 0
28- #endif
29- #elif TOUCH_CST816S_I2C
30- #define TOUCH_MODULES_CST_SELF
31- #define TOUCH_SDA_PIN CST816S_I2C_CONFIG_SDA_IO_NUM
32- #define TOUCH_SCL_PIN CST816S_I2C_CONFIG_SCL_IO_NUM
33- #define TOUCH_RST_PIN CST816S_TOUCH_CONFIG_RST_GPIO_NUM
34- #define TOUCH_INT_PIN CST816S_TOUCH_CONFIG_INT_GPIO_NUM
35- #define TOUCH_ADDR CTS820_SLAVE_ADDRESS
36- #endif
37-
38- #include < TouchLib.h>
39- class CYD_Touch : public TouchLib {
40- public:
41- TouchPoint t;
42- TP_Point ti;
43- CYD_Touch () : TouchLib(Wire, TOUCH_SDA_PIN, TOUCH_SCL_PIN, TOUCH_ADDR, TOUCH_RST_PIN) {}
44- inline bool begin () {
45- bool result = init ();
46- setRotation (ROTATION);
47- return result;
48- }
49- inline bool touched () { return read (); }
50- inline TouchPoint getPointScaled () {
51- ti = getPoint (0 );
52- #if TOUCH_INVERTED
53- t.x = ti.y ;
54- t.y = TFT_WIDTH - ti.x ;
55- #else
56- t.x = ti.x ;
57- t.y = (tftHeight + 20 ) - ti.y ;
58- #endif
59- t.pressed = true ;
60- TouchLib::raw_data[0 ] = 0 ; // resets the read raw reading, that triggers TouchLib::read() to true, and
61- // is not resetted at the lib
62- return t;
63- }
64- };
65- CYD_Touch touch;
66- #else
67- #include " CYD28_TouchscreenR.h"
68- #ifndef CYD28_DISPLAY_HOR_RES_MAX
69- #define CYD28_DISPLAY_HOR_RES_MAX TFT_HEIGHT
70- #endif
71-
72- #ifndef CYD28_DISPLAY_VER_RES_MAX
73- #define CYD28_DISPLAY_VER_RES_MAX TFT_WIDTH
74- #endif
75- CYD28_TouchR touch (CYD28_DISPLAY_HOR_RES_MAX, CYD28_DISPLAY_VER_RES_MAX);
76- #endif
77-
7824/* **************************************************************************************
7925** Function name: _setup_gpio()
8026** Location: main.cpp
8127** Description: initial setup for the device
8228***************************************************************************************/
8329void _setup_gpio () {
84- #if !defined(HAS_CAPACITIVE_TOUCH) && (defined(TOUCH_GT911_I2C) || defined(TOUCH_CST816S_I2C))
85- Wire.begin (TOUCH_SDA_PIN, TOUCH_SCL_PIN);
86- #endif
87- #if !defined(HAS_CAPACITIVE_TOUCH) && defined(ELECROW)
8830 pinMode (33 , OUTPUT); // touch CS
89- #endif
9031}
9132
9233/* **************************************************************************************
@@ -99,18 +40,52 @@ void _post_setup_gpio() {
9940 pinMode (TFT_BL, OUTPUT);
10041 ledcAttach (TFT_BL, TFT_BRIGHT_FREQ, TFT_BRIGHT_Bits);
10142 ledcWrite (TFT_BL, 255 );
43+ esp_err_t err = ESP_OK;
44+ uint16_t calData[5 ];
45+ auto nvsHandle = openNamespace (" calData" , NVS_READWRITE, err);
46+ if (nvsHandle) {
47+ err = nvsHandle->get_item (" x0" , calData[0 ]);
48+ err |= nvsHandle->get_item (" x1" , calData[1 ]);
49+ err |= nvsHandle->get_item (" y0" , calData[2 ]);
50+ err |= nvsHandle->get_item (" y1" , calData[3 ]);
51+ err |= nvsHandle->get_item (" r" , calData[4 ]);
52+ } else {
53+ err = 1 ;
54+ Serial.println (" Can't access calData namespace in NVS" );
55+ }
10256
103- if (!touch.begin (
104- #ifdef CYD28_TouchR_MOSI
105- #if TFT_MOSI == CYD28_TouchR_MOSI
106- &SPI
107- #endif
108- #endif
109-
110- )) {
111- Serial.println (" Touch IC not Started" );
112- log_i (" Touch IC not Started" );
113- } else Serial.println (" Touch IC Started" );
57+ if (err) {
58+ Serial.println (" No calData available" );
59+ tft->setRotation (1 );
60+ tft->setTextSize (2 );
61+ tft->drawCentreString (" Touch corners as indicated" , TFT_HEIGHT / 2 , TFT_WIDTH / 2 , 1 );
62+ tft->calibrateTouch (calData, TFT_GREEN, TFT_BLACK, 15 );
63+ tft->setTouch (calData);
64+ if (nvsHandle) {
65+ Serial.println (" Saving values into NVS" );
66+ err = ESP_OK;
67+ err = nvsHandle->set_item (" x0" , calData[0 ]);
68+ err |= nvsHandle->set_item (" x1" , calData[1 ]);
69+ err |= nvsHandle->set_item (" y0" , calData[2 ]);
70+ err |= nvsHandle->set_item (" y1" , calData[3 ]);
71+ err |= nvsHandle->set_item (" r" , calData[4 ]);
72+ if (err != ESP_OK) {
73+ Serial.printf (" Failed to store settings in NVS: %d\n " , err);
74+ } else {
75+ Serial.println (" Settings stored in NVS successfully" );
76+ }
77+ tft->setRotation (rotation);
78+ }
79+ } else {
80+ tft->setTouch (calData);
81+ }
82+ Serial.print (" \n calData[5] = " );
83+ Serial.print (" { " );
84+ for (uint8_t i = 0 ; i < 5 ; i++) {
85+ Serial.print (calData[i]);
86+ if (i < 4 ) Serial.print (" , " );
87+ }
88+ Serial.println (" }" );
11489}
11590
11691/* ********************************************************************
@@ -140,18 +115,24 @@ void _setBrightness(uint8_t brightval) {
140115** Function: InputHandler
141116** Handles the variables PrevPress, NextPress, SelPress, AnyKeyPress and EscPress
142117**********************************************************************/
118+ struct TouchPointPro {
119+ uint16_t x;
120+ uint16_t y;
121+ };
143122void InputHandler (void ) {
123+ TouchPointPro t;
144124 static long d_tmp = millis ();
125+ const uint16_t w = tftWidth;
126+ const uint16_t h = tftHeight + 20 ;
145127 if (millis () - d_tmp > 250 || LongPress) { // I know R3CK.. I Should NOT nest if statements..
146128 // but it is needed to not keep SPI bus used without need, it save resources
147- TouchPoint t;
148129#ifdef DONT_USE_INPUT_TASK
149130 checkPowerSaveTime ();
150131#endif
151- if (touch. touched ()) {
152- auto t = touch. getPointScaled ();
132+ bool pressed = tft-> getTouch (&t. x , &t. y );
133+ if (pressed) {
153134 d_tmp = millis ();
154- # ifdef DONT_USE_INPUT_TASK // need to reset the variables to avoid ghost click
135+ // need to reset the variables to avoid ghost click
155136 NextPress = false ;
156137 PrevPress = false ;
157138 UpPress = false ;
@@ -160,39 +141,26 @@ void InputHandler(void) {
160141 EscPress = false ;
161142 AnyKeyPress = false ;
162143 touchPoint.pressed = false ;
163- #endif
164144
165- #ifdef CYD28_TouchR_MOSI
166- #if TFT_MOSI == CYD28_TouchR_MOSI // S024R is inverted
167- int tmp = t.x ;
168- t.x = t.y ;
169- t.y = tmp;
170- #endif
171- #endif
172-
173- if (rotation == 3 ) {
174- t.y = (tftHeight + 20 ) - t.y ;
175- t.x = tftWidth - t.x ;
145+ Serial.printf (" \n Raw Touch on x=%d, y=%d, rot=%d\n " , t.x , t.y , rotation);
146+ if (rotation == 1 ) { // Landscape
147+ // Do Nothing
148+ }
149+ if (rotation == 3 ) { // Landscape
150+ t.y = h - t.y ; // invert y
151+ t.x = w - t.x ; // invert x
176152 }
177- if (rotation == 0 ) {
178- int tmp = t.x ;
179- t.x = tftWidth - t.y ;
153+ if (rotation == 0 ) { // Portrait
154+ int tmp = t.x ; // swap x y
155+ t.x = w - t.y ; // invert x
180156 t.y = tmp;
181157 }
182- if (rotation == 2 ) {
183- int tmp = t.x ;
158+ if (rotation == 2 ) { // Portrait
159+ int tmp = t.x ; // swap x y
184160 t.x = t.y ;
185- t.y = (tftHeight + 20 ) - tmp;
161+ t.y = h - tmp; // invert y
186162 }
187- Serial.printf (" \n Touch Pressed on x=%d, y=%d, rot=%d\n " , t.x , t.y , rotation);
188- log_i (" \n Touch Pressed on x=%d, y=%d, rot=%d\n " , t.x , t.y , rotation);
189- #if defined(CYD28_DISPLAY_VER_RES_MAX) && !defined(HAS_CAPACITIVE_TOUCH)
190- #if CYD28_DISPLAY_VER_RES_MAX > 340
191- auto t2 = touch.getPointRaw ();
192- Serial.printf (" RAW d Pressed on x=%d, y=%d\n " , t2.x , t2.y );
193- #endif
194- #endif
195-
163+ Serial.printf (" Touch Pressed on x=%d, y=%d, rot=%d\n " , t.x , t.y , rotation);
196164 if (!wakeUpScreen ()) AnyKeyPress = true ;
197165 else return ;
198166
@@ -203,10 +171,6 @@ void InputHandler(void) {
203171 touchHeatMap (touchPoint);
204172 }
205173 }
206- #ifdef TOUCH_GT911_I2C
207- else
208- touch.touched (); // keep calling it to keep refreshing raw readings for when needed it will be ok
209- #endif
210174}
211175
212176/* ********************************************************************
0 commit comments