you don’t stop the polling. you inject a fake touch event into the polling loop using the callback function.
as an example. This is in Python
PORTRAIT = const(-1)
LANDSCAPE = const(-2)
REVERSE_PORTRAIT = const(-3)
REVERSE_LANDSCAPE = const(-4)
height = 320
width = 480
rot = LANDSCAPE
# this is a class instance that stores calibration data for the touch screen
config = None
class ft6x36_touch_input_t:
last_x = -1
last_y = -1
current_state = 0
# function to alter the touch coordinates for rotation and also calibration
def remap(value, old_min, old_max, new_min, new_max):
return int((
((value - old_min) * (new_max - new_min)) / (old_max - old_min)
) + new_min)
data_buf = bytearray(5)
# this would be specific to the touch display you are using.
# it returns x, y coordinates of where the display as been touched.
def _get_coords(self):
try:
i2c.read(FT6X36_TD_STAT_REG, buf=data_buf)
except OSError:
return None
touch_pnt_cnt = data_buf[0]
if touch_pnt_cnt != 1:
return None
x = (
((data_buf[1] & FT6X36_MSB_MASK) << 8) |
(data_buf[2] & FT6X36_LSB_MASK)
)
y = (
((data_buf[3] & FT6X36_MSB_MASK) << 8) |
(data_buf[4] & FT6X36_LSB_MASK)
)
return x, y
touch_injection = False
injected_x = -1
injected_y = -1
def read(drv, data):
global touch_injection
coords = get_coords()
# check to see if the data returned is valid, if it is not then change the touched state to be
# released and pass the last known touch coordinates to LVGL
if not touch_injection and coords is None:
if ft6x36_touch_input_t.current_state != lv.INDEV_STATE.RELEASED:
ft6x36_touch_input_t.current_state = lv.INDEV_STATE.RELEASED
res = True
else:
res = False
data.point.x = ft6x36_touch_input_t.last_x
data.point.y = ft6x36_touch_input_t.last_y
data.state = ft6x36_touch_input_t.current_state
return res
this is where the injected input gets placed into the callback stream so LVGL can process it.
if touch_injection:
xpos = injected_x
ypos = injected_y
touch_injection = False
else:
x, y = coords
# handle display rotation and calibration
if rot == PORTRAIT:
xpos = remap(x, config.left, config.right, 0, width)
ypos = remap(y, config.top, config.bottom, 0, height)
elif rot == LANDSCAPE:
xpos = remap(y, config.top, config.bottom, 0, width)
ypos = remap(x, config.right, config.left, 0, height)
elif rot == REVERSE_PORTRAIT:
xpos = remap(x, config.right, config.left, 0, width)
ypos = remap(y, config.bottom, config.top, 0, height)
elif rot == REVERSE_LANDSCAPE:
xpos = remap(y, config.bottom, config.top, 0, width)
ypos = remap(x, config.left, config.right, 0, height)
else:
raise RuntimeError
# set the coordinates to store for later
ft6x36_touch_input_t.current_state = lv.INDEV_STATE.PRESSED
ft6x36_touch_input_t.last_x = xpos
ft6x36_touch_input_t.last_y = ypos
# set the data to bbe sent to LVGL
data.point.x = ft6x36_touch_input_t.last_x
data.point.y = ft6x36_touch_input_t.last_y
data.state = ft6x36_touch_input_t.current_state
return True
indev_drv = lv.indev_t()
indev_drv.init()
indev_drv.type = lv.INDEV_TYPE.POINTER
indev_drv.read_cb = read
indev_t = indev_drv.register()