Skip to content

Commit

Permalink
irq-driven fast button code
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Dec 13, 2023
1 parent 30462d6 commit 1ac905d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions ProffieOS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ V3TestScript script;
#endif
#include "buttons/rotary.h"
#include "buttons/pots.h"
#include "buttons/fast_button.h"

#include "ir/ir.h"
#include "ir/receiver.h"
Expand Down
35 changes: 35 additions & 0 deletions buttons/fast_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef BUTTONS_FAST_BUTTON_H
#define BUTTONS_FAST_BUTTON_H

// Like a button, but can respond to very short events.
// No de-bouncing, only generates EVENT_CLICK_SHORT
template<int PIN>
class FastButton : public Looper {
public:
USE_PIN_INPUT(PIN, FastButton<PIN>);

FastButton(const char* name, enum BUTTON button) : name_(name), button_(button) {
pinMode(PIN, INPUT_PULLUP);
attachInterrupt(PIN, &FastButton::irq, FALLING);
}

const char* name() override { return name_; }

void Loop() override {
if (pressed) {
pressed = false;
prop.Event(button_, EVENT_CLICK_SHORT);
}
}

private:
static void irq() { pressed = true; }

static volatile bool pressed;
const char* name_;
enum BUTTON button_;
};

template<int PIN> volatile bool FastButton<PIN>::pressed = false;

#endif

0 comments on commit 1ac905d

Please sign in to comment.