Skip to content

Commit 8b9ddb2

Browse files
Merge pull request #21 from ajamtli/MatRotaryEncoder
Added MatRotaryEncoderT class
2 parents ebd1b2d + cdc13cb commit 8b9ddb2

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/internal/Encoders.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,72 @@ namespace DcsBios {
226226
}
227227
};
228228
typedef RotaryAcceleratedEncoderT<> RotaryAcceleratedEncoder;
229+
230+
template <unsigned long pollIntervalMs = POLL_EVERY_TIME, StepsPerDetent stepsPerDetent = ONE_STEP_PER_DETENT>
231+
class MatRotaryEncoderT : PollingInput {
232+
private:
233+
const char* msg_;
234+
const char* decArg_;
235+
const char* incArg_;
236+
volatile unsigned char* addressA_;
237+
volatile unsigned char* addressB_;
238+
char lastState_;
239+
int delta_ = 0;
240+
241+
char readState() {
242+
return ((byte) *addressA_ << 1) | (byte) *addressB_;
243+
}
244+
245+
void resetState() {
246+
lastState_ = (lastState_==0)?-1:0;
247+
}
248+
249+
void pollInput() {
250+
char state = readState();
251+
switch(lastState_) {
252+
case 0:
253+
if (state == 2) delta_--;
254+
if (state == 1) delta_++;
255+
break;
256+
case 1:
257+
if (state == 0) delta_--;
258+
if (state == 3) delta_++;
259+
break;
260+
case 2:
261+
if (state == 3) delta_--;
262+
if (state == 0) delta_++;
263+
break;
264+
case 3:
265+
if (state == 1) delta_--;
266+
if (state == 2) delta_++;
267+
break;
268+
}
269+
lastState_ = state;
270+
271+
if (delta_ >= stepsPerDetent) {
272+
if (tryToSendDcsBiosMessage(msg_, incArg_))
273+
delta_ -= stepsPerDetent;
274+
}
275+
if (delta_ <= -stepsPerDetent) {
276+
if (tryToSendDcsBiosMessage(msg_, decArg_))
277+
delta_ += stepsPerDetent;
278+
}
279+
}
280+
281+
public:
282+
MatRotaryEncoderT(const char* msg, const char* decArg, const char* incArg, volatile unsigned char* argAddressA, volatile unsigned char* argAddressB) :
283+
PollingInput(pollIntervalMs) {
284+
msg_ = msg;
285+
decArg_ = decArg;
286+
incArg_ = incArg;
287+
addressA_ = argAddressA;
288+
addressB_ = argAddressB;
289+
delta_ = 0;
290+
lastState_ = readState();
291+
}
292+
};
293+
typedef MatRotaryEncoderT<> MatRotaryEncoder;
294+
229295
}
230296

231297
#endif

0 commit comments

Comments
 (0)