Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:db7f371c8530, committed 2016-07-20
- Comitter:
- Sateg
- Date:
- Wed Jul 20 00:30:23 2016 +0000
- Commit message:
- Working, conservative rotary encoder handler.
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 db7f371c8530 RotaryEncoder.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/RotaryEncoder.cpp Wed Jul 20 00:30:23 2016 +0000
@@ -0,0 +1,45 @@
+#include "RotaryEncoder.h"
+
+RotaryEncoder::RotaryEncoder(void (*cb)(void), PinName channelA_, PinName channelB_, PinName pushPin_, float period)
+ : channelA(channelA_), channelB(channelB_), pushPin(pushPin_)
+{
+ direction = 0;
+ counter = 0;
+ lastDirection = 0;
+
+ encoderTicker.attach(this, &RotaryEncoder::pulse, period);
+ pushPin.rise(cb);
+}
+
+RotaryEncoder::~RotaryEncoder() {};
+
+/** Public methods **/
+
+int8_t RotaryEncoder::getDirection() const
+{
+ return direction;
+}
+
+int RotaryEncoder::getCounter() const
+{
+ return counter;
+}
+
+bool RotaryEncoder::getPushButton() const
+{
+ return false;
+}
+
+/** Private methods **/
+
+void RotaryEncoder::pulse()
+{
+ lastDirection <<= 2;
+ if (channelB) lastDirection |= 1;
+ if (channelA) lastDirection |= 2;
+
+ direction = encoderTable[lastDirection & 0x0F];
+ counter += direction;
+}
+
+const int8_t RotaryEncoder::encoderTable[] = {0, 0, 0, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 0, 0, 0};
\ No newline at end of file
diff -r 000000000000 -r db7f371c8530 RotaryEncoder.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/RotaryEncoder.h Wed Jul 20 00:30:23 2016 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+
+#ifndef ROTARY_ENCODER_H_
+#define ROTARY_ENCODER_H_
+
+/**
+ Rotary encoder handling class.
+ Starts a ticker which checks the status of the encoder and
+ sets the rotation directions based on its status.
+*/
+class RotaryEncoder {
+public:
+ RotaryEncoder(void (*cb)(void), PinName channelA_, PinName channelB_, PinName pushPin_, float period = 0.01);
+ virtual ~RotaryEncoder();
+
+ /// Retrieve rotation direction.
+ int8_t getDirection() const;
+ /// Retrieve push button status.
+ bool getPushButton() const;
+ /// Launched by Ticker to determine rotation direction
+ void pulse();
+
+ int getCounter() const;
+
+ private:
+
+ /// Table which helps determine the rotation direction
+ static const int8_t encoderTable[];
+
+ int counter;
+ int8_t direction;
+ int8_t lastDirection;
+ InterruptIn channelA;
+ InterruptIn channelB;
+ InterruptIn pushPin;
+ Ticker encoderTicker;
+
+};
+
+#endif /* ROTARY_ENCODER_H_ */
\ No newline at end of file