Charles Young's development fork. Going forward I only want to push mature code to main repository.
Fork of GEO_COUNTER_L432KC by
main.cpp@25:0161bf1adc81, 2018-09-05 (annotated)
- Committer:
- Charles David Young
- Date:
- Wed Sep 05 10:15:52 2018 -0700
- Revision:
- 25:0161bf1adc81
- Parent:
- 24:db7494389c03
- Child:
- 28:c81d00ec28a3
Created LED7segDisplay class
Who changed what in which revision?
User | Revision | Line number | New 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 | |
walter76 | 1:75827d765e34 | 29 | // definitions of fixed parameters |
walter76 | 0:6d1742703713 | 30 | |
walter76 | 1:75827d765e34 | 31 | #define TGATE 10 // gate time (currently fixed for testing purpose) |
Charles David Young |
21:5640ebc362a0 | 32 | |
charlesdavidyoung | 5:4f90b458dbdf | 33 | // LED on processor board |
charlesdavidyoung | 5:4f90b458dbdf | 34 | DigitalOut led1(LED1); |
walter76 | 1:75827d765e34 | 35 | |
walter76 | 1:75827d765e34 | 36 | // definitions of peripherals and devices |
Charles David Young |
7:9f975e00600c | 37 | |
charlesdavidyoung | 17:6eed17197004 | 38 | I2C i2c(D4, D5); // I2C port |
charlesdavidyoung | 17:6eed17197004 | 39 | Ticker SecTenth_Beat; // .1 second ticker |
walter76 | 1:75827d765e34 | 40 | Ticker Sec_Beat; // 1 second ticker |
walter76 | 1:75827d765e34 | 41 | Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection) |
walter76 | 1:75827d765e34 | 42 | Serial GPS(D1, D0); // Serial port for GPS module |
walter76 | 0:6d1742703713 | 43 | |
walter76 | 0:6d1742703713 | 44 | // Global variables |
walter76 | 1:75827d765e34 | 45 | time_t seconds; // Real-Time Clock (RTC) timestamp |
walter76 | 1:75827d765e34 | 46 | unsigned int value = 0; // displayed value on the 6-digits of the display |
walter76 | 1:75827d765e34 | 47 | uint8_t gate = TGATE; // displayed value on the 2-digits display |
walter76 | 1:75827d765e34 | 48 | uint32_t Count1, Count2; // pulse counters (32-bit) |
walter76 | 1:75827d765e34 | 49 | char Text[40]=""; // used to send messages over the serial port |
walter76 | 1:75827d765e34 | 50 | uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs |
kd5byb | 4:b17c6556cf1f | 51 | bool Stopped = 0; // status of counting activity |
charlesdavidyoung | 5:4f90b458dbdf | 52 | bool StartStopPressed = 0;// status of counting activity |
walter76 | 1:75827d765e34 | 53 | double ADC_val; // used to read ADC value |
charlesdavidyoung | 24:db7494389c03 | 54 | float direction = 0; |
walter76 | 0:6d1742703713 | 55 | |
walter76 | 1:75827d765e34 | 56 | // ----- Prototypes of routines (defined below the main) ------------------- |
Charles David Young |
16:b2e77eb76ab4 | 57 | void UpdateInput(void); // periodically called by the ticker |
Charles David Young |
16:b2e77eb76ab4 | 58 | void UpdateOutput(void); // periodically called by the ticker |
walter76 | 1:75827d765e34 | 59 | void Count1_up(void); // called every time an edge is detected on TRIG1 pin |
walter76 | 1:75827d765e34 | 60 | void Count2_up(void); // called every time an edge is detected on TRIG2 pin |
walter76 | 1:75827d765e34 | 61 | void Beep(void); // used to generate a short beep (buzzer) |
walter76 | 0:6d1742703713 | 62 | |
walter76 | 0:6d1742703713 | 63 | //============================================================================== |
walter76 | 0:6d1742703713 | 64 | //============================================================================== |
walter76 | 0:6d1742703713 | 65 | |
walter76 | 0:6d1742703713 | 66 | int main() |
walter76 | 0:6d1742703713 | 67 | { |
walter76 | 0:6d1742703713 | 68 | |
walter76 | 1:75827d765e34 | 69 | PC.baud(115200); // set baud-rate of virtual COM port (PC connection) |
charlesdavidyoung | 5:4f90b458dbdf | 70 | PC.printf("\nGEO COUNTER V1 2108"); |
walter76 | 1:75827d765e34 | 71 | PC.printf(__DATE__); |
walter76 | 1:75827d765e34 | 72 | PC.printf(" "); |
walter76 | 1:75827d765e34 | 73 | PC.printf(__TIME__); |
walter76 | 1:75827d765e34 | 74 | |
walter76 | 2:ac0ed3d84d44 | 75 | GPS.baud(9600); // set the baud-rate of the serial port dedicated to the GPS |
walter76 | 0:6d1742703713 | 76 | |
walter76 | 1:75827d765e34 | 77 | CS1 = 1; // presets CS of MAX7219 |
walter76 | 1:75827d765e34 | 78 | CS2 = 1; // preset CS of 74HC595 |
walter76 | 1:75827d765e34 | 79 | |
Charles David Young |
25:0161bf1adc81 | 80 | DigitsDisplay.Display_6D_write(0x543210); |
Charles David Young |
25:0161bf1adc81 | 81 | DigitsDisplay.Display_2D_write(TGATE); |
walter76 | 0:6d1742703713 | 82 | |
walter76 | 1:75827d765e34 | 83 | // RTC is supposed to be loose time at power down (no backup battery) |
walter76 | 1:75827d765e34 | 84 | // An initialization is performed anyway |
walter76 | 1:75827d765e34 | 85 | set_time(0); // Set time |
charlesdavidyoung | 24:db7494389c03 | 86 | |
walter76 | 1:75827d765e34 | 87 | PWM.period_ms(3); // set the PWM period |
walter76 | 1:75827d765e34 | 88 | PWM.write(0.8); // set the PWM duty-cycle |
walter76 | 1:75827d765e34 | 89 | |
walter76 | 1:75827d765e34 | 90 | Beep(); // initial beep |
walter76 | 1:75827d765e34 | 91 | |
walter76 | 1:75827d765e34 | 92 | // set the 1 sec ticker to periodically call the Update() routine |
walter76 | 1:75827d765e34 | 93 | // NOTE: this is also the 1-sec time base for counters. A better approach |
walter76 | 1:75827d765e34 | 94 | // would replace the ticker with an interrupt from the RTC (to be implemented) |
charlesdavidyoung | 17:6eed17197004 | 95 | SecTenth_Beat.attach_us(&UpdateInput, 100000); |
Charles David Young |
16:b2e77eb76ab4 | 96 | Sec_Beat.attach_us(&UpdateOutput, 1000000); |
walter76 | 0:6d1742703713 | 97 | //RTC::attach(&Update, RTC::Second); |
walter76 | 1:75827d765e34 | 98 | //RTC::detach(RTC::Second); |
walter76 | 1:75827d765e34 | 99 | |
walter76 | 1:75827d765e34 | 100 | // main loop does nothing as all activities are interrupt driven |
walter76 | 0:6d1742703713 | 101 | while(1) |
walter76 | 0:6d1742703713 | 102 | { |
walter76 | 2:ac0ed3d84d44 | 103 | // dance (or drink a beer) |
walter76 | 0:6d1742703713 | 104 | } |
walter76 | 0:6d1742703713 | 105 | } |
walter76 | 0:6d1742703713 | 106 | |
walter76 | 0:6d1742703713 | 107 | |
walter76 | 1:75827d765e34 | 108 | //-------- END OF MAIN -------------- |
walter76 | 0:6d1742703713 | 109 | //============================================================================== |
walter76 | 0:6d1742703713 | 110 | |
walter76 | 1:75827d765e34 | 111 | // Definition of routines |
walter76 | 1:75827d765e34 | 112 | |
walter76 | 1:75827d765e34 | 113 | //--------------------------------------------------------------------------- |
walter76 | 1:75827d765e34 | 114 | // Update values to be displayed |
Charles David Young |
10:f48cc6be5ae8 | 115 | void logToPC() |
Charles David Young |
10:f48cc6be5ae8 | 116 | { |
Charles David Young |
10:f48cc6be5ae8 | 117 | PC.printf("\nADC: %.02f", ADC_val); |
Charles David Young |
10:f48cc6be5ae8 | 118 | PC.printf(Stopped ? " stopped" : " started"); |
Charles David Young |
10:f48cc6be5ae8 | 119 | // Timestamp to PC (debug) |
Charles David Young |
10:f48cc6be5ae8 | 120 | seconds = time(NULL); // get current time |
Charles David Young |
10:f48cc6be5ae8 | 121 | strftime(Text, 50, "%H:%M:%S", localtime(&seconds)); |
Charles David Young |
10:f48cc6be5ae8 | 122 | PC.printf(" RTC: %s, CNT1: %7d CNT2: %7d",Text, Count1, Count2); |
charlesdavidyoung | 24:db7494389c03 | 123 | PC.printf(" wheel %f", direction); |
Charles David Young |
10:f48cc6be5ae8 | 124 | } |
walter76 | 1:75827d765e34 | 125 | |
Charles David Young |
16:b2e77eb76ab4 | 126 | void UpdateOutput() |
charlesdavidyoung | 18:ef5678ab1889 | 127 | { |
charlesdavidyoung | 24:db7494389c03 | 128 | // This must be called periodically to update the LEDs |
charlesdavidyoung | 24:db7494389c03 | 129 | ModeSwitch.UpdateOutput(); |
charlesdavidyoung | 24:db7494389c03 | 130 | |
Charles David Young |
16:b2e77eb76ab4 | 131 | if(Stopped) |
Charles David Young |
16:b2e77eb76ab4 | 132 | { |
Charles David Young |
16:b2e77eb76ab4 | 133 | // disable interrupts on TRIG1 and TRIG2 |
Charles David Young |
16:b2e77eb76ab4 | 134 | |
Charles David Young |
16:b2e77eb76ab4 | 135 | TRIG1.rise(NULL); |
Charles David Young |
16:b2e77eb76ab4 | 136 | TRIG2.rise(NULL); |
Charles David Young |
16:b2e77eb76ab4 | 137 | |
Charles David Young |
16:b2e77eb76ab4 | 138 | // show zero gate time |
Charles David Young |
16:b2e77eb76ab4 | 139 | gate = 0; |
Charles David Young |
25:0161bf1adc81 | 140 | DigitsDisplay.Display_2D_write(gate); |
Charles David Young |
16:b2e77eb76ab4 | 141 | |
Charles David Young |
16:b2e77eb76ab4 | 142 | // show selected content on main display |
Charles David Young |
25:0161bf1adc81 | 143 | DigitsDisplay.Display_6D_write(Count1); // refresh the main display |
Charles David Young |
16:b2e77eb76ab4 | 144 | } |
Charles David Young |
16:b2e77eb76ab4 | 145 | |
Charles David Young |
16:b2e77eb76ab4 | 146 | else |
Charles David Young |
16:b2e77eb76ab4 | 147 | { |
Charles David Young |
16:b2e77eb76ab4 | 148 | // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2 |
Charles David Young |
16:b2e77eb76ab4 | 149 | TRIG1.rise(&Count1_up); |
Charles David Young |
16:b2e77eb76ab4 | 150 | TRIG2.rise(&Count2_up); |
Charles David Young |
16:b2e77eb76ab4 | 151 | |
Charles David Young |
16:b2e77eb76ab4 | 152 | if(gate==0) // show the counter value at the end of the gate time |
Charles David Young |
16:b2e77eb76ab4 | 153 | { |
Charles David Young |
25:0161bf1adc81 | 154 | DigitsDisplay.Display_6D_write(Count1); // refresh the main display |
Charles David Young |
7:9f975e00600c | 155 | |
Charles David Young |
16:b2e77eb76ab4 | 156 | Count1 = 0; // clear both counters |
Charles David Young |
16:b2e77eb76ab4 | 157 | Count2 = 0; |
Charles David Young |
16:b2e77eb76ab4 | 158 | gate = TGATE;// and reload the gate time |
Charles David Young |
16:b2e77eb76ab4 | 159 | } |
Charles David Young |
16:b2e77eb76ab4 | 160 | |
Charles David Young |
25:0161bf1adc81 | 161 | DigitsDisplay.Display_2D_write(gate); // show gate time countdown |
Charles David Young |
16:b2e77eb76ab4 | 162 | gate--; |
Charles David Young |
16:b2e77eb76ab4 | 163 | } |
Charles David Young |
16:b2e77eb76ab4 | 164 | } |
Charles David Young |
16:b2e77eb76ab4 | 165 | |
Charles David Young |
16:b2e77eb76ab4 | 166 | void UpdateInput() |
charlesdavidyoung | 24:db7494389c03 | 167 | { |
charlesdavidyoung | 24:db7494389c03 | 168 | // This must be called periodically to monitor switch input |
charlesdavidyoung | 24:db7494389c03 | 169 | direction = ModeSwitch.UpdateInput(); |
Charles David Young |
25:0161bf1adc81 | 170 | if (direction > 0) |
charlesdavidyoung | 24:db7494389c03 | 171 | { |
Charles David Young |
25:0161bf1adc81 | 172 | DigitsDisplay.Display_brightness_up(); |
charlesdavidyoung | 24:db7494389c03 | 173 | } |
charlesdavidyoung | 24:db7494389c03 | 174 | else |
Charles David Young |
25:0161bf1adc81 | 175 | if (direction < 0) |
charlesdavidyoung | 24:db7494389c03 | 176 | { |
Charles David Young |
25:0161bf1adc81 | 177 | DigitsDisplay.Display_brightness_down(); |
charlesdavidyoung | 24:db7494389c03 | 178 | } |
charlesdavidyoung | 24:db7494389c03 | 179 | |
Charles David Young |
16:b2e77eb76ab4 | 180 | ADC_val = KEYB.read(); // read voltage from keyboard |
Charles David Young |
16:b2e77eb76ab4 | 181 | if ( (ADC_val<0.1) // START/STOP pushbutton pressed |
Charles David Young |
16:b2e77eb76ab4 | 182 | && (!StartStopPressed)) |
Charles David Young |
16:b2e77eb76ab4 | 183 | { |
Charles David Young |
16:b2e77eb76ab4 | 184 | StartStopPressed = true; |
Charles David Young |
16:b2e77eb76ab4 | 185 | Stopped=!Stopped; // toggle status |
Charles David Young |
16:b2e77eb76ab4 | 186 | } |
Charles David Young |
16:b2e77eb76ab4 | 187 | else |
Charles David Young |
16:b2e77eb76ab4 | 188 | StartStopPressed = false; |
charlesdavidyoung | 5:4f90b458dbdf | 189 | |
Charles David Young |
16:b2e77eb76ab4 | 190 | if((ADC_val>0.6)&&(ADC_val<0.7)) // CLEAR pushbutton pressed |
Charles David Young |
16:b2e77eb76ab4 | 191 | { |
Charles David Young |
16:b2e77eb76ab4 | 192 | Count1 = 0; // clear counters |
Charles David Young |
16:b2e77eb76ab4 | 193 | Count2 = 0; |
charlesdavidyoung | 5:4f90b458dbdf | 194 | } |
Charles David Young |
16:b2e77eb76ab4 | 195 | logToPC(); |
Charles David Young |
16:b2e77eb76ab4 | 196 | return; |
Charles David Young |
16:b2e77eb76ab4 | 197 | } |
walter76 | 1:75827d765e34 | 198 | |
walter76 | 0:6d1742703713 | 199 | //--------------------------------------------------------------------------- |
walter76 | 1:75827d765e34 | 200 | // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt) |
walter76 | 1:75827d765e34 | 201 | |
walter76 | 1:75827d765e34 | 202 | void Count1_up(void) |
walter76 | 1:75827d765e34 | 203 | { |
walter76 | 1:75827d765e34 | 204 | Count1++; |
walter76 | 1:75827d765e34 | 205 | return; |
walter76 | 1:75827d765e34 | 206 | } |
walter76 | 1:75827d765e34 | 207 | |
walter76 | 1:75827d765e34 | 208 | //--------------------------------------------------------------------------- |
walter76 | 1:75827d765e34 | 209 | // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt) |
walter76 | 1:75827d765e34 | 210 | |
walter76 | 1:75827d765e34 | 211 | void Count2_up(void) |
walter76 | 1:75827d765e34 | 212 | { |
walter76 | 1:75827d765e34 | 213 | Count2++; |
walter76 | 1:75827d765e34 | 214 | return; |
walter76 | 1:75827d765e34 | 215 | } |
walter76 | 1:75827d765e34 | 216 | |
walter76 | 1:75827d765e34 | 217 | |
walter76 | 1:75827d765e34 | 218 | //--------------------------------------------------------------------------- |
walter76 | 1:75827d765e34 | 219 | //Generates a short beep via BUZZ |
walter76 | 1:75827d765e34 | 220 | |
walter76 | 1:75827d765e34 | 221 | void Beep(void) |
walter76 | 1:75827d765e34 | 222 | { |
walter76 | 1:75827d765e34 | 223 | BUZZ = 1; // turn-on the buzzer |
walter76 | 1:75827d765e34 | 224 | wait(0.3); // wait |
walter76 | 1:75827d765e34 | 225 | BUZZ = 0; // turn-off the buzzer |
walter76 | 0:6d1742703713 | 226 | return; |
walter76 | 0:6d1742703713 | 227 | } |
walter76 | 0:6d1742703713 | 228 | |
walter76 | 1:75827d765e34 | 229 | //-------- END OF FILE -------------- |
walter76 | 1:75827d765e34 | 230 | //============================================================================== |