Incremental rotary encoder class

Dependents:   PWM_LED_Lights

Committer:
nzupcic
Date:
Tue Sep 21 16:46:09 2021 +0000
Revision:
0:b245c23a1c44
1st commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nzupcic 0:b245c23a1c44 1 #include "RotaryEncoder.h"
nzupcic 0:b245c23a1c44 2
nzupcic 0:b245c23a1c44 3 RotaryEncoder::RotaryEncoder(PinName pEncoderA, PinName pEncoderB)
nzupcic 0:b245c23a1c44 4 : encoderA (pEncoderA), encoderB (pEncoderB){
nzupcic 0:b245c23a1c44 5 encoderA.rise(this, &RotaryEncoder::Count);
nzupcic 0:b245c23a1c44 6 encoderA.fall(this, &RotaryEncoder::Count);
nzupcic 0:b245c23a1c44 7 }
nzupcic 0:b245c23a1c44 8
nzupcic 0:b245c23a1c44 9 void RotaryEncoder::SetCounter(float setValue){
nzupcic 0:b245c23a1c44 10 RotaryEncoder::Value = setValue;
nzupcic 0:b245c23a1c44 11 }
nzupcic 0:b245c23a1c44 12
nzupcic 0:b245c23a1c44 13 void RotaryEncoder::Count(void){
nzupcic 0:b245c23a1c44 14 aState = RotaryEncoder::encoderA.read();
nzupcic 0:b245c23a1c44 15 if (RotaryEncoder::Enable) {
nzupcic 0:b245c23a1c44 16 if (debounce.read_ms() > 200){
nzupcic 0:b245c23a1c44 17 if (aState != aLastState){
nzupcic 0:b245c23a1c44 18 RotaryEncoder::Value += 5;
nzupcic 0:b245c23a1c44 19 RotaryEncoder::debounce.reset();
nzupcic 0:b245c23a1c44 20 }
nzupcic 0:b245c23a1c44 21 else{
nzupcic 0:b245c23a1c44 22 RotaryEncoder::Value -= 5;
nzupcic 0:b245c23a1c44 23 RotaryEncoder::debounce.reset();
nzupcic 0:b245c23a1c44 24 }
nzupcic 0:b245c23a1c44 25 }
nzupcic 0:b245c23a1c44 26 if(RotaryEncoder::Value > 100){
nzupcic 0:b245c23a1c44 27 RotaryEncoder::Value = 100;
nzupcic 0:b245c23a1c44 28 }
nzupcic 0:b245c23a1c44 29 if (RotaryEncoder::Value <= 0){
nzupcic 0:b245c23a1c44 30 RotaryEncoder::Value = 1;
nzupcic 0:b245c23a1c44 31 }
nzupcic 0:b245c23a1c44 32 }
nzupcic 0:b245c23a1c44 33 }
nzupcic 0:b245c23a1c44 34
nzupcic 0:b245c23a1c44 35 void RotaryEncoder::Init(float startValue){
nzupcic 0:b245c23a1c44 36 RotaryEncoder::Enable = false;
nzupcic 0:b245c23a1c44 37 RotaryEncoder::LastState = 0.0;
nzupcic 0:b245c23a1c44 38 RotaryEncoder::SetCounter(startValue);
nzupcic 0:b245c23a1c44 39 aLastState = RotaryEncoder::encoderA.read();
nzupcic 0:b245c23a1c44 40 debounce.start();
nzupcic 0:b245c23a1c44 41 }