diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index fb24d0b..420ca01 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -10,28 +10,22 @@ namespace core { void main() { VERBOSE("main"); - gps::powerOn(); - SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); - SIM808ChargingStatus battery = hardware::sim808::device.getChargingState(); - gps::powerOff(); - - if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { - tmElements_t time; - gps::getTime(time); - rtc::setTime(time); - - positions::appendLast(battery, gpsStatus, rtc::getTemperature()); - - uint8_t velocity; - gps::getVelocity(velocity); - core::setSleepTime(velocity); + PositionEntryMetadata metadata; + if (positions::acquire(metadata)) { + positions::appendLast(metadata); + updateSleepTime(); } if (positions::needsToSend()) { positions::send(); } + } - mainunit::deepSleep(core::sleepTime); + void updateSleepTime() { + VERBOSE("updateSleepTime"); + uint8_t velocity; + gps::getVelocity(velocity); + setSleepTime(velocity); } void setSleepTime(uint8_t velocity) { diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index 098c32f..509976e 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -15,4 +15,5 @@ namespace core { void main(); void setSleepTime(uint8_t velocity); + void updateSleepTime(); } \ No newline at end of file diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 79223fd..504e8f1 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -107,7 +107,7 @@ namespace debug { namespace details { inline void displayPosition(PositionEntry entry) { - Log.notice(F("%d%%, %dmV, %f°C, %d, %s\n"), entry.battery.level, entry.battery.voltage, entry.temperature, entry.status, entry.position); + Log.notice(F("%d%%, %dmV, %f°C, %ds %d, %s\n"), entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); } } @@ -223,7 +223,15 @@ namespace debug { SIM808ChargingStatus status = hardware::sim808::device.getChargingState(); hardware::sim808::powerOff(); - positions::appendLast(status, SIM808_GPS_STATUS::OFF, rtc::getTemperature()); + PositionEntryMetadata metadata = { + status.level, + status.voltage, + rtc::getTemperature(), + 0, + SIM808_GPS_STATUS::OFF + }; + + positions::appendLast(metadata); } void setRtcTime() { diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 12e2968..f2aa737 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -1,5 +1,5 @@ #include "GpsTracker.h" - +#include "Positions.h" bool bypassMenu = false; void setup() { @@ -9,7 +9,9 @@ void setup() { #else if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); #endif - + + Serial.println(sizeof(PositionEntry)); + Serial.println(sizeof(PositionEntryMetadata)); rtc::setup(); hardware::sim808::setup(); } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 97b2289..38f6121 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -19,16 +19,46 @@ namespace positions { return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); } - void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus, const float temperature) { + bool acquire(PositionEntryMetadata &metadata) { + VERBOSE("acquire"); + + timestamp_t before; + + gps::powerOn(); + before = rtc::getTime(); + SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); + SIM808ChargingStatus battery = hardware::sim808::device.getChargingState(); + gps::powerOff(); + + if (gpsStatus < SIM808_GPS_STATUS::FIX) return false; + + uint16_t timeToFix = rtc::getTime() - before; + + tmElements_t time; + gps::getTime(time); + rtc::setTime(time); + + metadata = { + battery.level, + battery.voltage, + rtc::getTemperature(), + timeToFix, + gpsStatus + }; + + return true; + } + + void appendLast(const PositionEntryMetadata &metadata) { VERBOSE("appendLast"); uint16_t entryAddress; - PositionEntry entry = { battery, temperature, gpsStatus }; + PositionEntry entry = { metadata }; strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); hardware::i2c::powerOn(); Config config = config::get(); - + config.lastEntry++; if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0; if (config.lastEntry == config.firstEntry) config.firstEntry++; @@ -37,8 +67,8 @@ namespace positions { entryAddress = getEntryAddress(config.lastEntry); hardware::i2c::eeprom.writeBlock(entryAddress, entry); - VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [%d, %s]", entryAddress, entry.battery.level, entry.battery.voltage, entry.temperature, entry.status, entry.position); - + VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + config::set(config); hardware::i2c::powerOff(); } @@ -53,7 +83,7 @@ namespace positions { hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [%d, %s]", entryAddress, entry.battery.level, entry.battery.voltage, entry.temperature, entry.status, entry.position); + VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [%d, %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); return true; } diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h index 21b29eb..a2c914f 100644 --- a/GpsTracker/Positions.h +++ b/GpsTracker/Positions.h @@ -4,15 +4,22 @@ #define POSITION_SIZE 115 -struct PositionEntry { - SIM808ChargingStatus battery; //sizeof = 4 +struct PositionEntryMetadata { + int8_t batteryLevel; //sizeof = 1 + int16_t batteryVoltage; //sizeof = 2 float temperature; //sizeof = 4 + uint16_t timeToFix; //sizeof = 2 SIM808_GPS_STATUS status; //sizeof = 1 +}; //sizeof = 10 + +struct PositionEntry { + PositionEntryMetadata metadata; //sizeof = 10 char position[POSITION_SIZE]; //sizeof = 115 -}; //sizeof = 119 +}; //sizeof = 125 namespace positions { - void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus, const float temperature); + bool acquire(PositionEntryMetadata &metadata); + void appendLast(const PositionEntryMetadata &metadata); bool get(uint16_t index, PositionEntry &entry); bool moveNext(uint16_t &index); diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index f901232..ecb400e 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -22,7 +22,7 @@ namespace rtc { time.Year = CalendarYrToTm(RTC.yyyy); } - void writeTimeToRegisters(tmElements_t &time) { + void writeTimeToRegisters(const tmElements_t &time) { RTC.s = time.Second; RTC.m = time.Minute; RTC.h = time.Hour; @@ -51,6 +51,12 @@ namespace rtc { return temperature; } + timestamp_t getTime() { + tmElements_t time; + getTime(time); + return makeTimestamp(time); + } + void getTime(tmElements_t &time) { hardware::i2c::powerOn(); RTC.readTime(); @@ -60,7 +66,13 @@ namespace rtc { VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); } - void setTime(tmElements_t &time) { + void setTime(const timestamp_t timestamp) { + tmElements_t time; + breakTime(timestamp, time); + setTime(time); + } + + void setTime(const tmElements_t &time) { VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); details::writeTimeToRegisters(time); @@ -79,7 +91,7 @@ namespace rtc { setAlarm(alarmTime); } - void setAlarm(tmElements_t &time) { + void setAlarm(const tmElements_t &time) { details::writeTimeToRegisters(time); hardware::i2c::powerOn(); diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index 16caef2..526998b 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -7,9 +7,11 @@ namespace rtc { void setup(); float getTemperature(); + timestamp_t getTime(); void getTime(tmElements_t &time); - void setTime(tmElements_t &time); + void setTime(timestamp_t time); + void setTime(const tmElements_t &time); void setAlarm(uint16_t seconds); - void setAlarm(tmElements_t &time); + void setAlarm(const tmElements_t &time); } \ No newline at end of file