init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Sun Dec 23 20:24:34 2018 +0000
Parent:
24:c161db07557f
Child:
26:fa3579737329
Commit message:
gfg

Changed in this revision

SI7060.cpp Show annotated file Show diff for this revision Revisions of this file
SI7060.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
modem.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SI7060.cpp	Sun Dec 23 17:24:42 2018 +0000
+++ b/SI7060.cpp	Sun Dec 23 20:24:34 2018 +0000
@@ -17,6 +17,7 @@
     _i2c.read(_address, _ret, 1);
     return _ret[0];
 };
+
 void SI7060::writeValue(uint8_t registerAdd, uint8_t value) {
     char cmd[2];
     cmd[0] = registerAdd;
@@ -24,8 +25,26 @@
     _i2c.write(_address, cmd, 2);
 };
 
+void SI7060::sleep() {
+  // Prepare Mesure
+  writeValue(CMD_SI7060_meas,0x01);
+};
+
+void SI7060::prepare() {
+    uint8_t _ret;
+    _ret = readValue(CMD_SI7060_ID);
+    _ret = readValue(CMD_SI7060_meas);
+    // Prepare Mesure
+    writeValue(CMD_SI7060_meas,0x04);
+    _ret = readValue(CMD_SI7060_meas);
+    writeValue(CMD_SI7060_sw_op,0x4E);
+    writeValue(CMD_SI7060_sw_hyst,0x1C);
+};
+
 float SI7060::getTemperature(void) 
 {
+    prepare();
+    
     float _temp;
     uint8_t _Dspsigm;
     uint8_t _Dspsigl;
@@ -35,6 +54,8 @@
     _ret = readValue(CMD_SI7060_Dspsigl);
     _Dspsigl = _ret;
     _temp = 55+ ((float)(256*_Dspsigm)+(float)(_Dspsigl-16384))/160;
+    
+    sleep();
     return _temp;
 }
 
--- a/SI7060.h	Sun Dec 23 17:24:42 2018 +0000
+++ b/SI7060.h	Sun Dec 23 20:24:34 2018 +0000
@@ -25,6 +25,8 @@
             //funcs
             char readValue(uint8_t registerAdd);
             void writeValue(uint8_t registerAdd, uint8_t value);
+            void prepare(void);
+            void sleep(void);
             float getTemperature(void);
             
         private:
--- a/main.cpp	Sun Dec 23 17:24:42 2018 +0000
+++ b/main.cpp	Sun Dec 23 20:24:34 2018 +0000
@@ -28,8 +28,6 @@
 bool GLOBAL_needToConfigureLis3dh = false;
 bool GLOBAL_registeredOnNetwork = false;
 bool GLOBAL_modemOn = false;
-long long GLOBAL_imei = 0;
-float GLOBAL_voltage = 0.0;
 time_t GLOBAL_RTCunixtime = 0;
 time_t GLOBAL_wakeTime = 0;
 
@@ -178,15 +176,16 @@
     DEBUG("RET_setting_beacon_interval_seconds:%d \n",RET_setting_beacon_interval_seconds);
     DEBUG("RET_setting_beacon_scan:%d \n",RET_setting_beacon_scan);
 }
