Repository navigation
Expand file tree
/
Copy pathInterruptRotator.cpp
More file actions
76 lines (64 loc) · 1.74 KB
/
Copy pathInterruptRotator.cpp
File metadata and controls
76 lines (64 loc) · 1.74 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
#include "InterruptRotator.h"
#include "GPAD_HAL.h"
#include "GPAD_menu.h"
#include "debug_macros.h"
#include "setup_status.h"
#include <new>
static RotaryEncoder *encoder = nullptr;
volatile unsigned long rotaryEncoderEventCount = 0;
// This global variable represents the state of the menu;
// we are either running the menu (true) or displaying other
// information (false)
extern bool running_menu;
bool initRotator()
{
DBG_PRINTLN(F("InterruptRotator init"));
encoder = new (std::nothrow) RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
if (encoder == nullptr)
{
setSetupError(SETUP_ERROR_ROTARY_ENCODER);
DBG_PRINTLN(F("Rotary encoder allocation failed; continuing without encoder input."));
return false;
}
attachInterrupt(digitalPinToInterrupt(PIN_IN1), checkPositionISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_IN2), checkPositionISR, CHANGE);
return true;
}
void updateRotator()
{
static int pos = 0;
if (encoder == nullptr)
{
return;
}
encoder->tick();
int newPos = encoder->getPosition();
if (pos != newPos)
{
rotaryEncoderEventCount++;
DBG_PRINT(F("pos: "));
DBG_PRINT(newPos);
DBG_PRINT(F(" dir: "));
int d = (int)(encoder->getDirection());
bool CW;
if (d == (int)RotaryEncoder::Direction::CLOCKWISE)
CW = true;
else
CW = false;
if (!running_menu)
{
alarmActionSelectorHandleRotation(CW);
pos = newPos;
return;
}
registerRotationEvent(CW);
pos = newPos;
}
}
void IRAM_ATTR checkPositionISR()
{
if (encoder != nullptr)
{
encoder->tick();
}
}