Skip to content

Commit 9fa8720

Browse files
BENB initial support for xy touch input
1 parent 6848542 commit 9fa8720

10 files changed

Lines changed: 424 additions & 284 deletions

File tree

‎common/src/c/grid_platform.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,6 @@ extern void grid_platform_mux_init(uint8_t mux_positions_bm);
114114

115115
extern void grid_platform_mux_write(uint8_t index);
116116

117+
extern void* grid_platform_allocate_volatile(size_t size);
118+
117119
#endif /* GRID_PLATFORM_H */

‎common/src/c/grid_protocol.h‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,19 @@
841841
#define GRID_LUA_FNC_T_TOUCH_Y_MAX_short "tyma"
842842
#define GRID_LUA_FNC_T_TOUCH_Y_MAX_human "touch_y_max"
843843

844-
#define GRID_LUA_FNC_T_TOUCH_AREA_index 8
845-
#define GRID_LUA_FNC_T_TOUCH_AREA_short "tar"
846-
#define GRID_LUA_FNC_T_TOUCH_AREA_human "touch_area"
844+
#define GRID_LUA_FNC_T_TOUCH_X_STATE_index 8
845+
#define GRID_LUA_FNC_T_TOUCH_X_STATE_short "txst"
846+
#define GRID_LUA_FNC_T_TOUCH_X_STATE_human "touch_x_state"
847847

848-
#define GRID_LUA_FNC_T_LIST_length 9
848+
#define GRID_LUA_FNC_T_TOUCH_Y_STATE_index 9
849+
#define GRID_LUA_FNC_T_TOUCH_Y_STATE_short "tyst"
850+
#define GRID_LUA_FNC_T_TOUCH_Y_STATE_human "touch_y_state"
851+
852+
#define GRID_LUA_FNC_T_LED_WIDTH_index 10
853+
#define GRID_LUA_FNC_T_LED_WIDTH_short "lwi"
854+
#define GRID_LUA_FNC_T_LED_WIDTH_human "led_width"
855+
856+
#define GRID_LUA_FNC_T_LIST_length 11
849857

850858
// ========================= UI EVENT HANDLER FUNCTIONS =========================== //
851859

‎common/src/c/grid_ui_touch.c‎

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ const luaL_Reg GRID_LUA_T_INDEX_META[] = {{GRID_LUA_FNC_T_ELEMENT_INDEX_short, X
1313
{GRID_LUA_FNC_T_LED_INDEX_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_LED_INDEX_index)},
1414
{GRID_LUA_FNC_T_TOUCH_X_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_X_index)},
1515
{GRID_LUA_FNC_T_TOUCH_Y_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_Y_index)},
16-
{GRID_LUA_FNC_T_TOUCH_AREA_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_AREA_index)},
1716
{GRID_LUA_FNC_T_TOUCH_X_MIN_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_X_MIN_index)},
1817
{GRID_LUA_FNC_T_TOUCH_X_MAX_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_X_MAX_index)},
1918
{GRID_LUA_FNC_T_TOUCH_Y_MIN_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_Y_MIN_index)},
2019
{GRID_LUA_FNC_T_TOUCH_Y_MAX_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_Y_MAX_index)},
20+
{GRID_LUA_FNC_T_TOUCH_X_STATE_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_X_STATE_index)},
21+
{GRID_LUA_FNC_T_TOUCH_Y_STATE_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_TOUCH_Y_STATE_index)},
22+
{GRID_LUA_FNC_T_LED_WIDTH_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_T_LED_WIDTH_index)},
2123
{GRID_LUA_FNC_G_TIMER_START_short, XAFTERX(GRID_LUA_FNC_META_NAME, gtt)},
2224
{GRID_LUA_FNC_G_TIMER_STOP_short, XAFTERX(GRID_LUA_FNC_META_NAME, gtp)},
2325
{GRID_LUA_FNC_G_EVENT_TRIGGER_short, XAFTERX(GRID_LUA_FNC_META_NAME, get)},
2426
{GRID_LUA_FNC_G_ELEMENTNAME_SET_short, XAFTERX(GRID_LUA_FNC_META_NAME, gsen)},
2527
{GRID_LUA_FNC_G_ELEMENTNAME_GET_short, XAFTERX(GRID_LUA_FNC_META_NAME, ggen)},
2628
{NULL, NULL}};
2729

