Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to run the button.loop function in the background using an interrupt timer ? #43

Closed
renejeanmercier opened this issue Oct 6, 2022 · 11 comments
Labels

Comments

@renejeanmercier
Copy link

Hello Mr. Hennings,

First thank you for your very useful library, Button2.

I would like the button.loop() function to run in the background, from a timer interrupt (like one of the timers on STM32F4xx) so that it would be independent of the aleas of other function execution lengths, and also avoid to periodically call the button.loop() function completely. The long click, single, double and triple click would simply set a flag that can be reset at any time after reading the status of the flag(s).

I have to say that I tried to call the button.loop() function from a timer interrupt function but without success.

Thank you for your help.

Regards,
Rene-Jean Mercier
renejeanmercier@gmail.com

@LennartHennigs
Copy link
Owner

Hey Rene-Jean,

well, sure, you could – in theory.
I haven't spent much time looking at the STM32F but you'd want a rather frequent one, otherwise, you'd miss the events.
And as interrupts should be processed fast, you'd need to make sure that you don't set any callback functions.
And then you could use the read() function to read the state of your button.
But this approach includes quite a few "ifs" and "but's".

I used callbacks and the loop() approach in order to keep the real loop() function clean and omit the need for checks somewhere else. If you don't do much processing in your main loop this results in cleaner code IMO.

@renejeanmercier
Copy link
Author

renejeanmercier commented Oct 11, 2022 via email

@LennartHennigs
Copy link
Owner

Hello René-Jean,
I will try to use an ESP32 and its interrupt timer with my class. Let's see...

@renejeanmercier
Copy link
Author

renejeanmercier commented Oct 30, 2022 via email

@LennartHennigs
Copy link
Owner

LennartHennigs commented Oct 31, 2022

Hey @renejeanmercier,

I looked at your code above, you actually check button events in your main loop.
An alternative would be to use different handlers for your clicks and handle it here.
This would declutter your main loop:

void longClickHandler(Button2& btn) {
  fp.normalModeSelectParameter(); 
  fp.normalModeModifyParameter(); 
}

void doubleClickHandler(Button2& btn) {
  fp.presetModeSelectPreset();
  fp.displayUpdatedConstantCurrentAndVoltageStatus(true); 
  fp.calculateAndDisplayOutputImpedance(true); 
  fp.calculateAndDisplayOutputPower(true); 
}

But I will still check out interrupt handlers regardless.

@renejeanmercier
Copy link
Author

renejeanmercier commented Oct 31, 2022 via email

@LennartHennigs
Copy link
Owner

Hey @renejeanmercier,
I used an ESP32 (a M5Stack Core Gray) to tie the loop() function to an interrupt. And it works fine.

Here is the sample code:

#include "Button2.h"

#define BUTTON_PIN 39

hw_timer_t *timer = NULL;
Button2 btn;

void IRAM_ATTR onTimer() {
  btn.loop();
}

void click(Button2 &b) {
  Serial.println("click");
}

void setup() {
  Serial.begin(9600);

  btn.begin(BUTTON_PIN);
  btn.setTapHandler(click);
  
  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 100000, true); // every 0.1 seconds
  timerAlarmEnable(timer);
}

void loop() {
}

Most of the code is based on this example. I adjusted the interval to get a decent reading. Something in the 100ms to 10ms range is a good idea.

So in general, it works fine.
Hope this proves helpful to you.
Cheers
l.

@renejeanmercier
Copy link
Author

renejeanmercier commented Nov 1, 2022 via email

@renejeanmercier
Copy link
Author

renejeanmercier commented Dec 2, 2022 via email

@LennartHennigs
Copy link
Owner

LennartHennigs commented Dec 13, 2022

Hey Rene-Jean,
I hope you are feeling better now.
Thank you for your code.
I will surely take a look at it.
In 2.1.0 I added an ESP32 interrupt example, as this is a common controller.

I moved the LongClick to and will answer it there to have proper tracking: #46

Regardless, get well soon.

@renejeanmercier
Copy link
Author

renejeanmercier commented Dec 14, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants