【GD32450I-EVAL】移植触摸到LittleVGL
<p>上一篇帖子进行了触摸驱动测试:<a href="https://bbs.eeworld.com.cn/forum.php?mod=viewthread&tid=1143661&page=1&extra=#pid3015018" target="_blank">SPI收发与触摸芯片XPT2046驱动及笔中断</a></p><p>接下来就是移植到 LittleVGL 。</p>
<p>上次移植完 LittleVGL 的显示时留下了一个:</p>
<pre>
<code class="language-cpp">static bool DEMO_ReadTouch(lv_indev_drv_t * indev_drv, lv_indev_data_t *data)</code></pre>
<p>这个函数里面没有进行什么操作,这时候只需要添加触摸位置点进去即可。</p>
<pre>
<code class="language-cpp">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;
}</code></pre>
<p>因为RAM容量的原因,只能支持270*180宽度显示,因此必须换算实际触摸点与 LittleVGL 内的位置,因此程序里加入了:</p>
<pre>
<code> touch_x -= 20;
touch_y -= 30;</code></pre>
<p>其中X显示的时候向左偏移了20像素,Y偏移了30像素,具体可以看<a href="https://bbs.eeworld.com.cn/thread-1140938-1-1.html" target="_blank">显示部分的移植</a>。</p>
<p> </p>
<p>ReadTPXYOver 函数是进行多次读取,经过冒泡排序后取中位数:</p>
<pre>
<code class="language-cpp">
void ReadTPXY(uint16_t *t_x, uint16_t *t_y)
{
char t_char = "";
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)</code></pre>
<p> </p>
<p>效果:</p>
<p></p>
<p><a href="https://bbs.eeworld.com.cn/thread-1140981-1-1.html" target="_blank">兆易GD32450I-EVAL</a></p>
<p>汇总贴:<a href="https://bbs.eeworld.com.cn/thread-1140981-1-1.html">https://bbs.eeworld.com.cn/thread-1140981-1-1.html</a></p>
<p>这个好,给力!</p>
页:
[1]