How to use Callback in touchgfx
Hello all,
I am trying to configure a click event upon a scrollwheel for selecting an item.
I am using Callback for achieving this.
Inside a handleClickEvent(), I need to give a callback to a CallbackHandler function. This function will eventually call scrollWheel1UpdateItem for performing any actions related to click event.
I am getting confused with the syntax related to declaring a callback and initializing it inside SetupScreen()
Please help me with the correct way of doing this and correct syntax for Callback declaration and usage.
Following is Screen1View.hpp
#ifndef SCREEN1_VIEW_HPP
#define SCREEN1_VIEW_HPP
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
class Screen1View : public Screen1ViewBase
{
public:
Screen1View();
virtual ~Screen1View() {}
virtual void setupScreen();
virtual void tearDownScreen();
void scrollWheel1UpdateItem(CustomContainer1& item, int16_t itemIndex);
void scrollWheel2UpdateItem(CustomContainer2& item, int16_t itemIndex);
// virtual void handleClickEvent(const ClickEvent& evt);
// virtual void handleDragEvent(const DragEvent& drgevt);
void MyCallbackHandler(const ScrollWheel &scroll, const ClickEvent &e);
protected:
// Declaring callback type of box and clickEvent
touchgfx::Callback<Screen1View, const ScrollWheel&, const ClickEvent&> MyCallback;
};
template<class T>
class ClickListener : public T
{
public:
ClickListener() : T(), clickAction(0)
{
T::setTouchable(true);
};
virtual void handleClickEvent(const ClickEvent& event)
{
T::handleClickEvent(event);
if (clickAction && clickAction->isValid())
{
clickAction->execute(*this, event);
}
}
void setClickAction(GenericCallback< const T&, const ClickEvent& >& callback)
{
clickAction = &callback;
}
protected:
touchgfx::GenericCallback<> *clickAction; ///< The callback to be executed when T is clicked
};
#endif // SCREEN1_VIEW_HPPFollowing is Screen1View.cpp file:
#include <gui/screen1_screen/Screen1View.hpp>
#include "string.h"
#include "main.h"
extern UART_HandleTypeDef huart5;
Screen1View::Screen1View()
{
//MyCallback(Screen1View, touchgfx::ScrollWheel, touchgfx::ClickEvent){}
MyCallback(this, &Screen1View::MyCallbackHandler){}
}
void Screen1View::setupScreen()
{
scrollWheel1.setClickAction(MyCallback);
}
void Screen1View::tearDownScreen()
{
}