@@ -7,8 +7,18 @@ | |||
namespace config { | |||
Config value; | |||
void format() { | |||
VERBOSE("format"); | |||
hardware::i2c::eepromPowerOn(); | |||
for (int i = 0; i < 1024; i++) | |||
{ | |||
hardware::i2c::eeprom.write(i, 0); | |||
} | |||
hardware::i2c::eepromPowerOff(); | |||
} | |||
void write() { | |||
VERBOSE_FORMAT("writeConfig", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
VERBOSE_FORMAT("write", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
hardware::i2c::eepromPowerOn(); | |||
int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); | |||
@@ -16,18 +26,19 @@ namespace config { | |||
} | |||
void reset() { | |||
VERBOSE("resetConfig"); | |||
VERBOSE("reset"); | |||
Config config = { | |||
CONFIG_SEED, | |||
VERSION, | |||
0, | |||
0 | |||
0xFFFF, | |||
0xFFFF | |||
}; | |||
value = config; | |||
format(); | |||
write(); | |||
VERBOSE_FORMAT("resetConfig", "value : %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
VERBOSE_FORMAT("reset", "value : %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
} | |||
void read() { | |||
@@ -36,7 +47,7 @@ namespace config { | |||
if (!String(CONFIG_SEED).equals(value.seed)) reset(); | |||
hardware::i2c::eepromPowerOff(); | |||
VERBOSE_FORMAT("readConfig", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
VERBOSE_FORMAT("read", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
} | |||
} |
@@ -15,7 +15,7 @@ struct Config { | |||
}; | |||
#define CONFIG_ADDR 0 | |||
#define CONFIG_SEED "UIYA" | |||
#define CONFIG_SEED "UIYB" | |||
#define VERSION "1.00" | |||
#define SLEEP_DEFAULT_TIME_SECONDS 1800 | |||
@@ -9,22 +9,24 @@ const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924074842.000,49.454862,1.144537 | |||
MENU_ENTRY(HEADER, "-- Debug Menu --"); | |||
MENU_ENTRY(SEPARATOR, "----"); | |||
MENU_ENTRY(RUN, "[R] Run"); | |||
MENU_ENTRY(RUN_ONCE, "[r] Run once"); | |||
MENU_ENTRY(RAM, "[f] Free RAM"); | |||
MENU_ENTRY(READ_BATTERY, "[b] Read battery"); | |||
MENU_ENTRY(GPS_ON, "[G] GPS On"); | |||
MENU_ENTRY(GPS_OFF, "[g] GPS Off"); | |||
MENU_ENTRY(GPS_GET, "[L] Get GPS position"); | |||
MENU_ENTRY(GPS_SET, "[l] Set last GPS position"); | |||
MENU_ENTRY(RTC_SET, "[T] Get RTC time"); | |||
MENU_ENTRY(RTC_GET, "[t] Set RTC time"); | |||
MENU_ENTRY(SD_WRITE_TEST, "[W] Write to test file"); | |||
MENU_ENTRY(EEPROM_GET_CONFIG, "[C] Get EEPROM config"); | |||
MENU_ENTRY(EEPROM_RESET_CONFIG, "[c] Reset EEPROM config"); | |||
MENU_ENTRY(EEPROM_GET_ENTRIES, "[E] Get EEPROM entries"); | |||
MENU_ENTRY(EEPROM_ADD_ENTRY, "[e] Add last entry to EEPROM"); | |||
MENU_ENTRY(QUESTION, "?"); | |||
MENU_ENTRY(RUN, "[R] Run"); | |||
MENU_ENTRY(RUN_ONCE, "[r] Run once"); | |||
MENU_ENTRY(RAM, "[f] Free RAM"); | |||
MENU_ENTRY(READ_BATTERY, "[b] Read battery"); | |||
MENU_ENTRY(GPS_ON, "[G] GPS On"); | |||
MENU_ENTRY(GPS_OFF, "[g] GPS Off"); | |||
MENU_ENTRY(GPS_GET, "[L] Get GPS position"); | |||
MENU_ENTRY(GPS_SET, "[l] Set last GPS position"); | |||
MENU_ENTRY(RTC_SET, "[T] Get RTC time"); | |||
MENU_ENTRY(RTC_GET, "[t] Set RTC time"); | |||
MENU_ENTRY(SD_WRITE_TEST, "[W] Write to test file"); | |||
MENU_ENTRY(EEPROM_GET_CONFIG, "[S] Get EEPROM config"); | |||
MENU_ENTRY(EEPROM_RESET_CONFIG, "[s] Reset EEPROM config"); | |||
MENU_ENTRY(EEPROM_GET_CONTENT, "[C] Get EEPROM content"); | |||
MENU_ENTRY(EEPROM_GET_ENTRIES, "[P] Get EEPROM entries"); | |||
MENU_ENTRY(EEPROM_GET_LAST_ENTRY, "[p] Get EEPROM last entry"); | |||
MENU_ENTRY(EEPROM_ADD_ENTRY, "[a] Add last entry to EEPROM"); | |||
MENU_ENTRY(QUESTION, "?"); | |||
const PROGMEM uint8_t commandIdMapping[] = { | |||
'R', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RUN), | |||
@@ -38,10 +40,12 @@ const PROGMEM uint8_t commandIdMapping[] = { | |||
'T', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RTC_GET), | |||
't', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RTC_SET), | |||
'W', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::SD_WRITE_TEST), | |||
'C', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONFIG), | |||
'c', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG), | |||
'E', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_ENTRIES), | |||
'e', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY), | |||
'S', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONFIG), | |||
's', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG), | |||
'C', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT), | |||
'P', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_ENTRIES), | |||
'p', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_LAST_ENTRY), | |||
'a', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY), | |||
}; | |||
const char * const MENU_ENTRIES[] PROGMEM = { | |||
@@ -74,7 +78,9 @@ const char * const MENU_ENTRIES[] PROGMEM = { | |||
MENU_EEPROM_GET_CONFIG, | |||
MENU_EEPROM_RESET_CONFIG, | |||
MENU_EEPROM_GET_CONTENT, | |||
MENU_EEPROM_GET_ENTRIES, | |||
MENU_EEPROM_GET_LAST_ENTRY, | |||
MENU_EEPROM_ADD_ENTRY, | |||
MENU_QUESTION | |||
@@ -88,6 +94,12 @@ int freeRam2() { // dirty hack because putting it in namespace doesn't compile | |||
namespace debug { | |||
namespace details { | |||
inline void displayPosition(PositionEntry entry) { | |||
Log.notice(F("%d%%, %dmV, %d, %s\n"), entry.battery.level, entry.battery.voltage, entry.status, entry.position); | |||
} | |||
} | |||
void waitForSerial() { | |||
while (!Serial); | |||
Serial.begin(DEBUG_SERIAL_SPEED); | |||
@@ -159,17 +171,41 @@ namespace debug { | |||
config::read(); | |||
} | |||
void getAndDisplayEepromContent() { | |||
char buffer[128]; | |||
hardware::i2c::eepromPowerOn(); | |||
//Serial.print('['); | |||
for (int i = 0; i < 8; i++) { | |||
hardware::i2c::eeprom.read(128 * i, buffer, 128); | |||
for (int i = 0; i < 128; i++) { | |||
//if (buffer[i] == 0) Serial.print(' '); | |||
//else | |||
Serial.print(buffer[i], HEX); | |||
} | |||
} | |||
//Serial.println(']'); | |||
Serial.println(); | |||
hardware::i2c::eepromPowerOff(); | |||
Log.notice("Done\n"); | |||
} | |||
void getAndDisplayEepromPositions() { | |||
uint16_t firstEntryIndex = config::value.firstEntry; | |||
uint16_t currentEntryIndex = firstEntryIndex; | |||
uint16_t currentEntryIndex = config::value.firstEntry; | |||
PositionEntry currentEntry; | |||
do { | |||
while(positions::moveNext(currentEntryIndex)) { | |||
positions::get(currentEntryIndex, currentEntry); | |||
Log.notice(F("%d%%, %dmV, %d, %s\n"), currentEntry.battery.level, currentEntry.battery.voltage, currentEntry.status, currentEntry.position); | |||
details::displayPosition(currentEntry); | |||
} | |||
} | |||
} while (currentEntryIndex != config::value.lastEntry); | |||
void getAndDisplayEepromLastPosition() { | |||
uint16_t lastEntryIndex = config::value.lastEntry; | |||
PositionEntry lastEntry; | |||
positions::get(lastEntryIndex, lastEntry); | |||
details::displayPosition(lastEntry); | |||
} | |||
void addLastPositionToEeprom() { | |||
@@ -43,8 +43,10 @@ namespace debug { | |||
SD_WRITE_TEST = 13, | |||
EEPROM_GET_CONFIG = 14, | |||
EEPROM_RESET_CONFIG = 15, | |||
EEPROM_GET_ENTRIES = 16, | |||
EEPROM_ADD_ENTRY = 17 | |||
EEPROM_GET_CONTENT = 16, | |||
EEPROM_GET_LAST_ENTRY = 17, | |||
EEPROM_GET_ENTRIES = 18, | |||
EEPROM_ADD_ENTRY = 19 | |||
}; | |||
void waitForSerial(); | |||
@@ -60,7 +62,9 @@ namespace debug { | |||
void setRtcTime(); | |||
void getAndDisplayEepromConfig(); | |||
void getAndDisplayEepromContent(); | |||
void getAndDisplayEepromPositions(); | |||
void getAndDisplayEepromLastPosition(); | |||
void addLastPositionToEeprom(); | |||
inline void displayFreeRam() { Serial.println(freeRam()); } | |||
@@ -59,9 +59,15 @@ void loop() { | |||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG: | |||
config::reset(); | |||
break; | |||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT: | |||
debug::getAndDisplayEepromContent(); | |||
break; | |||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_ENTRIES: | |||
debug::getAndDisplayEepromPositions(); | |||
break; | |||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_LAST_ENTRY: | |||
debug::getAndDisplayEepromLastPosition(); | |||
break; | |||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY: | |||
debug::addLastPositionToEeprom(); | |||
break; | |||
@@ -13,60 +13,67 @@ | |||
namespace positions { | |||
uint16_t getEntryAddress(uint16_t &index) { | |||
uint16_t address = ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); | |||
if (address + ENTRY_RESERVED_SIZE > E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize())) { | |||
address = ENTRIES_ADDR; | |||
index = 0; | |||
} | |||
uint16_t _maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; | |||
return address; | |||
} | |||
void setEntriesIndexes(uint16_t lastEntry) { | |||
uint16_t firstEntry = config::value.firstEntry; | |||
while (config::value.lastEntry < config::value.firstEntry) { | |||
config::value.firstEntry++; | |||
getEntryAddress(firstEntry); | |||
} | |||
config::value.lastEntry = lastEntry; | |||
config::value.firstEntry = firstEntry; | |||
uint16_t getEntryAddress(uint16_t index) { | |||
if (index > _maxEntryIndex) return -1; | |||
return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); | |||
} | |||
void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus) { | |||
VERBOSE("appendLast"); | |||
uint16_t entryIndex; | |||
uint16_t lastEntryIndex; | |||
uint16_t firstEntryIndex; | |||
uint16_t entryAddress; | |||
PositionEntry entry = { battery, gpsStatus }; | |||
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); | |||
storage::powerOn(); | |||
config::read(); | |||
entryIndex = config::value.lastEntry + 1; | |||
entryAddress = getEntryAddress(entryIndex); | |||
bool success = hardware::i2c::eeprom.writeBlock(entryAddress, entry); | |||
firstEntryIndex = config::value.firstEntry; | |||
lastEntryIndex = config::value.lastEntry; | |||
lastEntryIndex++; | |||
if (lastEntryIndex > _maxEntryIndex) lastEntryIndex = 0; | |||
if (lastEntryIndex == firstEntryIndex) firstEntryIndex++; | |||
if (firstEntryIndex > _maxEntryIndex) firstEntryIndex = 0; | |||
entryAddress = getEntryAddress(lastEntryIndex); | |||
bool success = hardware::i2c::eeprom.writeBlock(entryAddress, entry); | |||
if (success) { | |||
VERBOSE_MSG("appendLast", "written to EEPROM"); | |||
setEntriesIndexes(entryIndex); | |||
VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, battery.level, battery.voltage, gpsStatus, entry.position); | |||
config::value.firstEntry = firstEntryIndex; | |||
config::value.lastEntry = lastEntryIndex; | |||
config::write(); | |||
} | |||
storage::powerOff(); | |||
} | |||
void get(uint16_t &index, PositionEntry &entry) { | |||
void get(uint16_t index, PositionEntry &entry) { | |||
uint16_t entryAddress = getEntryAddress(index); | |||
VERBOSE_FORMAT("get", "Reading %d @ %d (%X)", index, entryAddress, entryAddress); | |||
if (entryAddress == -1) return; | |||
VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress); | |||
storage::powerOn(); | |||
hardware::i2c::eeprom.readBlock(entryAddress, entry); | |||
storage::powerOff(); | |||
VERBOSE_FORMAT("get", "Output index : %d", index); | |||
VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, entry.battery.level, entry.battery.voltage, entry.status, entry.position); | |||
} | |||
bool moveNext(uint16_t &index) { | |||
config::read(); | |||
if (index == config::value.lastEntry) return false; | |||
if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way | |||
else index++; | |||
return true; | |||
} | |||
bool needsToSend() { | |||
@@ -13,7 +13,8 @@ struct PositionEntry { | |||
namespace positions { | |||
void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus); | |||
void get(uint16_t &index, PositionEntry &entry); | |||
void get(uint16_t index, PositionEntry &entry); | |||
bool moveNext(uint16_t &index); | |||
bool needsToSend(); | |||
void send(); |
@@ -47,7 +47,7 @@ namespace rtc { | |||
hardware::i2c::rtcPowerOff(); | |||
details::readTimeFromRegisters(time); | |||
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) { | |||