-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIKTEC_Rotary.cpp
208 lines (199 loc) · 6.6 KB
/
SIKTEC_Rotary.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/******************************************************************************/
// Created by: Shlomo Hassid.
// Release Version : 1.0.6
// Creation Date: 2021-04-18
// Copyright 2021, SIKTEC / SIKDEV.
/******************************************************************************/
#include "SIKTEC_Rotary.h"
namespace SIKtec {
/*
* Constructor: initiate the Rotary Class with some required definitions
*/
Rotary::Rotary (
uint8_t encoderAPin,
uint8_t encoderBPin,
uint8_t encoderButtonPin,
bool circularRange,
RotaryRange rangeOf
) {
//Debounce realated:
Rotary::debounceDelay = ROTARY_DEBOUNCE_DELAY_DEFAULT;
Rotary::lastDebounceTime = millis();
//Define pins:
if (0 != encoderAPin) this->pinA = encoderAPin;
if (0 != encoderBPin) this->pinB = encoderBPin;
if (0 != encoderButtonPin) this->pinSW = encoderButtonPin;
pinMode(this->pinA, INPUT);
pinMode(this->pinB, INPUT);
pinMode(this->pinSW, INPUT);
//Save encoder range / steps / circular:
this->circular = circularRange;
this->setRange(rangeOf);
this->setPos(rangeOf.min);
//Define default callback:
this->setCallback([](const RotaryState){ });
}
/*
* attachInterupts: registers the interrupts
*/
void Rotary::attachInterupts(void (*A_callback)(void), void (*B_callback)(void)) {
attachInterrupt(digitalPinToInterrupt(this->pinA), A_callback, RISING);
attachInterrupt(digitalPinToInterrupt(this->pinB), B_callback, RISING);
}
/*
* activate: enables and disables the rotary counting
*/
void Rotary::activate(bool state) {
this->enabled = state;
this->changed = state;
}
/*
* enable: enables the rotary counting
*/
void Rotary::enable() {
this->enabled = true;
this->changed = true;
}
/*
* disable: disables the rotary counting
*/
void Rotary::disable() {
this->enabled = false;
this->changed = false;
}
/*
* setRange: sets the range of the rotary dynamicly
*/
ROTARY_COUNTER_TYPE Rotary::setRange(const RotaryRange rangeOf) {
cli();
this->range = rangeOf;
if (this->encoderPos > rangeOf.max || this->encoderPos < rangeOf.min) {
this->encoderPos = rangeOf.min;
this->encoderPosPrev = rangeOf.min;
if (this->enabled) this->changed = true;
}
sei();
return this->encoderPos;
}
/*
* setPos: sets programmatically the rotary counting position
*/
ROTARY_COUNTER_TYPE Rotary::setPos(const ROTARY_COUNTER_TYPE pos) {
cli();
if (pos <= this->range.max && pos >= this->range.min) {
this->encoderPos = pos;
this->encoderPosPrev = pos;
if (this->enabled) this->changed = true;
}
sei();
return this->encoderPos;
}
/*
* currentState: reads safely the rotary counting position
*/
RotaryState Rotary::currentState() {
RotaryState ret = {0, 0, 0};
ret.pos = this->encoderPos;
ret.prev = this->encoderPosPrev;
if (ret.pos > ret.prev) {
ret.delta = 1;
}
else if (ret.pos < ret.prev) {
ret.delta = -1;
}
else if (ret.pos == ret.prev) {
ret.delta = 0;
}
return ret;
}
/*
* readState: reads the rotary counting position and updates flags
*/
RotaryState Rotary::readState() {
cli();
RotaryState ret = this->currentState();
this->changed = false;
if (ret.delta != 0) this->encoderPosPrev = this->encoderPos;
sei();
return ret;
}
/*
* stepDown: performs a step decrement N times
*/
ROTARY_COUNTER_TYPE Rotary::stepDown(uint8_t times, bool callback) {
ROTARY_COUNTER_TYPE candid;
for (uint8_t i = 0; i < times; i++) {
candid = this->encoderPos - this->range.step;
if (!this->circular && candid < this->range.min) {
this->encoderPos = this->range.min;
//We avoid change flag here because its not circular.
} else if (this->circular && candid < this->range.min) {
this->encoderPos = this->range.max - (this->range.step - (this->encoderPos - this->range.min));
this->changed = true;
} else {
this->encoderPos -= this->range.step;
this->changed = true;
}
}
//Call attached callback:
if (callback)
this->localPointerToCallback(
this->currentState()
);
return this->encoderPos;
}
/*
* stepUp: performs a step increment N times
*/
ROTARY_COUNTER_TYPE Rotary::stepUp(uint8_t times, bool callback) {
ROTARY_COUNTER_TYPE candid;
for (uint8_t i = 0; i < times; i++) {
candid = this->encoderPos + this->range.step;
if (!this->circular && candid > this->range.max) {
this->encoderPos = this->range.max;
//We avoid change flag here because its not circular.
} else if (this->circular && candid > this->range.max) {
this->encoderPos = this->range.min + (this->range.step - (this->range.max - this->encoderPos));
this->changed = true;
} else {
this->encoderPos += this->range.step;
this->changed = true;
}
}
//Call attached callback:
if (callback)
this->localPointerToCallback(
this->currentState()
);
return this->encoderPos;
}
void Rotary::interA()
{
if (!this->enabled) return;
cli();
this->reading = PIND & 0xC;
if (this->reading == B00001100 && this->aFlag)
{
this->stepDown(1, true);
this->bFlag = 0;
this->aFlag = 0;
} else if (this->reading == B00000100)
this->bFlag = 1;
sei();
}
void Rotary::interB()
{
if (!this->enabled) return;
cli();
this->reading = PIND & 0xC;
if (this->reading == B00001100 && this->bFlag)
{
this->stepUp(1, true);
this->bFlag = 0;
this->aFlag = 0;
}
else if (this->reading == B00001000)
this->aFlag = 1;
sei();
}
}