Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Revision:
0:f45212966fb1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder.h	Mon Feb 18 13:13:34 2019 +0000
@@ -0,0 +1,49 @@
+//works only in x2 encoding (512 counts per second)
+class Encoder {  
+    private:
+    InterruptIn ChannelA; //connect this to channel A, 
+    DigitalIn ChannelB; //connect this to channel B,
+    Timer dT; //to calculate rate of change of encoder ticks.
+    int PET;
+    int CET;
+    
+    public:
+    
+    
+    Encoder(PinName A, PinName B) : ChannelA(A),ChannelB(B){
+        dT.reset();
+        ChannelA.rise(callback(this, &Encoder::riseEncode)); //
+        ChannelA.fall(callback(this, &Encoder::fallEncode));
+        CET = 0;
+        PET = 0;
+        dT.start();
+        };
+        
+    void riseEncode(){ //logic for what happens when you get a pulse in channel A
+        if (ChannelB.read() != 0) //essentially if the rise happens while B is 1 (high) means its going in reverse
+            {
+            CET --;
+            } else
+            {
+            CET ++;   
+            }
+        };
+        
+    void fallEncode(){ //logic, same as above but a fall in channel A
+        if (ChannelB.read() != 0){ //essentially if fall pulse in channel A happens while b ia high, means going forward
+            CET ++; 
+            } else
+            {
+            CET --;
+            }
+        };  
+        //refer to technical handbook quadrature encoder section.
+        
+    float encoderTickRate() {
+        int Temp = PET;
+        PET = CET;
+        dT.reset();
+        return (CET-Temp)/(int)dT.read();
+        };
+
+};
\ No newline at end of file