You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
ndg/lib/lv_drivers/indev/XPT2046.c

175 lines
3.6 KiB
C

/**
* @file XPT2046.c
*
*/
/*********************
* INCLUDES
*********************/
#include "XPT2046.h"
#if USE_XPT2046
#include <stddef.h>
#include LV_DRV_INDEV_INCLUDE
#include LV_DRV_DELAY_INCLUDE
/*********************
* DEFINES
*********************/
#define CMD_X_READ 0b10010000
#define CMD_Y_READ 0b11010000
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void xpt2046_corr(int16_t * x, int16_t * y);
static void xpt2046_avg(int16_t * x, int16_t * y);
/**********************
* STATIC VARIABLES
**********************/
int16_t avg_buf_x[XPT2046_AVG];
int16_t avg_buf_y[XPT2046_AVG];
uint8_t avg_last;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Initialize the XPT2046
*/
void xpt2046_init(void)
{
}
/**
* Get the current position and state of the touchpad
* @param data store the read data here
Squashed 'lib/lv_drivers/' changes from 718302577..0091dc612 0091dc612 fix(wayland): fix compile error wrt LV_WAYLAND_CLIENT_SIDE_DECORATIONS (#307) 451e659cf Add pkgconfig file (#301) 5a5b4a1a3 added x11 display driver port (#300) 4391dbf32 perf(drm): use wait_cb for direct double-buffered rendering (#299) 7e18481ce fix(drm): backport drm driver fixes from lvgl 9 (#298) f5b58b947 feat(evdev): backport multi device support from lvgl 9 (#296) ad66ff155 add return value to drm_init c239cc465 Removed clamp from Calibrate section as it has map in it e44e7f043 fix(build): Update xpt2046_read to match signature required by lv_indev_drv_t's read callback. (#290) 7b9dee11c Fix crash during startup in certain conditions (#286) 9d153fb65 Add USE_WAYLAND to smm.c (#285) 8669c6fc8 libInput: Add POINTER_MOTION_ABSOLUTE support (#284) c71e5f84b drm: Default to XRGB8888 framebuffer (#282) 57494ff8e Add a way to set SDL window title from lv_drv_conf.h (#277) b03c9dac9 fix(xkb): Fix memory leak by avoiding double reference (#276) ec0ad8296 feature(libinput): Expose function for querying capability (#275) 0d5de84e2 feature(libinput): Add function to reset driver states (#274) 94dc4ce06 Use win32 singly linked list instead of LVGL linked list for fixing memory access conflict issues for win32drv. (#273) 1792ab20a feature(wayland): add a shared memory manager, allowing backing buffers to be allocated on demand. (#270) f7935569f use SDL_RES instead of LV_RES for touch events (#268) ba9c3cc4f Update README.md (#261) f261225d9 follow lvgl changes ab5e30ccb Add the multiple display support for win32drv. (#259) 820341ea1 Improve the keyboard support for win32drv. (#258) b13361a1f fix: The mouse moves outside the screen area (#251) 829e0ffc3 chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) 5523f9974 fix(wayland-protocols): minimum version (#246) fe9de86e9 Wayland api fixes (#243) 59e698ce1 fix(wayland) : fix possible memory leak (#238) 2ed01feab fix(drm): Fix compiler warnings (#237) cf4e6d75a make windows.h lower case (#236) dc0d71a3a chore(sdl): fix warning (#232) c68b59e42 fix(fbdev): Fix rendering for 24 bits per pixel (#231) 4f98fddd2 hide the SDL2 include from public LVGL functions (#227) ff01834db fix(fbdev): Gracefully handle FBIOBLANK errors (#229) 73219786a bump version number to v9.0.0-dev git-subtree-dir: lib/lv_drivers git-subtree-split: 0091dc612facc94dce1061a9b78d641c77f1791a
9 months ago
* @return void
*/
Squashed 'lib/lv_drivers/' changes from 718302577..0091dc612 0091dc612 fix(wayland): fix compile error wrt LV_WAYLAND_CLIENT_SIDE_DECORATIONS (#307) 451e659cf Add pkgconfig file (#301) 5a5b4a1a3 added x11 display driver port (#300) 4391dbf32 perf(drm): use wait_cb for direct double-buffered rendering (#299) 7e18481ce fix(drm): backport drm driver fixes from lvgl 9 (#298) f5b58b947 feat(evdev): backport multi device support from lvgl 9 (#296) ad66ff155 add return value to drm_init c239cc465 Removed clamp from Calibrate section as it has map in it e44e7f043 fix(build): Update xpt2046_read to match signature required by lv_indev_drv_t's read callback. (#290) 7b9dee11c Fix crash during startup in certain conditions (#286) 9d153fb65 Add USE_WAYLAND to smm.c (#285) 8669c6fc8 libInput: Add POINTER_MOTION_ABSOLUTE support (#284) c71e5f84b drm: Default to XRGB8888 framebuffer (#282) 57494ff8e Add a way to set SDL window title from lv_drv_conf.h (#277) b03c9dac9 fix(xkb): Fix memory leak by avoiding double reference (#276) ec0ad8296 feature(libinput): Expose function for querying capability (#275) 0d5de84e2 feature(libinput): Add function to reset driver states (#274) 94dc4ce06 Use win32 singly linked list instead of LVGL linked list for fixing memory access conflict issues for win32drv. (#273) 1792ab20a feature(wayland): add a shared memory manager, allowing backing buffers to be allocated on demand. (#270) f7935569f use SDL_RES instead of LV_RES for touch events (#268) ba9c3cc4f Update README.md (#261) f261225d9 follow lvgl changes ab5e30ccb Add the multiple display support for win32drv. (#259) 820341ea1 Improve the keyboard support for win32drv. (#258) b13361a1f fix: The mouse moves outside the screen area (#251) 829e0ffc3 chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) 5523f9974 fix(wayland-protocols): minimum version (#246) fe9de86e9 Wayland api fixes (#243) 59e698ce1 fix(wayland) : fix possible memory leak (#238) 2ed01feab fix(drm): Fix compiler warnings (#237) cf4e6d75a make windows.h lower case (#236) dc0d71a3a chore(sdl): fix warning (#232) c68b59e42 fix(fbdev): Fix rendering for 24 bits per pixel (#231) 4f98fddd2 hide the SDL2 include from public LVGL functions (#227) ff01834db fix(fbdev): Gracefully handle FBIOBLANK errors (#229) 73219786a bump version number to v9.0.0-dev git-subtree-dir: lib/lv_drivers git-subtree-split: 0091dc612facc94dce1061a9b78d641c77f1791a
9 months ago
void xpt2046_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
static int16_t last_x = 0;
static int16_t last_y = 0;
uint8_t buf;
int16_t x = 0;
int16_t y = 0;
uint8_t irq = LV_DRV_INDEV_IRQ_READ;
if(irq == 0) {
LV_DRV_INDEV_SPI_CS(0);
LV_DRV_INDEV_SPI_XCHG_BYTE(CMD_X_READ); /*Start x read*/
buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0); /*Read x MSB*/
x = buf << 8;
buf = LV_DRV_INDEV_SPI_XCHG_BYTE(CMD_Y_READ); /*Until x LSB converted y command can be sent*/
x += buf;
buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0); /*Read y MSB*/
y = buf << 8;
buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0); /*Read y LSB*/
y += buf;
/*Normalize Data*/
x = x >> 3;
y = y >> 3;
xpt2046_corr(&x, &y);
xpt2046_avg(&x, &y);
last_x = x;
last_y = y;
data->state = LV_INDEV_STATE_PR;
LV_DRV_INDEV_SPI_CS(1);
} else {
x = last_x;
y = last_y;
avg_last = 0;
data->state = LV_INDEV_STATE_REL;
}
data->point.x = x;
data->point.y = y;
Squashed 'lib/lv_drivers/' changes from 718302577..0091dc612 0091dc612 fix(wayland): fix compile error wrt LV_WAYLAND_CLIENT_SIDE_DECORATIONS (#307) 451e659cf Add pkgconfig file (#301) 5a5b4a1a3 added x11 display driver port (#300) 4391dbf32 perf(drm): use wait_cb for direct double-buffered rendering (#299) 7e18481ce fix(drm): backport drm driver fixes from lvgl 9 (#298) f5b58b947 feat(evdev): backport multi device support from lvgl 9 (#296) ad66ff155 add return value to drm_init c239cc465 Removed clamp from Calibrate section as it has map in it e44e7f043 fix(build): Update xpt2046_read to match signature required by lv_indev_drv_t's read callback. (#290) 7b9dee11c Fix crash during startup in certain conditions (#286) 9d153fb65 Add USE_WAYLAND to smm.c (#285) 8669c6fc8 libInput: Add POINTER_MOTION_ABSOLUTE support (#284) c71e5f84b drm: Default to XRGB8888 framebuffer (#282) 57494ff8e Add a way to set SDL window title from lv_drv_conf.h (#277) b03c9dac9 fix(xkb): Fix memory leak by avoiding double reference (#276) ec0ad8296 feature(libinput): Expose function for querying capability (#275) 0d5de84e2 feature(libinput): Add function to reset driver states (#274) 94dc4ce06 Use win32 singly linked list instead of LVGL linked list for fixing memory access conflict issues for win32drv. (#273) 1792ab20a feature(wayland): add a shared memory manager, allowing backing buffers to be allocated on demand. (#270) f7935569f use SDL_RES instead of LV_RES for touch events (#268) ba9c3cc4f Update README.md (#261) f261225d9 follow lvgl changes ab5e30ccb Add the multiple display support for win32drv. (#259) 820341ea1 Improve the keyboard support for win32drv. (#258) b13361a1f fix: The mouse moves outside the screen area (#251) 829e0ffc3 chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) 5523f9974 fix(wayland-protocols): minimum version (#246) fe9de86e9 Wayland api fixes (#243) 59e698ce1 fix(wayland) : fix possible memory leak (#238) 2ed01feab fix(drm): Fix compiler warnings (#237) cf4e6d75a make windows.h lower case (#236) dc0d71a3a chore(sdl): fix warning (#232) c68b59e42 fix(fbdev): Fix rendering for 24 bits per pixel (#231) 4f98fddd2 hide the SDL2 include from public LVGL functions (#227) ff01834db fix(fbdev): Gracefully handle FBIOBLANK errors (#229) 73219786a bump version number to v9.0.0-dev git-subtree-dir: lib/lv_drivers git-subtree-split: 0091dc612facc94dce1061a9b78d641c77f1791a
9 months ago
data->continue_reading = false; /* No more data to be read */
}
/**********************
* STATIC FUNCTIONS
**********************/
static void xpt2046_corr(int16_t * x, int16_t * y)
{
#if XPT2046_XY_SWAP != 0
int16_t swap_tmp;
swap_tmp = *x;
*x = *y;
*y = swap_tmp;
#endif
if((*x) > XPT2046_X_MIN)(*x) -= XPT2046_X_MIN;
else(*x) = 0;
if((*y) > XPT2046_Y_MIN)(*y) -= XPT2046_Y_MIN;
else(*y) = 0;
(*x) = (uint32_t)((uint32_t)(*x) * XPT2046_HOR_RES) /
(XPT2046_X_MAX - XPT2046_X_MIN);
(*y) = (uint32_t)((uint32_t)(*y) * XPT2046_VER_RES) /
(XPT2046_Y_MAX - XPT2046_Y_MIN);
#if XPT2046_X_INV != 0
(*x) = XPT2046_HOR_RES - (*x);
#endif
#if XPT2046_Y_INV != 0
(*y) = XPT2046_VER_RES - (*y);
#endif
}
static void xpt2046_avg(int16_t * x, int16_t * y)
{
/*Shift out the oldest data*/
uint8_t i;
for(i = XPT2046_AVG - 1; i > 0 ; i--) {
avg_buf_x[i] = avg_buf_x[i - 1];
avg_buf_y[i] = avg_buf_y[i - 1];
}
/*Insert the new point*/
avg_buf_x[0] = *x;
avg_buf_y[0] = *y;
if(avg_last < XPT2046_AVG) avg_last++;
/*Sum the x and y coordinates*/
int32_t x_sum = 0;
int32_t y_sum = 0;
for(i = 0; i < avg_last ; i++) {
x_sum += avg_buf_x[i];
y_sum += avg_buf_y[i];
}
/*Normalize the sums*/
(*x) = (int32_t)x_sum / avg_last;
(*y) = (int32_t)y_sum / avg_last;
}
#endif