Repository navigation
Expand file tree
/
Copy pathknob.cpp
More file actions
92 lines (78 loc) · 2.42 KB
/
Copy pathknob.cpp
File metadata and controls
92 lines (78 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "knob.h"
#include <Arduino.h>
#include "board.h"
#include "config.h"
// Quadrature state table, indexed by (previous AB << 2) | current AB.
static const int8_t TRANSITIONS[16] = {
0, -1, 1, 0,
1, 0, 0, -1,
-1, 0, 0, 1,
0, 1, -1, 0,
};
static constexpr int QUARTER_STEPS_PER_DETENT = 4;
static portMUX_TYPE encoderMux = portMUX_INITIALIZER_UNLOCKED;
static volatile int32_t quarterSteps = 0;
static volatile uint8_t encoderState = 0;
static uint8_t readEncoder() {
return (digitalRead(pins::ENC_A) << 1) | digitalRead(pins::ENC_B);
}
static void IRAM_ATTR onEncoderEdge() {
const uint8_t current = readEncoder();
portENTER_CRITICAL_ISR(&encoderMux);
quarterSteps += TRANSITIONS[(encoderState << 2) | current];
encoderState = current;
portEXIT_CRITICAL_ISR(&encoderMux);
}
void knobBegin() {
pinMode(pins::ENC_A, INPUT);
pinMode(pins::ENC_B, INPUT);
pinMode(pins::ENC_SW, INPUT_PULLUP);
encoderState = readEncoder();
attachInterrupt(digitalPinToInterrupt(pins::ENC_A), onEncoderEdge, CHANGE);
attachInterrupt(digitalPinToInterrupt(pins::ENC_B), onEncoderEdge, CHANGE);
}
int knobTakeDetents() {
portENTER_CRITICAL(&encoderMux);
const int detents = quarterSteps / QUARTER_STEPS_PER_DETENT;
quarterSteps -= detents * QUARTER_STEPS_PER_DETENT;
portEXIT_CRITICAL(&encoderMux);
return ENCODER_REVERSED ? -detents : detents;
}
ButtonEvent buttonPoll() {
static bool lastRaw = false;
static bool pressed = false;
static uint32_t lastChangeMs = 0;
static uint32_t pressedAtMs = 0;
static uint32_t releasedAtMs = 0;
static bool longFired = false;
static uint8_t pendingClicks = 0;
const uint32_t now = millis();
const bool raw = digitalRead(pins::ENC_SW) == LOW;
if (raw != lastRaw) {
lastRaw = raw;
lastChangeMs = now;
}
if (raw != pressed && now - lastChangeMs >= BUTTON_DEBOUNCE_MS) {
pressed = raw;
if (pressed) {
pressedAtMs = now;
longFired = false;
} else if (!longFired) {
releasedAtMs = now;
if (++pendingClicks >= 2) {
pendingClicks = 0;
return ButtonEvent::DoubleClick;
}
}
}
if (pressed && !longFired && now - pressedAtMs >= BUTTON_LONG_PRESS_MS) {
longFired = true;
pendingClicks = 0;
return ButtonEvent::LongPress;
}
if (!pressed && pendingClicks == 1 && now - releasedAtMs >= BUTTON_DOUBLE_CLICK_MS) {
pendingClicks = 0;
return ButtonEvent::Click;
}
return ButtonEvent::None;
}