Skip to content

Commit ae9e8df

Browse files
committed
Fix touchscreen buttons rendering and handling
1 parent d47bcac commit ae9e8df

File tree

3 files changed

+73
-53
lines changed

3 files changed

+73
-53
lines changed

main/display.c

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,44 @@ static inline void fill_disp_buf_color(size_t opt_loop, color_t original, uint32
144144

145145
#define min(A, B) ((A) < (B) ? (A) : (B))
146146

147+
#if defined(CONFIG_DISPLAY_TOUCHSCREEN)
148+
#define TOUCH_BUTTON_WIDTH 40
149+
#define TOUCH_BUTTON_HEIGHT 40
150+
#define TOUCH_BUTTON_MARGIN 5
151+
152+
static void display_draw_touch_buttons(void)
153+
{
154+
/* The TwatchS3 and core s3 don't have buttons that can be used (just power and
155+
reset)
156+
but it has a touch panel, we use the bottom 40 pixels worth of height
157+
to display 3 buttons (prev, OK, next), we handle this here rather than
158+
in display_hw because we want to draw text inside the virtual buttons */
159+
160+
/* blank the bottom of the display with black */
161+
uint16_t line[CONFIG_DISPLAY_WIDTH] = { TFT_BLACK };
162+
for (int16_t i = 0; i < TOUCH_BUTTON_HEIGHT; ++i) {
163+
draw_bitmap(CONFIG_DISPLAY_OFFSET_X, CONFIG_DISPLAY_HEIGHT + i + CONFIG_DISPLAY_OFFSET_Y,
164+
CONFIG_DISPLAY_WIDTH + CONFIG_DISPLAY_OFFSET_X, 1, line);
165+
}
166+
167+
dispWin_t disp_win_virtual_buttons = { .x1 = TOUCH_BUTTON_MARGIN + CONFIG_DISPLAY_OFFSET_X,
168+
.y1 = CONFIG_DISPLAY_HEIGHT + TOUCH_BUTTON_MARGIN + CONFIG_DISPLAY_OFFSET_Y,
169+
.x2 = TOUCH_BUTTON_WIDTH + CONFIG_DISPLAY_OFFSET_X,
170+
.y2 = (CONFIG_DISPLAY_HEIGHT + (TOUCH_BUTTON_HEIGHT - TOUCH_BUTTON_MARGIN)) + CONFIG_DISPLAY_OFFSET_Y };
171+
172+
display_set_font(JADE_SYMBOLS_16x16_FONT, NULL);
173+
display_print_in_area("H", CENTER, CENTER, disp_win_virtual_buttons, 0);
174+
disp_win_virtual_buttons.x1 = ((CONFIG_DISPLAY_WIDTH / 2) + CONFIG_DISPLAY_OFFSET_X) - (TOUCH_BUTTON_WIDTH / 2);
175+
disp_win_virtual_buttons.x2 = ((CONFIG_DISPLAY_WIDTH / 2) + CONFIG_DISPLAY_OFFSET_X) + (TOUCH_BUTTON_WIDTH / 2);
176+
display_print_in_area("J", CENTER, CENTER, disp_win_virtual_buttons, 0);
177+
disp_win_virtual_buttons.x1
178+
= ((CONFIG_DISPLAY_WIDTH - TOUCH_BUTTON_MARGIN) + CONFIG_DISPLAY_OFFSET_X) - TOUCH_BUTTON_WIDTH;
179+
disp_win_virtual_buttons.x2 = (CONFIG_DISPLAY_WIDTH - TOUCH_BUTTON_MARGIN) + CONFIG_DISPLAY_OFFSET_X;
180+
display_print_in_area("I", CENTER, CENTER, disp_win_virtual_buttons, 0);
181+
display_set_font(DEFAULT_FONT, NULL);
182+
}
183+
#endif
184+
147185
void display_fill_rect(int x, int y, int w, int h, color_t color)
148186
{
149187
if ((x >= GUI_DISPLAY_WINDOW.x2) || (y > GUI_DISPLAY_WINDOW.y2)) {
@@ -183,8 +221,7 @@ void display_fill_rect(int x, int y, int w, int h, color_t color)
183221
|| ((y - CONFIG_DISPLAY_OFFSET_Y) + h > CONFIG_DISPLAY_HEIGHT)) {
184222
JADE_LOGE(
185223
"display_fill_rect called with bad params (ignored) x %d y %d w %d h %d color %u\n", x, y, w, h, color);
186-
#if !defined(CONFIG_BOARD_TYPE_M5_CORES3) && !defined(CONFIG_BOARD_TYPE_TTGO_TWATCHS3) \
187-
&& !defined(CONFIG_BOARD_TYPE_WS_TOUCH_LCD2)
224+
#if !defined(CONFIG_DISPLAY_TOUCHSCREEN)
188225
return;
189226
#endif
190227
}
@@ -245,43 +282,8 @@ void display_init(TaskHandle_t* gui_h)
245282
JADE_ASSERT(gui_h);
246283
display_hw_init(gui_h);
247284

248-
#if defined(CONFIG_BOARD_TYPE_TTGO_TWATCHS3) || defined(CONFIG_BOARD_TYPE_M5_CORES3) \
249-
|| defined(CONFIG_BOARD_TYPE_WS_TOUCH_LCD2)
250-
#define TOUCH_BUTTON_AREA 40
251-
#define TOUCH_BUTTON_MARGIN 5
252-
#define TOUCH_BUTTON_WIDTH 40
253-
/* The TwatchS3 and core s3 don't have buttons that can be used (just power and
254-
reset)
255-
but it has a touch panel, we use the bottom 40 pixels worth of height
256-
to display 3 buttons (prev, OK, next), we handle this here rather than
257-
in display_hw because we want to draw text inside the virtual buttons */
258-
259-
vTaskDelay(50 / portTICK_PERIOD_MS);
260-
261-
/* blank the bottom of the display with black */
262-
uint16_t line[CONFIG_DISPLAY_WIDTH] = { TFT_BLACK };
263-
for (int16_t i = 0; i < TOUCH_BUTTON_AREA; ++i) {
264-
draw_bitmap(CONFIG_DISPLAY_OFFSET_X, CONFIG_DISPLAY_HEIGHT + i + CONFIG_DISPLAY_OFFSET_Y,
265-
CONFIG_DISPLAY_WIDTH + CONFIG_DISPLAY_OFFSET_X, 1, line);
266-
}
267-
268-
dispWin_t disp_win_virtual_buttons = { .x1 = TOUCH_BUTTON_MARGIN + CONFIG_DISPLAY_OFFSET_X,
269-
.y1 = CONFIG_DISPLAY_HEIGHT + TOUCH_BUTTON_MARGIN + CONFIG_DISPLAY_OFFSET_Y,
270-
.x2 = TOUCH_BUTTON_WIDTH + CONFIG_DISPLAY_OFFSET_X,
271-
.y2 = (CONFIG_DISPLAY_HEIGHT + (TOUCH_BUTTON_AREA - TOUCH_BUTTON_MARGIN)) + CONFIG_DISPLAY_OFFSET_Y };
272-
273-
display_set_font(JADE_SYMBOLS_16x16_FONT, NULL);
274-
display_print_in_area("H", CENTER, CENTER, disp_win_virtual_buttons, 0);
275-
disp_win_virtual_buttons.x1 = ((CONFIG_DISPLAY_WIDTH / 2) + CONFIG_DISPLAY_OFFSET_X) - (TOUCH_BUTTON_WIDTH / 2);
276-
disp_win_virtual_buttons.x2 = ((CONFIG_DISPLAY_WIDTH / 2) + CONFIG_DISPLAY_OFFSET_X) + (TOUCH_BUTTON_WIDTH / 2);
277-
display_print_in_area("J", CENTER, CENTER, disp_win_virtual_buttons, 0);
278-
disp_win_virtual_buttons.x1
279-
= ((CONFIG_DISPLAY_WIDTH - TOUCH_BUTTON_MARGIN) + CONFIG_DISPLAY_OFFSET_X) - TOUCH_BUTTON_WIDTH;
280-
disp_win_virtual_buttons.x2 = (CONFIG_DISPLAY_WIDTH - TOUCH_BUTTON_MARGIN) + CONFIG_DISPLAY_OFFSET_X;
281-
display_print_in_area("I", CENTER, CENTER, disp_win_virtual_buttons, 0);
282-
display_set_font(DEFAULT_FONT, NULL);
283-
284-
vTaskDelay(50 / portTICK_PERIOD_MS);
285+
#if defined(CONFIG_DISPLAY_TOUCHSCREEN)
286+
display_draw_touch_buttons();
285287
#endif
286288
#endif
287289

@@ -296,7 +298,13 @@ void display_init(TaskHandle_t* gui_h)
296298
bool display_flip_orientation(const bool flipped_orientation)
297299
{
298300
#ifndef CONFIG_ETH_USE_OPENETH
299-
return display_hw_flip_orientation(flipped_orientation);
301+
display_hw_flip_orientation(flipped_orientation);
302+
303+
#if defined(CONFIG_DISPLAY_TOUCHSCREEN)
304+
display_draw_touch_buttons();
305+
#endif
306+
307+
return flipped_orientation;
300308
#else
301309
// Not supported for qemu
302310
return false;

main/display_hw.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ inline void display_hw_draw_bitmap(int x, int y, int w, int h, const uint16_t* c
267267
JADE_ASSERT(color_data);
268268
const int calculatedx = x - CONFIG_DISPLAY_OFFSET_X;
269269
const int calculatedy = y - CONFIG_DISPLAY_OFFSET_Y;
270-
#if (defined(CONFIG_BOARD_TYPE_M5_CORES3) || defined(CONFIG_BOARD_TYPE_TTGO_TWATCHS3) \
271-
|| defined(CONFIG_BOARD_TYPE_WS_TOUCH_LCD2)) \
272-
&& defined(CONFIG_DISPLAY_FULL_FRAME_BUFFER)
270+
#if defined(CONFIG_DISPLAY_TOUCHSCREEN) && defined(CONFIG_DISPLAY_FULL_FRAME_BUFFER)
273271
/* this is required for the virtual buttons */
274272
if (calculatedy >= CONFIG_DISPLAY_HEIGHT) {
275273
ESP_ERROR_CHECK(

main/input/touchscreen.inc

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#define ESP_LCD_TOUCH_NEW_I2C(han, cfg, ret) esp_lcd_touch_new_i2c_ft5x06(han, cfg, ret)
2020
#endif
2121

22+
#define TOUCH_BUTTON_WIDTH 40
23+
#define TOUCH_BUTTON_HEIGHT 40
24+
#define TOUCH_BUTTON_MARGIN 5
25+
2226
static volatile bool shutdown_requested = false;
2327
static volatile bool shutdown_finished = false;
2428

@@ -28,7 +32,6 @@ esp_err_t _i2c_deinit(i2c_master_bus_handle_t handle);
2832
static void touchscreen_task(void* ignored)
2933
{
3034
esp_lcd_touch_handle_t ret_touch = NULL;
31-
// FIXME: check mirror flags?
3235
const esp_lcd_touch_config_t tp_cfg = {
3336
.x_max = CONFIG_DISPLAY_WIDTH + CONFIG_DISPLAY_OFFSET_X,
3437
.y_max = CONFIG_DISPLAY_HEIGHT + CONFIG_DISPLAY_OFFSET_Y,
@@ -63,33 +66,44 @@ static void touchscreen_task(void* ignored)
6366
uint16_t touch_x[1];
6467
uint16_t touch_y[1];
6568
uint16_t touch_strength[1];
66-
uint8_t touch_cnt = 10;
69+
uint8_t touch_cnt = 1;
6770

68-
// FIXME: this doesn't currently work with Display -> Flip Orientation feature
69-
// but it could by changing the touch_y[0] > 200 logic with < 40 and inverting prev with next and viceversa
7071
while (!shutdown_requested) {
7172
if (esp_lcd_touch_read_data(ret_touch) == ESP_OK) {
7273
bool touchpad_pressed
7374
= esp_lcd_touch_get_coordinates(ret_touch, touch_x, touch_y, touch_strength, &touch_cnt, 1);
75+
7476
if (touchpad_pressed) {
75-
const uint16_t first_third_end = CONFIG_DISPLAY_WIDTH / 3;
76-
const uint16_t middle_thirds_end = (CONFIG_DISPLAY_WIDTH * 2) / 3;
77-
if (touch_y[0] > 200) {
78-
if (touch_x[0] <= first_third_end) {
77+
const uint16_t display_left = CONFIG_DISPLAY_OFFSET_X;
78+
const uint16_t display_right = CONFIG_DISPLAY_WIDTH + CONFIG_DISPLAY_OFFSET_X;
79+
const uint16_t display_top = CONFIG_DISPLAY_OFFSET_Y;
80+
const uint16_t display_bottom = CONFIG_DISPLAY_HEIGHT + TOUCH_BUTTON_HEIGHT + CONFIG_DISPLAY_OFFSET_Y;
81+
const uint16_t split_width = CONFIG_DISPLAY_WIDTH / 3;
82+
83+
bool is_flipped = gui_get_flipped_orientation();
84+
85+
if (
86+
(!is_flipped && touch_y[0] + TOUCH_BUTTON_HEIGHT >= display_bottom && touch_y[0] < display_bottom) ||
87+
(is_flipped && touch_y[0] >= display_top && touch_y[0] < display_top + TOUCH_BUTTON_HEIGHT)
88+
) {
89+
if (touch_x[0] >= display_left && touch_x[0] < display_left + split_width) {
7990
gui_prev();
80-
} else if (touch_x[0] > first_third_end && touch_x[0] < middle_thirds_end) {
91+
} else if (touch_x[0] >= display_left + split_width && touch_x[0] < display_left + split_width * 2) {
8192
gui_front_click();
82-
} else if (touch_x[0] >= middle_thirds_end) {
93+
} else if (touch_x[0] >= display_left + split_width * 2 && touch_x[0] < display_right) {
8394
gui_next();
8495
} else {
8596
continue;
8697
}
98+
8799
vTaskDelay(250 / portTICK_PERIOD_MS);
88100
}
89101
}
90102
}
103+
91104
vTaskDelay(20 / portTICK_PERIOD_MS);
92105
}
106+
93107
ESP_ERROR_CHECK(_i2c_deinit(touch_i2c_handle));
94108
shutdown_finished = true;
95109
vTaskDelete(NULL);

0 commit comments

Comments
 (0)