void lv_port_indev_init(void)
{
/**
* Here you will find example implementation of input devices supported by LittelvGL:
* - Touchpad
* - Mouse (with cursor support)
* - Keypad (supports GUI usage only with key)
* - Encoder (supports GUI usage only with: left, right, push)
* - Button (external buttons to press points on the screen)
*
* The `..._read()` function are only examples.
* You should shape them according to your hardware
*/
static lv_indev_drv_t indev_drv;
/*------------------
* Touchpad
* -----------------*/
/*Initialize your touchpad if you have*/
touchpad_init();
/*Register a touchpad input device 注册触摸输入设备*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = xpt2046_read; //使用自定义的读取坐标函数xpt2046_read,没有使用touchpad_read
indev_touchpad = lv_indev_drv_register(&indev_drv);
}
touch input
Touch function initialization
/*Initialize your touchpad*/
static void touchpad_init(void)
{
/*Your code comes here*/
}
Timing scan, read touch coordinates
/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
static lv_coord_t last_x = 0;
static lv_coord_t last_y = 0;
/*Save the pressed coordinates and the state*/
if(touchpad_is_pressed()) {
touchpad_get_xy(&last_x, &last_y);
data->state = LV_INDEV_STATE_PR;
} else {
data->state = LV_INDEV_STATE_REL;
}
/*Set the last pressed coordinates*/
data->point.x = last_x;
data->point.y = last_y;
}
If the touch is pressed, it returns true, otherwise it returns false
/*Return true is the touchpad is pressed*/
static bool touchpad_is_pressed(void)
{
/*Your code comes here*/
return false;
}
Get the xy coordinates of the touch position
/*Get the x and y coordinates if the touchpad is pressed*/
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
/*Your code comes here*/
(*x) = 0;
(*y) = 0;
}
Touch chip driver
Custom function to read touch coordinates
void xpt2046_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
uint8_t ret = 0;
static int16_t last_x = 0;
static int16_t last_y = 0;
_touch_xy strScreenSample;
if(XPT2046_TouchDetect() == TOUCH_DET_PRESSED)
{
XPT2046_CS_ENABLE();
ret = XPT2046_Get_TouchedPoint(&strScreenSample, touch_para);
XPT2046_CS_DISABLE();
if(ret==1)
{
last_x = strScreenSample.x;
last_y = strScreenSample.y;
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_PR;
}
}
else
{
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_REL;
}
}
- Detect if touch is pressed
/* 触摸屏状态:释放、等待释放、按下 */
typedef enum
{
TOUCH_RELEASE = 0,
TOUCH_WAITING = 1,
TOUCH_PRESSED = 2,
}_touch_state;
Use the state machine to detect the touch state and perform debounce processing
/**
* @brief 触摸屏检测状态机
* @retval 触摸状态
* 该返回值为以下值之一:
* @arg TOUCH_PRESSED :触摸按下
* @arg TOUCH_DET_NOT_PRESSED :无触摸
*/
uint8_t XPT2046_TouchDetect(void)
{
static _touch_state touch_state = TOUCH_RELEASE;
static uint32_t i;
uint8_t detectResult = TOUCH_DET_NOT_PRESSED;
switch(touch_state)
{
case TOUCH_RELEASE:
if(XPT2046_PENIRQ_Read() == XPT2046_PENIRQ_ActiveLevel) //第一次出现触摸信号
{
touch_state = TOUCH_WAITING;
detectResult =TOUCH_DET_NOT_PRESSED;
}
else //无触摸
{
touch_state = TOUCH_RELEASE;
detectResult =TOUCH_DET_NOT_PRESSED;
}
break;
case TOUCH_WAITING:
if(XPT2046_PENIRQ_Read() == XPT2046_PENIRQ_ActiveLevel)
{
i++;
//等待时间大于阈值则认为触摸被按下
//消抖时间 = DURIATION_TIME * 本函数被调用的时间间隔
//如在定时器中调用,每10ms调用一次,则消抖时间为:DURIATION_TIME*10ms
if(i > DURIATION_TIME)
{
i=0;
touch_state = TOUCH_PRESSED;
detectResult = TOUCH_DET_PRESSED;
}
else //等待时间累加
{
touch_state = TOUCH_WAITING;
detectResult = TOUCH_DET_NOT_PRESSED;
}
}
else //等待时间值未达到阈值就为无效电平,当成抖动处理
{
i = 0;
touch_state = TOUCH_RELEASE;
detectResult = TOUCH_DET_NOT_PRESSED;
}
break;
case TOUCH_PRESSED:
if(XPT2046_PENIRQ_Read() == XPT2046_PENIRQ_ActiveLevel) //触摸持续按下
{
touch_state = TOUCH_PRESSED;
detectResult = TOUCH_DET_PRESSED;
}
else //触摸释放
{
touch_state = TOUCH_RELEASE;
detectResult = TOUCH_DET_NOT_PRESSED;
}
break;
default:
touch_state = TOUCH_RELEASE;
detectResult = TOUCH_DET_NOT_PRESSED;
break;
}
return detectResult;
}
/**
* @brief 获取 XPT2046 触摸点(校准后)的坐标
* @param pDisplayCoordinate :该指针存放获取到的触摸点坐标
* @param pTouchPara:坐标校准系数
* @retval 获取情况
* 该返回值为以下值之一:
* @arg 1 :获取成功
* @arg 0 :获取失败
*/
uint8_t XPT2046_Get_TouchedPoint(_touch_xy *pDisplayCoordinate, _touch_para *pTouchPara )
{
_touch_xy strScreenCoordinate;
if(XPT2046_ReadAdc_Smooth_XY_qucik(&strScreenCoordinate))
{
pDisplayCoordinate->x = ( (pTouchPara[lcddev.scan_mode].dX_X * strScreenCoordinate.x) + (pTouchPara[lcddev.scan_mode].dX_Y * strScreenCoordinate.y) + pTouchPara[lcddev.scan_mode].dX );
pDisplayCoordinate->y = ( (pTouchPara[lcddev.scan_mode].dY_X * strScreenCoordinate.x) + (pTouchPara[lcddev.scan_mode].dY_Y * strScreenCoordinate.y) + pTouchPara[lcddev.scan_mode].dY );
return 1;
}
return 0; //如果获取的触点信息有误,则返回0
}
mouse input
/*Initialize your mouse*/
static void mouse_init(void)
{
/*Your code comes here*/
}
keyboard input
/*Initialize your keypad*/
static void keypad_init(void)
{
/*Your code comes here*/
}
Encoder input
/*Initialize your keypad*/
static void encoder_init(void)
{
/*Your code comes here*/
}
key input
/*Initialize your buttons*/
static void button_init(void)
{
/*Your code comes here*/
}