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:
20:fb73eaaf0894
Parent:
19:fd3e33641aa7
Child:
23:41260ab0f49d
diff -r fd3e33641aa7 -r fb73eaaf0894 RotarySwitch.cpp
--- a/RotarySwitch.cpp	Tue Sep 04 08:28:34 2018 -0700
+++ b/RotarySwitch.cpp	Tue Sep 04 19:57:31 2018 +0000
@@ -3,24 +3,75 @@
  *
  */
 
-#include "RotatySwitch.hpp"
+#include "RotarySwitch.hpp"
+
+// QEI class supports the mode wheel.  In its most basic function it will tell
+// us which direction the wheel is moving (right or left) so that we can
+// use it to select the current mode.
+QEI Wheel(D12, D11, NC, 16);    // Quadrature encoder
 
-RotatySwitch::RotatySwitch(PinName channelA,
-                           PinName channelB)
-   : channelA_(channelA), channelB_(channelB)
+#define CPM     0x01
+#define CPS     0x02
+#define PLS     0x04
+#define VOLTS   0x08
+#define CNT1    0x10
+#define CNT2    0x20
+#define HV      0x40
+#define MENU    0x80
+uint8_t LED_statuses[] = {CPM, CPS, PLS, VOLTS, CNT1, CNT2, HV, MENU};
+
+RotarySwitch::RotarySwitch()
 {
+   LED_status = CPM;
+   LED_status_index = 0;
+   WheelCurrent = 0;
+   WheelPrevious = 0;
+   QEPBpressed = false;
+   WheelStateTimer = 0;
+   
+   LEDs_write(0x00);   // initialize LEDs (CPM and CNT1 on)
    Wheel.reset();
-   LEDs_write(0x00);
-   SecTenth_Beat.attach_us(&UpdateInput,  100000);  
-   Sec_Beat.attach_us(&UpdateOutput, 1000000);  
 }
 
-void RotatySwitch::GetPosition(void)
+int RotarySwitch::GetPosition(void)
 {
    return LED_status_index;
 }
 
-void RotatySwitch::UpdateOutput()   
+//---------------------------------------------------------------------------
+//Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
+
+void RotarySwitch::LEDs_write(unsigned short data_val)       
+{                       
+#define DT      1      // delay time in us for SPI emulation
+
+    // Update 74HC595 shift registers 
+    unsigned short mask;
+    
+    SCK = 0;
+    wait_us(DT);
+    CS2 = 0;    
+    
+    for(mask = 0x80; mask!= 0; mask>>= 1)
+    {
+      wait_us(DT);
+      SCK = 0;
+      if(mask & data_val)
+        MOSI = 0;
+      else
+        MOSI = 1;
+      wait_us(DT);
+      SCK = 1;
+    }
+    
+    SCK = 0;
+    wait_us(DT);
+    CS2 = 1;
+
+    return; 
+}
+
+void RotarySwitch::UpdateOutput()   
 {
     // Blink mode LED
     if (WHEEL_MODE_SELECT == currentWheelState)
@@ -37,7 +88,7 @@
    }
 }
 
-void RotatySwitch::UpdateInput()   
+void RotarySwitch::UpdateInput()   
 {               
    LEDs_write(LED_statuses[LED_status_index]);
         
@@ -79,7 +130,7 @@
    return;
 }
 
-void RotatySwitch::WheelStateMachine(WheelStateEvent event)
+void RotarySwitch::WheelStateMachine(WheelStateEvent event)
 {                       
    switch (currentWheelState) {
       case WHEEL_INACTIVE: