Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

196 lignes
5.8 KiB

  1. #include "Debug.h"
  2. #include "Flash.h"
  3. #include "Positions.h"
  4. #define MENU_ENTRY(name, text) const char MENU_##name[] PROGMEM = text
  5. const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924074842.000,49.454862,1.144537,71.900,67.99,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,";
  6. MENU_ENTRY(HEADER, "-- Debug Menu --");
  7. MENU_ENTRY(SEPARATOR, "----");
  8. MENU_ENTRY(RUN, "[R] Run");
  9. MENU_ENTRY(RUN_ONCE, "[r] Run once");
  10. MENU_ENTRY(RAM, "[f] Free RAM");
  11. MENU_ENTRY(READ_BATTERY, "[b] Read battery");
  12. MENU_ENTRY(GPS_ON, "[G] GPS On");
  13. MENU_ENTRY(GPS_OFF, "[g] GPS Off");
  14. MENU_ENTRY(GPS_GET, "[L] Get GPS position");
  15. MENU_ENTRY(GPS_SET, "[l] Set last GPS position");
  16. MENU_ENTRY(I2C_ON, "[I] I2C devices On");
  17. MENU_ENTRY(I2C_OFF, "[i] I2C devices Off");
  18. MENU_ENTRY(RTC_SET, "[T] Get RTC time");
  19. MENU_ENTRY(RTC_GET, "[t] Set RTC time");
  20. MENU_ENTRY(SD_WRITE_TEST, "[W] Write to test file");
  21. MENU_ENTRY(EEPROM_GET_CONFIG, "[C] Get EEPROM config");
  22. MENU_ENTRY(EEPROM_RESET_CONFIG, "[c] Reset EEPROM config");
  23. MENU_ENTRY(EEPROM_GET_ENTRIES, "[E] Get EEPROM entries");
  24. MENU_ENTRY(EEPROM_ADD_ENTRY, "[e] Add last entry to EEPROM");
  25. MENU_ENTRY(QUESTION, "?");
  26. const PROGMEM uint8_t commandIdMapping[] = {
  27. 'R', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RUN),
  28. 'r', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::ONCE),
  29. 'f', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RAM),
  30. 'b', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::BATTERY),
  31. 'G', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::GPS_ON),
  32. 'g', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::GPS_OFF),
  33. 'L', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::GPS_GET),
  34. 'l', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::GPS_SET),
  35. 'I', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::I2C_ON),
  36. 'i', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::I2C_OFF),
  37. 'T', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RTC_GET),
  38. 't', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::RTC_SET),
  39. 'W', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::SD_WRITE_TEST),
  40. 'C', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONFIG),
  41. 'c', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG),
  42. 'E', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_ENTRIES),
  43. 'e', static_cast<uint8_t>(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY),
  44. };
  45. const char * const MENU_ENTRIES[] PROGMEM = {
  46. MENU_HEADER,
  47. MENU_RUN,
  48. MENU_RUN_ONCE,
  49. MENU_SEPARATOR,
  50. MENU_RAM,
  51. MENU_READ_BATTERY,
  52. MENU_SEPARATOR,
  53. MENU_GPS_ON,
  54. MENU_GPS_OFF,
  55. MENU_GPS_GET,
  56. MENU_GPS_SET,
  57. MENU_SEPARATOR,
  58. MENU_I2C_ON,
  59. MENU_I2C_OFF,
  60. MENU_RTC_SET,
  61. MENU_RTC_GET,
  62. MENU_SEPARATOR,
  63. MENU_SD_WRITE_TEST,
  64. MENU_SEPARATOR,
  65. MENU_EEPROM_GET_CONFIG,
  66. MENU_EEPROM_RESET_CONFIG,
  67. MENU_EEPROM_GET_ENTRIES,
  68. MENU_EEPROM_ADD_ENTRY,
  69. MENU_QUESTION
  70. };
  71. int freeRam2() { // dirty hack because putting it in namespace doesn't compile
  72. extern int __heap_start, *__brkval;
  73. int v;
  74. return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
  75. }
  76. namespace debug {
  77. void waitForSerial() {
  78. while (!Serial);
  79. Serial.begin(DEBUG_SERIAL_SPEED);
  80. Serial.println("Starting !");
  81. }
  82. int freeRam() {
  83. return freeRam2();
  84. }
  85. GPSTRACKER_DEBUG_COMMAND parseCommand(char id) {
  86. size_t mappingArraySize = flash::getArraySize(commandIdMapping);
  87. char commandId;
  88. for (uint8_t i = 0; i < mappingArraySize; i += 2) {
  89. commandId = pgm_read_byte_near(commandIdMapping + i);
  90. if (commandId == id) return static_cast<GPSTRACKER_DEBUG_COMMAND>(pgm_read_byte_near(commandIdMapping + i + 1));
  91. }
  92. return GPSTRACKER_DEBUG_COMMAND::NONE;
  93. }
  94. GPSTRACKER_DEBUG_COMMAND menu() {
  95. if (!Serial) return GPSTRACKER_DEBUG_COMMAND::RUN;
  96. GPSTRACKER_DEBUG_COMMAND command;
  97. size_t menuSize = flash::getArraySize(MENU_ENTRIES);
  98. do {
  99. for (uint8_t i = 0; i < menuSize; i++) {
  100. Serial.println(reinterpret_cast<const __FlashStringHelper *>(pgm_read_word_near(&MENU_ENTRIES[i])));
  101. }
  102. while (!Serial.available());
  103. command = parseCommand(Serial.read());
  104. while (Serial.available()) Serial.read();
  105. } while (command == GPSTRACKER_DEBUG_COMMAND::NONE);
  106. return command;
  107. }
  108. void getAndDisplayGpsPosition() {
  109. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  110. Log.notice(F("%d %s\n"), gpsStatus, gps::lastPosition);
  111. }
  112. void setFakeGpsPosition() {
  113. strncpy_P(gps::lastPosition, FAKE_GPS_ENTRY, GPS_POSITION_SIZE);
  114. Log.notice(F("Last position set to : %s\n"), gps::lastPosition);
  115. }
  116. void getAndDisplayBattery() {
  117. hardware::sim808::powerOn();
  118. SIM808ChargingStatus status = hardware::sim808::device.getChargingState();
  119. hardware::sim808::powerOff();
  120. Log.notice(F("%d %d%% %dmV\n"), status.state, status.level, status.voltage);
  121. }
  122. void getAndDisplayRtcTime() {
  123. tmElements_t time;
  124. rtc::getTime(time);
  125. Log.notice(F("%d/%d/%d %d:%d:%d\n"), tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  126. }
  127. void getAndDisplayEepromConfig() {
  128. config::read();
  129. }
  130. void getAndDisplayEepromPositions() {
  131. uint16_t firstEntryIndex = config::value.firstEntry;
  132. uint16_t currentEntryIndex = firstEntryIndex;
  133. PositionEntry currentEntry;
  134. do {
  135. positions::get(currentEntryIndex, currentEntry);
  136. Log.notice(F("%d%%, %dmV, %d, %s\n"), currentEntry.battery.level, currentEntry.battery.voltage, currentEntry.status, currentEntry.position);
  137. } while (currentEntryIndex != config::value.lastEntry);
  138. }
  139. void addLastPositionToEeprom() {
  140. hardware::sim808::powerOn();
  141. SIM808ChargingStatus status = hardware::sim808::device.getChargingState();
  142. hardware::sim808::powerOff();
  143. positions::appendLast(status, SIM808_GPS_STATUS::OFF);
  144. }
  145. void setRtcTime() {
  146. tmElements_t time;
  147. gps::getTime(time);
  148. rtc::setTime(time);
  149. Log.notice(F("OK"));
  150. }
  151. }