Derivative QEI library to add an error counter and the ability to set the count and revolutions, and reset the error counter.

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Wed Jan 13 20:12:03 2021 +0000
Parent:
0:5c2ad81551aa
Commit message:
Functional version suitable for testing

Changed in this revision

QEI.cpp Show annotated file Show diff for this revision Revisions of this file
QEI.h Show annotated file Show diff for this revision Revisions of this file
diff -r 5c2ad81551aa -r cecfd2d4d286 QEI.cpp
--- a/QEI.cpp	Thu Sep 02 16:48:55 2010 +0000
+++ b/QEI.cpp	Wed Jan 13 20:12:03 2021 +0000
@@ -3,6 +3,15 @@
  *
  * @section LICENSE
  *
+ * Derivative work created in Jan 2021 by D.Smart, which 
+ * has the following changes:
+ *  + Update for MBED OS 6 ('callback' added to member ISRs)
+ *  + Added counter for non-grey-code transitions
+ *  + Added set functions to initialize counts to known values
+ *  + Added get functions for error counters
+ *
+ * No copyright claim is being made on these changes.
+ *
  * Copyright (c) 2010 ARM Limited
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -137,6 +146,7 @@
 
     pulses_       = 0;
     revolutions_  = 0;
+    invalid_      = 0;
     pulsesPerRev_ = pulsesPerRev;
     encoding_     = encoding;
 
@@ -151,28 +161,43 @@
     //X2 encoding uses interrupts on only channel A.
     //X4 encoding uses interrupts on      channel A,
     //and on channel B.
-    channelA_.rise(this, &QEI::encode);
-    channelA_.fall(this, &QEI::encode);
+    channelA_.rise(callback(this, &QEI::encode));
+    channelA_.fall(callback(this, &QEI::encode));
 
     //If we're using X4 encoding, then attach interrupts to channel B too.
     if (encoding == X4_ENCODING) {
-        channelB_.rise(this, &QEI::encode);
-        channelB_.fall(this, &QEI::encode);
+        channelB_.rise(callback(this, &QEI::encode));
+        channelB_.fall(callback(this, &QEI::encode));
     }
     //Index is optional.
     if (index !=  NC) {
-        index_.rise(this, &QEI::index);
+        index_.rise(callback(this, &QEI::index));
     }
 
 }
 
+void QEI::setPulses(int newCount) {
+    pulses_     = newCount;
+}
+
+void QEI::setRevolutions(int newRevs) {
+    revolutions_    = newRevs;
+}
+
 void QEI::reset(void) {
 
     pulses_      = 0;
     revolutions_ = 0;
+    invalid_     = 0;
 
 }
 
+int QEI::getInvalidCount(void) {
+    int r = invalid_;
+    invalid_ = 0;
+    return r;
+}
+
 int QEI::getCurrentState(void) {
 
     return currState_;
@@ -260,6 +285,9 @@
             pulses_--;
 
         }
+        else {
+            invalid_++;
+        }
 
     } else if (encoding_ == X4_ENCODING) {
 
diff -r 5c2ad81551aa -r cecfd2d4d286 QEI.h
--- a/QEI.h	Thu Sep 02 16:48:55 2010 +0000
+++ b/QEI.h	Wed Jan 13 20:12:03 2021 +0000
@@ -3,6 +3,15 @@
  *
  * @section LICENSE
  *
+ * Derivative work created in Jan 2021 by D.Smart, which 
+ * has the following changes:
+ *  + Update for MBED OS 6 ('callback' added to member ISRs)
+ *  + Added counter for non-grey-code transitions
+ *  + Added set functions to initialize counts to known values
+ *  + Added get functions for error counters
+ *
+ * No copyright claim is being made on these changes.
+ *
  * Copyright (c) 2010 ARM Limited
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -195,6 +204,15 @@
     int getCurrentState(void);
 
     /**
+     * Read the number of invalid counts.
+     *
+     * @note Reading this resets the counter!
+     * 
+     * @return the number of times the gray-code transition was violated.
+     */
+    int getInvalidCount(void);
+
+    /**
      * Read the number of pulses recorded by the encoder.
      *
      * @return Number of pulses which have occured.
@@ -208,6 +226,23 @@
      */
     int getRevolutions(void);
 
+    /**
+     * Set the current pulse count in case you want to init it.
+     *
+     * @param[in] newCount is the count to set it to.
+     *
+     */
+     void setPulses(int newCount);
+
+    /**
+     * Set the current revolution count in case you want to init it.
+     *
+     * @param[in] newRevs is the count to set it to.
+     *
+     */
+    void setRevolutions(int newRevs);
+
+
 private:
 
     /**
@@ -235,6 +270,8 @@
     int          pulsesPerRev_;
     int          prevState_;
     int          currState_;
+    
+    volatile int invalid_;
 
     volatile int pulses_;
     volatile int revolutions_;