cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Callback in touchgfx

kvkhekale
Associate II

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_HPP

Following 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()
{
 
}

3 REPLIES 3
Martin KJELDSEN
Chief III

Hi @kvkhekale​,

Are you still having issues with this? The documentation for callbacks is not available at the moment. Let me try to shed a little light on the subject. Generally you deal with the following things:

  1. Declaring a callback which may be a Callback (requires a class-type as parameter) or GenericCallback (used when class-type cannot be determined).
  2. Defining a handler for that callback
  3. Configuring the callback to call into the handler on callback->execute().
/**
 * @class GenericCallback Callback.hpp touchgfx/Callback.hpp
 *
 * @brief GenericCallback is the base class for callbacks.
 *
 *        GenericCallback is the base class for callbacks. @see Callback for an explanation of
 *        callbacks.
 *
 *        The reason this base class exists, is that a normal Callback requires the class type
 *        where the callback function resides to be known. This is problematic for ie.
 *        framework widgets like AbstractButton, on which it should be possible to register a
 *        callback on object types that are user-specific and thus unknown to AbstractButton.
 *        This is solved by having AbstractButton contain a pointer to a GenericCallback
 *        instead. This pointer must then be initialized to point on an instance of Callback,
 *        created by the user, which is initialized with the appropriate object type.
 *
 * @note As with Callback, this class exists in four versions to support callback functions
 *       taking zero, one, two or three arguments.
 *
 * @tparam T1 The type of the first argument in the member function, or void if none.
 * @tparam T2 The type of the second argument in the member function, or void if none.
 * @tparam T3 The type of the third argument in the member function, or void if none.
 */

For some widgets in TouchGFX like Button (AbstractButton), this class defines a method that allows users to specify the actual callback (and thus also handler) to call back into.

You can check out AbstractButton.hpp/cpp (included in the distro) to see how it handles callbacks both setting and executing.

As you've done in MyCallback you've declared it using the View class type (like an anchor or origin) and then specified your handler without that class type, this is correct. Also, when calling ->execute() on that callback somewhere in your application you would not supply the class type. So, the parameters to the callback handler and the ->execute() method will be the same.

Does that make sense?

/Martin

kvkhekale
Associate II

Yeah.. Let me just make a separate project for applying Callback on my own.

Will surely get back to you in a while.

Great. Let me know! 🙂