GithubHelp home page GithubHelp logo

Comments (11)

LennartHennigs avatar LennartHennigs commented on June 30, 2024

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.

from button2.

renejeanmercier avatar renejeanmercier commented on June 30, 2024

from button2.

LennartHennigs avatar LennartHennigs commented on June 30, 2024

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

from button2.

renejeanmercier avatar renejeanmercier commented on June 30, 2024

from button2.

LennartHennigs avatar LennartHennigs commented on June 30, 2024

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.

from button2.

renejeanmercier avatar renejeanmercier commented on June 30, 2024

from button2.

LennartHennigs avatar LennartHennigs commented on June 30, 2024

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.

from button2.

renejeanmercier avatar renejeanmercier commented on June 30, 2024

from button2.

renejeanmercier avatar renejeanmercier commented on June 30, 2024

from button2.

LennartHennigs avatar LennartHennigs commented on June 30, 2024

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.

from button2.

renejeanmercier avatar renejeanmercier commented on June 30, 2024

from button2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.