basic lightning detector with gps and sd card logging

Dependencies:   AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile

Revision:
4:4d26ba1ae0f7
Parent:
3:e3974328d808
Child:
5:1d4fd419cfb7
--- a/main.cpp	Wed Jun 24 12:37:31 2015 +0000
+++ b/main.cpp	Wed Jun 24 16:36:55 2015 +0000
@@ -5,6 +5,8 @@
 #include "SDFileSystem.h"
 #include "AS3935.h"
 
+#define FW_VER  1
+
  // frdm-kl25z as3935 connections for spi1 
  // ------------------------------------------------
  // Header -- kl25z -- SD/MMC          
@@ -117,8 +119,55 @@
         pc.printf("Closed log file %s\r\n",logFilePath);
 }     
 
+
+void writeCfgFile(unsigned char *pBuff, unsigned char buffLen, unsigned char fwVer)
+{
+    char cfgFilePath[128];
+    FILE *fp;
+    unsigned char cnt = 0; 
+    
+    sprintf(cfgFilePath, "%s/%s", directory, "config_data.csv");
+    sd.mount();
+    fp = fopen(cfgFilePath, "w");
+    if(fp == NULL) {
+        // retry
+        wait_ms(200);
+        fp = fopen(cfgFilePath, "w");
+        if (fp == NULL)
+        {
+            printf("Could not open file %s for writing\r\n",cfgFilePath);
+            sd.unmount();
+            printf("unmount sd card \r\n");
+            return;
+        }
+    }
+    if (debug)
+        pc.printf("Opened log file %s\r\n",cfgFilePath);
+
+    // write the header 
+    fprintf(fp,"# FW_VER,REG0,REG1,REG2,REG3,REG4,REG5,REG6,REG7,REG8\r\n");
+    
+    // write the firmware version 
+    fprintf(fp,"%d,", fwVer);
+
+    // write all the configuration registers 
+    for (cnt = 0; cnt < buffLen && cnt < MAX_CONFIG_REGS; ++cnt) 
+        fprintf(fp,"0x%x,", pBuff[cnt]);
+    
+    fflush(fp);
+    f_sync((FIL*)fp); 
+    fclose(fp);
+    sd.unmount();
+
+    if (debug)
+        pc.printf("Closed cfg file %s\r\n",cfgFilePath);
+}     
+
+
+
 int main()
 {
+    unsigned char regBuff[MAX_CONFIG_REGS]; 
     char c;
     Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
     const int refresh_Time = 1000; //refresh time in ms
@@ -161,20 +210,16 @@
     ld.calibrateRCOs(IntLightning);
     measFreq = ld.tuneAntenna(IntLightning);
     
-    //ld.powerUp(); 
-    
-    //ld.setIndoors(); 
     ld.setOutdoors(); 
     ld.setMinimumLightnings(0);
     ld.setSpikeRejection(2);
     ld.setNoiseFloor(2);
-    //ld.disableDisturbers();
+
     ld.enableDisturbers();
     ld.setWatchdogThreshold(4);
     IntLightning.rise(&DetectLightning);
     int MinBlysk = ld.getMinimumLightnings();
     int Noise = ld.getNoiseFloor();
-    // ld.setTuneCap(5); //  500kHz
     int TuneCap = ld.getTuneCap();
     int SpikeRej = ld.getSpikeRejection();
     int WatchDog = ld.getWatchdogThreshold();
@@ -198,6 +243,12 @@
     sd.mount();
     mkdir(directory, 0777);
     sd.unmount();
+
+    // get a copy of all config registers
+    ld.getConfigRegisters(regBuff, sizeof(regBuff));
+   
+    // write to the config file 
+    writeCfgFile(regBuff, sizeof(regBuff), FW_VER); 
     
     bool gpsFix=false;
     while (1)
@@ -274,45 +325,7 @@
             //red = 0; // turn led on
             //pc.printf("turn green off\r\n");
             green = 1; // turn green led off
-            #if 0
-            // check the slider.  if touched, disable lighting interrupts and wait for another slider touch
-            if (tsi.readPercentage())
-            {
-                //IntLightning.disable_irq();
-                pc.printf("Touch slider to continue application\r\n");
-                while (1)
-                {
-                   
-                    green = 1;   // turn led off
-                    wait_ms(200);
-                    // break of of the loop if the slider is touched
-                    if (tsi.readPercentage())
-                    {
-                        green = 1;
-                        red = 1;
-                        pc.printf("Now monitoring for lightning strikes\r\n");
-                        //IntLightning.enable_irq();
-                        break;
-                    }
-                    green = 0;  // turn led on
-                    wait_ms(200);
-                    // break of of the loop if the slider is touched
-                    if (tsi.readPercentage())
-                    {
-                        green = 1;
-                        red = 1;
-                        pc.printf("Now monitoring for lightning strikes\r\n");
-                        //IntLightning.enable_irq();
-                        break;
-                    }
-                    
-                } // end while 1 for slider
-            } // end if tsi read      
-            #endif
         } // end else refresh timer            
-   
     }
-
-            
   
 }