Charles Young's development fork. Going forward I only want to push mature code to main repository.

Dependencies:   mbed

Fork of GEO_COUNTER_L432KC by Geo Electronics "Geo Counter"

Revision:
19:fd3e33641aa7
Child:
20:fb73eaaf0894
diff -r ef5678ab1889 -r fd3e33641aa7 RotarySwitch.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RotarySwitch.cpp	Tue Sep 04 08:28:34 2018 -0700
@@ -0,0 +1,116 @@
+/**
+ * @author Charles Young
+ *
+ */
+
+#include "RotatySwitch.hpp"
+
+RotatySwitch::RotatySwitch(PinName channelA,
+                           PinName channelB)
+   : channelA_(channelA), channelB_(channelB)
+{
+   Wheel.reset();
+   LEDs_write(0x00);
+   SecTenth_Beat.attach_us(&UpdateInput,  100000);  
+   Sec_Beat.attach_us(&UpdateOutput, 1000000);  
+}
+
+void RotatySwitch::GetPosition(void)
+{
+   return LED_status_index;
+}
+
+void RotatySwitch::UpdateOutput()   
+{
+    // Blink mode LED
+    if (WHEEL_MODE_SELECT == currentWheelState)
+       LEDs_write(0);
+
+   if (WheelStateTimer)
+   {
+      WheelStateTimer++;
+      if (++WheelStateTimer > WheelStateTimeout)
+      {
+         WheelStateMachine(WHEEL_Timeout);
+         WheelStateTimer = 0;
+      }
+   }
+}
+
+void RotatySwitch::UpdateInput()   
+{               
+   LEDs_write(LED_statuses[LED_status_index]);
+        
+   // react to wheel if in proper state
+   WheelCurrent = int(Wheel.getPulses());
+   if (   (WHEEL_MODE_SELECT    == currentWheelState)
+       || (WHEEL_SUBMODE_SELECT == currentWheelState))
+   {
+     if (WHEEL_MODE_SELECT == currentWheelState)
+     {
+        if (WheelCurrent > WheelPrevious)
+           LED_status_index = ++LED_status_index % sizeof(LED_statuses);
+        else
+            if (WheelCurrent < WheelPrevious)
+               LED_status_index = --LED_status_index % sizeof(LED_statuses);
+     }
+     // Keep resetting WheelStateTimer as long as wheel is moving
+     if (WheelPrevious != WheelCurrent)
+        WheelStateTimer = 1;
+   }
+   WheelPrevious = WheelCurrent;
+
+   // detect when wheel button is pressed but wait until it is released
+   // before doing anything
+   bool QEPBbutton = QEPB.read();
+   if (   (QEPBbutton)
+       && (!QEPBpressed))
+   {
+      QEPBpressed = true;
+   }
+   else
+      if (   (!QEPBbutton)
+          && (QEPBpressed))
+      {
+         QEPBpressed = false;
+         WheelStateMachine(WHEEL_Pressed);
+      }
+    
+   return;
+}
+
+void RotatySwitch::WheelStateMachine(WheelStateEvent event)
+{                       
+   switch (currentWheelState) {
+      case WHEEL_INACTIVE:
+      if (WHEEL_Pressed == event)
+      {
+         currentWheelState = WHEEL_MODE_SELECT;
+         WheelStateTimer = 1;
+      }
+      break;
+      case WHEEL_MODE_SELECT:
+      if (WHEEL_Pressed == event)
+      {
+         currentWheelState = WHEEL_SUBMODE_SELECT;
+         WheelStateTimer = 1;
+      }
+      else
+         if (WHEEL_Timeout == event)
+            currentWheelState = WHEEL_INACTIVE;
+      break;
+      case WHEEL_SUBMODE_SELECT:
+      if (WHEEL_Pressed == event)
+      {
+         currentWheelState = WHEEL_MODE_SELECT;
+         WheelStateTimer = 1;
+      }
+      else
+         if (WHEEL_Timeout == event)
+            currentWheelState = WHEEL_INACTIVE;
+      break;
+      default:
+      break;
+   }
+}
+