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 12:22:16 2018 -0700
Revision:
47:f004b4612069
Parent:
46:c7ce8fccb712
Child:
48:468220d69071
prepare to publish first fully functioning mode selection

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