@@ -10,28 +10,22 @@ namespace core { | |||||
void main() { | void main() { | ||||
VERBOSE("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()) { | if (positions::needsToSend()) { | ||||
positions::send(); | positions::send(); | ||||
} | } | ||||
} | |||||
mainunit::deepSleep(core::sleepTime); | |||||
void updateSleepTime() { | |||||
VERBOSE("updateSleepTime"); | |||||
uint8_t velocity; | |||||
gps::getVelocity(velocity); | |||||
setSleepTime(velocity); | |||||
} | } | ||||
void setSleepTime(uint8_t velocity) { | void setSleepTime(uint8_t velocity) { | ||||
@@ -15,4 +15,5 @@ namespace core { | |||||
void main(); | void main(); | ||||
void setSleepTime(uint8_t velocity); | void setSleepTime(uint8_t velocity); | ||||
void updateSleepTime(); | |||||
} | } |
@@ -107,7 +107,7 @@ namespace debug { | |||||
namespace details { | namespace details { | ||||
inline void displayPosition(PositionEntry entry) { | 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(); | SIM808ChargingStatus status = hardware::sim808::device.getChargingState(); | ||||
hardware::sim808::powerOff(); | 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() { | void setRtcTime() { | ||||
@@ -1,5 +1,5 @@ | |||||
#include "GpsTracker.h" | #include "GpsTracker.h" | ||||
#include "Positions.h" | |||||
bool bypassMenu = false; | bool bypassMenu = false; | ||||
void setup() { | void setup() { | ||||
@@ -9,7 +9,9 @@ void setup() { | |||||
#else | #else | ||||
if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); | if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); | ||||
#endif | #endif | ||||
Serial.println(sizeof(PositionEntry)); | |||||
Serial.println(sizeof(PositionEntryMetadata)); | |||||
rtc::setup(); | rtc::setup(); | ||||
hardware::sim808::setup(); | hardware::sim808::setup(); | ||||
} | } | ||||
@@ -19,16 +19,46 @@ namespace positions { | |||||
return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); | 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"); | VERBOSE("appendLast"); | ||||
uint16_t entryAddress; | uint16_t entryAddress; | ||||
PositionEntry entry = { battery, temperature, gpsStatus }; | |||||
PositionEntry entry = { metadata }; | |||||
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); | strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); | ||||
hardware::i2c::powerOn(); | hardware::i2c::powerOn(); | ||||
Config config = config::get(); | Config config = config::get(); | ||||
config.lastEntry++; | config.lastEntry++; | ||||
if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0; | if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0; | ||||
if (config.lastEntry == config.firstEntry) config.firstEntry++; | if (config.lastEntry == config.firstEntry) config.firstEntry++; | ||||
@@ -37,8 +67,8 @@ namespace positions { | |||||
entryAddress = getEntryAddress(config.lastEntry); | entryAddress = getEntryAddress(config.lastEntry); | ||||
hardware::i2c::eeprom.writeBlock(entryAddress, entry); | 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); | config::set(config); | ||||
hardware::i2c::powerOff(); | hardware::i2c::powerOff(); | ||||
} | } | ||||
@@ -53,7 +83,7 @@ namespace positions { | |||||
hardware::i2c::eeprom.readBlock(entryAddress, entry); | hardware::i2c::eeprom.readBlock(entryAddress, entry); | ||||
hardware::i2c::powerOff(); | 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; | return true; | ||||
} | } | ||||
@@ -4,15 +4,22 @@ | |||||
#define POSITION_SIZE 115 | #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 | float temperature; //sizeof = 4 | ||||
uint16_t timeToFix; //sizeof = 2 | |||||
SIM808_GPS_STATUS status; //sizeof = 1 | SIM808_GPS_STATUS status; //sizeof = 1 | ||||
}; //sizeof = 10 | |||||
struct PositionEntry { | |||||
PositionEntryMetadata metadata; //sizeof = 10 | |||||
char position[POSITION_SIZE]; //sizeof = 115 | char position[POSITION_SIZE]; //sizeof = 115 | ||||
}; //sizeof = 119 | |||||
}; //sizeof = 125 | |||||
namespace positions { | 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 get(uint16_t index, PositionEntry &entry); | ||||
bool moveNext(uint16_t &index); | bool moveNext(uint16_t &index); | ||||
@@ -22,7 +22,7 @@ namespace rtc { | |||||
time.Year = CalendarYrToTm(RTC.yyyy); | time.Year = CalendarYrToTm(RTC.yyyy); | ||||
} | } | ||||
void writeTimeToRegisters(tmElements_t &time) { | |||||
void writeTimeToRegisters(const tmElements_t &time) { | |||||
RTC.s = time.Second; | RTC.s = time.Second; | ||||
RTC.m = time.Minute; | RTC.m = time.Minute; | ||||
RTC.h = time.Hour; | RTC.h = time.Hour; | ||||
@@ -51,6 +51,12 @@ namespace rtc { | |||||
return temperature; | return temperature; | ||||
} | } | ||||
timestamp_t getTime() { | |||||
tmElements_t time; | |||||
getTime(time); | |||||
return makeTimestamp(time); | |||||
} | |||||
void getTime(tmElements_t &time) { | void getTime(tmElements_t &time) { | ||||
hardware::i2c::powerOn(); | hardware::i2c::powerOn(); | ||||
RTC.readTime(); | 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); | 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); | 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); | details::writeTimeToRegisters(time); | ||||
@@ -79,7 +91,7 @@ namespace rtc { | |||||
setAlarm(alarmTime); | setAlarm(alarmTime); | ||||
} | } | ||||
void setAlarm(tmElements_t &time) { | |||||
void setAlarm(const tmElements_t &time) { | |||||
details::writeTimeToRegisters(time); | details::writeTimeToRegisters(time); | ||||
hardware::i2c::powerOn(); | hardware::i2c::powerOn(); | ||||
@@ -7,9 +7,11 @@ namespace rtc { | |||||
void setup(); | void setup(); | ||||
float getTemperature(); | float getTemperature(); | ||||
timestamp_t getTime(); | |||||
void getTime(tmElements_t &time); | 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(uint16_t seconds); | ||||
void setAlarm(tmElements_t &time); | |||||
void setAlarm(const tmElements_t &time); | |||||
} | } |