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"

Committer:
Charles David Young
Date:
Thu Sep 06 10:03:01 2018 -0700
Revision:
38:1642320d83a0
Parent:
37:6f7bb913e08b
Child:
39:dee91966b45c
Fully implement mode select.  Need to debug.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
walter76 2:ac0ed3d84d44 1 // GEO COUNTER V1 firmware
walter76 2:ac0ed3d84d44 2 // This FW provides a basic operation of GEO-COUNTER
walter76 2:ac0ed3d84d44 3 //
walter76 2:ac0ed3d84d44 4 // Latest review: August 27, 2018 - Walter Trovo
walter76 1:75827d765e34 5 //
Charles David Young 25:0161bf1adc81 6 // Sep 5, 2018: Charles Young: Created LED7segDisplay class
charlesdavidyoung 24:db7494389c03 7 // Sep 5, 2018: Charles Young: Still developing mode selection. LEDs turn off
charlesdavidyoung 24:db7494389c03 8 // when not in mode selection. Pressing once enters mode selection.
charlesdavidyoung 24:db7494389c03 9 // Pressing again enters submode. Temporarily the rotary switch
charlesdavidyoung 24:db7494389c03 10 // adjusts the display brightness. This is just for testing.
charlesdavidyoung 24:db7494389c03 11 // Sep 4, 2018: Charles Young: Created RotarySwitch class to manage mode selection
walter76 1:75827d765e34 12 // Feb 14, 2018: initial release aimed to test the counters, the serial port
walter76 1:75827d765e34 13 // the PWM output and the MAX7219 operation.
walter76 1:75827d765e34 14 // Feb 15, 2018: Removed MAX7219 libray (replaced with custom routine).
walter76 1:75827d765e34 15 // Added 74HC595 routine. Added beep. Added Gate_write
walter76 1:75827d765e34 16 //
walter76 2:ac0ed3d84d44 17
walter76 1:75827d765e34 18
walter76 1:75827d765e34 19 // this block includes key libraries
walter76 1:75827d765e34 20 #include <string> // strings management
charlesdavidyoung 24:db7494389c03 21 #include "RotarySwitch.hpp"
Charles David Young 25:0161bf1adc81 22 #include "LED7segDisplay.hpp"
charlesdavidyoung 20:fb73eaaf0894 23
charlesdavidyoung 24:db7494389c03 24 // Everything associated with the rotary switch and the associated LEDs
charlesdavidyoung 24:db7494389c03 25 // is hidden in this class.
charlesdavidyoung 20:fb73eaaf0894 26 RotarySwitch ModeSwitch;
Charles David Young 25:0161bf1adc81 27 LED7segDisplay DigitsDisplay;
walter76 1:75827d765e34 28
Charles David Young 34:10550b327e3d 29 enum Modes {
Charles David Young 34:10550b327e3d 30 CNT1,
Charles David Young 34:10550b327e3d 31 CNT2,
Charles David Young 34:10550b327e3d 32 PROSPECT,
Charles David Young 34:10550b327e3d 33 NULL1,
Charles David Young 37:6f7bb913e08b 34 NULL2,
Charles David Young 38:1642320d83a0 35 VOLTS,
Charles David Young 38:1642320d83a0 36 VOLUME,
Charles David Young 34:10550b327e3d 37 DIM,
Charles David Young 34:10550b327e3d 38 NumberOfModes
Charles David Young 34:10550b327e3d 39 };
Charles David Young 38:1642320d83a0 40 uint8_t currentMode = CNT1;
Charles David Young 38:1642320d83a0 41 uint8_t currentModeToDisplay = CNT1;
Charles David Young 34:10550b327e3d 42
charlesdavidyoung 5:4f90b458dbdf 43 // LED on processor board
charlesdavidyoung 5:4f90b458dbdf 44 DigitalOut led1(LED1);
walter76 1:75827d765e34 45
walter76 1:75827d765e34 46 // definitions of peripherals and devices
Charles David Young 7:9f975e00600c 47
charlesdavidyoung 17:6eed17197004 48 I2C i2c(D4, D5); // I2C port
charlesdavidyoung 17:6eed17197004 49 Ticker SecTenth_Beat; // .1 second ticker
walter76 1:75827d765e34 50 Ticker Sec_Beat; // 1 second ticker
walter76 1:75827d765e34 51 Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection)
walter76 1:75827d765e34 52 Serial GPS(D1, D0); // Serial port for GPS module
walter76 0:6d1742703713 53
walter76 0:6d1742703713 54 // Global variables
walter76 1:75827d765e34 55 time_t seconds; // Real-Time Clock (RTC) timestamp
walter76 1:75827d765e34 56 unsigned int value = 0; // displayed value on the 6-digits of the display
walter76 1:75827d765e34 57 uint32_t Count1, Count2; // pulse counters (32-bit)
walter76 1:75827d765e34 58 char Text[40]=""; // used to send messages over the serial port
walter76 1:75827d765e34 59 uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs
kd5byb 4:b17c6556cf1f 60 bool Stopped = 0; // status of counting activity
charlesdavidyoung 5:4f90b458dbdf 61 bool StartStopPressed = 0;// status of counting activity
walter76 1:75827d765e34 62 double ADC_val; // used to read ADC value
charlesdavidyoung 24:db7494389c03 63 float direction = 0;
walter76 0:6d1742703713 64
Charles David Young 38:1642320d83a0 65 #define maxVolume 10
Charles David Young 38:1642320d83a0 66 uint8_t volume = maxVolume;
Charles David Young 38:1642320d83a0 67
walter76 1:75827d765e34 68 // ----- Prototypes of routines (defined below the main) -------------------
Charles David Young 16:b2e77eb76ab4 69 void UpdateInput(void); // periodically called by the ticker
Charles David Young 16:b2e77eb76ab4 70 void UpdateOutput(void); // periodically called by the ticker
walter76 1:75827d765e34 71 void Count1_up(void); // called every time an edge is detected on TRIG1 pin
walter76 1:75827d765e34 72 void Count2_up(void); // called every time an edge is detected on TRIG2 pin
walter76 1:75827d765e34 73 void Beep(void); // used to generate a short beep (buzzer)
walter76 0:6d1742703713 74
walter76 0:6d1742703713 75 //==============================================================================
walter76 0:6d1742703713 76 //==============================================================================
walter76 0:6d1742703713 77
walter76 0:6d1742703713 78 int main()
walter76 0:6d1742703713 79 {
walter76 0:6d1742703713 80
Charles David Young 28:c81d00ec28a3 81 PC.baud(115200); // set baud-rate of virtual COM port (PC connection)
Charles David Young 28:c81d00ec28a3 82 PC.printf("\nGEO COUNTER V1 2108");
Charles David Young 28:c81d00ec28a3 83 PC.printf(__DATE__);
Charles David Young 28:c81d00ec28a3 84 PC.printf(" ");
Charles David Young 28:c81d00ec28a3 85 PC.printf(__TIME__);
walter76 1:75827d765e34 86
Charles David Young 28:c81d00ec28a3 87 GPS.baud(9600); // set the baud-rate of the serial port dedicated to the GPS
walter76 0:6d1742703713 88
Charles David Young 28:c81d00ec28a3 89 CS1 = 1; // presets CS of MAX7219
Charles David Young 28:c81d00ec28a3 90 CS2 = 1; // preset CS of 74HC595
walter76 1:75827d765e34 91
Charles David Young 28:c81d00ec28a3 92 DigitsDisplay.Display_6D_write(0x543210);
Charles David Young 28:c81d00ec28a3 93 DigitsDisplay.Display_2D_write(0);
walter76 0:6d1742703713 94
Charles David Young 28:c81d00ec28a3 95 // RTC is supposed to be loose time at power down (no backup battery)
Charles David Young 28:c81d00ec28a3 96 // An initialization is performed anyway
Charles David Young 28:c81d00ec28a3 97 set_time(0); // Set time
charlesdavidyoung 24:db7494389c03 98
Charles David Young 28:c81d00ec28a3 99 PWM.period_ms(3); // set the PWM period
Charles David Young 28:c81d00ec28a3 100 PWM.write(0.8); // set the PWM duty-cycle
walter76 1:75827d765e34 101
Charles David Young 28:c81d00ec28a3 102 Beep(); // initial beep
walter76 1:75827d765e34 103
Charles David Young 28:c81d00ec28a3 104 // set the 1 sec ticker to periodically call the Update() routine
Charles David Young 28:c81d00ec28a3 105 // NOTE: this is also the 1-sec time base for counters. A better approach
Charles David Young 28:c81d00ec28a3 106 // would replace the ticker with an interrupt from the RTC (to be implemented)
Charles David Young 28:c81d00ec28a3 107 SecTenth_Beat.attach_us(&UpdateInput, 100000);
Charles David Young 28:c81d00ec28a3 108 Sec_Beat.attach_us(&UpdateOutput, 1000000);
Charles David Young 28:c81d00ec28a3 109 //RTC::attach(&Update, RTC::Second);
Charles David Young 28:c81d00ec28a3 110 //RTC::detach(RTC::Second);
walter76 1:75827d765e34 111
Charles David Young 28:c81d00ec28a3 112 // main loop does nothing as all activities are interrupt driven
Charles David Young 28:c81d00ec28a3 113 while(1)
Charles David Young 28:c81d00ec28a3 114 {
Charles David Young 28:c81d00ec28a3 115 // dance (or drink a beer)
Charles David Young 28:c81d00ec28a3 116 }
walter76 0:6d1742703713 117 }
walter76 0:6d1742703713 118
walter76 0:6d1742703713 119
walter76 1:75827d765e34 120 //-------- END OF MAIN --------------
walter76 0:6d1742703713 121 //==============================================================================
walter76 0:6d1742703713 122
walter76 1:75827d765e34 123 // Definition of routines
walter76 1:75827d765e34 124
walter76 1:75827d765e34 125 //---------------------------------------------------------------------------
walter76 1:75827d765e34 126 // Update values to be displayed
Charles David Young 10:f48cc6be5ae8 127 void logToPC()
Charles David Young 10:f48cc6be5ae8 128 {
Charles David Young 10:f48cc6be5ae8 129 PC.printf("\nADC: %.02f", ADC_val);
Charles David Young 10:f48cc6be5ae8 130 PC.printf(Stopped ? " stopped" : " started");
Charles David Young 10:f48cc6be5ae8 131 // Timestamp to PC (debug)
Charles David Young 10:f48cc6be5ae8 132 seconds = time(NULL); // get current time
Charles David Young 10:f48cc6be5ae8 133 strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
Charles David Young 10:f48cc6be5ae8 134 PC.printf(" RTC: %s, CNT1: %7d CNT2: %7d",Text, Count1, Count2);
charlesdavidyoung 24:db7494389c03 135 PC.printf(" wheel %f", direction);
Charles David Young 10:f48cc6be5ae8 136 }
walter76 1:75827d765e34 137
Charles David Young 16:b2e77eb76ab4 138 void UpdateOutput()
charlesdavidyoung 18:ef5678ab1889 139 {
Charles David Young 28:c81d00ec28a3 140 // This must be called periodically to update the LEDs
Charles David Young 28:c81d00ec28a3 141 ModeSwitch.UpdateOutput();
charlesdavidyoung 24:db7494389c03 142
Charles David Young 38:1642320d83a0 143 switch (currentModeToDisplay) {
Charles David Young 34:10550b327e3d 144 case CNT1:
Charles David Young 34:10550b327e3d 145 DigitsDisplay.Display_6D_write(Count1);
Charles David Young 38:1642320d83a0 146 // DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 147 break;
Charles David Young 34:10550b327e3d 148 case CNT2:
Charles David Young 34:10550b327e3d 149 DigitsDisplay.Display_6D_write(Count2);
Charles David Young 38:1642320d83a0 150 // DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 151 break;
Charles David Young 34:10550b327e3d 152 case PROSPECT:
Charles David Young 34:10550b327e3d 153 DigitsDisplay.Display_6D_write(Count1);
Charles David Young 38:1642320d83a0 154 // DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 155 break;
Charles David Young 34:10550b327e3d 156 case NULL1:
Charles David Young 34:10550b327e3d 157 DigitsDisplay.Display_6D_write(0);
Charles David Young 38:1642320d83a0 158 // DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 159 break;
Charles David Young 37:6f7bb913e08b 160 case NULL2:
Charles David Young 34:10550b327e3d 161 DigitsDisplay.Display_6D_write(0);
Charles David Young 38:1642320d83a0 162 // DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 163 break;
Charles David Young 38:1642320d83a0 164 case VOLTS:
Charles David Young 34:10550b327e3d 165 DigitsDisplay.Display_6D_write(0);
Charles David Young 38:1642320d83a0 166 // DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 167 break;
Charles David Young 38:1642320d83a0 168 case VOLUME:
Charles David Young 34:10550b327e3d 169 case DIM:
Charles David Young 34:10550b327e3d 170 default:
Charles David Young 34:10550b327e3d 171 break;
Charles David Young 34:10550b327e3d 172 }
Charles David Young 34:10550b327e3d 173
Charles David Young 16:b2e77eb76ab4 174 if(Stopped)
Charles David Young 16:b2e77eb76ab4 175 {
Charles David Young 16:b2e77eb76ab4 176 // disable interrupts on TRIG1 and TRIG2
Charles David Young 16:b2e77eb76ab4 177 TRIG1.rise(NULL);
Charles David Young 16:b2e77eb76ab4 178 TRIG2.rise(NULL);
Charles David Young 16:b2e77eb76ab4 179 }
Charles David Young 16:b2e77eb76ab4 180 else
Charles David Young 16:b2e77eb76ab4 181 {
Charles David Young 34:10550b327e3d 182 // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2
Charles David Young 34:10550b327e3d 183 TRIG1.rise(&Count1_up);
Charles David Young 34:10550b327e3d 184 TRIG2.rise(&Count2_up);
Charles David Young 7:9f975e00600c 185
Charles David Young 28:c81d00ec28a3 186 Count1 = 0; // clear both counters
Charles David Young 28:c81d00ec28a3 187 Count2 = 0;
Charles David Young 16:b2e77eb76ab4 188 }
Charles David Young 16:b2e77eb76ab4 189 }
Charles David Young 16:b2e77eb76ab4 190
Charles David Young 16:b2e77eb76ab4 191 void UpdateInput()
charlesdavidyoung 24:db7494389c03 192 {
charlesdavidyoung 24:db7494389c03 193 // This must be called periodically to monitor switch input
Charles David Young 38:1642320d83a0 194 direction = ModeSwitch.UpdateInput();
Charles David Young 34:10550b327e3d 195 currentMode = ModeSwitch.GetPosition();
Charles David Young 34:10550b327e3d 196
Charles David Young 34:10550b327e3d 197 switch (currentMode) {
Charles David Young 34:10550b327e3d 198 case CNT1:
Charles David Young 34:10550b327e3d 199 case CNT2:
Charles David Young 34:10550b327e3d 200 case PROSPECT:
Charles David Young 34:10550b327e3d 201 case VOLTS:
Charles David Young 38:1642320d83a0 202 currentModeToDisplay = currentMode;
Charles David Young 34:10550b327e3d 203 break;
Charles David Young 34:10550b327e3d 204 case NULL1:
Charles David Young 37:6f7bb913e08b 205 case NULL2:
Charles David Young 34:10550b327e3d 206 break;
Charles David Young 38:1642320d83a0 207 case VOLUME:
Charles David Young 38:1642320d83a0 208 if (!isnan(direction))
Charles David Young 38:1642320d83a0 209 {
Charles David Young 38:1642320d83a0 210 if (direction > 0)
Charles David Young 38:1642320d83a0 211 (volume < maxVolume ? volume++:);
Charles David Young 38:1642320d83a0 212 else
Charles David Young 38:1642320d83a0 213 if (direction < 0)
Charles David Young 38:1642320d83a0 214 (volume > 0 ? volume--:);
Charles David Young 38:1642320d83a0 215 }
Charles David Young 38:1642320d83a0 216 DigitsDisplay.Display_2D_write(volume);
Charles David Young 34:10550b327e3d 217 break;
Charles David Young 34:10550b327e3d 218 case DIM:
Charles David Young 38:1642320d83a0 219 if (!isnan(direction))
Charles David Young 34:10550b327e3d 220 {
Charles David Young 38:1642320d83a0 221 if (direction > 0)
Charles David Young 38:1642320d83a0 222 DigitsDisplay.Display_brightness_up();
Charles David Young 38:1642320d83a0 223 else
Charles David Young 38:1642320d83a0 224 if (direction < 0)
Charles David Young 38:1642320d83a0 225 DigitsDisplay.Display_brightness_down();
Charles David Young 34:10550b327e3d 226 }
Charles David Young 38:1642320d83a0 227 DigitsDisplay.Display_2D_write(DigitsDisplay.GetBrightness());
Charles David Young 34:10550b327e3d 228 break;
Charles David Young 34:10550b327e3d 229 default:
Charles David Young 34:10550b327e3d 230 break;
charlesdavidyoung 24:db7494389c03 231 }
Charles David Young 34:10550b327e3d 232
Charles David Young 16:b2e77eb76ab4 233 ADC_val = KEYB.read(); // read voltage from keyboard
Charles David Young 16:b2e77eb76ab4 234 if ( (ADC_val<0.1) // START/STOP pushbutton pressed
Charles David Young 16:b2e77eb76ab4 235 && (!StartStopPressed))
Charles David Young 16:b2e77eb76ab4 236 {
Charles David Young 16:b2e77eb76ab4 237 StartStopPressed = true;
Charles David Young 16:b2e77eb76ab4 238 Stopped=!Stopped; // toggle status
Charles David Young 16:b2e77eb76ab4 239 }
Charles David Young 16:b2e77eb76ab4 240 else
Charles David Young 16:b2e77eb76ab4 241 StartStopPressed = false;
charlesdavidyoung 5:4f90b458dbdf 242
Charles David Young 16:b2e77eb76ab4 243 if((ADC_val>0.6)&&(ADC_val<0.7)) // CLEAR pushbutton pressed
Charles David Young 16:b2e77eb76ab4 244 {
Charles David Young 16:b2e77eb76ab4 245 Count1 = 0; // clear counters
Charles David Young 16:b2e77eb76ab4 246 Count2 = 0;
charlesdavidyoung 5:4f90b458dbdf 247 }
Charles David Young 16:b2e77eb76ab4 248 logToPC();
Charles David Young 16:b2e77eb76ab4 249 return;
Charles David Young 16:b2e77eb76ab4 250 }
walter76 1:75827d765e34 251
walter76 0:6d1742703713 252 //---------------------------------------------------------------------------
walter76 1:75827d765e34 253 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
walter76 1:75827d765e34 254
walter76 1:75827d765e34 255 void Count1_up(void)
walter76 1:75827d765e34 256 {
Charles David Young 33:35e3ab4d5ba7 257 //Beep();
Charles David Young 28:c81d00ec28a3 258 Count1++;
Charles David Young 28:c81d00ec28a3 259 return;
walter76 1:75827d765e34 260 }
walter76 1:75827d765e34 261
walter76 1:75827d765e34 262 //---------------------------------------------------------------------------
walter76 1:75827d765e34 263 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
walter76 1:75827d765e34 264
walter76 1:75827d765e34 265 void Count2_up(void)
walter76 1:75827d765e34 266 {
Charles David Young 33:35e3ab4d5ba7 267 //Beep();
Charles David Young 28:c81d00ec28a3 268 Count2++;
Charles David Young 28:c81d00ec28a3 269 return;
walter76 1:75827d765e34 270 }
walter76 1:75827d765e34 271
walter76 1:75827d765e34 272
walter76 1:75827d765e34 273 //---------------------------------------------------------------------------
walter76 1:75827d765e34 274 //Generates a short beep via BUZZ
walter76 1:75827d765e34 275
walter76 1:75827d765e34 276 void Beep(void)
walter76 1:75827d765e34 277 {
Charles David Young 28:c81d00ec28a3 278 BUZZ = 1; // turn-on the buzzer
Charles David Young 33:35e3ab4d5ba7 279 wait_us(100); // wait
Charles David Young 28:c81d00ec28a3 280 BUZZ = 0; // turn-off the buzzer
Charles David Young 28:c81d00ec28a3 281 return;
walter76 0:6d1742703713 282 }
walter76 0:6d1742703713 283
walter76 1:75827d765e34 284 //-------- END OF FILE --------------
walter76 1:75827d765e34 285 //==============================================================================