diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 2929a62..f3ab001 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -1,22 +1,42 @@ #include "Core.h" #include "Config.h" +#include "Flash.h" #define LOGGER_NAME "Core" namespace core { uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;; - namespace details { - template size_t getFlashArraySize(T(&)[N]) { return N; } - template void readFromFlash(const T *source, T &dest) { - memcpy_P(&dest, source, sizeof(T)); + void main() { + gps::powerOn(); + SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); + gps::powerOff(); + + if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { + tmElements_t time; + gps::getTime(time); + rtc::powerOn(); + rtc::setTime(time); + rtc::powerOff(); + + positions::appendLast(); + + uint8_t velocity; + gps::getVelocity(velocity); + core::setSleepTime(velocity); } + + if (positions::needsToSend()) { + positions::send(); + } + + mainunit::deepSleep(core::sleepTime); } void setSleepTime(uint8_t velocity) { - for (uint8_t i = 0; i < details::getFlashArraySize(config::defaultSleepTimings); i++) { + for (uint8_t i = 0; i < flash::getFlashArraySize(config::defaultSleepTimings); i++) { sleepTimings_t timing; - details::readFromFlash(&config::defaultSleepTimings[i], timing); + flash::readFromFlash(&config::defaultSleepTimings[i], timing); if (velocity > timing.speed) continue; diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index 16c3db7..098c32f 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -3,9 +3,16 @@ #include #include "Debug.h" +#include "Gps.h" +#include "MainUnit.h" +#include "Network.h" +#include "Rtc.h" +#include "Pins.h" +#include "Positions.h" namespace core { extern uint16_t sleepTime; + void main(); void setSleepTime(uint8_t velocity); } \ No newline at end of file diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 6b6c83d..dac94fb 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -1,16 +1,110 @@ #include "Debug.h" +#include "Flash.h" + +#define MENU_ENTRY(name, text) const char MENU_##name[] PROGMEM = text + +MENU_ENTRY(HEADER, "-- Debug Menu --"); +MENU_ENTRY(SEPARATOR, "----"); + +MENU_ENTRY(RUN, "[0] Run"); +MENU_ENTRY(RUN_ONCE, "[1] Run once"); +MENU_ENTRY(RAM, "[2] Read battery"); +MENU_ENTRY(READ_BATTERY, "[3] Read battery"); +MENU_ENTRY(GPS_ON, "[4] GPS On"); +MENU_ENTRY(GPS_OFF, "[5] GPS Off"); +MENU_ENTRY(GPS_GET, "[6] Get GPS position"); +MENU_ENTRY(RTC_ON, "[7] RTC On"); +MENU_ENTRY(RTC_OFF, "[8] RTC Off"); +MENU_ENTRY(RTC_SET, "[9] Get RTC time"); +MENU_ENTRY(RTC_GET, "[10] Set RTC time"); +MENU_ENTRY(QUESTION, "?"); + +const char * const MENU_ENTRIES[] PROGMEM = { + MENU_HEADER, + MENU_RUN, + MENU_RUN_ONCE, + + MENU_SEPARATOR, + + MENU_RAM, + MENU_READ_BATTERY, + + MENU_SEPARATOR, + + MENU_GPS_ON, + MENU_GPS_OFF, + MENU_GPS_GET, + + MENU_SEPARATOR, + + MENU_RTC_ON, + MENU_RTC_OFF, + MENU_RTC_SET, + MENU_RTC_GET, + + MENU_QUESTION + + +}; + +int freeRam2() { + extern int __heap_start, *__brkval; + int v; + return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval); +} namespace debug { void waitForSerial() { while (!Serial); - Serial.begin(115200); + Serial.begin(DEBUG_SERIAL_SPEED); } int freeRam() { - extern int __heap_start, *__brkval; - int v; - return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval); + return freeRam2(); + } + + GPSTRACKER_DEBUG_COMMAND menu() { + if (!Serial) return GPSTRACKER_DEBUG_COMMAND::NONE; + + uint8_t command; + + while (command > static_cast(GPSTRACKER_DEBUG_COMMAND::RTC_SET)) { + for (uint8_t i = 0; i < flash::getFlashArraySize(MENU_ENTRIES); i++) { + Serial.println(reinterpret_cast(pgm_read_word(MENU_ENTRIES[i]))); + } + + while (!Serial.available()); + command = static_cast(Serial.parseInt()); + } + + return static_cast(command); } + void getAndDisplayGpsPosition() { + SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); + + Log.notice(F("%d %s"), gpsStatus, gps::lastPosition); + } + + void getAndDisplayBattery() { + SIM808ChargingStatus status = hardware::sim808::device.getChargingState(); + + Log.notice("%d %d%% %dmV", status.state, status.level, status.voltage); + } + + void getAndDisplayRtcTime() { + tmElements_t time; + rtc::getTime(time); + + Log.notice(F("%d/%d/%d %d:%d:%d"), time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); + } + + void setRtcTime() { + tmElements_t time; + gps::getTime(time); + rtc::setTime(time); + + Log.notice(F("OK")); + } } \ No newline at end of file diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index f0266a3..b79892e 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -1,10 +1,14 @@ #pragma once #include +#include -#ifdef _DEBUG +#include "Config.h" +#include "Hardware.h" +#include "Gps.h" +#include "Rtc.h" -#include +#ifdef _DEBUG #define VERBOSE(f) Log.verbose(F("[" LOGGER_NAME "::" f "]\n")) #define VERBOSE_MSG(f, msg) Log.verbose(F("[" LOGGER_NAME "::" f "] " msg "\n")) @@ -20,9 +24,34 @@ #endif +#define DEBUG_SERIAL_SPEED 9600 + namespace debug { + enum class GPSTRACKER_DEBUG_COMMAND : uint8_t { + NONE = 0, + ONCE = 1, + RAM = 2, + BATTERY = 3, + GPS_ON = 4, + GPS_OFF = 5, + GPS_GET = 6, + RTC_ON = 7, + RTC_OFF = 8, + RTC_GET = 9, + RTC_SET = 10 + }; + void waitForSerial(); int freeRam(); + GPSTRACKER_DEBUG_COMMAND menu(); + + void getAndDisplayBattery(); + void getAndDisplayGpsPosition(); + + void getAndDisplayRtcTime(); + void setRtcTime(); + inline void displayFreeRam() { Serial.println(freeRam()); } + } diff --git a/GpsTracker/Flash.cpp b/GpsTracker/Flash.cpp new file mode 100644 index 0000000..13a8c9d --- /dev/null +++ b/GpsTracker/Flash.cpp @@ -0,0 +1,3 @@ +#include "Flash.h" + + diff --git a/GpsTracker/Flash.h b/GpsTracker/Flash.h new file mode 100644 index 0000000..15ab915 --- /dev/null +++ b/GpsTracker/Flash.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace flash { + + template size_t getFlashArraySize(T(&)[N]) { return N; } + template void readFromFlash(const T *source, T &dest) { + memcpy_P(&dest, source, sizeof(T)); + } +} \ No newline at end of file diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h index a1c5bd0..8e90e49 100644 --- a/GpsTracker/GpsTracker.h +++ b/GpsTracker/GpsTracker.h @@ -8,12 +8,4 @@ #include "Config.h" #include "Core.h" -#include "Gps.h" -#include "MainUnit.h" -#include "Network.h" -#include "Rtc.h" -#include "Pins.h" -#include "Positions.h" - - #define LOGGER_NAME "GpsTracker" diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 931156d..ef7e97d 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -1,39 +1,58 @@ #include "GpsTracker.h" +bool bypassMenu = false; void setup() { #ifdef _DEBUG debug::waitForSerial(); Log.begin(LOG_LEVEL_VERBOSE, &Serial); +#else + if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); #endif - + rtc::powerOn(); rtc::setup(); - rtc::powerOff(); + rtc::powerOff(); } void loop() { - gps::powerOn(); - SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); - gps::powerOff(); - - if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { - tmElements_t time; - gps::getTime(time); - rtc::powerOn(); - rtc::setTime(time); - rtc::powerOff(); - - positions::appendLast(); - - uint8_t velocity; - gps::getVelocity(velocity); - core::setSleepTime(velocity); + debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::NONE; + if(!bypassMenu) command = debug::menu(); + + bypassMenu = command == debug::GPSTRACKER_DEBUG_COMMAND::NONE; + + switch (command) { + case debug::GPSTRACKER_DEBUG_COMMAND::NONE: + case debug::GPSTRACKER_DEBUG_COMMAND::ONCE: + core::main(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::RAM: + debug::displayFreeRam(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::BATTERY: + debug::getAndDisplayBattery(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::GPS_ON: + gps::powerOn(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::GPS_OFF: + gps::powerOff(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::GPS_GET: + debug::getAndDisplayGpsPosition(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::RTC_ON: + rtc::powerOn(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::RTC_OFF: + rtc::powerOff(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::RTC_GET: + debug::getAndDisplayRtcTime(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::RTC_SET: + debug::setRtcTime(); + default: + Serial.println(F("Unsupported command !")); } - - if (positions::needsToSend()) { - positions::send(); - } - - mainunit::deepSleep(core::sleepTime); }