-void getBatteryV() { //this creates an extra 400ua of power usage!!!
+float getBatteryV() { //this creates an extra 400ua of power usage!!!
     NRF52_SAADC batteryIn;
     batteryIn.addChannel(9); // vdd for battery
     batteryIn.calibrate();
-    for (uint8_t i = 1; i <= 2; i++) { // need to get it 3 times to get accurate data, first one is always low for some reason
+    float voltage = 0.0;
+    for (uint8_t i = 1; i <= 2; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
         batteryIn.updateData();
-        GLOBAL_voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
+        voltage = (batteryIn.getData()[0])*(1.0/1024.0)*3.65;
     }
-    DEBUG("Voltage:%.2f\n",GLOBAL_voltage);
+    return voltage;
 }
 float getTemperature() {
     SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
@@ -312,9 +311,6 @@
     //PERIPHERAL RESET
     lis3dh_configureForSleep(10,127);
 }
-void copyRETtoGLOBAL() {
-    GLOBAL_imei = RET_imei; 
-}
 bool selfTest() {
     int test_count = 0;
     int test_pass = 0;
@@ -346,7 +342,7 @@
 //------------------------------------------------------------------------------ 
 void checkMotion() {
     if (lis3dh_int2) {
-        if (GLOBAL_debugLED) LED1blink(1,100);
+        if (GLOBAL_debugLED) LED1blink(1,50);
         RET_motionTriggered = true;
         GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
         if (!RET_motionPendingOnState) {
@@ -371,7 +367,7 @@
         if (inMotionForSeconds >= RET_setting_motion_start_seconds && RET_motionState == false) {
             RET_motionState = true;
             RET_motionTriggeredInTXInterval = true;
-            if (GLOBAL_debugLED) LED1blink(10,100);
+            if (GLOBAL_debugLED) LED1blink(5,50);
             if (RET_setting_location_tx_failsafe_hrs > 0) {
                 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
                 sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins);
@@ -385,7 +381,7 @@
             RET_motionState = false;
             GLOBAL_motionStopFlagTriggered = true;
             RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); 
-            if (GLOBAL_debugLED) LED1blink(5,500);
+            if (GLOBAL_debugLED) LED1blink(3,400);
             if (RET_setting_location_tx_failsafe_hrs > 0) {
                 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
                 sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins);
@@ -472,16 +468,18 @@
 //------------------------------------------------------------------------------ 
 bool setup() {
     bool pass = true;
+    float voltage = getBatteryV();
+    float temperature = getTemperature();
+    bool selftestresult = selfTest();
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
-        RET_imei = modem.getIMEI();
-        GLOBAL_imei = RET_imei;
-        DEBUG("imei: %lld \n",RET_imei);
+        //RET_imei = modem.getIMEI();
+        //DEBUG("imei: %lld \n",RET_imei);
         //char* modemModel = modem.getModemModel();
         if (modem.registerOnNetwork(2,180000)) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,im:%lld,a:loc,f:%d,v:%.2f,e:%d,z:SETUP,c:1)\0",GLOBAL_defaultApi,GLOBAL_imei,FW_VERSION,GLOBAL_voltage,timetaken);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.2f,e:%d,z:SETUP,c:1)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken);
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
@@ -512,7 +510,7 @@
 //------------------------------------------------------------------------------ 
 void event_shakertest_tx() {
     DEBUG("SHAKER TEST\n");
-    getBatteryV();  
+    float voltage = getBatteryV();  
     float temperature = getTemperature();
     bool selftestresult = selfTest();
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
@@ -520,7 +518,7 @@
         if (modem.registerOnNetwork(2,180000)) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.2f,v:%.2f,z:SHAKERTEST-%d,e:%d,c:1)\0",GLOBAL_defaultApi,FW_VERSION,temperature,GLOBAL_voltage,selftestresult,timetaken);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.2f,v:%.2f,z:SHAKERTEST-%d,e:%d,c:1)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
@@ -539,8 +537,9 @@
 
 void event_location_tx() {
     DEBUG("LOCATION TX\n");
-    getBatteryV();
+    float voltage = getBatteryV();
     float temperature = getTemperature();
+    bool selftestresult = selfTest();
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
         char locString[70];
@@ -552,7 +551,7 @@
         if (modem.registerOnNetwork(2,180000)) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytesToSend[160];
-            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.2f,v:%.2f,z:OK,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,GLOBAL_voltage,timetaken,locString);
+            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.2f,v:%.2f,z:OK%d,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken,locString);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
@@ -571,7 +570,6 @@
 
 void event_activity_tx() {
     DEBUG("ACTIVITY TX\n");
-    getBatteryV();
     float temperature = getTemperature();
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
@@ -602,9 +600,8 @@
         case STATE_SETUP :
             DEBUG("STATE:SETUP\n");
             factoryReset();
-            getBatteryV();
             if (setup()) {
-                copyRETtoGLOBAL();
+                // All good
             } else {
                 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs
                 setState(STATE_DORMANT);
@@ -618,21 +615,19 @@
             
             //LOCATION EVENT
             bool run_location_tx = false;
-            if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { DEBUG("INTERVAL LOC TX...\n"); run_location_tx = true; }
+            if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
             //Location Failsafe timer catchall
             if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
-            if (run_location_tx) {
-                event_location_tx();
-            }
+            if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
             
             //ACTIVITY EVENT
             bool run_activity_tx = false;
-            if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) { 
+            if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0 && strlen(RET_activityData) > 1) { 
                 run_activity_tx = true; 
-                //event_shakertest_tx(); 
+                //event_shakertest_tx();
             }
             if (strlen(RET_activityData) > 130) { run_activity_tx = true; }
-            if (run_activity_tx) {event_activity_tx();}
+            if (run_activity_tx) { DEBUG("ACTIVITY TX...\n"); event_activity_tx();}
             
             break;
         case STATE_DORMANT :
@@ -692,7 +687,6 @@
     RTCticker.attach(&RTCtick, 1.0);
     button.fall(&buttonPress); //does this affect power?
     button.rise(&buttonRelease);
-    copyRETtoGLOBAL();
     
     //CHECK IF THIS IS RESET
     //0x00000004 == soft reset  //0x00000002 == watchdog  //0x00000001 == button/hardreset 
@@ -726,8 +720,7 @@
         system_reset();
     }
     
-    while(true) {
-        if (!memoryIntegrityCheck()) { RET_coldBoot = 1;}
+    //while(true) {
         watchdogKick();
         //LOG START TIME
         GLOBAL_wakeTime = RET_RTCunixtime;
@@ -735,20 +728,19 @@
         //MAIN LOGIC
         DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
         DEBUG("ACTIVITY:%s\n",RET_activityData);
+        float temperature = getTemperature();
+        DEBUG("temp:%.2f\n",temperature);
         mainStateEngine();
         
-        //sprintf(RET_activityData+strlen(RET_activityData),"test");
-        //int result = strlen(RET_activityData);
-        //DEBUG("size:%d\n",result)
-        
         //PRE-SLEEP ACTIONS
         if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(10,127); }
-        if (GLOBAL_modemOn) {
+        turnOffEverything();
+        /*if (GLOBAL_modemOn) {
             DEBUG("SHUTTING DOWN MODEM\n");
             Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
             modem.off(true);
-        }
-        RET_coldBoot = 0;
+        }*/
+        
         
         //SLEEP DIFFERENT FOR THIS SHAKER TEST VERSION - AS IS WILL GIVE 450ua sleep if in motion
         /*
@@ -756,7 +748,9 @@
         NRF_UART0->ENABLE = 0; //NO MORE SERIAL OR DEBUG OUTPUTS AFTER THIS
         mainthread.wait(SLEEP_FRAME);
         */
+        
+        RET_coldBoot = 0;
         RET_sleepToggle = 1;
         system_reset();
-    }
+    //}
 }
\ No newline at end of file
--- a/main.h	Sun Dec 23 17:24:42 2018 +0000
+++ b/main.h	Sun Dec 23 20:24:34 2018 +0000
@@ -66,8 +66,6 @@
 extern bool GLOBAL_needToConfigureLis3dh;
 extern bool GLOBAL_registeredOnNetwork;
 extern bool GLOBAL_modemOn;
-extern long long GLOBAL_imei;
-extern float GLOBAL_voltage;
 extern time_t GLOBAL_RTCunixtime;
 extern time_t GLOBAL_wakeTime;
 
--- a/modem.cpp	Sun Dec 23 17:24:42 2018 +0000
+++ b/modem.cpp	Sun Dec 23 20:24:34 2018 +0000
@@ -238,7 +238,7 @@
     int messageLength = strlen(message);
     if (messageLength > USSD_MAXLENGTH) {
         char message_failsafe[100];
-        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,im:%lld,z:TOOBIG,c:%d)\0",api,GLOBAL_imei,messageIndex);
+        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,z:TOOBIG,c:%d)\0",api,messageIndex);
         result = USSDsend(message_failsafe, maxAttempts);
     } else {
         result = USSDsend(message, maxAttempts);