You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

  1. #include "Core.h"
  2. #include "Config.h"
  3. #include "Flash.h"
  4. #define LOGGER_NAME "Core"
  5. using namespace utils;
  6. namespace core {
  7. uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;;
  8. uint8_t increaseInARow = 0;
  9. void main() {
  10. VERBOSE("main");
  11. PositionEntryMetadata metadata;
  12. if (positions::acquire(metadata)) {
  13. positions::appendLast(metadata);
  14. updateSleepTime(gps::getVelocity());
  15. }
  16. positions::doBackup();
  17. mainunit::deepSleep(sleepTime);
  18. }
  19. void updateSleepTime(uint8_t velocity) {
  20. uint16_t result = computeSleepTime(velocity);
  21. if (result > sleepTime) {
  22. increaseInARow++;
  23. if (increaseInARow < SLEEP_DEFAULT_INCREASE_THRESHOLD) return;
  24. }
  25. else increaseInARow = 0;
  26. sleepTime = result;
  27. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  28. }
  29. uint16_t computeSleepTime(uint8_t velocity) {
  30. uint16_t result;
  31. for (uint8_t i = 0; i < flash::getArraySize(config::defaultSleepTimings); i++) {
  32. sleepTimings_t timing;
  33. flash::read(&config::defaultSleepTimings[i], timing);
  34. if (velocity > timing.speed) continue;
  35. result = timing.seconds;
  36. break;
  37. }
  38. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  39. return result;
  40. }
  41. }