25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

71 satır
1.7 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 = 0;
  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(gps::getVelocity());
  16. }
  17. positions::doBackup(forceBackup);
  18. mainunit::deepSleep(sleepTime);
  19. }
  20. bool updateSleepTime(uint8_t velocity) {
  21. uint16_t result = mapSleepTime(velocity);
  22. bool goingLongSleep = false;
  23. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  24. stoppedInARow++;
  25. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  26. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  27. }
  28. else goingLongSleep = true;
  29. }
  30. else stoppedInARow = 0;
  31. sleepTime = result;
  32. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  33. return goingLongSleep;
  34. }
  35. uint16_t mapSleepTime(uint8_t velocity) {
  36. uint16_t result;
  37. uint16_t currentTime = 0xFFFF;
  38. if (rtc::isAccurate()) {
  39. tmElements_t time;
  40. rtc::getTime(time);
  41. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  42. }
  43. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  44. sleepTimings_t timing;
  45. flash::read(&config::defaultSleepTimings[i], timing);
  46. if (velocity < timing.speed) continue;
  47. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  48. result = timing.seconds;
  49. break;
  50. }
  51. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  52. return result;
  53. }
  54. }