一、touchGFX工程搭建

1、touchGFX界面搭建

放置一个box作为背景,使用实体按键更改背景颜色。

touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_#include

2、touchGFX interactions添加

红色框内填写的是实体按键返回值哦。

touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_#define_02


touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_学习_03


touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_#include_04

二、代码修改

1、实现按键初始化和扫描函数

按键返回值要和touchgfx designer interactions里面设置的一样哦,
我的工程返回值为:

#define KEY0_PRES   '0'   //KEY0���º󷵻�ֵ
#define KEY1_PRES '1' //KEY1���º󷵻�ֵ
#define KEY2_PRES '2' //KEY2���º󷵻�ֵ
#define WKUP_PRES '3' //WKUP���º󷵻�ֵ

touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_单片机_05


key.c

#include "key.h"
#include "delay.h"

//?????????????
void key_init(void)
{
GPIO_InitTypeDef GPIO_Initure;

__HAL_RCC_GPIOA_CLK_ENABLE(); //????GPIOA???
__HAL_RCC_GPIOC_CLK_ENABLE(); //????GPIOC???
__HAL_RCC_GPIOH_CLK_ENABLE(); //????GPIOH???

GPIO_Initure.Pin=GPIO_PIN_0; //PA0
GPIO_Initure.Mode=GPIO_MODE_INPUT; //????
GPIO_Initure.Pull=GPIO_PULLDOWN; //????
GPIO_Initure.Speed=GPIO_SPEED_FREQ_VERY_HIGH; //????
HAL_GPIO_Init(GPIOA,&GPIO_Initure);

GPIO_Initure.Pin=GPIO_PIN_13; //PC13
GPIO_Initure.Mode=GPIO_MODE_INPUT; //????
GPIO_Initure.Pull=GPIO_PULLUP; //????
GPIO_Initure.Speed=GPIO_SPEED_FREQ_VERY_HIGH; //????
HAL_GPIO_Init(GPIOC,&GPIO_Initure);

GPIO_Initure.Pin=GPIO_PIN_2|GPIO_PIN_3; //PH2,3
HAL_GPIO_Init(GPIOH,&GPIO_Initure);
}

//????????????
//????????
//mode:0,???????????;1,?????????;
//0??????κ????????
//1??WKUP???? WK_UP
//?????????????????,KEY0>KEY1>KEY2>WK_UP!!
u8 key_scan(u8 mode)
{
static u8 key_up=1; //??????????
if(mode==1)key_up=1; //???????
if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
{
key_up=0;
if(KEY0==0) return KEY0_PRES;
else if(KEY1==0) return KEY1_PRES;
else if(KEY2==0) return KEY2_PRES;
else if(WK_UP==1) return WKUP_PRES;
}else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;
return 0; //?????????
}

key.h

#ifndef _KEY_H
#define _KEY_H
#include "sys.h"
#ifdef __cplusplus
extern "C"
{
#endif

#define KEY0 HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_3) //KEY0����PH3
#define KEY1 HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_2) //KEY1����PH2
#define KEY2 HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) //KEY2����PC13
#define WK_UP HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) //WKUP����PA0

#define KEY0_PRES '0' //KEY0���º󷵻�ֵ
#define KEY1_PRES '1' //KEY1���º󷵻�ֵ
#define KEY2_PRES '2' //KEY2���º󷵻�ֵ
#define WKUP_PRES '3' //WKUP���º󷵻�ֵ

void key_init(void); //����IO��ʼ������
u8 key_scan(u8 mode); //����ɨ�躯��

#ifdef __cplusplus
}
#endif
#endif

2、添加一个KeyController类

KeyController类要继承ButtonController类。在ButtonController中有两个纯虚函数,子类必须实现

touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_touchgfx_06


KeyController.hpp

#ifndef _KEYCONTROLLER_T_
#define _KEYCONTROLLER_T_


#include <platform/driver/button/ButtonController.hpp>


namespace touchgfx
{
class KeyController : public ButtonController
{
public:
KeyController();
virtual ~KeyController();

virtual void init();
virtual bool sample(uint8_t& key);

};
}
#endif

KeyController.cpp

#include "KeyController.hpp"
#include "key.h"

using namespace touchgfx;

KeyController::KeyController()
{

}
KeyController::~KeyController()
{

}

void KeyController::init()
{
key_init();
}
bool KeyController::sample(uint8_t& key)
{
uint8_t keyValue = key_scan(0);
if(keyValue !=0)
{
key = keyValue;

return true;
}

return false;
}

3、将新添加的类赋值为系统

打开​​TouchGFXHAL.cpp​​​文件新建一个​​KeyController​​对象

static touchgfx::KeyController keyController;

然后在​​void TouchGFXHAL::initialize()​​里面这个对象赋值给系统,记得调用初始化函数

keyController.init();
setButtonController(&keyController);

touchGFX综合学习七、touchGFX使用实体按键(正点原子H750开发版)_学习_07

#include <TouchGFXHAL.hpp>
#include <KeyController.hpp>

static touchgfx::KeyController keyController;
/* USER CODE BEGIN TouchGFXHAL.cpp */

using namespace touchgfx;

void TouchGFXHAL::initialize()
{
// Calling parent implementation of initialize().
//
// To overwrite the generated implementation, omit call to parent function
// and implemented needed functionality here.
// Please note, HAL::initialize() must be called to initialize the framework.

TouchGFXGeneratedHAL::initialize();

uint32_t frameSize = DISPLAY_HEIGHT*DISPLAY_WIDTH*3;
setFrameBufferStartAddresses((void *)(0xC0000000),(void *)(0xC0000000+frameSize),(void *)(0xC0000000+frameSize*2));
setFrameRateCompensation(true);

keyController.init();
setButtonController(&keyController);
}