選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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