Skip to main content
Roger3
Associate
April 1, 2020
Solved

External events as triggers

  • April 1, 2020
  • 4 replies
  • 4575 views

Hello dear

It would be very nice to give us a simple example to explain all your codes to order to change a widget on a screen.

You tellus to use "TouchGFX HAL Development" in your example gpio https://support.touchgfx.com/docs/development/board-bring-up/example-gpio/ but where is it possible to put it?

Thank you for you aid.

Roger

This topic has been closed for replies.
Best answer by Martin KJELDSEN

Hi Roger,

Let me just paste some of the code here from that article:

#include <platform/driver/button/ButtonController.hpp>
class H7B3ButtonController : public touchgfx::ButtonController
{
 virtual void init() { }
 virtual bool sample(uint8_t& key)
 {
 
 if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) != GPIO_PIN_RESET)
 {
 key = 1;
 return true;
 }
 return false;
 }
private:
 
};
 
...
H7B3ButtonController bc;
void TouchGFXHAL::initialize()
{
 TouchGFXGeneratedHAL::initialize();
 hal.setButtonController(&bc);
}

In this code you're defining a class that inherits from ButtonController and implementing the sample() function so that it reads a GPIO port where a button is connected. You can define this class anywhere you want in your program structure. Add the line hal.setButtonController(&bc) inside the initialize() function of your TouchGFXHAL.cpp.

When you configure your HAL via setButtonController() function to use the ButtonController you just defined to distribute the value returned from sample() to the active application. And by telling .touchgfx file about it you can now use that value as a tigger in an interaction

/Martin

4 replies

Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
April 1, 2020

Hi Roger,

Let me just paste some of the code here from that article:

#include <platform/driver/button/ButtonController.hpp>
class H7B3ButtonController : public touchgfx::ButtonController
{
 virtual void init() { }
 virtual bool sample(uint8_t& key)
 {
 
 if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) != GPIO_PIN_RESET)
 {
 key = 1;
 return true;
 }
 return false;
 }
private:
 
};
 
...
H7B3ButtonController bc;
void TouchGFXHAL::initialize()
{
 TouchGFXGeneratedHAL::initialize();
 hal.setButtonController(&bc);
}

In this code you're defining a class that inherits from ButtonController and implementing the sample() function so that it reads a GPIO port where a button is connected. You can define this class anywhere you want in your program structure. Add the line hal.setButtonController(&bc) inside the initialize() function of your TouchGFXHAL.cpp.

When you configure your HAL via setButtonController() function to use the ButtonController you just defined to distribute the value returned from sample() to the active application. And by telling .touchgfx file about it you can now use that value as a tigger in an interaction

/Martin

Roger3
Roger3Author
Associate
April 1, 2020

hello dear Martin

Sorry but a lot of problems

YAdan
Associate
April 5, 2020

Hello everyone,

It doesnt seem to be a popluar subject for the tutors although very important.

I am doing something similar but still learning how to do it. So far the closest example I found is the following https://www.youtube.com/watch?v=jQO7zhX0e0Q&t=2819s

On thouchgfx I have created an image with 2 possible led images gray and red.

 0693W000000VKVGQA4.png

On the viewer code I have added the followinng where animation1 is the name of the top led image.

void Screen1View::advanceAnimation()

{

uint8_t msg = 0;

if (xQueueReceive(gui_msg_q, &msg, 0) == pdTRUE)

{

animation1.setBitmap(Bitmap(BITMAP_BUTTON_BLANK_GRAY_ICON_ID));

animation1.invalidate();

}

else

{

animation1.setBitmap(Bitmap(BITMAP_BUTTON_BLANK_RED_ICON_ID));

animation1.invalidate();

}

}

When I press the button the virtual led turns from grey to red. But doesnt go back to gray once the button is released.

While I am trying to solve it any help is welcome

Martin KJELDSEN
Principal III
April 6, 2020

Did you try debugging? Are you even hitting both parts if your if statement? Debug further down into the task that samples the button GPIO and sends the message. Also, it seems like you're missing something. You need to send a signal from your task on both PRESSED and RELEASED and in your view you're just checking if there's something in the queue.

Also, try not to have target specific code directly in your view to make it more portable (use a signal on your modelListener, e.g modelListener->buttonPressed() which you respond to in your presenter - Then you can abstract this functionality to work in a simulator as well)

/Martin

YAdan
Associate
April 20, 2020

Hi Matin,

Thanks for the advice. Eventually I learned how to use the debugger.

The Hardware integration on STM32F769 with TouchGFX – Webinar code helped eventually finding the problem. I have found the solution on their LED part. I am not very familiar with the queue concept, so it took me a while to realise I was not getting the message from the queue as you suggested.

I have used the following code as a task in the main:

gui_msg_q = xQueueGenericCreate(1, 1, 0);

if(HAL_GPIO_ReadPin(User_Button_GPIO_Port, User_Button_Pin) == GPIO_PIN_SET )

      {

             // Send empty message. Queue item implicitly means PRESSED.

             msg = 1;

             printf("btn on\n");

      }

      else

      {             msg = 0;

             printf("btn off\n");

      }

      if (gui_msg_q)

                          {

                                 xQueueSend(gui_msg_q, &msg, 0);

                          }

On the view I received the queue message with the following code :

      if ( xQueueReceive(gui_msg_q, &msg, 0) == pdTRUE )

      {

                          switch(msg)

                          {

                                 case 0:

                                       animation1.setBitmap(Bitmap(BITMAP_BUTTON_BLANK_GRAY_ICON_ID));

                                        animation1.invalidate();

                                        break;

                                 case 1:

                                       animation1.setBitmap(Bitmap(BITMAP_BUTTON_BLANK_RED_ICON_ID));

                                        animation1.invalidate();

                                        vQueueDelete(gui_msg_q);

                                        break;

                                 default:

                                        break;

                          }

                    }

I am sure for experts like yourself it can be done in a better way but at least I found a starting point to for learning.

I am not sure I understand your comment about the portability.

In the Model I put :

void Model::btnPressed()

{

   modelListener->btnPressed();

}

And the presenter has :

void Screen1Presenter::btnPressed()

{

      view.advanceAnimation();

}

and the first sample above in the viewer is in the following function void Screen1View::advanceAnimation()

Any comment for improvement is more than welcome.

ChintanParmar
Associate II
April 13, 2020

Hey @Martin KJELDSEN​ ,

I am very new to C++.

I am trying to use push button of STM32F429 Discovery kit to change screen. I have configured correctly with given article. Defined ButtonController class properly.

I just want to know how below line will work.

if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) != GPIO_PIN_RESET)

As HAL_GPIO_ReadPin() function is defined in stm32f4xx_hal_gpio.c

and #defines are also defined in perticular .h files.

So how can I include that in MyButtonController.cpp file?

After compiling it is showing an error,

'GPIOC' was not declared in this scope likewise same error for other defines and functions.

Can you help me please to figure out this issue?

Martin KJELDSEN
Principal III
April 14, 2020

Did you take that code directly from the article? GPIO Port C Pin 13 is for the STM32H7B-DISCO _specifically_, so it won't work for any other board unless they have the same button connceted to the same GPIO.

You need to get the schematics for the 429-disco kit and check which GPIO the button is connected to.

/Martin

ChintanParmar
Associate II
April 14, 2020

Thanks for your quick reply Martin.

As you have said, I have checked properly in the 429-disco kit schematics for GPIO button and correctly configured through CubeMX.

I have a different issue with #defines used in .h header files. I didn't get that thing.

Senior
December 22, 2023

Hi, there's a video about this subject on youtube(there's part 1 & 2). Link part 1 :  https://www.youtube.com/watch?v=ufvJ5bcesL8

Link part 2: https://www.youtube.com/watch?v=QgEDSjvGAlk

The website of TouchGFX also adresses this subject, but the video is better. Link TouchGFX : https://support.touchgfx.com/4.21/docs/development/scenarios/example-gpio