-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotaryEncoder.cpp
168 lines (134 loc) · 4.26 KB
/
RotaryEncoder.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
RotaryEncoder.cpp - Library for interfacing rotary encoders with STM32duino
Created by datenpirat, January 8, 2019.
Released into the public domain.
*/
#include "Arduino.h"
#include "RotaryEncoder.h"
static int8_t rot_enc_table[] = {0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0};
boolean RotaryEncoderA_set = false;
boolean RotaryEncoderB_set = false;
uint8_t DEBOUNCE_TIME_ENCODER = 1;
uint16_t DEBOUNCE_TIME_BUTTON = 5;
RotaryEncoder::RotaryEncoder(int PIN1, int PIN2, int PINBTN)
{
pinMode(PIN1, INPUT_PULLUP);
pinMode(PIN2, INPUT_PULLUP);
pinMode(PINBTN, INPUT_PULLUP);
_pin1 = PIN1;
_pin2 = PIN2;
_pinBtn = PINBTN;
buttonIsPressing = 0;
fallTime = millis();
}
int8_t RotaryEncoder::Read() {
if(!useInterrupts){
// A vald CW or CCW move returns 1, invalid returns 0.
prevNextCode <<= 2;
if(gpio_read_bit(PIN_MAP[_pin1].gpio_device, PIN_MAP[_pin1].gpio_bit)) prevNextCode |= 0x02;
if(gpio_read_bit(PIN_MAP[_pin2].gpio_device, PIN_MAP[_pin2].gpio_bit)) prevNextCode |= 0x01;
prevNextCode &= 0x0f;
// If valid then store as 16 bit data.
if (rot_enc_table[prevNextCode] ) {
store <<= 4;
store |= prevNextCode;
if ((store&0xff)==0x2b) return 1;
if ((store&0xff)==0x17) return -1;
}
return 0;
}
else{
return encoderPos;
}
}
int8_t RotaryEncoder::HasEncoderChanged(){
int8_t result = 0;
if (lastReportedPos != encoderPos) {
result = encoderPos - lastReportedPos;
lastReportedPos = encoderPos;
}
return result;
}
int8_t RotaryEncoder::GetEncoderValue(){
return encoderPos;
}
uint8_t RotaryEncoder::HasButtonChanged(){
int8_t result = buttonChanged;
buttonChanged = 0;
return result;
}
uint8_t RotaryEncoder::GetButtonDown(){
return GetButtonIsPressing();
}
uint8_t RotaryEncoder::GetButtonUp(){
return !GetButtonIsPressing();
}
uint8_t RotaryEncoder::GetButtonIsPressing(){
return buttonIsPressing;
}
uint32_t RotaryEncoder::GetButtonPressedTime(){
if(fallTime > riseTime){
return(fallTime - riseTime);
}
else{
return 0;
}
}
void RotaryEncoder::UseInterrupts(){
useInterrupts = 1;
ihp1.pin = _pin1;
ihp1.p_val = &encoderPos;
ihp1.p_tick = &lastTick;
ihp2.pin = _pin2;
ihp2.p_val = &encoderPos;
ihp2.p_tick = &lastTick;
btnhp.pin = _pinBtn;
btnhp.p_lastRising = &lastRising;
btnhp.p_riseTime = &riseTime;
btnhp.p_fallTime = &fallTime;
btnhp.p_buttonChanged = &buttonChanged;
btnhp.p_buttonIsPressing = &buttonIsPressing;
attachInterrupt(_pin1,IRQPIN1,(void*)&ihp1,CHANGE);
attachInterrupt(_pin2,IRQPIN2,(void*)&ihp2,CHANGE);
attachInterrupt(_pinBtn,IRQBTN,(void*)&btnhp,CHANGE);
}
void RotaryEncoder::IRQPIN1(void *p){
IRQHandlerParameters *_p = (IRQHandlerParameters *)p;
if(gpio_read_bit(PIN_MAP[_p->pin].gpio_device, PIN_MAP[_p->pin].gpio_bit) != RotaryEncoderA_set) {
RotaryEncoderA_set = !RotaryEncoderA_set;
if ( RotaryEncoderA_set && !RotaryEncoderB_set ) {
if((millis() - *_p->p_tick) > DEBOUNCE_TIME_ENCODER){
*_p->p_val += 1;
}
}
*_p->p_tick = millis();
}
}
void RotaryEncoder::IRQPIN2(void *p){
IRQHandlerParameters *_p = (IRQHandlerParameters *)p;
if(gpio_read_bit(PIN_MAP[_p->pin].gpio_device, PIN_MAP[_p->pin].gpio_bit) != RotaryEncoderB_set) {
RotaryEncoderB_set = !RotaryEncoderB_set;
if( RotaryEncoderB_set && !RotaryEncoderA_set ) {
if((millis() - *_p->p_tick) > DEBOUNCE_TIME_ENCODER){
*_p->p_val -= 1;
}
}
*_p->p_tick = millis();
}
}
void RotaryEncoder::IRQBTN(void* p){
BtnIRQHandlerParameters *_p = (BtnIRQHandlerParameters *)p;
if((millis() - *_p->p_lastRising) > DEBOUNCE_TIME_BUTTON){
uint32_t c = gpio_read_bit(PIN_MAP[_p->pin].gpio_device, PIN_MAP[_p->pin].gpio_bit) ? HIGH : LOW;
if(c == LOW){ // RISING, PULLUP!
*_p->p_riseTime = millis();
*_p->p_buttonIsPressing = 1;
*_p->p_buttonChanged = 1;
}
else if(c == HIGH){ // FALLING, PULLUP!
*_p->p_fallTime = millis();
*_p->p_buttonChanged = 1;
*_p->p_buttonIsPressing = 0;
}
}
}