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:
18:ef5678ab1889
Child:
21:5640ebc362a0
--- a/main.cpp	Tue Sep 04 08:28:34 2018 -0700
+++ b/main.cpp	Tue Sep 04 19:57:31 2018 +0000
@@ -3,6 +3,7 @@
 //
 //  Latest review: August 27, 2018 - Walter Trovo
 //
+//  Sep  4, 2018: Charles Young: Created RotarySwitch class to manage mode selection
 //  Feb 14, 2018: initial release aimed to test the counters, the serial port
 //                the PWM output and the MAX7219 operation.
 //  Feb 15, 2018: Removed MAX7219 libray (replaced with custom routine). 
@@ -11,9 +12,12 @@
 
 
 // this block includes key libraries
-#include "mbed.h"       // global Mbed library (always needed)
 #include <string>       // strings management
-#include "QEI.h"        // Quadrature Encoder functions
+#include "RotarySwitch.hpp"
+
+// Everything associated with the rotary switch and the associated LEDs
+// is hidden in this class.
+RotarySwitch ModeSwitch;
 
 // definitions of fixed parameters
 
@@ -29,61 +33,11 @@
 #define TGATE   10       // gate time (currently fixed for testing purpose)
 #define MAX_VAL 999999  // Max value managed by the 6-digits display
 
-
-#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};
-uint8_t LED_status = CPM;
-uint8_t LED_status_index = 0;
-
-// definitions of the input/outputs (pins)
-DigitalOut  AUX   (D2);    // AUX control for GPS module
-InterruptIn TRIG1 (D3);    // Counter 1 trigger
-InterruptIn TRIG2 (D6);    // Counter 2 trigger
-DigitalIn   QEPB  (D9);    // Quadrature encoder pushbutton
-PwmOut      PWM   (D10);   // PWM output
-DigitalOut  BUZZ  (D13);   // Buzzer
-
-AnalogIn    AIN0  (A0);    // ADC input 0 (High Voltage)
-AnalogIn    AIN1  (A1);    // ADC input 1 (aux)
-DigitalOut  CS2   (A2);    // 74HC595 RCLK (pin 12)
-DigitalOut  CS1   (A3);    // MAX7219 CS (pin 12)
-DigitalOut  SCK   (A4);    // 74HC595 SRCLK (pin 11) & MAX7219 SCK (pin 13)
-AnalogIn    KEYB  (A5);    // Keyboard input (SW2 & SW3) 
-DigitalOut  MOSI  (A6);    // 74HC595 SER (pin 14) & MAX7219 DIN (pin 1)
-DigitalIn   UN    (A7);    // Unused (in V1 PCB A5 and A7 must be connected)
-
 // LED on processor board
 DigitalOut led1(LED1);
 
 // definitions of peripherals and devices
 
-// 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
-int WheelCurrent = 0;
-int WheelPrevious = 0;
-bool QEPBpressed = false; // only react to button when pressed
-int WheelStateTimer = 0;
-const int WheelStateTimeout = 5; //timeout for wheel button pushed
-enum WheelState {
-   WHEEL_INACTIVE       = 0,
-   WHEEL_MODE_SELECT    = 1,
-   WHEEL_SUBMODE_SELECT = 2
-};
-enum WheelStateEvent {
-   WHEEL_Pressed = 0,
-   WHEEL_Timeout = 1
-};
-WheelState currentWheelState;
-
 I2C     i2c(D4, D5);             // I2C port
 Ticker  SecTenth_Beat;           // .1 second ticker
 Ticker  Sec_Beat;                // 1 second ticker
@@ -109,11 +63,9 @@
 void Count1_up(void);  // called every time an edge is detected on TRIG1 pin
 void Count2_up(void); // called every time an edge is detected on TRIG2 pin
 void Beep(void);    // used to generate a short beep (buzzer)
-void LEDs_write(unsigned short);    // write to 74HC595 (8x LEDs)
 void Display_init(void);    // initialize MAX7219
 void Display_6D_write(uint8_t);    // write to MAX7219 (Main 6-digits display)
 void Display_2D_write(unsigned short);    // write to MAX7219 (Gate 2-digits display)
-void WheelStateMachine(WheelStateEvent event);
 
 //==============================================================================
 //==============================================================================
@@ -139,13 +91,10 @@
     // RTC is supposed to be loose time at power down (no backup battery)
     // An initialization is performed anyway 
     set_time(0); // Set time
- 
-    Wheel.reset();        // clear the variable associated to the encoder    
     
     PWM.period_ms(3);    // set the PWM period
     PWM.write(0.8);      // set the PWM duty-cycle
     
-    LEDs_write(0x00);   // initialize LEDs (CPM and CNT1 on)
     Beep(); // initial beep    
     
     // set the 1 sec ticker to periodically call the Update() routine
@@ -179,24 +128,13 @@
    seconds = time(NULL);   // get current time 
    strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
    PC.printf(" RTC: %s, CNT1: %7d CNT2: %7d",Text, Count1, Count2);
-   PC.printf(" wheel %d %d", WheelCurrent, QEPB.read());
+   //PC.printf(" wheel %d %d", WheelCurrent, QEPB.read());
 }
 
 void UpdateOutput()   
 {
-    // Blink mode LED
-    if (WHEEL_MODE_SELECT == currentWheelState)
-       LEDs_write(0);
-
-   if (WheelStateTimer)
-   {
-      WheelStateTimer++;
-      if (++WheelStateTimer > WheelStateTimeout)
-      {
-         WheelStateMachine(WHEEL_Timeout);
-         WheelStateTimer = 0;
-      }
-   }
+    // This must be called periodically to update the LEDs
+    ModeSwitch.UpdateOutput();
       
    if(Stopped)
    {    
@@ -238,8 +176,9 @@
 }
 
 void UpdateInput()   
-{               
-   LEDs_write(LED_statuses[LED_status_index]);
+{
+   // This must be called periodically to monitor switch input
+   ModeSwitch.UpdateInput();
         
    ADC_val = KEYB.read();  // read voltage from keyboard
    if (   (ADC_val<0.1)    // START/STOP pushbutton pressed
@@ -256,81 +195,10 @@
       Count1 = 0; // clear counters     
       Count2 = 0;              
    }
-
-   // 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);
-      }
-    
    logToPC();
    return;
 }
 
-void 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;
-   }
-}
-
 //---------------------------------------------------------------------------
 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
 
@@ -340,7 +208,6 @@
     return; 
 }
 
-
 //---------------------------------------------------------------------------
 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
 
@@ -362,39 +229,6 @@
     return; 
 }
 
-
-//---------------------------------------------------------------------------
-//Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
-
-void LEDs_write(unsigned short data_val)       
-{                       
-    // 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; 
-}
-
-
 //---------------------------------------------------------------------------
 // Initialize the MAX7219