Skip to main content
reniervdw1
Associate
October 7, 2019
Solved

How to perform action on REPEAT button release

  • October 7, 2019
  • 15 replies
  • 4636 views

I have a simple repeat button that increments a value as you hold it in. However, I would like to change the repeat button on release so that the next time it is held in, it decrements the value.

I'm new to object orientated programming as well so a complete answer will be highly appreciated. Thanks

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

When the RepeatButton is PRESSED, an internal timer starts. For each tick, it executes some code and in this code a callback is fired (the standard callback inherited from the Button class).

void RepeatButton::handleTickEvent()
{
 Button::handleTickEvent();
 
 if (pressed)
 {
 ...
 }
}

So, basically, you need to do two things:

  • Keep track of a new variable that toggles between modes (increase, decrease)
  • Modify the handleTickEvent method to be able to also decrease a counter and not just increase.

Take a look at RepeatButton.cpp.

/Martin

15 replies

TDK
October 8, 2019

You don't need any object oriented programming to do this. Are you new to all programming? Have you looked at any sample projects? Have you made a project which just blinks an LED? Gotta start somewhere.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
October 8, 2019

When the RepeatButton is PRESSED, an internal timer starts. For each tick, it executes some code and in this code a callback is fired (the standard callback inherited from the Button class).

void RepeatButton::handleTickEvent()
{
 Button::handleTickEvent();
 
 if (pressed)
 {
 ...
 }
}

So, basically, you need to do two things:

  • Keep track of a new variable that toggles between modes (increase, decrease)
  • Modify the handleTickEvent method to be able to also decrease a counter and not just increase.

Take a look at RepeatButton.cpp.

/Martin

reniervdw1
Associate
October 8, 2019

Hi Martin,

Thanks for the reply I appreciate it

Seems like all is working now.

I had to make use of static functions and variables inside the Model. Is that bad practice or can I leave it that way?

Esbenimu
Visitor II
February 21, 2023

Thanks for your quick answer. Yes, I added the RepeatButton.cpp software as follows, but it didn't work at all.

void RepeatButton::handleTickEvent()

{

  Button::handleTickEvent();

  if (!pressed)

  {

   Long_Press_Control = 0; // esbenimu

    return;

  }

  if (ticks == ticksBeforeContinuous)

  {

   Long_Press_Control = BUT_STILL_PUSH; // esbenimu

    executeAction();

    ticks = 0;

    ticksBeforeContinuous = ticksInterval;

  }

  else

  {

    ticks++;

  }

}

I also added a code as below. In this code, only the first button is pressed, it becomes Pressed, as long as the button is pressed, it is not pressed again.

In other words, although the button remains pressed, the led does not stay on, it goes out.

I also checked if it is constantly in this routine, it does.

(Timer_Set_Value ++;

Timer_Set_Value_Change = 1;)

MainScreenView.cpp

void MainScreenView::handleTickEvent()

{

if (ManuelButton.getPressedState())

{

Long_Press_Control = BUT_STILL_PUSH;

HAL_GPIO_WritePin(BUT3_LED_RD_GPIO_Port, BUT3_LED_RD_Pin, GPIO_PIN_SET);

}

else

{

Long_Press_Control = 0;

Timer_Set_Value ++;

Timer_Set_Value_Change = 1;

HAL_GPIO_WritePin(BUT3_LED_RD_GPIO_Port, BUT3_LED_RD_Pin, GPIO_PIN_RESET);

}

}