Контроллер кнопок можно использовать для добавления поддержки простых аппаратных кнопок в приложения TouchGFX.
Когда TouchGFX настроен на использование контроллера кнопок, TouchGFX вызовет метод:
virtual void handleKeyEvent (ключ uint8_t);
на текущем просмотре при нажатии физической кнопки.
Не забудьте реализовать этот метод в ваших классах View или создать Interaction в Designer.
Чтобы использовать физические кнопки, для целевого оборудования должен быть реализован подкласс интерфейса touchgfx :: ButtonController.
Интерфейс ButtonController.hpp
находится в touchgfx / платформа / драйвер / кнопка /
#ifndef BUTTONCONTROLLER_HPP
#define BUTTONCONTROLLER_HPP
#include <touchgfx/hal/Types.hpp>
namespace touchgfx
{
class ButtonController
{
public:
virtual ~ButtonController() {}
virtual void init() = 0;
virtual bool sample(uint8_t& key) = 0;
virtual void reset() {}
};
} // namespace touchgfx
#endif /* BUTTONCONTROLLER_HPP */
Функция touchgfx::ButtonController::sample()
вызывается в каждом тике. Он должен возвращать true, если нажата клавиша, и false в противном случае. Численное значение ключа , которая была нажата, возвращается в беззнаковом 8-битном, key
.
Ниже приведена шаблонная реализация интерфейса:
KeySampler.hpp:
класс KeySampler: public touchgfx :: ButtonController
{
public:
KeySampler () {
init ();
}
virtual ~ KeySampler () {}
virtual void init ();
пример виртуального bool (uint8_t & key);
}; KeySampler.cpp:
#include "KeySampler.hpp"
#include "touchgfx/HAL/Buttons.hpp"
using namespace touchgfx;
void KeySampler::init()
{
//Initialise gpio for buttons here
}
bool KeySampler::sample(uint8_t& key)
{
unsigned int buttons = ...; //sample GPIO
//Return true if button 1 was pressed, save 1 to key argument
//Return true if button 2 was pressed, save 2 to key argument
...
return false;
}
Необходимо настроить touchgfx::HAL
для использования этой кнопки контроллера:
...
//Keysampler for buttons
KeySampler btnCtrl;
namespace touchgfx
{
void touchgfx_init()
{
//Instantiate HAL
HAL& hal = touchgfx_generic_init<STM32F7HAL>(dma, display, tc, 480, 272, 0, 0);
hal.setFrameBufferStartAddress((uint16_t*)SDRAM_BASE);
hal.setButtonController(&btnCtrl); //Enable button controller
...
Interfacing with physical buttons
A Button Controller can be used to add simple hardware button support to TouchGFX applications.
When TouchGFX is configured to use a Button Controller, TouchGFX will call the method:
virtual void handleKeyEvent(uint8_t key);
on the current View when a physical button is pressed.
Remember to implement this method in your View classes or create an Interaction in the Designer.
To use physical buttons, a subclass of the touchgfx::ButtonController interface must be implemented for the target hardware.
The interface ButtonController.hpp
is found in touchgfx/platform/driver/button/
#ifndef BUTTONCONTROLLER_HPP
#define BUTTONCONTROLLER_HPP
#include <touchgfx/hal/Types.hpp>
namespace touchgfx
{
class ButtonController
{
public:
virtual ~ButtonController() {}
virtual void init() = 0;
virtual bool sample(uint8_t& key) = 0;
virtual void reset() {}
};
} // namespace touchgfx
#endif /* BUTTONCONTROLLER_HPP */
The touchgfx::ButtonController::sample()
funtion is called in every tick. It must return true if a key is pressed, false otherwise. The numerical value for the key that was pressed is returned in the unsigned 8-bit reference, key
.
Below is a template implementation of the interface:
KeySampler.hpp:
class KeySampler : public touchgfx::ButtonController
{
public:
KeySampler() {
init();
}
virtual ~KeySampler() {}
virtual void init();
virtual bool sample(uint8_t& key);
};
KeySampler.cpp:
#include "KeySampler.hpp"
#include "touchgfx/HAL/Buttons.hpp"
using namespace touchgfx;
void KeySampler::init()
{
//Initialise gpio for buttons here
}
bool KeySampler::sample(uint8_t& key)
{
unsigned int buttons = ...; //sample GPIO
//Return true if button 1 was pressed, save 1 to key argument
//Return true if button 2 was pressed, save 2 to key argument
...
return false;
}
It is necessary to configure touchgfx::HAL
to use this button controller:
...
//Keysampler for buttons
KeySampler btnCtrl;
namespace touchgfx
{
void touchgfx_init()
{
//Instantiate HAL
HAL& hal = touchgfx_generic_init<STM32F7HAL>(dma, display, tc, 480, 272, 0, 0);
hal.setFrameBufferStartAddress((uint16_t*)SDRAM_BASE);
hal.setButtonController(&btnCtrl); //Enable button controller
...
Добавить комментарий