BLE Lightning sensor for Nordic NRF51822 based module

Dependencies:   AS3935 AS3935_ext BLE_API mbed nRF51822 nrf51_rtc

Revision:
4:8f815f8d804e
Parent:
3:2ea547dab8a8
Child:
5:b8961da0d06a
--- a/main.cpp	Sun Aug 30 10:15:31 2015 +0000
+++ b/main.cpp	Sun Aug 30 12:20:29 2015 +0000
@@ -2,6 +2,8 @@
 
 */
 
+#include "string.h"
+
 #include "mbed.h"
 #include "nrf51_rtc.h"
 #include "AS3935_ext.h"
@@ -137,13 +139,23 @@
 
 void onDataWritten(const GattWriteCallbackParams *params)
 {
-    uint8_t buff[21];
+    char buff[21];
     if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
         DEBUG("received %u bytes\n\r", bytesRead);
         strncpy((char*)buff,(char*)params->data,bytesRead);
         // ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), params->data, bytesRead);
-        ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), buff, bytesRead);
+        // ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), buff, bytesRead);
+        
+        if ( strncmp("get time",buff,bytesRead) == 0 ) {
+            time_t rawtime=rtc.time();
+            BLEC("Current Time:%d\n",rawtime);
+        } else if ( strncmp("get record ",buff,strlen("Get Record ")) == 0 ) {
+            int i=atoi(&buff[strlen("Get Record ")]);
+            if ( i >= 0 && i < s_evrecord ) {
+                BLEC("record %1d:%d:%1x:%2x\n",i,evrecord[i].time,evrecord[i].event,evrecord[i].distance);
+            }
+        }
     }
 }
 
@@ -165,6 +177,13 @@
     OriginInt = Lightning.interruptSource();
     distance = Lightning.lightningDistanceKm();
 
+    for(int i=1;i<s_evrecord;i++) {
+        evrecord[i] = evrecord[i-1];
+    }
+    evrecord[0].time = rawtime;
+    evrecord[0].event = OriginInt;
+    evrecord[0].distance = distance;
+
     if (OriginInt == 1) {
         led2 = !led2; 
         DEBUG("%24s : Noise level too high. %d km\r\n",date,distance);
@@ -259,25 +278,9 @@
 #endif // #if NEED_BLE_CONSOLE
 
     while (true) {
-        if ( buff_flash_flag != 0) {
-            wait_ms(4000);
-            BLEC("test1\n");
-            wait_ms(300);
-            time_t rawtime=rtc.time();
-            BLEC("%4x:current time\n",rawtime);
-    
-            for(int i=0;i<s_evrecord;i++) {
-                BLEC("%1d:%4x:%1x:%2x\n",i,evrecord[i].time,evrecord[i].event,evrecord[i].distance);
-                wait_ms(300);
-            }
-            BLEC("finished\n");
-
-            buff_flash_flag = 0;
-            
-        } else {
-            BLEC("pong\n");
-            wait(5);
-        }
+#if NEED_BLE_CONSOLE
+         ble.waitForEvent();
+#endif // #if NEED_BLE_CONSOLE
     }
 }