STM32F407VET6 transplant emwin5.3 (including Touch) (2) - transplant touch

The function that needs to be added for touch is GUI_X_Touch_Analog.c, see the previous section.
The whole idea of ​​touch is,
1. The GUI calls GUI_TOUCH_Exec(); this function at least 10ms once to refresh the keys in real time.
2. GUI_TOUCH_Exec(); Called in
int  GUI_TOUCH_X_MeasureX(void);
int  GUI_TOUCH_X_MeasureY(void);
to read the AD physical coordinates of the touch.
3. In GUI_TOUCH_Exec();, the actual coordinate value is calculated by using the coefficient obtained by the calibration function during initialization.
4. Call the structure GUI_PID_STATE, and store the four information such as the coordinate value in it.
5. Called in the key task
int GUI_TOUCH_GetState(GUI_PID_STATE * pState);
to get the actual logical coordinate information.
So first of all, we need to prepare the touch drive, and the X-axis AD value read out is judged whether it is in the touch area or not. If it is in the area, read the Y-axis value again. Then pass the coordinate AD value to the GUI. The transfer function uses void GUI_TOUCH_StoreState(int x, int y); then GUI_TOUCH_Exec(); this function will take out the x and y values ​​and call a structure in GUI_Type.h. Defined as follows:
typedef struct {
int x,y;
U8 Pressed;
U8 Layer;
} GUI_PID_STATE;
The first two parameters are the coordinate AD value, and the third parameter is whether there is a press. When it is 1, it means that there is a touch press, other will be called, and the actual coordinates will be passed in. The last parameter Layer is the meaning of the layer, which represents which layer of window is currently touched.
for example

TOUCH_X() and TOUCH_Y() are touch hardware drivers. It should be noted that when no touch is detected, x and y should be assigned -1, indicating that no button is pressed.
We need to have a calibration process during initialization, and the function called is
int GUI_TOUCH_Calibrate(int Coord, int Log0, int Log1, int Phys0, int Phys1);
其中Coord为0时代表的是X轴,为1时代表的是Y轴。第二个和第三个代表的是逻辑地址,第四个和第五个代表的是物理地址。在emWin5.3的说明书中用了这个例子:

上面的四个定义常量,就是我们校准时候得到的触摸AD值。可以用常量直接定义,这些常量来自于我们的经验值。也可以做一个校准过程,比如先点左上角再点右下角这些,校准的时候算出来这些值,将这些值保存在EEPROM中,每次开机的时候再调用这些参数校准。有了这些 系数,GUI_TOUCH_Exec();中就可以将物理坐标转换为逻辑坐标并保存在结构体中。
然后我们需要在我们的按键任务中得到坐标结构体并做处理:
while(1) {
GUI_PID_STATE State;
GUI_TOUCH_GetState(&State);
if (State.Pressed == 1) {
//GUI_FillCircle(State.x, State.y, 3);
}
GUI_Delay(10);
}
其中GUI_TOUCH_GetState()得到的就是逻辑坐标信息。如果有按下,判断键值并传递 消息即可。
在GUI_X_Touch_Analog.c中还有两个函数,
void GUI_TOUCH_X_ActivateX(void);
void GUI_TOUCH_X_ActivateY(void);
这两个函数是为了验证x和y是否正确的用的,说明书里面说可以用着两个函数输出两个方波,用示波器观察来验证。我们直接用仿真来验证
附上emWin5.3说明书中关于这一块儿的建议:

这里面有很多优化的地方,比如触摸得到AD值得算法,怎么保证计算出来的坐标是准确的,且不会抖动,是一个很值得探讨的问题。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394864&siteId=291194637