【GD32450I-EVAL】Transplant touch to LittleVGL
[Copy link]
The previous post tested the touch driver: SPI transceiver and touch chip XPT2046 driver and pen interrupt
The next step is to port to LittleVGL.
Last time when I ported LittleVGL's display, I left behind this:
static bool DEMO_ReadTouch(lv_indev_drv_t * indev_drv, lv_indev_data_t *data)
There is no operation in this function. At this time, you just need to add the touch position and click it.
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;
}
Due to RAM capacity, only 270*180 width display can be supported, so the actual touch point and the position in LittleVGL must be converted, so the program adds:
touch_x -= 20;
touch_y -= 30;
When X is displayed, it is offset to the left by 20 pixels, and Y is offset by 30 pixels. For details, see the transplantation of the display part .
The ReadTPXYOver function performs multiple reads and takes the median after bubble sorting:
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)
Effect:
|