Skip to content

Commit

Permalink
Merge pull request #345 from shorepine/yscale
Browse files Browse the repository at this point in the history
Setting y-scale for touchscreen, #343
  • Loading branch information
bwhitman authored Sep 3, 2024
2 parents efefedf + 5c69514 commit 5bb7fb4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/tulip_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ tulip.keyboard_callback() # removes callbacks.

# Modify the touch screen calibration if needed (on Tulip CC only)
# Run ex/calibrate.py to determine this for your panel
tulip.touch_delta(-20, 0) # -20 x, 0 y
tulip.touch_delta(-20, 0, 0.8) # -20 x, 0 y, 0.8 y scale
tulip.touch_delta() # returns current delta

# Set up a callback to receive raw touch events. up == 1 when finger / mouse lifted up
Expand Down
8 changes: 4 additions & 4 deletions tulip/esp32s3/gt911_touchscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
// Default for my V4R11
int16_t touch_x_delta = -2;
int16_t touch_y_delta = -12;
float y_scale = 0.8f;
float touch_y_scale = 0.8f;
#else
int16_t touch_x_delta = 0;
int16_t touch_y_delta = 0;
float y_scale = 1.0f;
float touch_y_scale = 1.0f;
#endif

esp_lcd_touch_handle_t tp;
Expand Down Expand Up @@ -120,9 +120,9 @@ void run_gt911(void *param) {
last_touch_x[i] = touch_x[i] + touch_x_delta;
#ifdef TDECK
// tdeck has swapped y that mirror_y doesn't fix
last_touch_y[i] = ((V_RES-touch_y[i]) + touch_y_delta)*y_scale;
last_touch_y[i] = ((V_RES-touch_y[i]) + touch_y_delta)*touch_y_scale;
#else
last_touch_y[i] = (touch_y[i] + touch_y_delta)*y_scale;
last_touch_y[i] = (touch_y[i] + touch_y_delta)*touch_y_scale;
#endif
}
//fprintf(stderr, "touch DOWN %d %d\n", last_touch_x[0], last_touch_y[0]);
Expand Down
13 changes: 9 additions & 4 deletions tulip/fs/ex/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ def touch_cb(x):
for (x,y) in deltas:
x_mean += x
y_mean += y


old_delta = tulip.touch_delta()
x_mean = int(float(x_mean) / float(len(deltas)))
y_mean = int(float(y_mean) / float(len(deltas)))

old_delta = tulip.touch_delta()
print("New computed delta is [%d, %d]. It was [%d, %d]. " %(x_mean, y_mean, old_delta[0], old_delta[1]))
# TODO: compute this from the deltas
y_scale = old_delta[2]

print("New computed delta is [%d, %d, %f]. It was [%d, %d, %f]. " %(x_mean, y_mean, y_scale, old_delta[0], old_delta[1], old_delta[2]))
if(tulip.prompt("Set it?")):
tulip.touch_delta(x_mean, y_mean)
tulip.touch_delta(x_mean, y_mean, y_scale)
if(tulip.prompt("Set. Add calibration to boot.py?")):
tulip.add_to_bootpy("tulip.touch_delta(%d,%d)" % (x_mean, y_mean))
tulip.add_to_bootpy("tulip.touch_delta(%d,%d,%f)" % (x_mean, y_mean, y_scale))
print("Added.")

tulip.bg_clear()
Expand Down
11 changes: 8 additions & 3 deletions tulip/shared/modtulip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,26 +1051,31 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_touch_obj, 0, 0, tulip_touch);
#ifdef ESP_PLATFORM
extern int16_t touch_x_delta;
extern int16_t touch_y_delta;
extern float touch_y_scale;
#else
int16_t touch_x_delta = 0;
int16_t touch_y_delta = 0;
float touch_y_scale = 1.0;
#endif

STATIC mp_obj_t tulip_touch_delta(size_t n_args, const mp_obj_t *args) {
if(n_args > 0) {
touch_x_delta = mp_obj_get_int(args[0]);
touch_y_delta = mp_obj_get_int(args[1]);
// Allow people to still set x,y delta without scale
if(n_args > 2) touch_y_scale = mp_obj_get_float(args[2]);
} else {
mp_obj_t tuple[2];
mp_obj_t tuple[3];
tuple[0] = mp_obj_new_int(touch_x_delta);
tuple[1] = mp_obj_new_int(touch_y_delta);
return mp_obj_new_tuple(2, tuple);
tuple[2] = mp_obj_new_float(touch_y_scale);
return mp_obj_new_tuple(3, tuple);
}
return mp_const_none;

}

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_touch_delta_obj, 0, 2, tulip_touch_delta);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tulip_touch_delta_obj, 0, 3, tulip_touch_delta);


STATIC mp_obj_t tulip_key_remap(size_t n_args, const mp_obj_t *args) {
Expand Down

0 comments on commit 5bb7fb4

Please sign in to comment.