您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

77 行
2.1 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 stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1;
  9. void main() {
  10. bool forceBackup = false;
  11. positions::prepareBackup();
  12. PositionEntryMetadata metadata;
  13. if (positions::acquire(metadata)) {
  14. positions::appendLast(metadata);
  15. forceBackup = updateSleepTime();
  16. gps::preserveCurrentCoordinates();
  17. }
  18. positions::doBackup(forceBackup);
  19. mainunit::deepSleep(sleepTime);
  20. }
  21. bool updateSleepTime() {
  22. uint8_t velocity = gps::getVelocity();
  23. uint16_t result = mapSleepTime(velocity);
  24. bool goingLongSleep = false;
  25. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  26. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  27. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  28. else stoppedInARow = max(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  29. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  30. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  31. }
  32. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  33. }
  34. else stoppedInARow = 0;
  35. sleepTime = result;
  36. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  37. return goingLongSleep;
  38. }
  39. uint16_t mapSleepTime(uint8_t velocity) {
  40. uint16_t result;
  41. uint16_t currentTime = 0xFFFF;
  42. if (rtc::isAccurate()) {
  43. tmElements_t time;
  44. rtc::getTime(time);
  45. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  46. }
  47. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  48. sleepTimings_t timing;
  49. flash::read(&config::defaultSleepTimings[i], timing);
  50. if (velocity < timing.speed) continue;
  51. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  52. result = timing.seconds;
  53. break;
  54. }
  55. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  56. return result;
  57. }
  58. }