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:
24:db7494389c03
Parent:
21:5640ebc362a0
Child:
25:0161bf1adc81
--- a/main.cpp	Tue Sep 04 17:51:33 2018 -0700
+++ b/main.cpp	Wed Sep 05 16:05:07 2018 +0000
@@ -3,6 +3,11 @@
 //
 //  Latest review: August 27, 2018 - Walter Trovo
 //
+//  Sep  5, 2018: Charles Young: Still developing mode selection.  LEDs turn off
+//                when not in mode selection.  Pressing once enters mode selection.
+//                Pressing again enters submode.  Temporarily the rotary switch
+//                adjusts the display brightness.  This is just for testing.
+//  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,16 +16,16 @@
 
 
 // this block includes key libraries
-#include "mbed.h"       // global Mbed library (always needed)
 #include <string>       // strings management
+#include "RotarySwitch.hpp"
 
-#include <RotarySwitch.hpp>
+// Everything associated with the rotary switch and the associated LEDs
+// is hidden in this class.
 RotarySwitch ModeSwitch;
 
 // definitions of fixed parameters
 
 #define DEC_MODE    0x09FF  // BCD decoding on all digits
-#define BRIGHTNESS  0x0A0F  // max brightness
 #define SCAN_LIM    0x0B07  // use all 8 digits  
 #define TURN_ON     0x0C01  // no shutdown (operating)
 #define SHUTDOWN    0x0C00  // shutdown
@@ -31,21 +36,9 @@
 #define TGATE   10       // gate time (currently fixed for testing purpose)
 #define MAX_VAL 999999  // Max value managed by the 6-digits display
 
-// 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
-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)
+#define BRIGHTNESS_MIN  0x0A00  // brightness in 16 steps - min to max is 0x0A00 to  0x0A0F
+#define BRIGHTNESS_MAX  0x0A0F  // brightness in 16 steps - min to max is 0x0A00 to  0x0A0F
+uint16_t brightness = BRIGHTNESS_MAX;
 
 // LED on processor board
 DigitalOut led1(LED1);
@@ -70,6 +63,7 @@
 bool        Stopped = 0;        // status of counting activity
 bool        StartStopPressed = 0;// status of counting activity
 double      ADC_val;    // used to read ADC value
+float       direction = 0;
 
 // ----- Prototypes of routines (defined below the main) -------------------
 void UpdateInput(void);  // periodically called by the ticker
@@ -80,6 +74,8 @@
 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 sendDataToDisplay(uint16_t data_to_send);
+void Display_brightness(uint16_t brightness);
 
 //==============================================================================
 //==============================================================================
@@ -105,7 +101,7 @@
     // RTC is supposed to be loose time at power down (no backup battery)
     // An initialization is performed anyway 
     set_time(0); // Set time
- 
+    
     PWM.period_ms(3);    // set the PWM period
     PWM.write(0.8);      // set the PWM duty-cycle
     
@@ -142,11 +138,14 @@
    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 %f", direction);
 }
 
 void UpdateOutput()   
 {
+    // This must be called periodically to update the LEDs
+    ModeSwitch.UpdateOutput();
+      
    if(Stopped)
    {    
       // disable interrupts on TRIG1 and TRIG2
@@ -187,7 +186,21 @@
 }
 
 void UpdateInput()   
-{               
+{
+   // This must be called periodically to monitor switch input
+   direction = ModeSwitch.UpdateInput();
+   if (   (direction > 0)
+       && (brightness < BRIGHTNESS_MAX))
+   {
+      Display_brightness(++brightness);
+   }
+   else
+      if (   (direction < 0)
+          && (brightness > BRIGHTNESS_MIN))
+   {
+       Display_brightness(--brightness);
+   }
+        
    ADC_val = KEYB.read();  // read voltage from keyboard
    if (   (ADC_val<0.1)    // START/STOP pushbutton pressed
        && (!StartStopPressed))
@@ -203,7 +216,6 @@
       Count1 = 0; // clear counters     
       Count2 = 0;              
    }
-    
    logToPC();
    return;
 }
@@ -217,7 +229,6 @@
     return; 
 }
 
-
 //---------------------------------------------------------------------------
 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
 
@@ -239,41 +250,26 @@
     return; 
 }
 
-
 //---------------------------------------------------------------------------
 // Initialize the MAX7219
 
 void Display_init(void)
 {
     uint8_t i;
-    uint16_t mask;
-    uint16_t data_to_send[6] = {SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
-             //{SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
-    for(i = 0; i <6; i++)
+    uint16_t data_to_send[6] = {SHUTDOWN, TURN_ON, DEC_MODE, brightness, SCAN_LIM, TEST};
+
+    for(i = 0; i < sizeof(data_to_send)/sizeof(uint16_t); i++)
     {
-        CS1 = 0;
-      
-        for(mask = 0x8000; mask!= 0; mask>>= 1)
-        {
-            wait_us(DT);
-            SCK = 0;
-            if(mask & data_to_send[i])
-                MOSI = 1;
-            else
-                MOSI = 0;
-            wait_us(DT);    
-            SCK = 1;
-        }
-        
-        wait_us(DT);
-        SCK = 0;
-        wait_us(DT);
-        CS1 = 1;
+       sendDataToDisplay(data_to_send[i]);
     }
     
   return;
 }
 
+void Display_brightness(uint16_t brightness)
+{
+   sendDataToDisplay(brightness);
+}
 
 //---------------------------------------------------------------------------
 // Refresh the 6 digits of the main display
@@ -282,7 +278,7 @@
 {
  
     uint8_t digit;
-    uint16_t mask, data_to_send;
+    uint16_t data_to_send;
     char TextString[6];
     
     // int to string, then string to digits
@@ -308,27 +304,8 @@
         data_to_send = 7 - digit;
         data_to_send<<=8;
         data_to_send = data_to_send | Disp_Digit[digit-1];
-        
-        CS1 = 0;
-        
-        for(mask = 0x8000; mask!= 0; mask>>= 1)
-        {
-            wait_us(DT);
-            SCK = 0;
-            if(mask & data_to_send)
-                MOSI = 1;
-            else
-                MOSI = 0;
-                
-            wait_us(DT);
-            SCK = 1;
-        }
-        
-        wait_us(DT);    
-        SCK = 0;
-        wait_us(DT);
-        CS1 = 1;
-    }
+        sendDataToDisplay(data_to_send);
+   }
     
   return;
 }
@@ -341,7 +318,7 @@
 {
  
     uint8_t digit;
-    uint16_t mask, data_to_send;
+    uint16_t data_to_send;
     char TextString[2];
 
     // int to string, then string to digits
@@ -366,14 +343,19 @@
         data_to_send = digit;
         data_to_send<<=8;
         data_to_send = data_to_send | Disp_Digit[digit-1];
-        
+        sendDataToDisplay(data_to_send);
+    }
+    
+  return;
+}
+
+void sendDataToDisplay(uint16_t data_to_send)
+{
         CS1 = 0;
-        
-        for(mask = 0x8000; mask!= 0; mask>>= 1)
+        for(uint16_t mask = 0x8000; mask!= 0; mask>>= 1)
         {
             wait_us(DT);
             SCK = 0;
-            
             if(mask & data_to_send)
                 MOSI = 1;
             else
@@ -387,13 +369,7 @@
         SCK = 0;
         wait_us(DT);
         CS1 = 1;
-    }
-    
-  return;
 }
 
 //-------- END OF FILE --------------
 //==============================================================================
-
-
-