STM32 Touchgfx的使用【三】 触摸控制

本文介绍了如何在STM32CUBEMX中配置IIC接口,并结合TouchGFX进行触摸控制。详细讲解了如何在STM32TouchController.cpp中添加代码以实现触摸事件的采集、坐标变换和状态更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STM32CUBEMX配置

打开一个IIC接口配置如下
在这里插入图片描述

TouchGFX设置

找到按钮 添加一个按钮

在这里插入图片描述

仿真效果

在这里插入图片描述
点击屏幕之后会变状态
在这里插入图片描述

代码分析

在 STM32TouchController.cpp 中添加代码

extern "C"
{
	#include "bsp_ft6336.h"
}
extern "C" FT6336_TouchPointType tp;
extern volatile uint32_t ft6336_on_touch_count;

在原代码中添加触摸采集部分

bool STM32TouchController::sampleTouch(int32_t& x, int32_t& y)
{
    /**
     * By default sampleTouch returns false,
     * return true if a touch has been detected, otherwise false.
     *
     * Coordinates are passed to the caller by reference by x and y.
     *
     * This function is called by the TouchGFX framework.
     * By default sampleTouch is called every tick, this can be adjusted by HAL::setTouchSampleRate(int8_t);
     *
     */
	uint16_t xDiff = 0,yDiff = 0;	
		static uint16_t pI_Touch_X = 0, pI_Touch_Y = 0;
		//触摸中断产生
		if (ft6336_on_touch_count) // irq done
		{
			uint8_t id1 = FT6336_read_touch1_id(); // id1 = 0 or 1
			tp.tp[id1].status = (tp.tp[id1].status == release) ? touch : stream;
			tp.tp[id1].x = FT6336_read_touch1_x();
			tp.tp[id1].y = FT6336_read_touch1_y();
			tp.tp[~id1 & 0x01].status = release;
			//更新数据至TouchGFX
			if (tp.tp[0].status == 1)	//最多支持两个触点
			{ 
				xDiff = tp.tp[0].x > pI_Touch_X ? (tp.tp[0].x - pI_Touch_X): (pI_Touch_X - tp.tp[0].x);
				yDiff = tp.tp[0].y > pI_Touch_Y ? (tp.tp[0].y - pI_Touch_Y): (pI_Touch_Y - tp.tp[0].y);
				//判断阈值
				if ((xDiff + yDiff) > 5)
				{
					pI_Touch_X = tp.tp[0].x;
					pI_Touch_Y = tp.tp[0].y;
				}
				//更新触摸,横屏触摸坐标变换
				x = 320 - pI_Touch_Y;
				y = pI_Touch_X;
			}
			
			ft6336_on_touch_count = 0;
			//
			return true;
		}
    return false;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cat_milk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值