CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
2:1f78675feba0
debug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charles David Young 3:c547dba5d39b 1 #include "./mbed/mbed.h"
Charles David Young 2:1f78675feba0 2 #include "SDFileSystem.h"
Charles David Young 2:1f78675feba0 3 #include "ST7565_LCD.h"
Charles David Young 2:1f78675feba0 4 #include "QEI.h"
Charles David Young 2:1f78675feba0 5
Charles David Young 2:1f78675feba0 6 #define BAT_GAIN 5.0
Charles David Young 2:1f78675feba0 7 #define BKL_TH_OFF 0.83 // Ambient ligh threshold for auto backlight off
Charles David Young 2:1f78675feba0 8 #define BKL_TH_ON 0.81 // Ambient ligh threshold for auto backlight on
Charles David Young 2:1f78675feba0 9 #define BKL_LOW 0.0 // Ambient ligh offset for auto backlight
Charles David Young 2:1f78675feba0 10 #define FRM_ROW1 10 // Background frame, first horizontal line
Charles David Young 2:1f78675feba0 11 #define FRM_ROW2 60
Charles David Young 2:1f78675feba0 12 #define BUZZ_VOL 0.1 // Buzzer/speaker volume
Charles David Young 2:1f78675feba0 13
Charles David Young 2:1f78675feba0 14
Charles David Young 2:1f78675feba0 15 PwmOut BEEP (D2); // PA_10 Buzzer/speaker (PWM output)
Charles David Young 2:1f78675feba0 16 PwmOut BKL (D3); // PB_3 LCD backlight control (PMW output)
Charles David Young 2:1f78675feba0 17 DigitalOut KAL (PC_8); // PC_8 Keep-Alive/turn-off
Charles David Young 2:1f78675feba0 18 DigitalOut BTC (PC_4); // PC_4 Aux BT module control
Charles David Young 2:1f78675feba0 19 DigitalIn Button (D4); // PB_5 Pushbutton (digital input)
Charles David Young 2:1f78675feba0 20 AnalogIn BATT (A0); // PA_0 Battery monitor
Charles David Young 2:1f78675feba0 21 AnalogIn ALS (A1); // PA_1 Ambient Light sensor
Charles David Young 2:1f78675feba0 22 AnalogIn VCH1 (A2); // PA_4 Analog input 1
Charles David Young 2:1f78675feba0 23 AnalogIn VCH2 (A3); // PB_0 Analog input 2
Charles David Young 2:1f78675feba0 24 AnalogIn VCH3 (A4); // PC_1 Analog input 3
Charles David Young 2:1f78675feba0 25 AnalogIn VCH4 (A5); // PC_0 Analog input 4
Charles David Young 2:1f78675feba0 26 InterruptIn TRIG1 (PC_13); // PC_13 Counter 1 trigger
Charles David Young 2:1f78675feba0 27 InterruptIn TRIG2 (PC_2); // PC_2 Counter 2 trigger
Charles David Young 2:1f78675feba0 28 InterruptIn TRIG3 (PA_15); // PA_15 Counter 3 trigger
Charles David Young 2:1f78675feba0 29 DigitalOut PLED (PC_3); // PC_3 Pulse LED
Charles David Young 2:1f78675feba0 30 DigitalOut ALED (PC_9); // PC_9 Alarm LED
Charles David Young 2:1f78675feba0 31 DigitalIn CHRG (PC_10); // PC_10 Charge in progress
Charles David Young 2:1f78675feba0 32 DigitalIn EOCH (PC_12); // PC_12 Endo Of Charge
Charles David Young 2:1f78675feba0 33 DigitalOut SDPW (PB_2); // PB_2 SD-Card power enable
Charles David Young 2:1f78675feba0 34
Charles David Young 2:1f78675feba0 35 SDFileSystem sd(PB_15, PB_14, PB_13, PB_1, "sd"); // MOSI, MISO, SCK, CS
Charles David Young 2:1f78675feba0 36
Charles David Young 2:1f78675feba0 37 // Quadrature encoder
Charles David Young 2:1f78675feba0 38 QEI Wheel(D6, D5, NC, 16); // PB_10, PB_4
Charles David Young 2:1f78675feba0 39 // Tickers
Charles David Young 2:1f78675feba0 40 Ticker Sec_Beat; // Timer ticker
Charles David Young 2:1f78675feba0 41 Ticker Display_Refresh; // Display refresh ticker
Charles David Young 2:1f78675feba0 42
Charles David Young 2:1f78675feba0 43 //Serial ports
Charles David Young 2:1f78675feba0 44 Serial PC(USBTX, USBRX); // Virtual COM via USB
Charles David Young 2:1f78675feba0 45
Charles David Young 2:1f78675feba0 46 // GPS module
Charles David Young 2:1f78675feba0 47 Serial GPS(PA_11, PA_12, 9600); // PA_11=TX, PA_12=RX, default baud-rate
Charles David Young 2:1f78675feba0 48 I2C GPS_I2C(PB_9,PB_8); // SDA=PB_9, SCL=PB_8
Charles David Young 2:1f78675feba0 49
Charles David Young 2:1f78675feba0 50 extern unsigned int buffer[128*64/8]; // RAM buffer used by LCD
Charles David Young 2:1f78675feba0 51 time_t seconds; // timestamp
Charles David Young 2:1f78675feba0 52 char Text[40]=""; // Text string used by LCD
Charles David Young 2:1f78675feba0 53 float Vbatt, AmbLight; // battery votage and ambient light level
Charles David Young 2:1f78675feba0 54 uint16_t CNT1, CNT2, CNT3; // pulse counters
Charles David Young 2:1f78675feba0 55 uint8_t FLASH_Status;
Charles David Young 2:1f78675feba0 56 float V1, V2, V3, V4;
Charles David Young 2:1f78675feba0 57 bool Pulse=0, ExtPwr=0;
Charles David Young 2:1f78675feba0 58
Charles David Young 2:1f78675feba0 59 // used by GPS:
Charles David Young 2:1f78675feba0 60 double latitude = 0.0;
Charles David Young 2:1f78675feba0 61 double longitude = 0.0;
Charles David Young 2:1f78675feba0 62 double altitude = 0.0;
Charles David Young 2:1f78675feba0 63 int num_sat;
Charles David Young 2:1f78675feba0 64 float hori_dilute;
Charles David Young 2:1f78675feba0 65 float alt = 0.0;
Charles David Young 2:1f78675feba0 66 float geoid, GPS_time;
Charles David Young 2:1f78675feba0 67 char ns = 'N', ew='E';
Charles David Young 2:1f78675feba0 68 char GPS_stream[256]="none";
Charles David Young 2:1f78675feba0 69 char gu, hu;
Charles David Young 2:1f78675feba0 70 //const int GPS_addr = 0x42;
Charles David Young 2:1f78675feba0 71
Charles David Young 2:1f78675feba0 72 // ------------------- Prototypes -----------------------
Charles David Young 2:1f78675feba0 73 void Timer_tick(void);
Charles David Young 2:1f78675feba0 74 void Update_Display(void);
Charles David Young 2:1f78675feba0 75 void Set_Time(void);
Charles David Young 2:1f78675feba0 76 void Read_Voltages(void);
Charles David Young 2:1f78675feba0 77 void PowerOff(void);
Charles David Young 2:1f78675feba0 78 void DrawFrame(void);
Charles David Young 2:1f78675feba0 79 void CNT1_count(void);
Charles David Young 2:1f78675feba0 80 void CNT2_count(void);
Charles David Young 2:1f78675feba0 81 void CNT3_count(void);
Charles David Young 2:1f78675feba0 82 uint8_t WriteEEPROM(uint32_t, uint8_t);
Charles David Young 2:1f78675feba0 83 uint8_t ReadEEPROM(uint32_t);
Charles David Young 2:1f78675feba0 84 void SDCard_test(void);
Charles David Young 2:1f78675feba0 85 void EEPROM_test(void);
Charles David Young 2:1f78675feba0 86 void Init_All(void);
Charles David Young 2:1f78675feba0 87 int GPS_test(void);
Charles David Young 2:1f78675feba0 88 void GPS_getline(void);
Charles David Young 2:1f78675feba0 89 int GPS_get_stream(void);
Charles David Young 2:1f78675feba0 90
Charles David Young 2:1f78675feba0 91 int main()
Charles David Young 2:1f78675feba0 92 {
Charles David Young 2:1f78675feba0 93 Init_All();
Charles David Young 2:1f78675feba0 94
Charles David Young 2:1f78675feba0 95 if(Button) // if turn-on via pushbutton
Charles David Young 2:1f78675feba0 96 {
Charles David Young 2:1f78675feba0 97 KAL = 1; // self sustain power from battery
Charles David Young 2:1f78675feba0 98 ExtPwr=0;
Charles David Young 2:1f78675feba0 99 }
Charles David Young 2:1f78675feba0 100 else
Charles David Young 2:1f78675feba0 101 ExtPwr = 1; // otherwise power comes from USB
Charles David Young 2:1f78675feba0 102
Charles David Young 2:1f78675feba0 103 // debug: force
Charles David Young 2:1f78675feba0 104 //ExtPwr = 0;
Charles David Young 2:1f78675feba0 105
Charles David Young 2:1f78675feba0 106
Charles David Young 2:1f78675feba0 107 // enable LCD refresh ticker
Charles David Young 2:1f78675feba0 108 Display_Refresh.attach(&Update_Display, 0.1);
Charles David Young 2:1f78675feba0 109
Charles David Young 2:1f78675feba0 110 wait(1.5);
Charles David Young 2:1f78675feba0 111 Clear_buffer(buffer);
Charles David Young 2:1f78675feba0 112
Charles David Young 2:1f78675feba0 113 if(Button) // if pushbutton still pressed after splash-screen
Charles David Young 2:1f78675feba0 114 Set_Time(); // set RTC time and date
Charles David Young 2:1f78675feba0 115
Charles David Young 2:1f78675feba0 116 // launch self-tests
Charles David Young 2:1f78675feba0 117 SDCard_test();
Charles David Young 2:1f78675feba0 118 wait(1);
Charles David Young 2:1f78675feba0 119 //EEPROM_test();
Charles David Young 2:1f78675feba0 120
Charles David Young 2:1f78675feba0 121 // draw background frame
Charles David Young 2:1f78675feba0 122 Clear_buffer(buffer);
Charles David Young 2:1f78675feba0 123 DrawFrame();
Charles David Young 2:1f78675feba0 124
Charles David Young 2:1f78675feba0 125 if(ExtPwr) // if powered via USB, no need for backlight (recharge)
Charles David Young 2:1f78675feba0 126 BKL.write(0);
Charles David Young 2:1f78675feba0 127
Charles David Young 2:1f78675feba0 128 // enable sec-beat ticker
Charles David Young 2:1f78675feba0 129 Sec_Beat.attach(&Timer_tick, 1.113);
Charles David Young 2:1f78675feba0 130
Charles David Young 2:1f78675feba0 131 //tm t = RTC::getDefaultTM();
Charles David Young 2:1f78675feba0 132 //RTC::attach(&Sec_Beat, RTC::Second);
Charles David Young 2:1f78675feba0 133
Charles David Young 2:1f78675feba0 134 // enable & attach interrupts on rising edge of digital inputs
Charles David Young 2:1f78675feba0 135 TRIG1.rise(&CNT1_count);
Charles David Young 2:1f78675feba0 136 TRIG2.rise(&CNT2_count);
Charles David Young 2:1f78675feba0 137 TRIG3.rise(&CNT3_count);
Charles David Young 2:1f78675feba0 138
Charles David Young 2:1f78675feba0 139 while(1)
Charles David Young 2:1f78675feba0 140 {
Charles David Young 2:1f78675feba0 141
Charles David Young 2:1f78675feba0 142 if(Button)
Charles David Young 2:1f78675feba0 143 PowerOff(); // Power-off test
Charles David Young 2:1f78675feba0 144 /*
Charles David Young 2:1f78675feba0 145 // DEBUG: PC/GPS serial pass-through
Charles David Young 2:1f78675feba0 146 if(PC.readable())
Charles David Young 2:1f78675feba0 147 GPS.putc(PC.getc());
Charles David Young 2:1f78675feba0 148
Charles David Young 2:1f78675feba0 149 if(GPS.readable())
Charles David Young 2:1f78675feba0 150 PC.putc(GPS.getc());
Charles David Young 2:1f78675feba0 151 */
Charles David Young 2:1f78675feba0 152
Charles David Young 2:1f78675feba0 153 }
Charles David Young 2:1f78675feba0 154
Charles David Young 2:1f78675feba0 155 }
Charles David Young 2:1f78675feba0 156
Charles David Young 2:1f78675feba0 157
Charles David Young 2:1f78675feba0 158 //===========================================================================
Charles David Young 2:1f78675feba0 159
Charles David Young 2:1f78675feba0 160 // ------------- Called every second ----------------------
Charles David Young 2:1f78675feba0 161
Charles David Young 2:1f78675feba0 162 void Timer_tick()
Charles David Young 2:1f78675feba0 163 {
Charles David Young 2:1f78675feba0 164 seconds = time(NULL);
Charles David Young 2:1f78675feba0 165 strftime(Text, 50, "%d-%b-%Y %H:%M:%S", localtime(&seconds));
Charles David Young 2:1f78675feba0 166 LCD_drawstring(buffer, 0, 0, Text);
Charles David Young 2:1f78675feba0 167
Charles David Young 2:1f78675feba0 168 //TRIG1.rise(NULL); // detach counters
Charles David Young 2:1f78675feba0 169 //TRIG2.rise(NULL); //
Charles David Young 2:1f78675feba0 170 //TRIG3.rise(NULL); //
Charles David Young 2:1f78675feba0 171
Charles David Young 2:1f78675feba0 172 // read voltages
Charles David Young 2:1f78675feba0 173 if(ExtPwr)
Charles David Young 2:1f78675feba0 174 KAL = 1;
Charles David Young 2:1f78675feba0 175 Read_Voltages();
Charles David Young 2:1f78675feba0 176 if(ExtPwr)
Charles David Young 2:1f78675feba0 177 KAL = 0;
Charles David Young 2:1f78675feba0 178
Charles David Young 2:1f78675feba0 179
Charles David Young 2:1f78675feba0 180 if(!ExtPwr)
Charles David Young 2:1f78675feba0 181 if(AmbLight>BKL_TH_OFF)
Charles David Young 2:1f78675feba0 182 BKL.write(BKL_LOW);
Charles David Young 2:1f78675feba0 183 else
Charles David Young 2:1f78675feba0 184 if(AmbLight<BKL_TH_ON)
Charles David Young 2:1f78675feba0 185 BKL.write(AmbLight+(1-BKL_TH_ON));
Charles David Young 2:1f78675feba0 186
Charles David Young 2:1f78675feba0 187 // write values to buffer
Charles David Young 2:1f78675feba0 188 sprintf(Text,"VBATT= %4.2f", Vbatt);
Charles David Young 2:1f78675feba0 189 LCD_drawstring(buffer, 0, 2, Text);
Charles David Young 2:1f78675feba0 190 sprintf(Text,"%4.1f %4.1f %4.1f %4.1f", V1, V2, V3, V4 );
Charles David Young 2:1f78675feba0 191 LCD_drawstring(buffer, 0, 3, Text);
Charles David Young 2:1f78675feba0 192 sprintf(Text,"CPS1= %5d", CNT1);
Charles David Young 2:1f78675feba0 193 LCD_drawstring(buffer, 0, 4, Text);
Charles David Young 2:1f78675feba0 194 sprintf(Text,"CPS2= %5d", CNT2);
Charles David Young 2:1f78675feba0 195 LCD_drawstring(buffer, 0, 5, Text);
Charles David Young 2:1f78675feba0 196 sprintf(Text,"CPS3= %5d", CNT3);
Charles David Young 2:1f78675feba0 197 LCD_drawstring(buffer, 0, 6, Text);
Charles David Young 2:1f78675feba0 198
Charles David Young 2:1f78675feba0 199 CNT1=CNT2=CNT3=0;
Charles David Young 2:1f78675feba0 200
Charles David Young 2:1f78675feba0 201 //TRIG1.rise(&CNT1_count); //attach CNT1_count(); to TRIG1
Charles David Young 2:1f78675feba0 202 //TRIG2.rise(&CNT2_count); //attach CNT2_count(); to TRIG2
Charles David Young 2:1f78675feba0 203 //TRIG3.rise(&CNT3_count); //attach CNT3_count(); to TRIG3
Charles David Young 2:1f78675feba0 204
Charles David Young 2:1f78675feba0 205 GPS_get_stream(); // GPS test
Charles David Young 2:1f78675feba0 206
Charles David Young 2:1f78675feba0 207 return;
Charles David Young 2:1f78675feba0 208 }
Charles David Young 2:1f78675feba0 209
Charles David Young 2:1f78675feba0 210
Charles David Young 2:1f78675feba0 211 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 212 void Update_Display(void)
Charles David Young 2:1f78675feba0 213 {
Charles David Young 2:1f78675feba0 214
Charles David Young 2:1f78675feba0 215 if(Pulse)
Charles David Young 2:1f78675feba0 216 {
Charles David Young 2:1f78675feba0 217 PLED = 0;
Charles David Young 2:1f78675feba0 218 BEEP.write(0);
Charles David Young 2:1f78675feba0 219 Pulse = 0;
Charles David Young 2:1f78675feba0 220 }
Charles David Young 2:1f78675feba0 221 LCD_write_buffer(buffer); // LCD update
Charles David Young 2:1f78675feba0 222 return;
Charles David Young 2:1f78675feba0 223 }
Charles David Young 2:1f78675feba0 224
Charles David Young 2:1f78675feba0 225 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 226 void Set_Time(void)
Charles David Young 2:1f78675feba0 227 {
Charles David Young 2:1f78675feba0 228 uint8_t Year=0, Month=0, Day=0, Hours=0, Mins=0, Secs=0;
Charles David Young 2:1f78675feba0 229 time_t seconds;
Charles David Young 2:1f78675feba0 230 struct tm t;
Charles David Young 2:1f78675feba0 231
Charles David Young 2:1f78675feba0 232 Clear_buffer(buffer);
Charles David Young 2:1f78675feba0 233 sprintf(Text,"TIME & DATE SETTING");
Charles David Young 2:1f78675feba0 234 LCD_drawstring(buffer, 0, 0, Text);
Charles David Young 2:1f78675feba0 235
Charles David Young 2:1f78675feba0 236 // Set year
Charles David Young 2:1f78675feba0 237 while(Button);
Charles David Young 2:1f78675feba0 238 wait_ms(50);
Charles David Young 2:1f78675feba0 239
Charles David Young 2:1f78675feba0 240 while(!Button)
Charles David Young 2:1f78675feba0 241 {
Charles David Young 2:1f78675feba0 242 if(int(Wheel.getPulses())<0)
Charles David Young 2:1f78675feba0 243 Wheel.reset();
Charles David Young 2:1f78675feba0 244 Year = (uint8_t)(Wheel.getPulses());
Charles David Young 2:1f78675feba0 245
Charles David Young 2:1f78675feba0 246 if(Year>99)
Charles David Young 2:1f78675feba0 247 Wheel.reset();
Charles David Young 2:1f78675feba0 248
Charles David Young 2:1f78675feba0 249 sprintf(Text, "Year: %2d", Year);
Charles David Young 2:1f78675feba0 250 LCD_drawstring(buffer, 0, 2, Text);
Charles David Young 2:1f78675feba0 251
Charles David Young 2:1f78675feba0 252 }
Charles David Young 2:1f78675feba0 253
Charles David Young 2:1f78675feba0 254 // Set month
Charles David Young 2:1f78675feba0 255 while(Button);
Charles David Young 2:1f78675feba0 256 wait_ms(50);
Charles David Young 2:1f78675feba0 257 Wheel.reset();
Charles David Young 2:1f78675feba0 258 while(!Button)
Charles David Young 2:1f78675feba0 259 {
Charles David Young 2:1f78675feba0 260 if(int(Wheel.getPulses())<0)
Charles David Young 2:1f78675feba0 261 Wheel.reset();
Charles David Young 2:1f78675feba0 262 Month = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 2:1f78675feba0 263
Charles David Young 2:1f78675feba0 264 if(Month>11)
Charles David Young 2:1f78675feba0 265 Wheel.reset();
Charles David Young 2:1f78675feba0 266
Charles David Young 2:1f78675feba0 267 sprintf(Text, "Month: %2d", Month+1);
Charles David Young 2:1f78675feba0 268 LCD_drawstring(buffer, 0, 3, Text);
Charles David Young 2:1f78675feba0 269
Charles David Young 2:1f78675feba0 270 }
Charles David Young 2:1f78675feba0 271
Charles David Young 2:1f78675feba0 272
Charles David Young 2:1f78675feba0 273 // Set day
Charles David Young 2:1f78675feba0 274 while(Button);
Charles David Young 2:1f78675feba0 275 wait_ms(50);
Charles David Young 2:1f78675feba0 276 Wheel.reset();
Charles David Young 2:1f78675feba0 277 while(!Button)
Charles David Young 2:1f78675feba0 278 {
Charles David Young 2:1f78675feba0 279 if(int(Wheel.getPulses())<0)
Charles David Young 2:1f78675feba0 280 Wheel.reset();
Charles David Young 2:1f78675feba0 281 Day = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 2:1f78675feba0 282
Charles David Young 2:1f78675feba0 283 if(Day>30)
Charles David Young 2:1f78675feba0 284 Wheel.reset();
Charles David Young 2:1f78675feba0 285
Charles David Young 2:1f78675feba0 286 sprintf(Text, "Day: %2d", Day+1);
Charles David Young 2:1f78675feba0 287 LCD_drawstring(buffer, 0, 4, Text);
Charles David Young 2:1f78675feba0 288
Charles David Young 2:1f78675feba0 289 }
Charles David Young 2:1f78675feba0 290
Charles David Young 2:1f78675feba0 291 // Set hours
Charles David Young 2:1f78675feba0 292 while(Button);
Charles David Young 2:1f78675feba0 293 wait_ms(50);
Charles David Young 2:1f78675feba0 294 Wheel.reset();
Charles David Young 2:1f78675feba0 295 while(!Button)
Charles David Young 2:1f78675feba0 296 {
Charles David Young 2:1f78675feba0 297 if(int(Wheel.getPulses())<0)
Charles David Young 2:1f78675feba0 298 Wheel.reset();
Charles David Young 2:1f78675feba0 299 Hours = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 2:1f78675feba0 300
Charles David Young 2:1f78675feba0 301 if(Hours>22)
Charles David Young 2:1f78675feba0 302 Wheel.reset();
Charles David Young 2:1f78675feba0 303
Charles David Young 2:1f78675feba0 304 sprintf(Text, "Hours: %2d", Hours);
Charles David Young 2:1f78675feba0 305 LCD_drawstring(buffer, 0, 5, Text);
Charles David Young 2:1f78675feba0 306
Charles David Young 2:1f78675feba0 307 }
Charles David Young 2:1f78675feba0 308 //Hours++;
Charles David Young 2:1f78675feba0 309
Charles David Young 2:1f78675feba0 310 // Set minutes
Charles David Young 2:1f78675feba0 311 while(Button);
Charles David Young 2:1f78675feba0 312 wait_ms(50);
Charles David Young 2:1f78675feba0 313 Wheel.reset();
Charles David Young 2:1f78675feba0 314 while(!Button)
Charles David Young 2:1f78675feba0 315 {
Charles David Young 2:1f78675feba0 316 if(int(Wheel.getPulses())<0)
Charles David Young 2:1f78675feba0 317 Wheel.reset();
Charles David Young 2:1f78675feba0 318 Mins = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 2:1f78675feba0 319
Charles David Young 2:1f78675feba0 320 if(Mins>59)
Charles David Young 2:1f78675feba0 321 Wheel.reset();
Charles David Young 2:1f78675feba0 322
Charles David Young 2:1f78675feba0 323 sprintf(Text, "Minutes: %2d", Mins);
Charles David Young 2:1f78675feba0 324 LCD_drawstring(buffer, 0, 6, Text);
Charles David Young 2:1f78675feba0 325
Charles David Young 2:1f78675feba0 326 }
Charles David Young 2:1f78675feba0 327
Charles David Young 2:1f78675feba0 328 t.tm_year = Year + 100;
Charles David Young 2:1f78675feba0 329 t.tm_mon = Month;
Charles David Young 2:1f78675feba0 330 t.tm_mday = Day + 1;
Charles David Young 2:1f78675feba0 331 t.tm_hour = Hours;
Charles David Young 2:1f78675feba0 332 t.tm_min = Mins;
Charles David Young 2:1f78675feba0 333 t.tm_sec = Secs;
Charles David Young 2:1f78675feba0 334
Charles David Young 2:1f78675feba0 335 seconds = mktime(&t);
Charles David Young 2:1f78675feba0 336 set_time(seconds);
Charles David Young 2:1f78675feba0 337
Charles David Young 2:1f78675feba0 338 Clear_buffer(buffer);
Charles David Young 2:1f78675feba0 339
Charles David Young 2:1f78675feba0 340 return;
Charles David Young 2:1f78675feba0 341 }
Charles David Young 2:1f78675feba0 342
Charles David Young 2:1f78675feba0 343
Charles David Young 2:1f78675feba0 344 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 345 void Read_Voltages(void)
Charles David Young 2:1f78675feba0 346 {
Charles David Young 2:1f78675feba0 347
Charles David Young 2:1f78675feba0 348 double ADC_value;
Charles David Young 2:1f78675feba0 349 uint8_t smooth = 10; // Number of samples to smooth
Charles David Young 2:1f78675feba0 350 uint8_t i;
Charles David Young 2:1f78675feba0 351
Charles David Young 2:1f78675feba0 352 // Read battery voltage
Charles David Young 2:1f78675feba0 353
Charles David Young 2:1f78675feba0 354 ADC_value = BATT.read(); // cleanup
Charles David Young 2:1f78675feba0 355 wait_ms(5);
Charles David Young 2:1f78675feba0 356 ADC_value = 0;
Charles David Young 2:1f78675feba0 357 for(i=0;i<smooth;i++)
Charles David Young 2:1f78675feba0 358 ADC_value += BATT.read();
Charles David Young 2:1f78675feba0 359
Charles David Young 2:1f78675feba0 360 ADC_value = ADC_value/smooth;
Charles David Young 2:1f78675feba0 361 Vbatt = (float)(ADC_value*BAT_GAIN);
Charles David Young 2:1f78675feba0 362
Charles David Young 2:1f78675feba0 363
Charles David Young 2:1f78675feba0 364 // Read Ambient Light Level
Charles David Young 2:1f78675feba0 365
Charles David Young 2:1f78675feba0 366 ADC_value = ALS.read(); // cleanup
Charles David Young 2:1f78675feba0 367 wait_ms(5);
Charles David Young 2:1f78675feba0 368 ADC_value = 0;
Charles David Young 2:1f78675feba0 369 for(i=0;i<smooth;i++)
Charles David Young 2:1f78675feba0 370 ADC_value += ALS.read();
Charles David Young 2:1f78675feba0 371
Charles David Young 2:1f78675feba0 372 ADC_value = ADC_value/smooth;
Charles David Young 2:1f78675feba0 373 AmbLight = (float)(ADC_value);
Charles David Young 2:1f78675feba0 374
Charles David Young 2:1f78675feba0 375
Charles David Young 2:1f78675feba0 376 // Read AIN1
Charles David Young 2:1f78675feba0 377
Charles David Young 2:1f78675feba0 378 ADC_value = VCH1.read(); // cleanup
Charles David Young 2:1f78675feba0 379 wait_ms(5);
Charles David Young 2:1f78675feba0 380 ADC_value = 0;
Charles David Young 2:1f78675feba0 381 for(i=0;i<smooth;i++)
Charles David Young 2:1f78675feba0 382 ADC_value += VCH1.read();
Charles David Young 2:1f78675feba0 383
Charles David Young 2:1f78675feba0 384 ADC_value = ADC_value/smooth;
Charles David Young 2:1f78675feba0 385 V1 = (float)(ADC_value);
Charles David Young 2:1f78675feba0 386
Charles David Young 2:1f78675feba0 387 // Read AIN2
Charles David Young 2:1f78675feba0 388
Charles David Young 2:1f78675feba0 389 ADC_value = VCH2.read(); // cleanup
Charles David Young 2:1f78675feba0 390 wait_ms(5);
Charles David Young 2:1f78675feba0 391 ADC_value = 0;
Charles David Young 2:1f78675feba0 392 for(i=0;i<smooth;i++)
Charles David Young 2:1f78675feba0 393 ADC_value += VCH2.read();
Charles David Young 2:1f78675feba0 394
Charles David Young 2:1f78675feba0 395 ADC_value = ADC_value/smooth;
Charles David Young 2:1f78675feba0 396 V2 = (float)(ADC_value);
Charles David Young 2:1f78675feba0 397
Charles David Young 2:1f78675feba0 398 // Read AIN3
Charles David Young 2:1f78675feba0 399
Charles David Young 2:1f78675feba0 400 ADC_value = VCH3.read(); // cleanup
Charles David Young 2:1f78675feba0 401 wait_ms(5);
Charles David Young 2:1f78675feba0 402 ADC_value = 0;
Charles David Young 2:1f78675feba0 403 for(i=0;i<smooth;i++)
Charles David Young 2:1f78675feba0 404 ADC_value += VCH3.read();
Charles David Young 2:1f78675feba0 405
Charles David Young 2:1f78675feba0 406 ADC_value = ADC_value/smooth;
Charles David Young 2:1f78675feba0 407 V3 = (float)(ADC_value);
Charles David Young 2:1f78675feba0 408
Charles David Young 2:1f78675feba0 409 // Read AIN4
Charles David Young 2:1f78675feba0 410
Charles David Young 2:1f78675feba0 411 ADC_value = VCH4.read(); // cleanup
Charles David Young 2:1f78675feba0 412 wait_ms(5);
Charles David Young 2:1f78675feba0 413 ADC_value = 0;
Charles David Young 2:1f78675feba0 414 for(i=0;i<smooth;i++)
Charles David Young 2:1f78675feba0 415 ADC_value += VCH4.read();
Charles David Young 2:1f78675feba0 416
Charles David Young 2:1f78675feba0 417 ADC_value = ADC_value/smooth;
Charles David Young 2:1f78675feba0 418 V4 = (float)(ADC_value);
Charles David Young 2:1f78675feba0 419
Charles David Young 2:1f78675feba0 420
Charles David Young 2:1f78675feba0 421 return;
Charles David Young 2:1f78675feba0 422 }
Charles David Young 2:1f78675feba0 423
Charles David Young 2:1f78675feba0 424
Charles David Young 2:1f78675feba0 425 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 426 void PowerOff(void)
Charles David Young 2:1f78675feba0 427 {
Charles David Young 2:1f78675feba0 428 BKL.write(1);
Charles David Young 2:1f78675feba0 429 Display_Refresh.detach();
Charles David Young 2:1f78675feba0 430 Clear_buffer(buffer);
Charles David Young 2:1f78675feba0 431 sprintf(Text,"POWERING OFF");
Charles David Young 2:1f78675feba0 432 LCD_drawstring(buffer, 30, 3, Text);
Charles David Young 2:1f78675feba0 433 LCD_write_buffer(buffer);
Charles David Young 2:1f78675feba0 434 wait(2);
Charles David Young 2:1f78675feba0 435 Clear_buffer(buffer);
Charles David Young 2:1f78675feba0 436 KAL = 0;
Charles David Young 2:1f78675feba0 437 }
Charles David Young 2:1f78675feba0 438
Charles David Young 2:1f78675feba0 439
Charles David Young 2:1f78675feba0 440 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 441 void DrawFrame(void)
Charles David Young 2:1f78675feba0 442 {
Charles David Young 2:1f78675feba0 443 uint8_t i;
Charles David Young 2:1f78675feba0 444
Charles David Young 2:1f78675feba0 445 for(i=0; i<128; i++)
Charles David Young 2:1f78675feba0 446 {
Charles David Young 2:1f78675feba0 447 LCD_setpixel(buffer, i, FRM_ROW1, 1);
Charles David Young 2:1f78675feba0 448 //LCD_setpixel(buffer, i, FRM_ROW2, 1);
Charles David Young 2:1f78675feba0 449 }
Charles David Young 2:1f78675feba0 450 return;
Charles David Young 2:1f78675feba0 451 }
Charles David Young 2:1f78675feba0 452
Charles David Young 2:1f78675feba0 453
Charles David Young 2:1f78675feba0 454 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 455 void CNT1_count(void)
Charles David Young 2:1f78675feba0 456 { //function to call upon interrupt
Charles David Young 2:1f78675feba0 457 CNT1++; //increment counter object
Charles David Young 2:1f78675feba0 458 PLED = 1;
Charles David Young 2:1f78675feba0 459 BEEP.write(BUZZ_VOL);
Charles David Young 2:1f78675feba0 460 Pulse = 1;
Charles David Young 2:1f78675feba0 461 return;
Charles David Young 2:1f78675feba0 462 }
Charles David Young 2:1f78675feba0 463
Charles David Young 2:1f78675feba0 464
Charles David Young 2:1f78675feba0 465 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 466 void CNT2_count(void)
Charles David Young 2:1f78675feba0 467 { //function to call upon interrupt
Charles David Young 2:1f78675feba0 468 CNT2++; //increment counter object
Charles David Young 2:1f78675feba0 469 return;
Charles David Young 2:1f78675feba0 470 }
Charles David Young 2:1f78675feba0 471
Charles David Young 2:1f78675feba0 472
Charles David Young 2:1f78675feba0 473 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 474 void CNT3_count(void)
Charles David Young 2:1f78675feba0 475 { //function to call upon interrupt
Charles David Young 2:1f78675feba0 476 CNT3++; //increment counter object
Charles David Young 2:1f78675feba0 477 return;
Charles David Young 2:1f78675feba0 478 }
Charles David Young 2:1f78675feba0 479
Charles David Young 2:1f78675feba0 480
Charles David Young 2:1f78675feba0 481 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 482 uint8_t WriteEEPROM(uint32_t address, uint8_t data)
Charles David Young 2:1f78675feba0 483 {
Charles David Young 2:1f78675feba0 484 HAL_StatusTypeDef status;
Charles David Young 2:1f78675feba0 485
Charles David Young 2:1f78675feba0 486 address = address + 0x08000000;
Charles David Young 2:1f78675feba0 487 HAL_FLASH_Unlock();
Charles David Young 2:1f78675feba0 488 status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, data);
Charles David Young 2:1f78675feba0 489 HAL_FLASH_Lock();
Charles David Young 2:1f78675feba0 490
Charles David Young 2:1f78675feba0 491 return status;
Charles David Young 2:1f78675feba0 492 }
Charles David Young 2:1f78675feba0 493
Charles David Young 2:1f78675feba0 494
Charles David Young 2:1f78675feba0 495 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 496 uint8_t ReadEEPROM(uint32_t address)
Charles David Young 2:1f78675feba0 497 {
Charles David Young 2:1f78675feba0 498 uint8_t tmp = 0;
Charles David Young 2:1f78675feba0 499
Charles David Young 2:1f78675feba0 500 address = address + 0x08000000;
Charles David Young 2:1f78675feba0 501 tmp = *(__IO uint32_t*)address;
Charles David Young 2:1f78675feba0 502
Charles David Young 2:1f78675feba0 503 return tmp;
Charles David Young 2:1f78675feba0 504 }
Charles David Young 2:1f78675feba0 505
Charles David Young 2:1f78675feba0 506
Charles David Young 2:1f78675feba0 507
Charles David Young 2:1f78675feba0 508 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 509 void SDCard_test(void)
Charles David Young 2:1f78675feba0 510 {
Charles David Young 2:1f78675feba0 511 // SD-Card test
Charles David Young 2:1f78675feba0 512 printf("SD-Card test... ");
Charles David Young 2:1f78675feba0 513 //mkdir("/sd/system", 0777);
Charles David Young 2:1f78675feba0 514 FILE *fp = fopen("/sd/system/version.dat", "a");
Charles David Young 2:1f78675feba0 515 if(fp == NULL)
Charles David Young 2:1f78675feba0 516 {
Charles David Young 2:1f78675feba0 517 printf("ERROR\n");
Charles David Young 2:1f78675feba0 518 sprintf(Text,"SD-CARD ERROR");
Charles David Young 2:1f78675feba0 519 LCD_drawstring(buffer, 15, 2, Text);
Charles David Young 2:1f78675feba0 520 LCD_write_buffer(buffer);
Charles David Young 2:1f78675feba0 521 }
Charles David Young 2:1f78675feba0 522 else
Charles David Young 2:1f78675feba0 523 {
Charles David Young 2:1f78675feba0 524 printf("OK\n");
Charles David Young 2:1f78675feba0 525 sprintf(Text,"SD-CARD DETECTED");
Charles David Young 2:1f78675feba0 526 LCD_drawstring(buffer, 15, 2, Text);
Charles David Young 2:1f78675feba0 527 LCD_write_buffer(buffer);
Charles David Young 2:1f78675feba0 528 fprintf(fp, "Geo Electronics 2107\n");
Charles David Young 2:1f78675feba0 529 fprintf(fp, __DATE__);
Charles David Young 2:1f78675feba0 530 fprintf(fp, "\t");
Charles David Young 2:1f78675feba0 531 fprintf(fp, __TIME__);
Charles David Young 2:1f78675feba0 532 fprintf(fp, "\n");
Charles David Young 2:1f78675feba0 533 fclose(fp);
Charles David Young 2:1f78675feba0 534 }
Charles David Young 2:1f78675feba0 535 printf("SD-Card end of test\n");
Charles David Young 2:1f78675feba0 536
Charles David Young 2:1f78675feba0 537 return;
Charles David Young 2:1f78675feba0 538 }
Charles David Young 2:1f78675feba0 539
Charles David Young 2:1f78675feba0 540
Charles David Young 2:1f78675feba0 541 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 542 void EEPROM_test(void)
Charles David Young 2:1f78675feba0 543 {
Charles David Young 2:1f78675feba0 544 // internal EEPROM test
Charles David Young 2:1f78675feba0 545 PC.printf("Attempting to write to EEPROM...\n");
Charles David Young 2:1f78675feba0 546
Charles David Young 2:1f78675feba0 547 for (uint32_t i = 0; i < 8; i++)
Charles David Young 2:1f78675feba0 548 {
Charles David Young 2:1f78675feba0 549 FLASH_Status = WriteEEPROM(i,(uint8_t)(i));
Charles David Young 2:1f78675feba0 550 PC.printf("Writing %d at %d\n", i, i);
Charles David Young 2:1f78675feba0 551 }
Charles David Young 2:1f78675feba0 552 if(FLASH_Status == HAL_FLASH_ERROR_NONE)
Charles David Young 2:1f78675feba0 553 {PC.printf("Success!!\r\n");}
Charles David Young 2:1f78675feba0 554 else
Charles David Young 2:1f78675feba0 555 {PC.printf("Failed!!\r\n");}
Charles David Young 2:1f78675feba0 556
Charles David Young 2:1f78675feba0 557 for (uint32_t i = 0; i < 8; i++)
Charles David Young 2:1f78675feba0 558 {
Charles David Young 2:1f78675feba0 559 uint8_t storedValue = ReadEEPROM(i);
Charles David Young 2:1f78675feba0 560 PC.printf("Reading %d at %d\n", storedValue, i);
Charles David Young 2:1f78675feba0 561 }
Charles David Young 2:1f78675feba0 562
Charles David Young 2:1f78675feba0 563 // end of EEPROM test
Charles David Young 2:1f78675feba0 564 return;
Charles David Young 2:1f78675feba0 565
Charles David Young 2:1f78675feba0 566 }
Charles David Young 2:1f78675feba0 567
Charles David Young 2:1f78675feba0 568
Charles David Young 2:1f78675feba0 569 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 570 void Init_All(void)
Charles David Young 2:1f78675feba0 571 {
Charles David Young 2:1f78675feba0 572
Charles David Young 2:1f78675feba0 573 GPS_I2C.frequency(300000); // I2C GPS speed (400k max)
Charles David Young 2:1f78675feba0 574 //Button.mode(PullUp); // enable pushbutton pull-up
Charles David Young 2:1f78675feba0 575 BKL.period_ms(5); // set LCD backlight PWM
Charles David Young 2:1f78675feba0 576 BKL.write(1.0);
Charles David Young 2:1f78675feba0 577 BEEP.period_us(2000); // set initial buzzer period and duty-cycle
Charles David Young 2:1f78675feba0 578 BEEP.write(0);
Charles David Young 2:1f78675feba0 579 Wheel.reset(); // clear encoder
Charles David Young 2:1f78675feba0 580 LCD_reset();
Charles David Young 2:1f78675feba0 581 CNT1=CNT2=CNT3=0; // clear counters
Charles David Young 2:1f78675feba0 582 SDPW = 0; // Enable SC-card VDD
Charles David Young 2:1f78675feba0 583 // splash screen with date and time
Charles David Young 2:1f78675feba0 584 sprintf(Text,__DATE__);
Charles David Young 2:1f78675feba0 585 LCD_drawstring(buffer, 60, 5, Text);
Charles David Young 2:1f78675feba0 586 sprintf(Text,__TIME__);
Charles David Young 2:1f78675feba0 587 LCD_drawstring(buffer, 78, 6, Text);
Charles David Young 2:1f78675feba0 588 LCD_write_buffer(buffer);
Charles David Young 2:1f78675feba0 589
Charles David Young 2:1f78675feba0 590 // buzzer beep and blink LEDs
Charles David Young 2:1f78675feba0 591 BEEP.write(BUZZ_VOL);
Charles David Young 2:1f78675feba0 592 ALED = 1;
Charles David Young 2:1f78675feba0 593 wait(0.2);
Charles David Young 2:1f78675feba0 594 BEEP.period_us(1000);
Charles David Young 2:1f78675feba0 595 ALED = 0;
Charles David Young 2:1f78675feba0 596 PLED = 1;
Charles David Young 2:1f78675feba0 597 wait(0.2);
Charles David Young 2:1f78675feba0 598 BEEP.write(0);
Charles David Young 2:1f78675feba0 599 PLED = 0;
Charles David Young 2:1f78675feba0 600 // enable internal pull-ups for digital inputs
Charles David Young 2:1f78675feba0 601 TRIG1.mode(PullUp);
Charles David Young 2:1f78675feba0 602 TRIG2.mode(PullUp);
Charles David Young 2:1f78675feba0 603 TRIG3.mode(PullUp);
Charles David Young 2:1f78675feba0 604
Charles David Young 2:1f78675feba0 605 return;
Charles David Young 2:1f78675feba0 606
Charles David Young 2:1f78675feba0 607 }
Charles David Young 2:1f78675feba0 608
Charles David Young 2:1f78675feba0 609
Charles David Young 2:1f78675feba0 610
Charles David Young 2:1f78675feba0 611 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 612 int GPS_test()
Charles David Young 2:1f78675feba0 613 {
Charles David Young 2:1f78675feba0 614 //float time, hori_dilute, alt,geoid;
Charles David Young 2:1f78675feba0 615
Charles David Young 2:1f78675feba0 616 // $GPGGA,104534.000,7791.0381,N,06727.4434,E,1,08,0.9,510.4,M,43.9,M,,*47
Charles David Young 2:1f78675feba0 617 // $GPGGA,HHMMSS.SSS,latitude,N,longitude,E,FQ,NOS,HDP,altitude,M,height,M,,checksum data
Charles David Young 2:1f78675feba0 618
Charles David Young 2:1f78675feba0 619 int lock, n;
Charles David Young 2:1f78675feba0 620
Charles David Young 2:1f78675feba0 621 if(!GPS.readable())
Charles David Young 2:1f78675feba0 622 {
Charles David Young 2:1f78675feba0 623 return -1;
Charles David Young 2:1f78675feba0 624 }
Charles David Young 2:1f78675feba0 625
Charles David Young 2:1f78675feba0 626
Charles David Young 2:1f78675feba0 627 //PC.printf("Readable ");
Charles David Young 2:1f78675feba0 628
Charles David Young 2:1f78675feba0 629 GPS_stream[0] = '\0';
Charles David Young 2:1f78675feba0 630
Charles David Young 2:1f78675feba0 631 for (n = 0; n < 456; n++)
Charles David Young 2:1f78675feba0 632 {
Charles David Young 2:1f78675feba0 633 GPS_stream[n] = GPS.getc();
Charles David Young 2:1f78675feba0 634 if(GPS_stream[n] == '\r')
Charles David Young 2:1f78675feba0 635 {
Charles David Young 2:1f78675feba0 636 GPS_stream[n] = '\0';
Charles David Young 2:1f78675feba0 637 PC.printf("%s\n", GPS_stream);
Charles David Young 2:1f78675feba0 638 return;
Charles David Young 2:1f78675feba0 639 }
Charles David Young 2:1f78675feba0 640
Charles David Young 2:1f78675feba0 641 }
Charles David Young 2:1f78675feba0 642
Charles David Young 2:1f78675feba0 643
Charles David Young 2:1f78675feba0 644
Charles David Young 2:1f78675feba0 645
Charles David Young 2:1f78675feba0 646 while(GPS.readable())
Charles David Young 2:1f78675feba0 647 {
Charles David Young 2:1f78675feba0 648
Charles David Young 2:1f78675feba0 649 GPS_getline();
Charles David Young 2:1f78675feba0 650
Charles David Young 2:1f78675feba0 651 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
Charles David Young 2:1f78675feba0 652 //wait(5);
Charles David Young 2:1f78675feba0 653
Charles David Young 2:1f78675feba0 654 PC.printf("GPS: %s\n", GPS_stream);
Charles David Young 2:1f78675feba0 655
Charles David Young 2:1f78675feba0 656 if(sscanf(GPS_stream, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f,%c", &GPS_time, &latitude, &ns, &longitude, &ew, &lock, &num_sat, &hori_dilute, &alt, &hu, &geoid, &gu/*, &age_diff, &diff_ID*/) >= 1)
Charles David Young 2:1f78675feba0 657 {
Charles David Young 2:1f78675feba0 658 if(!lock)
Charles David Young 2:1f78675feba0 659 {
Charles David Young 2:1f78675feba0 660 longitude = 0.0;
Charles David Young 2:1f78675feba0 661 latitude = 0.0;
Charles David Young 2:1f78675feba0 662 ns = 'Z';
Charles David Young 2:1f78675feba0 663 ew = 'Z';
Charles David Young 2:1f78675feba0 664 alt = 0.0;
Charles David Young 2:1f78675feba0 665 return 0;
Charles David Young 2:1f78675feba0 666 }
Charles David Young 2:1f78675feba0 667 else
Charles David Young 2:1f78675feba0 668 {
Charles David Young 2:1f78675feba0 669 //if(ns == 'S') { latitude *= -1.0; }
Charles David Young 2:1f78675feba0 670 // if(ew == 'W') { longitude *= -1.0; }
Charles David Young 2:1f78675feba0 671 // float degrees = trunc(latitude / 100.0f);
Charles David Young 2:1f78675feba0 672 // float minutes = latitude - (degrees * 100.0f);
Charles David Young 2:1f78675feba0 673 // latitude = degrees + minutes / 60.0f;
Charles David Young 2:1f78675feba0 674 // degrees = trunc(longitude / 100.0f * 0.01f);
Charles David Young 2:1f78675feba0 675 // minutes = longitude - (degrees * 100.0f);
Charles David Young 2:1f78675feba0 676 // longitude = degrees + minutes / 60.0f;
Charles David Young 2:1f78675feba0 677 // pc1.printf(msg);
Charles David Young 2:1f78675feba0 678 PC.printf("\n\rlongitude is %f\n\r", longitude);
Charles David Young 2:1f78675feba0 679 PC.printf("\n\rtime is %f\n\r", GPS_time);
Charles David Young 2:1f78675feba0 680 PC.printf("ns is %c\n\r", ns);
Charles David Young 2:1f78675feba0 681 PC.printf("ew is %c\n\r", ew);
Charles David Young 2:1f78675feba0 682 PC.printf("alt is %f\n\r", alt);
Charles David Young 2:1f78675feba0 683
Charles David Young 2:1f78675feba0 684 latitude /= 100;
Charles David Young 2:1f78675feba0 685 longitude /= 100;
Charles David Young 2:1f78675feba0 686
Charles David Young 2:1f78675feba0 687 return 1;
Charles David Young 2:1f78675feba0 688 }
Charles David Young 2:1f78675feba0 689
Charles David Young 2:1f78675feba0 690 }
Charles David Young 2:1f78675feba0 691
Charles David Young 2:1f78675feba0 692 return 0;
Charles David Young 2:1f78675feba0 693 }
Charles David Young 2:1f78675feba0 694
Charles David Young 2:1f78675feba0 695 return(-1);
Charles David Young 2:1f78675feba0 696
Charles David Young 2:1f78675feba0 697 }
Charles David Young 2:1f78675feba0 698
Charles David Young 2:1f78675feba0 699
Charles David Young 2:1f78675feba0 700 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 701 void GPS_getline()
Charles David Young 2:1f78675feba0 702 {
Charles David Young 2:1f78675feba0 703 int i;
Charles David Young 2:1f78675feba0 704 char a;
Charles David Young 2:1f78675feba0 705 int n;
Charles David Young 2:1f78675feba0 706
Charles David Young 2:1f78675feba0 707 //strcpy(GPS_stream, '\0');
Charles David Young 2:1f78675feba0 708 GPS_stream[0] = '\0';
Charles David Young 2:1f78675feba0 709
Charles David Young 2:1f78675feba0 710
Charles David Young 2:1f78675feba0 711 while(GPS.readable())
Charles David Young 2:1f78675feba0 712 {
Charles David Young 2:1f78675feba0 713
Charles David Young 2:1f78675feba0 714 i = 0;
Charles David Young 2:1f78675feba0 715 a = GPS.getc();
Charles David Young 2:1f78675feba0 716
Charles David Young 2:1f78675feba0 717 GPS_stream[i] = a;
Charles David Young 2:1f78675feba0 718
Charles David Young 2:1f78675feba0 719 if (a == '$')
Charles David Young 2:1f78675feba0 720 {
Charles David Young 2:1f78675feba0 721 //PC.printf("%c",a);
Charles David Young 2:1f78675feba0 722 a = GPS.getc();
Charles David Young 2:1f78675feba0 723 GPS_stream[i] = a;
Charles David Young 2:1f78675feba0 724 i++;
Charles David Young 2:1f78675feba0 725 if (a == 'G')
Charles David Young 2:1f78675feba0 726 {
Charles David Young 2:1f78675feba0 727 //PC.printf("%c",a);
Charles David Young 2:1f78675feba0 728 a = GPS.getc();
Charles David Young 2:1f78675feba0 729 GPS_stream[i] = a;
Charles David Young 2:1f78675feba0 730 i++;
Charles David Young 2:1f78675feba0 731 if (a == 'P')
Charles David Young 2:1f78675feba0 732 {
Charles David Young 2:1f78675feba0 733 //PC.printf("%c",a);
Charles David Young 2:1f78675feba0 734 a = GPS.getc();
Charles David Young 2:1f78675feba0 735 GPS_stream[i] = a;
Charles David Young 2:1f78675feba0 736 i++;
Charles David Young 2:1f78675feba0 737 if (a == 'G')
Charles David Young 2:1f78675feba0 738 {
Charles David Young 2:1f78675feba0 739 //PC.printf("%c",a);
Charles David Young 2:1f78675feba0 740 a = GPS.getc();
Charles David Young 2:1f78675feba0 741 GPS_stream[i] = a;
Charles David Young 2:1f78675feba0 742 i++;
Charles David Young 2:1f78675feba0 743 if (a == 'G')
Charles David Young 2:1f78675feba0 744 {
Charles David Young 2:1f78675feba0 745 //PC.printf("%c",a);
Charles David Young 2:1f78675feba0 746 a = GPS.getc();
Charles David Young 2:1f78675feba0 747 GPS_stream[i] = a;
Charles David Young 2:1f78675feba0 748 i++;
Charles David Young 2:1f78675feba0 749 if (a == 'A')
Charles David Young 2:1f78675feba0 750 {
Charles David Young 2:1f78675feba0 751 //PC.printf("%c",a);
Charles David Young 2:1f78675feba0 752 //a = GPS.getc();
Charles David Young 2:1f78675feba0 753 //msg[i] = a;
Charles David Young 2:1f78675feba0 754 //PC.printf(msg);
Charles David Young 2:1f78675feba0 755 //PC.printf("\r\n");
Charles David Young 2:1f78675feba0 756
Charles David Young 2:1f78675feba0 757 for (n = 5; n < 456; n++)
Charles David Young 2:1f78675feba0 758 {
Charles David Young 2:1f78675feba0 759 GPS_stream[n] = GPS.getc();
Charles David Young 2:1f78675feba0 760 //PC.printf("%c", GPS_stream[n]);
Charles David Young 2:1f78675feba0 761 if(GPS_stream[n] == '\r')
Charles David Young 2:1f78675feba0 762 {
Charles David Young 2:1f78675feba0 763 GPS_stream[n] = '0';
Charles David Young 2:1f78675feba0 764 return;
Charles David Young 2:1f78675feba0 765 }
Charles David Young 2:1f78675feba0 766 }
Charles David Young 2:1f78675feba0 767 }
Charles David Young 2:1f78675feba0 768 }
Charles David Young 2:1f78675feba0 769 }
Charles David Young 2:1f78675feba0 770 }
Charles David Young 2:1f78675feba0 771 }
Charles David Young 2:1f78675feba0 772 }
Charles David Young 2:1f78675feba0 773 // while(GPS.getc() != '$') {
Charles David Young 2:1f78675feba0 774 // //char a = GPS.getc();
Charles David Young 2:1f78675feba0 775 // for(i = 0; i < 256; i++) {
Charles David Young 2:1f78675feba0 776 // msg[i] = GPS.getc();
Charles David Young 2:1f78675feba0 777 // pc1.printf("%c", msg[i]);
Charles David Young 2:1f78675feba0 778 // if(msg[i] == '\r') {
Charles David Young 2:1f78675feba0 779 // msg[i] = 0;
Charles David Young 2:1f78675feba0 780 // return;
Charles David Young 2:1f78675feba0 781 // }
Charles David Young 2:1f78675feba0 782 // }
Charles David Young 2:1f78675feba0 783 //
Charles David Young 2:1f78675feba0 784 //
Charles David Young 2:1f78675feba0 785 // }
Charles David Young 2:1f78675feba0 786 // while(GPS.getc() != '$'); // wait for the start of a line
Charles David Young 2:1f78675feba0 787 // for(int i=0; i<256; i++) {
Charles David Young 2:1f78675feba0 788 // msg[i] = GPS.getc();
Charles David Young 2:1f78675feba0 789 // if(msg[i] == '\r') {
Charles David Young 2:1f78675feba0 790 // msg[i] = 0;
Charles David Young 2:1f78675feba0 791 // return;
Charles David Young 2:1f78675feba0 792 // }
Charles David Young 2:1f78675feba0 793 // }
Charles David Young 2:1f78675feba0 794 // error("Overflowed message limit");
Charles David Young 2:1f78675feba0 795 }
Charles David Young 2:1f78675feba0 796
Charles David Young 2:1f78675feba0 797 return;
Charles David Young 2:1f78675feba0 798 }
Charles David Young 2:1f78675feba0 799
Charles David Young 2:1f78675feba0 800
Charles David Young 2:1f78675feba0 801 //---------------------------------------------------------------------------
Charles David Young 2:1f78675feba0 802
Charles David Young 2:1f78675feba0 803 int GPS_get_stream(void)
Charles David Young 2:1f78675feba0 804 {
Charles David Young 2:1f78675feba0 805 char cmd[256];
Charles David Young 2:1f78675feba0 806 int i;
Charles David Young 2:1f78675feba0 807
Charles David Young 2:1f78675feba0 808 GPS_stream[0] = '\0'; // buffer cleanup
Charles David Young 2:1f78675feba0 809
Charles David Young 2:1f78675feba0 810 cmd[0] = 0xFF;
Charles David Young 2:1f78675feba0 811 GPS_I2C.write(0x42, cmd, 1);
Charles David Young 2:1f78675feba0 812 GPS_I2C.read(0x42, cmd, 100);
Charles David Young 2:1f78675feba0 813 //cmd[21] = '\0';
Charles David Young 2:1f78675feba0 814 i=0;
Charles David Young 2:1f78675feba0 815 while((i<100))
Charles David Young 2:1f78675feba0 816 {
Charles David Young 2:1f78675feba0 817 PC.printf("%c", cmd[i]);
Charles David Young 2:1f78675feba0 818 //if(cmd[i]=='\n')
Charles David Young 2:1f78675feba0 819 // i=100;
Charles David Young 2:1f78675feba0 820 //else
Charles David Young 2:1f78675feba0 821 i++;
Charles David Young 2:1f78675feba0 822 }
Charles David Young 2:1f78675feba0 823
Charles David Young 2:1f78675feba0 824
Charles David Young 2:1f78675feba0 825
Charles David Young 2:1f78675feba0 826 return 1;
Charles David Young 2:1f78675feba0 827 }
Charles David Young 2:1f78675feba0 828
Charles David Young 3:c547dba5d39b 829 // ==========================================================================