init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Revision 16:3bf5f1a5f869, committed 2018-12-17
- Comitter:
- pathfindr
- Date:
- Mon Dec 17 22:47:13 2018 +0000
- Parent:
- 15:7aad9a7f970c
- Child:
- 17:ba55c026b1d6
- Commit message:
- 7
Changed in this revision
--- a/main.cpp Mon Dec 17 19:17:39 2018 +0000
+++ b/main.cpp Mon Dec 17 22:47:13 2018 +0000
@@ -29,42 +29,54 @@
//------------------------------------------------------------------------------
#if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS
//IDENTITY
-static long long RET_imei __attribute__((section("noinit"),zero_init));
-static char RET_pfIdentifier[6] __attribute__((section("noinit"),zero_init));
+static long long RET_imei __attribute__((section("noinit"),zero_init));
+static char RET_pf_identifier[6] __attribute__((section("noinit"),zero_init));
+//SETTINGS
+static int RET_setting_firmware __attribute__((section("noinit"),zero_init));
+static uint8_t RET_setting_location_accuracy __attribute__((section("noinit"),zero_init));
+static uint32_t RET_setting_location_tx_interval_mins __attribute__((section("noinit"),zero_init));
+static uint32_t RET_setting_location_tx_failsafe_hrs __attribute__((section("noinit"),zero_init));
+static uint16_t RET_setting_location_timeout __attribute__((section("noinit"),zero_init));
+static uint8_t RET_setting_activity_mode __attribute__((section("noinit"),zero_init));
+static uint32_t RET_setting_activity_tx_interval_mins __attribute__((section("noinit"),zero_init));
+static uint32_t RET_setting_environmental_tx_interval_mins __attribute__((section("noinit"),zero_init));
+static uint16_t RET_setting_motion_g __attribute__((section("noinit"),zero_init));
+static time_t RET_setting_motion_start_seconds __attribute__((section("noinit"),zero_init));
+static time_t RET_setting_motion_stop_seconds __attribute__((section("noinit"),zero_init));
+static uint16_t RET_setting_impact_g __attribute__((section("noinit"),zero_init));
+static uint8_t RET_setting_impact_alert __attribute__((section("noinit"),zero_init));
+static uint16_t RET_setting_connection_timeout __attribute__((section("noinit"),zero_init));
+static uint16_t RET_setting_beacon_interval_seconds __attribute__((section("noinit"),zero_init));
+static uint16_t RET_setting_beacon_scan __attribute__((section("noinit"),zero_init));
//STATE
-static uint8_t RET_coldBoot __attribute__((section("noinit"),zero_init));
-static bool RET_haveSettings __attribute__((section("noinit"),zero_init));
-static uint8_t RET_mode __attribute__((section("noinit"),zero_init));
-static uint8_t RET_mode_prev __attribute__((section("noinit"),zero_init));
-static uint8_t RET_buttonPressCount __attribute__((section("noinit"),zero_init));
-static time_t RET_buttonPressTime __attribute__((section("noinit"),zero_init));
-static time_t RET_buttonReleaseTime __attribute__((section("noinit"),zero_init));
-static time_t RET_buttonHoldTime __attribute__((section("noinit"),zero_init));
-static time_t RET_RTCunixtime __attribute__((section("noinit"),zero_init));
-static long RET_RTCmicros __attribute__((section("noinit"),zero_init));
-static bool RET_requireImpactFlag __attribute__((section("noinit"),zero_init));
-//MOTION SETTINGS
-static bool RET_impactTriggered __attribute__((section("noinit"),zero_init));
-static bool RET_motionTriggered __attribute__((section("noinit"),zero_init));
-static time_t RET_motionStartTime __attribute__((section("noinit"),zero_init));
-static time_t RET_motionStopTime __attribute__((section("noinit"),zero_init));
-static time_t RET_motionStartThreshold_seconds __attribute__((section("noinit"),zero_init));
-static time_t RET_motionStopThreshold_seconds __attribute__((section("noinit"),zero_init));
-static bool RET_motionPendingOnState __attribute__((section("noinit"),zero_init));
-static bool RET_motionPendingOffState __attribute__((section("noinit"),zero_init));
-static bool RET_motionState __attribute__((section("noinit"),zero_init));
-static float RET_motionTotalActivityHours __attribute__((section("noinit"),zero_init));
-static char RET_motionData[100] __attribute__((section("noinit"),zero_init));
+static uint8_t RET_coldBoot __attribute__((section("noinit"),zero_init));
+static bool RET_haveSettings __attribute__((section("noinit"),zero_init));
+static uint8_t RET_state __attribute__((section("noinit"),zero_init));
+static uint8_t RET_state_prev __attribute__((section("noinit"),zero_init));
+static uint8_t RET_buttonPressCount __attribute__((section("noinit"),zero_init));
+static time_t RET_buttonPressTime __attribute__((section("noinit"),zero_init));
+static time_t RET_buttonReleaseTime __attribute__((section("noinit"),zero_init));
+static time_t RET_buttonHoldTime __attribute__((section("noinit"),zero_init));
+static time_t RET_RTCunixtime __attribute__((section("noinit"),zero_init));
+static long RET_RTCmicros __attribute__((section("noinit"),zero_init));
+//MOTION STATE
+static bool RET_motionTriggered __attribute__((section("noinit"),zero_init));
+static time_t RET_motionStartTime __attribute__((section("noinit"),zero_init));
+static time_t RET_motionStopTime __attribute__((section("noinit"),zero_init));
+static bool RET_motionPendingOnState __attribute__((section("noinit"),zero_init));
+static bool RET_motionPendingOffState __attribute__((section("noinit"),zero_init));
+static bool RET_motionState __attribute__((section("noinit"),zero_init));
+static float RET_motionTotalActivityHours __attribute__((section("noinit"),zero_init));
+static char RET_activityData[100] __attribute__((section("noinit"),zero_init));
+//IMPACT
+static bool RET_impactTriggered __attribute__((section("noinit"),zero_init));
//EVENT HANDLING
-static time_t RET_eventInterval_location_log __attribute__((section("noinit"),zero_init));
-static time_t RET_eventTime_location_log __attribute__((section("noinit"),zero_init));
-static time_t RET_eventTime_location_bc __attribute__((section("noinit"),zero_init));
-
-static time_t RET_eventInterval_environmental_log __attribute__((section("noinit"),zero_init));
-static time_t RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init));
-static time_t RET_eventTime_environmental_bc __attribute__((section("noinit"),zero_init));
-static time_t RET_eventTime_activity_bc __attribute__((section("noinit"),zero_init));
-static time_t RET_eventTime_wakeFromDormant __attribute__((section("noinit"),zero_init));
+static time_t RET_eventTime_location_log __attribute__((section("noinit"),zero_init));
+static time_t RET_eventTime_location_tx __attribute__((section("noinit"),zero_init));
+static time_t RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init));
+static time_t RET_eventTime_environmental_tx __attribute__((section("noinit"),zero_init));
+static time_t RET_eventTime_activity_tx __attribute__((section("noinit"),zero_init));
+static time_t RET_eventTime_wakeFromDormant __attribute__((section("noinit"),zero_init));
#elif defined ( __GNUC__ )
#elif defined ( __ICCARM__ )
#endif
@@ -123,20 +135,20 @@
if (RET_buttonHoldTime > 4000) {
RET_buttonHoldTime = 0;
RET_buttonPressCount = 0;
- RET_mode_prev = RET_mode;
- RET_mode = MODE_BUTTONHOLD;
+ RET_state_prev = RET_state;
+ RET_state = STATE_BUTTONHOLD;
mainthread.release();
} else {
if((RET_RTCmicros - RET_buttonPressTime) > 500) {
switch (RET_buttonPressCount) {
case 1 :
- RET_mode_prev = RET_mode;
- RET_mode = MODE_BUTTONPRESS1;
+ RET_state_prev = RET_state;
+ RET_state = STATE_BUTTONPRESS1;
mainthread.release();
break;
case 2 :
- RET_mode_prev = RET_mode;
- RET_mode = MODE_BUTTONPRESS2;
+ RET_state_prev = RET_state;
+ RET_state = STATE_BUTTONPRESS2;
mainthread.release();
break;
default :
@@ -150,41 +162,59 @@
void factoryReset() {
DEBUG("Factory Reset \n");
Thread::wait(500); //wait to ensure that devices are turned off
- GLOBAL_requireSoftReset = false;
-
- //RESET RETAINED VARS
+ //IDENTITY
RET_imei = 0;
- memset(RET_pfIdentifier,0,sizeof(RET_pfIdentifier));
+ memset(RET_pf_identifier,0,sizeof(RET_pf_identifier));
+ //SETTINGS
+ RET_setting_firmware = 0;
+ RET_setting_location_accuracy = 1;
+ RET_setting_location_tx_interval_mins = 1440;
+ RET_setting_location_tx_failsafe_hrs = (7*24); //7 days
+ RET_setting_location_timeout = 180;
+ RET_setting_activity_tx_interval_mins = 0;
+ RET_setting_environmental_tx_interval_mins = 0;
+ RET_setting_motion_g = 11;
+ RET_setting_motion_start_seconds = 120;
+ RET_setting_motion_stop_seconds = 120;
+ RET_setting_impact_g = 0;
+ RET_setting_impact_alert = 0;
+ RET_setting_connection_timeout = 180;
+ RET_setting_beacon_interval_seconds = 10;
+ RET_setting_beacon_scan = 0;
+ //STATE
RET_coldBoot = 1;
RET_haveSettings = 0;
- RET_mode = MODE_SETUP;
- RET_mode_prev = RET_mode;
+ RET_state = STATE_SETUP;
+ RET_state_prev = RET_state;
RET_RTCunixtime = 0;
RET_RTCmicros = 0;
-
- RET_eventInterval_environmental_log = 0;
- RET_eventTime_location_log = 0;
- RET_eventTime_location_bc = 0;
- RET_eventTime_environmental_log = 0;
- RET_eventTime_environmental_bc = 0;
- RET_eventTime_activity_bc = 0;
- RET_eventTime_wakeFromDormant = 0;
-
- RET_motionPendingOffState = 0;
- RET_motionPendingOnState = 0;
+ RET_buttonPressCount = 0;
+ RET_buttonPressTime = 0;
+ RET_buttonReleaseTime = 0;
+ RET_buttonHoldTime = 0;
+ RET_RTCunixtime = 0;
+ RET_RTCmicros = 0;
+ //MOTION STATE
RET_motionTriggered = 0;
- RET_impactTriggered = 0;
- RET_motionState = 0;
RET_motionStartTime = 0;
RET_motionStopTime = 0;
- RET_motionStartThreshold_seconds = 60;
- RET_motionStopThreshold_seconds = 60;
+ RET_motionPendingOnState = 0;
+ RET_motionPendingOffState = 0;
+ RET_motionState = 0;
RET_motionTotalActivityHours = 0.0;
- RET_buttonPressTime = 0;
- RET_buttonPressCount = 0;
- RET_buttonHoldTime = 0;
-
- //CONFIGURE ACCELL DEFAULT
+ memset(RET_activityData,0,sizeof(RET_activityData));
+ //IMPACT
+ RET_impactTriggered = 0;
+ //EVENT HANDLING
+ RET_eventTime_location_log = 0;
+ RET_eventTime_location_tx = 0;
+ RET_eventTime_environmental_log = 0;
+ RET_eventTime_environmental_tx = 0;
+ RET_eventTime_activity_tx = 0;
+ RET_eventTime_wakeFromDormant = 0;
+ //OTHER LOCAL GLOBALS
+ GLOBAL_requireSoftReset = false;
+ //PERIPHERAL RESET
lis3dh_configureForSleep(10,127);
}
void copyRETtoGLOBAL() {
@@ -242,14 +272,14 @@
if (RET_motionPendingOnState) {
//check if above threshold
time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime);
- if (inMotionForSeconds >= RET_motionStartThreshold_seconds) {
+ if (inMotionForSeconds >= RET_setting_motion_start_seconds) {
RET_motionState = true;
if (GLOBAL_debugLED) LED1blink(10,100);
}
}
if (RET_motionPendingOffState) {
time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime);
- if (noMotionForSeconds >= RET_motionStartThreshold_seconds) {
+ if (noMotionForSeconds >= RET_setting_motion_stop_seconds) {
RET_motionState = false;
RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0);
if (GLOBAL_debugLED) LED1blink(5,500);
@@ -296,34 +326,31 @@
bool setup() {
DEBUG("SETUP\n");
bool pass = true;
- //LED1on(0);
Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
if (modem.on()) {
RET_imei = modem.getIMEI();
GLOBAL_imei = RET_imei;
DEBUG("imei: %lld \n",RET_imei);
if (modem.registerOnNetwork(3,60000)) {
- char* bytestosend;
- sprintf(bytestosend,"(im:%lld,%s,v:%.2f,fr:1)\0",GLOBAL_imei,GLOBAL_defaultApi,GLOBAL_voltage);
- //DEBUG("SENDING:%s\n",bytestosend);
- char* result = modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi);
- if (result != "err") {
- DEBUG("result: %s \n",result);
- saveSettings(result);
- }
+ char* bytestosend;
+ sprintf(bytestosend,"(im:%lld,%s,v:%.2f,fr:1,z:SETUP,c:1)\0",GLOBAL_imei,GLOBAL_defaultApi,GLOBAL_voltage);
+ char* result = modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi);
+ if (result != "err") {
+ DEBUG("result: %s \n",result);
+ saveSettings(result);
+ }
} else {
- //modem failed to register on network
+ //FAILUREMODE modem failed to register on network
}
} else {
pass = false;
- //Modem failed to turn on
+ //FAILUREMODE Modem failed to turn on
}
- //LED1off();
- while (1) {
- Thread::wait(60000);
- watchdog.kick();
- }
+ //while (1) {
+ // Thread::wait(60000);
+ // watchdog.kick();
+ //}
//RESULT
if (pass) {
@@ -338,16 +365,16 @@
// STATE ENGINE
//------------------------------------------------------------------------------
void mainStateEngine() {
- DEBUG("StateEngine:%d \n",RET_mode);
- switch(RET_mode) {
- case MODE_SETUP :
+ DEBUG("StateEngine:%d \n",RET_state);
+ switch(RET_state) {
+ case STATE_SETUP :
factoryReset();
if (setup()) {
copyRETtoGLOBAL();
- RET_mode = MODE_NORMAL;
+ RET_state = STATE_NORMAL;
}
break;
- case MODE_NORMAL :
+ case STATE_NORMAL :
//check and log motion
checkMotion();
@@ -357,7 +384,7 @@
//getdata_environmental();
//event_location_log();
}
- if(RET_RTCunixtime > RET_eventTime_location_bc && RET_eventTime_location_bc > 0) {
+ if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
//getdata_battery();
//event_location_broadcast();
}
@@ -369,35 +396,35 @@
DEBUG("Temperature log: %u,%f \n", RET_RTCunixtime,temperature_c);
filesystem.addLogEntry_temperature(RET_RTCunixtime,temperature_c);
}
- if(RET_RTCunixtime > RET_eventTime_environmental_bc && RET_eventTime_environmental_bc > 0) {
+ if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) {
//event_environmental_bc();
//Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
}
//Activity
- if(RET_RTCunixtime > RET_eventTime_activity_bc && RET_eventTime_activity_bc > 0) {
+ if(RET_RTCunixtime > RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
//event_activity_bc();
}
break;
- case MODE_DORMANT :
+ case STATE_DORMANT :
if (RET_RTCunixtime > RET_eventTime_wakeFromDormant) {
- RET_mode = MODE_NORMAL;
+ RET_state = STATE_NORMAL;
}
break;
- case MODE_BUTTONPRESS1 :
+ case STATE_BUTTONPRESS1 :
LED1blink(1,300);
- RET_mode = RET_mode_prev;
+ RET_state = RET_state_prev;
break;
- case MODE_BUTTONPRESS2 :
+ case STATE_BUTTONPRESS2 :
LED1blink(3,300);
- RET_mode = RET_mode_prev;
+ RET_state = RET_state_prev;
break;
- case MODE_BUTTONHOLD :
+ case STATE_BUTTONHOLD :
LED1blink(10,300);
RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
- RET_mode = MODE_DORMANT;
+ RET_state = STATE_DORMANT;
break;
default :
- RET_mode = MODE_SETUP;
+ RET_state = STATE_SETUP;
}
}
@@ -444,7 +471,7 @@
GLOBAL_wakeTime = RET_RTCunixtime;
//MAIN LOGIC
- DEBUG("State: %i time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
+ DEBUG("State: %i time: %i, %i, %i, %i \n", RET_state, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
mainStateEngine();
//PRE-SLEEP ACTIONS
--- a/main.h Mon Dec 17 19:17:39 2018 +0000 +++ b/main.h Mon Dec 17 22:47:13 2018 +0000 @@ -54,7 +54,7 @@ //------------------------------------------------------------------------------ //Application headers //------------------------------------------------------------------------------ -#include "modes.h" +#include "states.h" #include "common.h" #include "sensors.h" #include "modem.h" @@ -63,8 +63,8 @@ //------------------------------------------------------------------------------ //Global macros //------------------------------------------------------------------------------ -#define lowByte(w) ((uint8_t) ((w) & 0xff)) -#define highByte(w) ((uint8_t) ((w) >> 8)) +//#define lowByte(w) ((uint8_t) ((w) & 0xff)) +//#define highByte(w) ((uint8_t) ((w) >> 8)) #define CONSOLE_DEBUG 1 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */ #if CONSOLE_DEBUG @@ -90,4 +90,4 @@ extern float GLOBAL_voltage; extern time_t GLOBAL_RTCunixtime; extern time_t GLOBAL_wakeTime; -#endif +#endif \ No newline at end of file
--- a/modem.cpp Mon Dec 17 19:17:39 2018 +0000
+++ b/modem.cpp Mon Dec 17 22:47:13 2018 +0000
@@ -190,16 +190,18 @@
char* Modem::USSDmessage(char* message, bool needResponse, int maxAttempts, char* api)
-{
- char bytestosend[160];
+{
uint8_t messageIndex = 1;
+ bool result;
int messageLength = strlen(message);
if (messageLength > USSD_MAXLENGTH) {
- snprintf(bytestosend,sizeof(bytestosend),"(im:%lld,%s,z:TOOBIG,c:%d)\0",messageIndex,GLOBAL_imei,api,messageIndex);
+ char message_failsafe[160];
+ snprintf(message_failsafe,sizeof(message_failsafe),"(im:%lld,%s,z:TOOBIG,c:%d)\0",messageIndex,GLOBAL_imei,api,messageIndex);
+ result = USSDsend(message_failsafe, maxAttempts);
} else {
- memcpy(message, bytestosend, sizeof(bytestosend));
+ result = USSDsend(message, maxAttempts);
}
- if (USSDsend(bytestosend, maxAttempts)) {
+ if (result) {
if (needResponse) {
char* response = USSDreceive(messageIndex);
if (response != "err") {
--- a/modem.h Mon Dec 17 19:17:39 2018 +0000
+++ b/modem.h Mon Dec 17 22:47:13 2018 +0000
@@ -7,7 +7,7 @@
#define UC20_BAUDRATE 115200
#define SIM7600_BAUDRATE 115200
- #define USSD_MAXLENGTH 1
+ #define USSD_MAXLENGTH 160
class Modem
{
--- a/modes.h Mon Dec 17 19:17:39 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -#define MODE_SETUP 0 -#define MODE_NORMAL 1 -#define MODE_DORMANT 99 -#define MODE_BUTTONPRESS1 80 -#define MODE_BUTTONPRESS2 81 -#define MODE_BUTTONHOLD 82 -//TASKS - -/* -Location - OperationMode, FixType, timeout, timesbitmap, -Environmental - Log interval, Broadcast Interval -Impact - Threshold, Log, AlarmBroadcast -Activity - G Threshold, T Threshold, Broadcast Interval - -*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/states.h Mon Dec 17 22:47:13 2018 +0000 @@ -0,0 +1,15 @@ +#define STATE_SETUP 0 +#define STATE_NORMAL 1 +#define STATE_DORMANT 99 +#define STATE_BUTTONPRESS1 80 +#define STATE_BUTTONPRESS2 81 +#define STATE_BUTTONHOLD 82 +//TASKS + +/* +Location - OperationMode, FixType, timeout, timesbitmap, +Environmental - Log interval, Broadcast Interval +Impact - Threshold, Log, AlarmBroadcast +Activity - G Threshold, T Threshold, Broadcast Interval + +*/ \ No newline at end of file