![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
basic lightning detector with gps and sd card logging
Dependencies: AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile
main.cpp@6:96b0dbe76357, 2015-06-30 (annotated)
- Committer:
- cmkachur
- Date:
- Tue Jun 30 16:21:39 2015 +0000
- Revision:
- 6:96b0dbe76357
- Parent:
- 5:1d4fd419cfb7
- Child:
- 8:f8830b6c6d9b
Add ability to read a zeus.cfg configuration file used for setting registers in the AS3935. If the file does not exist, create the file with default parameters.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ftagius | 0:3328df4c3116 | 1 | #include "mbed.h" |
ftagius | 0:3328df4c3116 | 2 | #include "GPS.h" |
ftagius | 0:3328df4c3116 | 3 | #include "main.h" |
ftagius | 0:3328df4c3116 | 4 | #include "TSISensor.h" |
ftagius | 0:3328df4c3116 | 5 | #include "SDFileSystem.h" |
ftagius | 0:3328df4c3116 | 6 | #include "AS3935.h" |
cmkachur | 6:96b0dbe76357 | 7 | #include "ConfigFile.h" |
cmkachur | 6:96b0dbe76357 | 8 | #include <string> |
ftagius | 0:3328df4c3116 | 9 | |
cmkachur | 6:96b0dbe76357 | 10 | #define FW_VER 3 |
cmkachur | 4:4d26ba1ae0f7 | 11 | |
ftagius | 3:e3974328d808 | 12 | // frdm-kl25z as3935 connections for spi1 |
ftagius | 0:3328df4c3116 | 13 | // ------------------------------------------------ |
ftagius | 0:3328df4c3116 | 14 | // Header -- kl25z -- SD/MMC |
ftagius | 0:3328df4c3116 | 15 | // J2-20 -- PTE1 -- MOSI |
ftagius | 0:3328df4c3116 | 16 | // J9-13 -- PTE4 -- CS |
ftagius | 0:3328df4c3116 | 17 | // J2-14 -- GND -- Vss (GND) |
ftagius | 0:3328df4c3116 | 18 | // J9-9 -- PTE2 -- SCK |
ftagius | 0:3328df4c3116 | 19 | // J9-11 -- PTE3 -- MISO |
ftagius | 0:3328df4c3116 | 20 | |
ftagius | 0:3328df4c3116 | 21 | AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 1000000); // MOSI, MISO, SCK, CS, SPI bus freq (hz) |
ftagius | 0:3328df4c3116 | 22 | InterruptIn IntLightning(PTA12); //IRQ AS3935 |
ftagius | 0:3328df4c3116 | 23 | |
ftagius | 0:3328df4c3116 | 24 | |
ftagius | 0:3328df4c3116 | 25 | // frdm-kl25z sd card connections spi0 |
ftagius | 0:3328df4c3116 | 26 | // ------------------------------------------------ |
ftagius | 0:3328df4c3116 | 27 | // Header -- kl25z -- SPI |
ftagius | 0:3328df4c3116 | 28 | // J2-8 -- PTD2 -- MOSI |
ftagius | 0:3328df4c3116 | 29 | // J2-6 -- PTD0 -- CS |
ftagius | 0:3328df4c3116 | 30 | // J9-12 -- GND -- Vss (GND) |
ftagius | 0:3328df4c3116 | 31 | // J9-4 -- P3V3 -- Vdd (+3.3v) |
ftagius | 0:3328df4c3116 | 32 | // J2-12 -- PTD1 -- SCK |
ftagius | 0:3328df4c3116 | 33 | // J9-14 -- GND -- Vss (GND) |
ftagius | 0:3328df4c3116 | 34 | // J2-10 -- PTD3 -- MISO |
ftagius | 0:3328df4c3116 | 35 | |
ftagius | 0:3328df4c3116 | 36 | SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // MOSI, MISO, SCK, CS |
ftagius | 0:3328df4c3116 | 37 | |
ftagius | 0:3328df4c3116 | 38 | Serial pc(USBTX, USBRX); |
ftagius | 0:3328df4c3116 | 39 | GPS gpsd(PTE20, PTE21); |
ftagius | 0:3328df4c3116 | 40 | DigitalOut red(LED_RED); |
ftagius | 0:3328df4c3116 | 41 | DigitalOut green(LED_GREEN); |
ftagius | 0:3328df4c3116 | 42 | //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi |
ftagius | 3:e3974328d808 | 43 | bool debug=false; |
ftagius | 0:3328df4c3116 | 44 | int day, month,year,hour,minute,seconds; |
cmkachur | 2:3edb129c60b2 | 45 | void writeLogFile(int interruptSource, int distance, long energy); |
ftagius | 1:10d2a051285e | 46 | char logName[]="lightning_data.csv"; |
ftagius | 1:10d2a051285e | 47 | int rdistance, rinterrupt; |
ftagius | 1:10d2a051285e | 48 | char directory[]="/sd/lightning_data"; |
ftagius | 3:e3974328d808 | 49 | int OriginInt=-1; |
cmkachur | 6:96b0dbe76357 | 50 | int gDistance=-1; |
ftagius | 3:e3974328d808 | 51 | long energy=-1; |
ftagius | 0:3328df4c3116 | 52 | |
cmkachur | 6:96b0dbe76357 | 53 | typedef struct |
cmkachur | 6:96b0dbe76357 | 54 | { |
cmkachur | 6:96b0dbe76357 | 55 | unsigned int bOutdoorMode; |
cmkachur | 6:96b0dbe76357 | 56 | unsigned int noiseFloor; |
cmkachur | 6:96b0dbe76357 | 57 | unsigned int wdogThresh; |
cmkachur | 6:96b0dbe76357 | 58 | unsigned int bClrStat; |
cmkachur | 6:96b0dbe76357 | 59 | unsigned int minNumLight; |
cmkachur | 6:96b0dbe76357 | 60 | unsigned int spikeRej; |
cmkachur | 6:96b0dbe76357 | 61 | unsigned int maskDisturber; |
cmkachur | 6:96b0dbe76357 | 62 | }sys_cfg_t; |
cmkachur | 6:96b0dbe76357 | 63 | |
cmkachur | 6:96b0dbe76357 | 64 | // default configuration values |
cmkachur | 6:96b0dbe76357 | 65 | sys_cfg_t sysCfg = { .bOutdoorMode = 1, |
cmkachur | 6:96b0dbe76357 | 66 | .noiseFloor = 2, |
cmkachur | 6:96b0dbe76357 | 67 | .wdogThresh = 2, |
cmkachur | 6:96b0dbe76357 | 68 | .bClrStat = 0, |
cmkachur | 6:96b0dbe76357 | 69 | .minNumLight = 0, |
cmkachur | 6:96b0dbe76357 | 70 | .spikeRej = 2, |
cmkachur | 6:96b0dbe76357 | 71 | .maskDisturber = 0, |
cmkachur | 6:96b0dbe76357 | 72 | }; |
cmkachur | 6:96b0dbe76357 | 73 | |
cmkachur | 6:96b0dbe76357 | 74 | |
ftagius | 0:3328df4c3116 | 75 | void DetectLightning() |
ftagius | 0:3328df4c3116 | 76 | { |
ftagius | 0:3328df4c3116 | 77 | OriginInt = ld.interruptSource(); |
cmkachur | 6:96b0dbe76357 | 78 | gDistance = ld.lightningDistanceKm(); |
ftagius | 3:e3974328d808 | 79 | energy = ld.getEnergy(); |
ftagius | 0:3328df4c3116 | 80 | } |
ftagius | 0:3328df4c3116 | 81 | |
cmkachur | 2:3edb129c60b2 | 82 | void writeLogFile(int interruptSource, int distance, long energy) |
ftagius | 0:3328df4c3116 | 83 | { |
ftagius | 1:10d2a051285e | 84 | char logFilePath[128]; |
ftagius | 0:3328df4c3116 | 85 | static bool header=false; |
ftagius | 0:3328df4c3116 | 86 | FILE *fp; |
ftagius | 1:10d2a051285e | 87 | |
ftagius | 1:10d2a051285e | 88 | sprintf(logFilePath, "%s/%s", directory,logName); |
ftagius | 0:3328df4c3116 | 89 | sd.mount(); |
ftagius | 1:10d2a051285e | 90 | fp = fopen(logFilePath, "a"); |
ftagius | 0:3328df4c3116 | 91 | if(fp == NULL) { |
ftagius | 0:3328df4c3116 | 92 | // retry |
ftagius | 3:e3974328d808 | 93 | wait_ms(200); |
ftagius | 1:10d2a051285e | 94 | fp = fopen(logFilePath, "a"); |
ftagius | 0:3328df4c3116 | 95 | if (fp == NULL) |
ftagius | 0:3328df4c3116 | 96 | { |
ftagius | 1:10d2a051285e | 97 | printf("Could not open file %s for writing\r\n",logFilePath); |
ftagius | 0:3328df4c3116 | 98 | sd.unmount(); |
ftagius | 1:10d2a051285e | 99 | printf("unmount sd card \r\n"); |
ftagius | 0:3328df4c3116 | 100 | return; |
ftagius | 0:3328df4c3116 | 101 | } |
ftagius | 0:3328df4c3116 | 102 | } |
ftagius | 3:e3974328d808 | 103 | if (debug) |
ftagius | 3:e3974328d808 | 104 | pc.printf("Opened log file %s\r\n",logFilePath); |
ftagius | 0:3328df4c3116 | 105 | // write the log file header |
ftagius | 0:3328df4c3116 | 106 | if (header == false) |
ftagius | 0:3328df4c3116 | 107 | { |
cmkachur | 2:3edb129c60b2 | 108 | fprintf(fp,"# date,time,raw timestamp,latitude,longitude,distance,interrupt,energy\r\n"); |
ftagius | 0:3328df4c3116 | 109 | header = true; |
ftagius | 0:3328df4c3116 | 110 | } |
ftagius | 0:3328df4c3116 | 111 | // write to the current log file |
ftagius | 0:3328df4c3116 | 112 | fprintf(fp,"%02d/%02d/20%02d,", gpsd.month, gpsd.day, gpsd.year); |
ftagius | 0:3328df4c3116 | 113 | fprintf(fp,"%02d:%02d:%02d,", gpsd.hour, gpsd.minute, gpsd.seconds); |
ftagius | 0:3328df4c3116 | 114 | fprintf(fp,"%7.0f,",gpsd.timef); |
ftagius | 0:3328df4c3116 | 115 | fprintf(fp,"%5.7f,%5.7f,", gpsd.lat_deg, gpsd.lon_deg); |
ftagius | 1:10d2a051285e | 116 | fprintf(fp,"%d,",distance); |
cmkachur | 2:3edb129c60b2 | 117 | fprintf(fp,"%d,",interruptSource); |
cmkachur | 2:3edb129c60b2 | 118 | fprintf(fp,"%ld",energy); |
ftagius | 0:3328df4c3116 | 119 | fprintf(fp,"\r\n"); |
ftagius | 0:3328df4c3116 | 120 | fflush(fp); |
ftagius | 0:3328df4c3116 | 121 | f_sync((FIL*)fp); |
ftagius | 0:3328df4c3116 | 122 | fclose(fp); |
ftagius | 0:3328df4c3116 | 123 | sd.unmount(); |
ftagius | 3:e3974328d808 | 124 | pc.printf("Event: "); |
ftagius | 3:e3974328d808 | 125 | switch (interruptSource) |
ftagius | 3:e3974328d808 | 126 | { |
ftagius | 3:e3974328d808 | 127 | case 1: |
ftagius | 3:e3974328d808 | 128 | pc.printf("Noise level too high\r\n"); |
ftagius | 3:e3974328d808 | 129 | break; |
ftagius | 3:e3974328d808 | 130 | case 4: |
ftagius | 3:e3974328d808 | 131 | pc.printf("Disturber\r\n"); |
ftagius | 3:e3974328d808 | 132 | break; |
ftagius | 3:e3974328d808 | 133 | case 8: |
ftagius | 3:e3974328d808 | 134 | pc.printf("Lightning detection, distance=%dkm energy=%ld\r\n", distance, energy); |
cmkachur | 6:96b0dbe76357 | 135 | |
cmkachur | 6:96b0dbe76357 | 136 | if (sysCfg.bClrStat) |
cmkachur | 6:96b0dbe76357 | 137 | ld.clearStats(); |
ftagius | 3:e3974328d808 | 138 | break; |
ftagius | 3:e3974328d808 | 139 | default: |
ftagius | 3:e3974328d808 | 140 | pc.printf("Unknown interrupt %d\r\n", OriginInt); |
ftagius | 3:e3974328d808 | 141 | |
ftagius | 3:e3974328d808 | 142 | } |
ftagius | 3:e3974328d808 | 143 | |
ftagius | 3:e3974328d808 | 144 | if (debug) |
ftagius | 3:e3974328d808 | 145 | pc.printf("Closed log file %s\r\n",logFilePath); |
ftagius | 0:3328df4c3116 | 146 | } |
ftagius | 0:3328df4c3116 | 147 | |
cmkachur | 4:4d26ba1ae0f7 | 148 | |
cmkachur | 4:4d26ba1ae0f7 | 149 | void writeCfgFile(unsigned char *pBuff, unsigned char buffLen, unsigned char fwVer) |
cmkachur | 4:4d26ba1ae0f7 | 150 | { |
cmkachur | 4:4d26ba1ae0f7 | 151 | char cfgFilePath[128]; |
cmkachur | 4:4d26ba1ae0f7 | 152 | FILE *fp; |
cmkachur | 4:4d26ba1ae0f7 | 153 | unsigned char cnt = 0; |
cmkachur | 4:4d26ba1ae0f7 | 154 | |
cmkachur | 4:4d26ba1ae0f7 | 155 | sprintf(cfgFilePath, "%s/%s", directory, "config_data.csv"); |
cmkachur | 4:4d26ba1ae0f7 | 156 | sd.mount(); |
cmkachur | 4:4d26ba1ae0f7 | 157 | fp = fopen(cfgFilePath, "w"); |
cmkachur | 4:4d26ba1ae0f7 | 158 | if(fp == NULL) { |
cmkachur | 4:4d26ba1ae0f7 | 159 | // retry |
cmkachur | 4:4d26ba1ae0f7 | 160 | wait_ms(200); |
cmkachur | 4:4d26ba1ae0f7 | 161 | fp = fopen(cfgFilePath, "w"); |
cmkachur | 4:4d26ba1ae0f7 | 162 | if (fp == NULL) |
cmkachur | 4:4d26ba1ae0f7 | 163 | { |
cmkachur | 4:4d26ba1ae0f7 | 164 | printf("Could not open file %s for writing\r\n",cfgFilePath); |
cmkachur | 4:4d26ba1ae0f7 | 165 | sd.unmount(); |
cmkachur | 4:4d26ba1ae0f7 | 166 | printf("unmount sd card \r\n"); |
cmkachur | 4:4d26ba1ae0f7 | 167 | return; |
cmkachur | 4:4d26ba1ae0f7 | 168 | } |
cmkachur | 4:4d26ba1ae0f7 | 169 | } |
cmkachur | 4:4d26ba1ae0f7 | 170 | if (debug) |
cmkachur | 4:4d26ba1ae0f7 | 171 | pc.printf("Opened log file %s\r\n",cfgFilePath); |
cmkachur | 4:4d26ba1ae0f7 | 172 | |
cmkachur | 4:4d26ba1ae0f7 | 173 | // write the header |
cmkachur | 4:4d26ba1ae0f7 | 174 | fprintf(fp,"# FW_VER,REG0,REG1,REG2,REG3,REG4,REG5,REG6,REG7,REG8\r\n"); |
cmkachur | 4:4d26ba1ae0f7 | 175 | |
cmkachur | 4:4d26ba1ae0f7 | 176 | // write the firmware version |
cmkachur | 4:4d26ba1ae0f7 | 177 | fprintf(fp,"%d,", fwVer); |
cmkachur | 4:4d26ba1ae0f7 | 178 | |
cmkachur | 4:4d26ba1ae0f7 | 179 | // write all the configuration registers |
cmkachur | 4:4d26ba1ae0f7 | 180 | for (cnt = 0; cnt < buffLen && cnt < MAX_CONFIG_REGS; ++cnt) |
cmkachur | 4:4d26ba1ae0f7 | 181 | fprintf(fp,"0x%x,", pBuff[cnt]); |
cmkachur | 4:4d26ba1ae0f7 | 182 | |
cmkachur | 4:4d26ba1ae0f7 | 183 | fflush(fp); |
cmkachur | 4:4d26ba1ae0f7 | 184 | f_sync((FIL*)fp); |
cmkachur | 4:4d26ba1ae0f7 | 185 | fclose(fp); |
cmkachur | 4:4d26ba1ae0f7 | 186 | sd.unmount(); |
cmkachur | 4:4d26ba1ae0f7 | 187 | |
cmkachur | 4:4d26ba1ae0f7 | 188 | if (debug) |
cmkachur | 4:4d26ba1ae0f7 | 189 | pc.printf("Closed cfg file %s\r\n",cfgFilePath); |
cmkachur | 4:4d26ba1ae0f7 | 190 | } |
cmkachur | 4:4d26ba1ae0f7 | 191 | |
cmkachur | 4:4d26ba1ae0f7 | 192 | |
cmkachur | 6:96b0dbe76357 | 193 | bool readCfgFile(char *paDirectory, sys_cfg_t *paSysCfg) |
cmkachur | 6:96b0dbe76357 | 194 | { |
cmkachur | 6:96b0dbe76357 | 195 | bool bRetVal = false; |
cmkachur | 6:96b0dbe76357 | 196 | sys_cfg_t lSysCfg; |
cmkachur | 6:96b0dbe76357 | 197 | typedef struct |
cmkachur | 6:96b0dbe76357 | 198 | { |
cmkachur | 6:96b0dbe76357 | 199 | const char *token; |
cmkachur | 6:96b0dbe76357 | 200 | unsigned int *value; |
cmkachur | 6:96b0dbe76357 | 201 | }token_map_t; |
cmkachur | 6:96b0dbe76357 | 202 | |
cmkachur | 6:96b0dbe76357 | 203 | token_map_t tokenMap[] = { {"outdoor_mode", &lSysCfg.bOutdoorMode}, |
cmkachur | 6:96b0dbe76357 | 204 | {"noise_floor", &lSysCfg.noiseFloor}, |
cmkachur | 6:96b0dbe76357 | 205 | {"watchdog_threshold", &lSysCfg.wdogThresh}, |
cmkachur | 6:96b0dbe76357 | 206 | {"clear_stat", &lSysCfg.bClrStat}, |
cmkachur | 6:96b0dbe76357 | 207 | {"minimum_num_light", &lSysCfg.minNumLight}, |
cmkachur | 6:96b0dbe76357 | 208 | {"spike_reject", &lSysCfg.spikeRej}, |
cmkachur | 6:96b0dbe76357 | 209 | {"mask_disturber", &lSysCfg.maskDisturber}, |
cmkachur | 6:96b0dbe76357 | 210 | }; |
cmkachur | 6:96b0dbe76357 | 211 | ConfigFile *plCfgFile; |
cmkachur | 6:96b0dbe76357 | 212 | string fileName; |
cmkachur | 6:96b0dbe76357 | 213 | int count = sizeof(tokenMap) / sizeof(token_map_t); |
cmkachur | 6:96b0dbe76357 | 214 | char szValue[8]; |
cmkachur | 6:96b0dbe76357 | 215 | |
cmkachur | 6:96b0dbe76357 | 216 | if ((NULL == paDirectory) || (NULL == paSysCfg)) |
cmkachur | 6:96b0dbe76357 | 217 | return false; |
cmkachur | 6:96b0dbe76357 | 218 | |
cmkachur | 6:96b0dbe76357 | 219 | // start with whatever configuration params are passed in, in case some params are not found in the file |
cmkachur | 6:96b0dbe76357 | 220 | lSysCfg = *paSysCfg; |
cmkachur | 6:96b0dbe76357 | 221 | |
cmkachur | 6:96b0dbe76357 | 222 | plCfgFile = new ConfigFile(); |
cmkachur | 6:96b0dbe76357 | 223 | fileName = paDirectory; |
cmkachur | 6:96b0dbe76357 | 224 | fileName += "/"; |
cmkachur | 6:96b0dbe76357 | 225 | fileName += "zeus.cfg"; |
cmkachur | 6:96b0dbe76357 | 226 | |
cmkachur | 6:96b0dbe76357 | 227 | sd.mount(); |
cmkachur | 6:96b0dbe76357 | 228 | printf ("\n\rReading configuration file[%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 229 | |
cmkachur | 6:96b0dbe76357 | 230 | // try to read values from the configuration file |
cmkachur | 6:96b0dbe76357 | 231 | if (plCfgFile->read((char *)fileName.c_str())) |
cmkachur | 6:96b0dbe76357 | 232 | { |
cmkachur | 6:96b0dbe76357 | 233 | for (int i = 0; i < count; ++i) |
cmkachur | 6:96b0dbe76357 | 234 | { |
cmkachur | 6:96b0dbe76357 | 235 | if (plCfgFile->getValue((char *)tokenMap[i].token, szValue, sizeof(szValue))) |
cmkachur | 6:96b0dbe76357 | 236 | { |
cmkachur | 6:96b0dbe76357 | 237 | if (1 == sscanf(szValue, "%d", tokenMap[i].value)) |
cmkachur | 6:96b0dbe76357 | 238 | { |
cmkachur | 6:96b0dbe76357 | 239 | printf ("Convert success %s[%d]\n\r", tokenMap[i].token, *(tokenMap[i].value)); |
cmkachur | 6:96b0dbe76357 | 240 | } |
cmkachur | 6:96b0dbe76357 | 241 | } |
cmkachur | 6:96b0dbe76357 | 242 | } |
cmkachur | 6:96b0dbe76357 | 243 | |
cmkachur | 6:96b0dbe76357 | 244 | // copy out the new found values |
cmkachur | 6:96b0dbe76357 | 245 | *paSysCfg = lSysCfg; |
cmkachur | 6:96b0dbe76357 | 246 | bRetVal = true; |
cmkachur | 6:96b0dbe76357 | 247 | } |
cmkachur | 6:96b0dbe76357 | 248 | else |
cmkachur | 6:96b0dbe76357 | 249 | { |
cmkachur | 6:96b0dbe76357 | 250 | printf ("Failed to read configuration file[%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 251 | |
cmkachur | 6:96b0dbe76357 | 252 | // try to create a default configuration file |
cmkachur | 6:96b0dbe76357 | 253 | for (int i = 0; i < count; ++i) |
cmkachur | 6:96b0dbe76357 | 254 | { |
cmkachur | 6:96b0dbe76357 | 255 | if (snprintf(szValue, sizeof(szValue), "%d", *(tokenMap[i].value))) |
cmkachur | 6:96b0dbe76357 | 256 | { |
cmkachur | 6:96b0dbe76357 | 257 | if (plCfgFile->setValue((char *)tokenMap[i].token, szValue)) |
cmkachur | 6:96b0dbe76357 | 258 | { |
cmkachur | 6:96b0dbe76357 | 259 | printf ("Token creation success %s[%s]\n\r", tokenMap[i].token, szValue); |
cmkachur | 6:96b0dbe76357 | 260 | } |
cmkachur | 6:96b0dbe76357 | 261 | } |
cmkachur | 6:96b0dbe76357 | 262 | } |
cmkachur | 6:96b0dbe76357 | 263 | if (plCfgFile->write((char *)fileName.c_str(), NULL, DOS)) |
cmkachur | 6:96b0dbe76357 | 264 | printf ("Success: Created default configuration file [%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 265 | else |
cmkachur | 6:96b0dbe76357 | 266 | printf ("Failed to create default configuration file [%s]\n\r", fileName.c_str()); |
cmkachur | 6:96b0dbe76357 | 267 | } |
cmkachur | 6:96b0dbe76357 | 268 | |
cmkachur | 6:96b0dbe76357 | 269 | if (plCfgFile) |
cmkachur | 6:96b0dbe76357 | 270 | delete plCfgFile; |
cmkachur | 6:96b0dbe76357 | 271 | |
cmkachur | 6:96b0dbe76357 | 272 | sd.unmount(); |
cmkachur | 6:96b0dbe76357 | 273 | return bRetVal; |
cmkachur | 6:96b0dbe76357 | 274 | } |
cmkachur | 6:96b0dbe76357 | 275 | |
cmkachur | 4:4d26ba1ae0f7 | 276 | |
ftagius | 0:3328df4c3116 | 277 | int main() |
ftagius | 0:3328df4c3116 | 278 | { |
cmkachur | 6:96b0dbe76357 | 279 | |
cmkachur | 4:4d26ba1ae0f7 | 280 | unsigned char regBuff[MAX_CONFIG_REGS]; |
ftagius | 0:3328df4c3116 | 281 | char c; |
ftagius | 0:3328df4c3116 | 282 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
ftagius | 0:3328df4c3116 | 283 | const int refresh_Time = 1000; //refresh time in ms |
ftagius | 0:3328df4c3116 | 284 | TSISensor tsi; // touch slider |
ftagius | 0:3328df4c3116 | 285 | unsigned long measFreq; |
ftagius | 1:10d2a051285e | 286 | rdistance=-1; |
ftagius | 1:10d2a051285e | 287 | rinterrupt=-1; |
ftagius | 0:3328df4c3116 | 288 | pc.baud(9600); |
ftagius | 0:3328df4c3116 | 289 | // initializations for gps |
ftagius | 0:3328df4c3116 | 290 | green = 1; |
ftagius | 0:3328df4c3116 | 291 | gpsd.setBaud(9600); |
ftagius | 0:3328df4c3116 | 292 | gpsd.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
ftagius | 0:3328df4c3116 | 293 | gpsd.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
ftagius | 0:3328df4c3116 | 294 | gpsd.sendCommand(PGCMD_ANTENNA); |
ftagius | 0:3328df4c3116 | 295 | gpsd.day=01; |
ftagius | 0:3328df4c3116 | 296 | gpsd.month=01; |
ftagius | 0:3328df4c3116 | 297 | gpsd.year=15; |
ftagius | 0:3328df4c3116 | 298 | gpsd.hour=1; |
ftagius | 0:3328df4c3116 | 299 | gpsd.minute=1; |
ftagius | 0:3328df4c3116 | 300 | gpsd.seconds=1; |
ftagius | 0:3328df4c3116 | 301 | red = 1; |
ftagius | 0:3328df4c3116 | 302 | green = 1; |
ftagius | 3:e3974328d808 | 303 | pc.printf("Touch slider to start lightning detector application\r\n"); |
ftagius | 0:3328df4c3116 | 304 | while(1) { |
ftagius | 0:3328df4c3116 | 305 | green = 1; // turn led off |
ftagius | 0:3328df4c3116 | 306 | wait_ms(200); |
ftagius | 0:3328df4c3116 | 307 | if (tsi.readPercentage()) |
ftagius | 0:3328df4c3116 | 308 | break; |
ftagius | 0:3328df4c3116 | 309 | green = 0; // turn led on |
ftagius | 0:3328df4c3116 | 310 | wait_ms(200); |
ftagius | 0:3328df4c3116 | 311 | if (tsi.readPercentage()) |
ftagius | 0:3328df4c3116 | 312 | break; |
ftagius | 0:3328df4c3116 | 313 | } |
cmkachur | 6:96b0dbe76357 | 314 | //Mount the filesystem |
cmkachur | 6:96b0dbe76357 | 315 | sd.mount(); |
cmkachur | 6:96b0dbe76357 | 316 | mkdir(directory, 0777); |
cmkachur | 6:96b0dbe76357 | 317 | sd.unmount(); |
cmkachur | 6:96b0dbe76357 | 318 | |
cmkachur | 6:96b0dbe76357 | 319 | // read configuration values fro SD file system to allow override of defaults in sysCfg |
cmkachur | 6:96b0dbe76357 | 320 | readCfgFile(directory, &sysCfg); |
cmkachur | 6:96b0dbe76357 | 321 | |
ftagius | 0:3328df4c3116 | 322 | |
ftagius | 3:e3974328d808 | 323 | pc.printf("\r\nInitialize lightning detector\r\n"); |
ftagius | 0:3328df4c3116 | 324 | //initializations for lightning detector |
ftagius | 0:3328df4c3116 | 325 | ld.init(); |
ftagius | 0:3328df4c3116 | 326 | ld.clearStats(); |
ftagius | 1:10d2a051285e | 327 | |
cmkachur | 5:1d4fd419cfb7 | 328 | /* The precision of the calibration will depend on the |
cmkachur | 5:1d4fd419cfb7 | 329 | accuracy of the resonance frequency of the antenna. It is |
cmkachur | 5:1d4fd419cfb7 | 330 | recommended to first trim the receiver antenna before the |
cmkachur | 5:1d4fd419cfb7 | 331 | calibration of both oscillators is done. |
cmkachur | 5:1d4fd419cfb7 | 332 | */ |
cmkachur | 5:1d4fd419cfb7 | 333 | measFreq = ld.tuneAntenna(IntLightning); |
cmkachur | 2:3edb129c60b2 | 334 | ld.calibrateRCOs(IntLightning); |
ftagius | 1:10d2a051285e | 335 | |
cmkachur | 6:96b0dbe76357 | 336 | if (sysCfg.bOutdoorMode) |
cmkachur | 6:96b0dbe76357 | 337 | ld.setOutdoors(); |
cmkachur | 6:96b0dbe76357 | 338 | else |
cmkachur | 6:96b0dbe76357 | 339 | ld.setIndoors(); |
cmkachur | 6:96b0dbe76357 | 340 | |
cmkachur | 6:96b0dbe76357 | 341 | ld.setMinimumLightnings(sysCfg.minNumLight); |
cmkachur | 6:96b0dbe76357 | 342 | ld.setSpikeRejection(sysCfg.spikeRej); |
cmkachur | 6:96b0dbe76357 | 343 | ld.setNoiseFloor(sysCfg.noiseFloor); |
cmkachur | 4:4d26ba1ae0f7 | 344 | |
cmkachur | 6:96b0dbe76357 | 345 | if (sysCfg.maskDisturber) |
cmkachur | 6:96b0dbe76357 | 346 | ld.disableDisturbers(); |
cmkachur | 6:96b0dbe76357 | 347 | else |
cmkachur | 6:96b0dbe76357 | 348 | ld.enableDisturbers(); |
cmkachur | 6:96b0dbe76357 | 349 | |
cmkachur | 6:96b0dbe76357 | 350 | ld.setWatchdogThreshold(sysCfg.wdogThresh); |
ftagius | 0:3328df4c3116 | 351 | IntLightning.rise(&DetectLightning); |
cmkachur | 6:96b0dbe76357 | 352 | |
cmkachur | 6:96b0dbe76357 | 353 | int MinLight = ld.getMinimumLightnings(); |
cmkachur | 6:96b0dbe76357 | 354 | int Noise = ld.getNoiseFloor(); |
cmkachur | 6:96b0dbe76357 | 355 | int TuneCap = ld.getTuneCap(); |
cmkachur | 6:96b0dbe76357 | 356 | int SpikeRej = ld.getSpikeRejection(); |
cmkachur | 6:96b0dbe76357 | 357 | int WatchDog = ld.getWatchdogThreshold(); |
ftagius | 0:3328df4c3116 | 358 | |
cmkachur | 6:96b0dbe76357 | 359 | pc.printf("\r\n Min lightning: %i", MinLight); |
ftagius | 0:3328df4c3116 | 360 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 361 | pc.printf(" Gain: 0x%02x\r\n",ld.getGain()); |
ftagius | 0:3328df4c3116 | 362 | pc.printf(" Noise: %i", Noise); |
ftagius | 0:3328df4c3116 | 363 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 364 | pc.printf(" Tune CAP: %i", TuneCap); |
ftagius | 0:3328df4c3116 | 365 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 366 | pc.printf(" Spike rej: %i", SpikeRej); |
ftagius | 0:3328df4c3116 | 367 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 368 | pc.printf(" Watchdog: %i", WatchDog); |
ftagius | 0:3328df4c3116 | 369 | pc.printf("\r\n"); |
ftagius | 0:3328df4c3116 | 370 | pc.printf(" LCO calibration: %ld Hz\n\r", measFreq); |
ftagius | 0:3328df4c3116 | 371 | |
ftagius | 0:3328df4c3116 | 372 | refresh_Timer.start(); //starts the clock on the timer |
cmkachur | 4:4d26ba1ae0f7 | 373 | |
cmkachur | 4:4d26ba1ae0f7 | 374 | // get a copy of all config registers |
cmkachur | 4:4d26ba1ae0f7 | 375 | ld.getConfigRegisters(regBuff, sizeof(regBuff)); |
cmkachur | 4:4d26ba1ae0f7 | 376 | |
cmkachur | 6:96b0dbe76357 | 377 | pc.printf("\n\rSystem Registers:\n\r"); |
cmkachur | 6:96b0dbe76357 | 378 | for (int i = 0; i < sizeof(regBuff); ++i) |
cmkachur | 6:96b0dbe76357 | 379 | pc.printf("REG%d [0x%02x]:\n\r", i, regBuff[i]); |
cmkachur | 6:96b0dbe76357 | 380 | pc.printf("\n\r"); |
cmkachur | 6:96b0dbe76357 | 381 | |
cmkachur | 4:4d26ba1ae0f7 | 382 | // write to the config file |
cmkachur | 4:4d26ba1ae0f7 | 383 | writeCfgFile(regBuff, sizeof(regBuff), FW_VER); |
ftagius | 1:10d2a051285e | 384 | |
ftagius | 0:3328df4c3116 | 385 | bool gpsFix=false; |
ftagius | 0:3328df4c3116 | 386 | while (1) |
ftagius | 0:3328df4c3116 | 387 | { |
ftagius | 3:e3974328d808 | 388 | if (OriginInt != -1) |
ftagius | 3:e3974328d808 | 389 | { |
ftagius | 3:e3974328d808 | 390 | // the ld detector generated an interrupt, log the event |
ftagius | 3:e3974328d808 | 391 | IntLightning.disable_irq(); |
cmkachur | 6:96b0dbe76357 | 392 | writeLogFile(OriginInt,gDistance, energy); |
cmkachur | 6:96b0dbe76357 | 393 | //ld.clearStats(); |
ftagius | 3:e3974328d808 | 394 | OriginInt = -1; |
cmkachur | 6:96b0dbe76357 | 395 | gDistance = -1; |
ftagius | 3:e3974328d808 | 396 | energy = -1; |
ftagius | 3:e3974328d808 | 397 | IntLightning.enable_irq(); |
ftagius | 3:e3974328d808 | 398 | } |
ftagius | 3:e3974328d808 | 399 | |
ftagius | 0:3328df4c3116 | 400 | c = gpsd.read(); //queries the GPS |
ftagius | 0:3328df4c3116 | 401 | if (debug) |
ftagius | 0:3328df4c3116 | 402 | { |
ftagius | 0:3328df4c3116 | 403 | if (c) { |
ftagius | 0:3328df4c3116 | 404 | printf("%c", c); //this line will echo the GPS data if not paused |
ftagius | 0:3328df4c3116 | 405 | continue; |
ftagius | 0:3328df4c3116 | 406 | } |
ftagius | 0:3328df4c3116 | 407 | } |
ftagius | 0:3328df4c3116 | 408 | |
ftagius | 0:3328df4c3116 | 409 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
ftagius | 0:3328df4c3116 | 410 | if ( gpsd.newNMEAreceived() ) { |
ftagius | 0:3328df4c3116 | 411 | if ( !gpsd.parse(gpsd.lastNMEA()) ) { |
ftagius | 0:3328df4c3116 | 412 | continue; |
ftagius | 0:3328df4c3116 | 413 | } |
ftagius | 0:3328df4c3116 | 414 | } |
ftagius | 0:3328df4c3116 | 415 | |
ftagius | 0:3328df4c3116 | 416 | // update globals with the lastest gps time stamp |
ftagius | 0:3328df4c3116 | 417 | day=gpsd.day; |
ftagius | 0:3328df4c3116 | 418 | month=gpsd.month; |
ftagius | 0:3328df4c3116 | 419 | year=gpsd.year; |
ftagius | 0:3328df4c3116 | 420 | hour=gpsd.hour; |
ftagius | 0:3328df4c3116 | 421 | minute=gpsd.minute; |
ftagius | 0:3328df4c3116 | 422 | seconds=gpsd.seconds; |
ftagius | 0:3328df4c3116 | 423 | |
ftagius | 0:3328df4c3116 | 424 | //check if enough time has passed to warrant printing GPS info to screen |
ftagius | 0:3328df4c3116 | 425 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
ftagius | 0:3328df4c3116 | 426 | if (refresh_Timer.read_ms() >= refresh_Time) |
ftagius | 0:3328df4c3116 | 427 | { |
ftagius | 0:3328df4c3116 | 428 | if (gpsd.fix) { |
ftagius | 0:3328df4c3116 | 429 | // got a gps fix |
ftagius | 0:3328df4c3116 | 430 | if (gpsFix == false) |
ftagius | 0:3328df4c3116 | 431 | { |
ftagius | 0:3328df4c3116 | 432 | // first time fix obtained |
ftagius | 0:3328df4c3116 | 433 | gpsFix = true; |
ftagius | 0:3328df4c3116 | 434 | pc.printf("GPS fix obtained on %02d/%02d/20%02d_%02d:%02d:%02d (UTC)\r\n",gpsd.month,gpsd.day,gpsd.year,gpsd.hour,gpsd.minute,gpsd.seconds); |
ftagius | 1:10d2a051285e | 435 | //pc.printf("Touch slider to suspend application\r\n"); |
ftagius | 0:3328df4c3116 | 436 | pc.printf("Waiting for lighting detection...\r\n"); |
ftagius | 0:3328df4c3116 | 437 | } |
ftagius | 0:3328df4c3116 | 438 | |
ftagius | 0:3328df4c3116 | 439 | //red = 1; // turn led off |
ftagius | 0:3328df4c3116 | 440 | //pc.printf("turn green on\r\n"); |
ftagius | 0:3328df4c3116 | 441 | green = 0; // turn led on |
ftagius | 0:3328df4c3116 | 442 | wait_ms(50); |
ftagius | 0:3328df4c3116 | 443 | } |
ftagius | 0:3328df4c3116 | 444 | else |
ftagius | 0:3328df4c3116 | 445 | { |
ftagius | 3:e3974328d808 | 446 | gpsFix = false; |
ftagius | 0:3328df4c3116 | 447 | pc.printf("Waiting for GPS FIX\r\n"); |
ftagius | 0:3328df4c3116 | 448 | red = 0; // turn led on |
ftagius | 0:3328df4c3116 | 449 | } |
ftagius | 0:3328df4c3116 | 450 | |
ftagius | 0:3328df4c3116 | 451 | // restart the timer for the gps print loop |
ftagius | 0:3328df4c3116 | 452 | // writeLogFile(-2); |
ftagius | 0:3328df4c3116 | 453 | refresh_Timer.reset(); |
ftagius | 0:3328df4c3116 | 454 | } |
ftagius | 0:3328df4c3116 | 455 | else |
ftagius | 0:3328df4c3116 | 456 | { |
ftagius | 0:3328df4c3116 | 457 | //red = 0; // turn led on |
ftagius | 0:3328df4c3116 | 458 | //pc.printf("turn green off\r\n"); |
ftagius | 0:3328df4c3116 | 459 | green = 1; // turn green led off |
ftagius | 0:3328df4c3116 | 460 | } // end else refresh timer |
ftagius | 0:3328df4c3116 | 461 | } |
ftagius | 0:3328df4c3116 | 462 | |
ftagius | 0:3328df4c3116 | 463 | } |