【GD32450I-EVAL】移植触摸到LittleVGL
[复制链接]
上一篇帖子进行了触摸驱动测试:SPI收发与触摸芯片XPT2046驱动及笔中断
接下来就是移植到 LittleVGL 。
上次移植完 LittleVGL 的显示时留下了一个:
static bool DEMO_ReadTouch(lv_indev_drv_t * indev_drv, lv_indev_data_t *data)
这个函数里面没有进行什么操作,这时候只需要添加触摸位置点进去即可。
static bool DEMO_ReadTouch(lv_indev_drv_t * indev_drv, lv_indev_data_t *data)
{
static int touch_x = 0;
static int touch_y = 0;
uint16_t t_xx = 0, t_yy = 0;
data->state = LV_INDEV_STATE_REL;
if(gpio_input_bit_get(GPIOI, GPIO_PIN_3)==RESET)
{
ReadTPXYOver(&t_xx, &t_yy);
t_yy = 4096-t_yy;
touch_x = 480 * t_xx / 4096;
touch_y = 272 * t_yy / 4096;
touch_x -= 20;
touch_y -= 30;
if (touch_x<0 || touch_x > LCD_WIDTH){
touch_x =0;
touch_y =0;
}
else if (touch_y<0 || touch_y > LCD_HEIGHT){
touch_x =0;
touch_y =0;
}
else
data->state = LV_INDEV_STATE_PR;
}
/*Set the last pressed coordinates*/
data->point.x = touch_x;
data->point.y = touch_y;
/*Return `false` because we are not buffering and no more data to read*/
return false;
}
因为RAM容量的原因,只能支持270*180宽度显示,因此必须换算实际触摸点与 LittleVGL 内的位置,因此程序里加入了:
touch_x -= 20;
touch_y -= 30;
其中X显示的时候向左偏移了20像素,Y偏移了30像素,具体可以看显示部分的移植。
ReadTPXYOver 函数是进行多次读取,经过冒泡排序后取中位数:
void ReadTPXY(uint16_t *t_x, uint16_t *t_y)
{
char t_char[30] = "";
gpio_bit_reset(GPIOF, GPIO_PIN_6);
//X
while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI4, 0xD0);
while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
spi_i2s_data_receive(SPI4);
// delay_us(100);
//read 1
while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI4, 0x00);
while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
*t_x = spi_i2s_data_receive(SPI4);
*t_x = (*t_x& 0x7F)<<8 ;
//read2
while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI4, 0x90);
while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
*t_x = *t_x|spi_i2s_data_receive(SPI4);
// sprintf(t_char, "x:%x\t", *t_x);
// PRINTF_UART0(t_char);
*t_x = *t_x>>3;
// delay_us(100);
while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI4, 0x00);
while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
*t_y = spi_i2s_data_receive(SPI4);
*t_y = (*t_y& 0x7F) <<8;
while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI4, 0x00);
while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
*t_y = *t_y|spi_i2s_data_receive(SPI4);
// sprintf(t_char, "y:%x\t", *t_y);
// PRINTF_UART0(t_char);
*t_y = *t_y>>3;
}
void ReadTPXYOver(uint16_t *t_x, uint16_t *t_y)
效果:
|