EmlaLockSafe
UsedInterrupts.h
Go to the documentation of this file.
1
5#pragma once
6
8
9#include <Arduino.h>
10#include <functional>
11
12void ICACHE_RAM_ATTR encoderInterrupt(void);
13
20private:
24 static bool interruptsAttached;
25
26public:
30 static void attach() {
31 if (!interruptsAttached) {
32 attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_CLK), encoderInterrupt, CHANGE);
33 attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_DT), encoderInterrupt, CHANGE);
34 attachInterrupt(digitalPinToInterrupt(ENCODER_SWITCH), encoderInterrupt, CHANGE);
35 interruptsAttached = true;
36 }
37 }
38
39public:
43 static void detach() {
45 detachInterrupt(digitalPinToInterrupt(ENCODER_PIN_CLK));
46 detachInterrupt(digitalPinToInterrupt(ENCODER_PIN_DT));
47 detachInterrupt(digitalPinToInterrupt(ENCODER_SWITCH));
48 interruptsAttached = false;
49 }
50 }
51
52public:
58 static void executeWithoutInterrupts(std::function<void(void)> f) {
60 detach();
61 try {
62 f();
63 }
64 catch (...) {
65 attach();
66 throw;
67 }
68 attach();
69 }
70 else {
71 f();
72 }
73 }
74};
#define ENCODER_PIN_DT
GPIO of the ky-040 dt pin.
Definition: HardwareConfiguration.h:40
#define ENCODER_SWITCH
GPIO of the ky-040 sw pin.
Definition: HardwareConfiguration.h:45
#define ENCODER_PIN_CLK
GPIO of the ky-040 clk pin.
Definition: HardwareConfiguration.h:35
void ICACHE_RAM_ATTR encoderInterrupt(void)
Callback if one of the encoder's pin are changed.
Definition: main.cpp:78
Class used to globally disable enable interrupts.
Definition: UsedInterrupts.h:19
static void executeWithoutInterrupts(std::function< void(void)> f)
Execute the passed function without interrupts.
Definition: UsedInterrupts.h:58
static void attach()
attach the encoder pins to a interrupt
Definition: UsedInterrupts.h:30
static bool interruptsAttached
true if at attach() was called at least once
Definition: UsedInterrupts.h:24
static void detach()
detach the interrupts
Definition: UsedInterrupts.h:43