Dependents:   PWM_LED_Lights

Files at this revision

API Documentation at this revision

Comitter:
nzupcic
Date:
Tue Sep 21 16:46:09 2021 +0000
Commit message:
1st commit

Changed in this revision

RotaryEncoder.cpp Show annotated file Show diff for this revision Revisions of this file
RotaryEncoder.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b245c23a1c44 RotaryEncoder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RotaryEncoder.cpp	Tue Sep 21 16:46:09 2021 +0000
@@ -0,0 +1,41 @@
+#include "RotaryEncoder.h"
+
+RotaryEncoder::RotaryEncoder(PinName pEncoderA, PinName pEncoderB)
+                     : encoderA (pEncoderA), encoderB (pEncoderB){
+    encoderA.rise(this, &RotaryEncoder::Count);
+    encoderA.fall(this, &RotaryEncoder::Count);
+}
+
+void RotaryEncoder::SetCounter(float setValue){
+    RotaryEncoder::Value = setValue;
+}
+
+void RotaryEncoder::Count(void){
+    aState = RotaryEncoder::encoderA.read();
+    if (RotaryEncoder::Enable) {
+        if (debounce.read_ms() > 200){
+            if (aState != aLastState){
+                RotaryEncoder::Value += 5;
+                RotaryEncoder::debounce.reset();
+            }
+            else{
+                RotaryEncoder::Value -= 5;
+                RotaryEncoder::debounce.reset();
+            }
+        }
+        if(RotaryEncoder::Value > 100){
+            RotaryEncoder::Value = 100;
+        }
+        if (RotaryEncoder::Value <= 0){
+            RotaryEncoder::Value = 1;
+        }
+    }
+}
+
+void RotaryEncoder::Init(float startValue){
+    RotaryEncoder::Enable    = false;
+    RotaryEncoder::LastState = 0.0;
+    RotaryEncoder::SetCounter(startValue);
+    aLastState = RotaryEncoder::encoderA.read();
+    debounce.start();
+}
\ No newline at end of file
diff -r 000000000000 -r b245c23a1c44 RotaryEncoder.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RotaryEncoder.h	Tue Sep 21 16:46:09 2021 +0000
@@ -0,0 +1,23 @@
+#ifndef ROTARYENCODER_H
+#define ROTARYENCODER_H
+
+#include "mbed.h"
+
+class RotaryEncoder{
+    public:
+        RotaryEncoder(PinName pEncoderA, PinName pEncoderB);
+        void        SetCounter(float setValue);
+        void        Init(float startValue);
+        void        Count();
+        bool        Enable;
+        float       Value;
+        float       LastState;
+    private:
+        int         aState;
+        int         aLastState;
+        InterruptIn encoderA;
+        DigitalIn   encoderB;
+        Timer       debounce;
+};
+
+#endif
\ No newline at end of file