-
Notifications
You must be signed in to change notification settings - Fork 1
/
REncoderWithSwitch.cpp
121 lines (108 loc) · 4.29 KB
/
REncoderWithSwitch.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <Arduino.h>
#include "REncoderWithSwitch.h"
REncoderWithSwitch::REncoderWithSwitch(int8_t clkPin, int8_t dtPin, int8_t swPin)
: REncoderWithoutSwitch::REncoderWithoutSwitch (clkPin, dtPin) {
this->clkPin = clkPin >= 0 ? clkPin : -1;
this->dtPin = dtPin >= 0 ? dtPin : -1;
this->swPin = swPin >= 0 ? swPin : -1;
this->init();
};
#if !defined(RENCODER_ENABLE_SWITCH_IRQ) && !defined(RENCODER_ENABLE_ENCODER_IRQ)
void REncoderWithSwitch::enableContinuousSwitchDetection(bool enableState) {
this->continousSwitchDetection = enableState;
};
void REncoderWithSwitch::enableEncoderWhenSwitchPushed(bool enableState) {
this->enableRotationDetectionWhenSwitchActive = enableState;
};
#endif
void REncoderWithSwitch::init() {
REncoderWithoutSwitch::init();
if (this->swPin >= 0) {
pinMode(this->swPin, INPUT_PULLUP);
this->lastSwState = digitalRead(this->swPin);
}
};
#ifndef RENCODER_ENABLE_SWITCH_IRQ
#ifdef RENCODER_ENABLE_SWITCH_SOFTWARE_DEBOUNCING
REncoderWithSwitch::Event REncoderWithSwitch::reStateSwitch() {
REncoderWithSwitch::Event switchEvent = REncoderWithSwitch::Event::REncoder_Event_None;
if (this->swPin >= 0) {
if ((millis() - this->lastSwitchDebounceTime) > this->debounceSwitchTolerance) {
byte swState = digitalRead(this->swPin);
if (swState == LOW) {
if (this->continousSwitchDetection) {
switchEvent = REncoderWithSwitch::Event::REncoder_Event_Switch_Pushed;
}
else if (this->lastSwState == HIGH) {
switchEvent = REncoderWithSwitch::Event::REncoder_Event_Switch_Pushed;
this->lastSwState = LOW;
}
} else {
this->lastSwState = HIGH;
}
this->lastSwitchDebounceTime = millis();
}
}
return switchEvent;
};
#else
REncoderWithSwitch::Event REncoderWithSwitch::reStateSwitch() {
REncoderWithSwitch::Event switchEvent = REncoderWithSwitch::Event::REncoder_Event_None;
if (this->swPin >= 0) {
byte swState = digitalRead(this->swPin);
if (swState == LOW) {
if (this->continousSwitchDetection) {
switchEvent = REncoderWithSwitch::Event::REncoder_Event_Switch_Pushed;
} else if (this->lastSwState == HIGH) {
switchEvent = REncoderWithSwitch::Event::REncoder_Event_Switch_Pushed;
this->lastSwState = LOW;
}
} else {
this->lastSwState = HIGH;
}
}
return switchEvent;
};
#endif
#endif
#ifndef RENCODER_ENABLE_ENCODER_IRQ
REncoderWithSwitch::Event REncoderWithSwitch::reStateEncoder() {
// make sure that PW pin is not LOW. When the PW pin is LOW, then the button
// switch is pressed and during this time, the rotation of the encoder shall
// have no effect.
if (!this->enableRotationDetectionWhenSwitchActive && this->lastSwState == LOW) {
#ifdef RENCODER_ENABLE_ENCODER_SOFTWARE_DEBOUNCING
this->lastEncoderDebounceTime = millis();
#endif
return REncoderWithSwitch::Event::REncoder_Event_None;
}
// make sure that the CLK and DT pins are defined and check if the encoder changed state
return REncoderWithoutSwitch::reState();
};
#endif
#if !defined(RENCODER_ENABLE_SWITCH_IRQ) || !defined(RENCODER_ENABLE_ENCODER_IRQ)
REncoderWithSwitch::Event REncoderWithSwitch::reState(void) {
REncoderWithSwitch::Event event = REncoderWithSwitch::Event::REncoder_Event_None;
#ifndef RENCODER_ENABLE_SWITCH_IRQ
event = this->reStateSwitch();
// 'SWITCH Activated' (i.e., button pushed) event has higher priority
if (event != REncoderWithSwitch::Event::REncoder_Event_None) {
return event;
}
#endif
#ifndef RENCODER_ENABLE_ENCODER_IRQ
event = this->reStateEncoder();
#endif
return event;
}
#endif
#ifdef RENCODER_ENABLE_SWITCH_IRQ
void REncoderWithSwitch::attachSwitchHandler(void (*switchHandler)(void)) {
if (this->swPin >= 0 && switchHandler) {
// remove any possibly already attached interrupt for the SWITCH pin
detachInterrupt(digitalPinToInterrupt(this->swPin));
// attache interrupt in FALLING pin mode (HIGH state changes to LOW state)
attachInterrupt(digitalPinToInterrupt(this->swPin), switchHandler, FALLING);
}
};
#endif