30+
void grid_ui_touch_state_init(struct grid_ui_touch_state* state, uint8_t adc_bit_depth) { state->adc_bit_depth = adc_bit_depth; }
31+
2832
void grid_ui_element_touch_init(struct grid_ui_element* ele) {
2933

3034
ele->type = GRID_PARAMETER_ELEMENT_TOUCH;
@@ -41,6 +45,9 @@ void grid_ui_element_touch_init(struct grid_ui_element* ele) {
4145
ele->template_initializer = &grid_ui_element_touch_template_parameter_init;
4246
ele->template_parameter_list_length = GRID_LUA_FNC_T_LIST_length;
4347

48+
ele->template_parameter_element_position_index_1 = GRID_LUA_FNC_T_TOUCH_X_STATE_index;
49+
ele->template_parameter_element_position_index_2 = GRID_LUA_FNC_T_TOUCH_Y_STATE_index;
50+
4451
ele->event_clear_cb = NULL;
4552
ele->page_change_cb = NULL;
4653
}
@@ -54,44 +61,51 @@ void grid_ui_element_touch_template_parameter_init(struct grid_ui_template_buffe
5461
template_parameter_list[GRID_LUA_FNC_T_LED_INDEX_index] = element_index;
5562
template_parameter_list[GRID_LUA_FNC_T_TOUCH_X_index] = 0;
5663
template_parameter_list[GRID_LUA_FNC_T_TOUCH_Y_index] = 0;
57-
template_parameter_list[GRID_LUA_FNC_T_TOUCH_AREA_index] = 0;
5864
template_parameter_list[GRID_LUA_FNC_T_TOUCH_X_MIN_index] = 0;
5965
template_parameter_list[GRID_LUA_FNC_T_TOUCH_X_MAX_index] = 127;
6066
template_parameter_list[GRID_LUA_FNC_T_TOUCH_Y_MIN_index] = 0;
6167
template_parameter_list[GRID_LUA_FNC_T_TOUCH_Y_MAX_index] = 127;
68+
template_parameter_list[GRID_LUA_FNC_T_TOUCH_X_STATE_index] = 0;
69+
template_parameter_list[GRID_LUA_FNC_T_TOUCH_Y_STATE_index] = 0;
70+
template_parameter_list[GRID_LUA_FNC_T_LED_WIDTH_index] = 5; // TODO
6271
}
6372

64-
static int32_t touch_scale(uint16_t raw, int32_t out_min, int32_t out_max) {
65-
int32_t scaled = out_min + ((int64_t)raw * (out_max - out_min)) / 1023;
66-
return clampi32(scaled, MIN(out_min, out_max), MAX(out_min, out_max));
67-
}
73+
static double lerp(double a, double b, double x) { return a * (1.0 - x) + (b * x); }
6874

69-
void grid_ui_touch_store_input(struct grid_ui_touch_state* state, uint16_t x, uint16_t y, uint8_t area) {
75+
void grid_ui_touch_store_input(struct grid_ui_touch_state* state, struct touchinfo_t info) {
7076

7177
struct grid_ui_element* ele = state->parent;
7278
int32_t* tpl = ele->template_parameter_list;
7379

74-
state->x = x;
75-
state->y = y;
76-
state->area = area;
80+
// Invert x axis to place the origin at the bottom left
81+
info.x = (1 << state->adc_bit_depth) - 1 - info.x;
82+
83+
int32_t tmin[2] = {tpl[GRID_LUA_FNC_T_TOUCH_X_MIN_index], tpl[GRID_LUA_FNC_T_TOUCH_Y_MIN_index]};
84+
int32_t tmax[2] = {tpl[GRID_LUA_FNC_T_TOUCH_X_MAX_index], tpl[GRID_LUA_FNC_T_TOUCH_Y_MAX_index]};
85+
int32_t min[2] = {MIN(tmin[0], tmax[0]), MIN(tmin[1], tmax[1])};
86+
int32_t max[2] = {MAX(tmin[0], tmax[0]), MAX(tmin[1], tmax[1])};
7787

78-
x = 1023 - x;
88+
int32_t new[2] = {info.x, info.y};
89+
for (int i = 0; i < 2; ++i) {
7990

80-
int32_t x_min = tpl[GRID_LUA_FNC_T_TOUCH_X_MIN_index];
81-
int32_t x_max = tpl[GRID_LUA_FNC_T_TOUCH_X_MAX_index];
82-
int32_t y_min = tpl[GRID_LUA_FNC_T_TOUCH_Y_MIN_index];
83-
int32_t y_max = tpl[GRID_LUA_FNC_T_TOUCH_Y_MAX_index];
91+
new[i] = lerp(min[i], max[i] + 1, new[i] / (double)(1 << state->adc_bit_depth));
92+
93+
new[i] = clampi32(new[i], min[i], max[i]);
94+
95+
if (tmin[i] > tmax[i]) {
96+
new[i] = mirrori32(new[i], min[i], max[i]);
97+
}
98+
}
8499

85-
int32_t new_x = touch_scale(x, x_min, x_max);
86-
int32_t new_y = touch_scale(y, y_min, y_max);
100+
if (new[0] != tpl[GRID_LUA_FNC_T_TOUCH_X_index] || new[1] != tpl[GRID_LUA_FNC_T_TOUCH_Y_index]) {
87101

88-
bool changed = (new_x != tpl[GRID_LUA_FNC_T_TOUCH_X_index]) || (new_y != tpl[GRID_LUA_FNC_T_TOUCH_Y_index]);
102+
tpl[GRID_LUA_FNC_T_TOUCH_X_index] = new[0];
103+
tpl[GRID_LUA_FNC_T_TOUCH_Y_index] = new[1];
89104

90-
tpl[GRID_LUA_FNC_T_TOUCH_X_index] = new_x;
91-
tpl[GRID_LUA_FNC_T_TOUCH_Y_index] = new_y;
92-
tpl[GRID_LUA_FNC_T_TOUCH_AREA_index] = area;
105+
uint8_t diff_res = state->adc_bit_depth - 7;
106+
tpl[GRID_LUA_FNC_T_TOUCH_X_STATE_index] = info.x >> diff_res;
107+
tpl[GRID_LUA_FNC_T_TOUCH_Y_STATE_index] = info.y >> diff_res;
93108

94-
if (changed) {
95109
struct grid_ui_event* eve = grid_ui_event_find(ele, GRID_PARAMETER_EVENT_TOUCH);
96110
grid_ui_event_state_set(eve, GRID_EVE_STATE_TRIG);
97111
}

‎common/src/c/grid_ui_touch.h‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,54 @@
66
#include "grid_protocol.h"
77
#include "grid_ui.h"
88

9-
struct grid_ui_touch_state {
10-
struct grid_ui_element* parent;
9+
struct touchinfo_t {
10+
uint8_t id;
11+
uint8_t type;
1112
uint16_t x;
1213
uint16_t y;
13-
uint8_t area; // 0 = not pressed
1414
};
1515

16+
struct grid_ui_touch_state {
17+
struct grid_ui_element* parent;
18+
uint8_t adc_bit_depth;
19+
};
20+
21+
void grid_ui_touch_state_init(struct grid_ui_touch_state* state, uint8_t adc_bit_depth);
22+
1623
void grid_ui_element_touch_init(struct grid_ui_element* ele);
1724
void grid_ui_element_touch_template_parameter_init(struct grid_ui_template_buffer* buf);
1825

1926
static inline struct grid_ui_touch_state* grid_ui_touch_get_state(struct grid_ui_element* ele) { return (struct grid_ui_touch_state*)ele->primary_state; }
2027

21-
void grid_ui_touch_store_input(struct grid_ui_touch_state* state, uint16_t x, uint16_t y, uint8_t area);
28+
void grid_ui_touch_store_input(struct grid_ui_touch_state* state, struct touchinfo_t info);
29+
30+
// clang-format off
2231

2332
#define GRID_LUA_T_TYPE "Touch"
2433

2534
extern const luaL_Reg GRID_LUA_T_INDEX_META[];
2635

27-
#define GRID_LUA_T_META_init \
28-
GRID_LUA_T_TYPE " = { __index = {" \
29-
"type = 'touch', " \
30-
"post_init_cb = function (self) " \
31-
"self:" GRID_LUA_FNC_A_INIT_short "() " \
32-
"self:" GRID_LUA_FNC_A_TOUCH_short "() " \
33-
"end," GRID_LUA_FNC_ASSIGN_META_PAR1_RET("gen", GRID_LUA_FNC_G_ELEMENTNAME_short) "," \
34-
"}}"
35-
36-
#define GRID_ACTIONSTRING_TOUCH_INIT "--[[@cb]]--[[Touch Init]]"
37-
38-
#define GRID_ACTIONSTRING_TOUCH_TOUCH "--[[@cb]]print(self:ind(),self:tsx(),self:tsy(),self:tar())"
36+
#define GRID_LUA_T_META_init \
37+
GRID_LUA_T_TYPE " = { __index = {" \
38+
\
39+
"type = 'touch', " \
40+
\
41+
"post_init_cb = function (self) " \
42+
"self:" GRID_LUA_FNC_A_INIT_short "() " \
43+
"self:" GRID_LUA_FNC_A_TOUCH_short "() " \
44+
"end," \
45+
\
46+
GRID_LUA_FNC_ASSIGN_META_PAR1_RET("gen", GRID_LUA_FNC_G_ELEMENTNAME_short) "," \
47+
\
48+
GRID_LUA_FNC_ASSIGN_META_EVENT(INIT, TOUCH_INIT) \
49+
GRID_LUA_FNC_ASSIGN_META_EVENT(TOUCH, TOUCH_TOUCH) \
50+
\
51+
"}}"
52+
53+
#define GRID_ACTIONSTRING_TOUCH_INIT "--[[@cb]] function touch_dist(led,x,y)local lw=self:lwi()local lp={led%lw+0.0,(lw-1)-led//lw+0.0}local tp={x*(lw-1),y*(lw-1)}local d={tp[1]-lp[1],tp[2]-lp[2]}return math.sqrt(d[1]*d[1]+d[2]*d[2])end local leds=self:lwi()*self:lwi()for i=0,leds-1 do gln(i,1,0,0,0)gld(i,1,127,127,127)glx(i,1,255,255,255)glp(i,1,0)end"
54+
55+
#define GRID_ACTIONSTRING_TOUCH_TOUCH "--[[@cb]] local leds=self:lwi()*self:lwi()for i=0,leds-1 do local dist=touch_dist(i,self:tsx()/127,self:tsy()/127)local pha=255-dist*255 if dist>=1 then pha=0 end glp(i,1,pha//1)end"
56+
57+
// clang-format on
3958

4059
#endif /* GRID_UI_TOUCH_H */

‎esp32s3/components/grid_esp32_module_xy/grid_esp32_module_xy.c‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,27 @@
2222
#define XY_SENSOR_RESET_GPIO 39
2323
#define XY_SENSOR_INT_GPIO 42
2424

25-
void grid_esp32_module_xy_handle_touch(void) {
26-
TOUCHINFO ti = {};
27-
if (!grid_esp32_touch_get_samples(&grid_esp32_touch_state, &ti)) {
25+
void grid_esp32_module_xy_process_touch(struct touchinfo_t* info) {
26+
27+
struct grid_ui_element* ele = grid_ui_element_find(&grid_ui_state, info->id);
28+
if (!ele) {
2829
return;
2930
}
3031

31-
for (uint8_t i = 0; i < 5; i++) {
32-
struct grid_ui_element* ele = grid_ui_element_find(&grid_ui_state, i);
33-
if (!ele) {
34-
continue;
35-
}
36-
struct grid_ui_touch_state* state = grid_ui_touch_get_state(ele);
37-
38-
if (i < ti.count) {
39-
grid_ui_touch_store_input(state, ti.x[i], ti.y[i], ti.area[i]);
40-
} else if (state->area > 0) {
41-
grid_ui_touch_store_input(state, state->x, state->y, 0);
42-
}
43-
}
32+
grid_ui_touch_store_input(grid_ui_touch_get_state(ele), *info);
4433
}
4534

46-
void grid_esp32_module_xy_init(struct grid_sys_model* sys, struct grid_ui_model* ui) {
35+
void grid_esp32_module_xy_init(struct grid_sys_model* sys, struct grid_ui_model* ui, TaskHandle_t touch_task) {
36+
37+
grid_esp32_touch_init(&grid_esp32_touch_state, XY_I2C_PORT, XY_I2C_SCL_GPIO, XY_I2C_SDA_GPIO, XY_SENSOR_RESET_GPIO, XY_SENSOR_INT_GPIO, XY_I2C_FREQ_HZ, grid_esp32_module_xy_process_touch,
38+
touch_task);
4739

48-
grid_esp32_touch_init(&grid_esp32_touch_state, XY_I2C_PORT, XY_I2C_SCL_GPIO, XY_I2C_SDA_GPIO, XY_SENSOR_RESET_GPIO, XY_SENSOR_INT_GPIO, XY_I2C_FREQ_HZ, NULL);
40+
for (int i = 0; i < ui->element_list_length; ++i) {
41+
struct grid_ui_element* ele = &ui->element_list[i];
42+
if (ele->type == GRID_PARAMETER_ELEMENT_TOUCH) {
43+
grid_ui_touch_state_init(grid_ui_touch_get_state(ele), 10);
44+
}
45+
}
4946

5047
grid_ui_bulk_start_with_state(ui, grid_ui_bulk_conf_read, 0, 0, NULL);
5148
grid_ui_bulk_flush(ui);

‎esp32s3/components/grid_esp32_module_xy/grid_esp32_module_xy.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ extern "C" {
1212
#include "grid_sys.h"
1313
#include "grid_ui.h"
1414

15-
void grid_esp32_module_xy_init(struct grid_sys_model* sys, struct grid_ui_model* ui);
16-
void grid_esp32_module_xy_handle_touch(void);
15+
#include "freertos/FreeRTOS.h"
16+
17+
void grid_esp32_module_xy_init(struct grid_sys_model* sys, struct grid_ui_model* ui, TaskHandle_t touch_task);
1718

1819
#ifdef __cplusplus
1920
}

‎esp32s3/components/grid_esp32_touch/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ idf_component_register(
44
INCLUDE_DIRS
55
"."
66
REQUIRES
7-
"driver" "esp_driver_gpio" "freertos"
7+
"driver" "esp_driver_gpio" "esp_driver_i2c" "freertos" "grid_common"
88
)

0 commit comments

Comments
 (0)