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.

49 line
1.1 KiB

  1. #include "MainUnit.h"
  2. #include "Rtc.h"
  3. #include "Pins.h"
  4. #include "Debug.h"
  5. #define LOGGER_NAME "MainUnit"
  6. namespace mainunit {
  7. namespace details {
  8. void prepareSleep() {
  9. //forcing the power off of ALL devices for safety
  10. hardware::sim808::powerOff();
  11. hardware::i2c::powerOff(true);
  12. hardware::sim808::simSerial.end(); //avoid woke up by SoftwareSerial interrupt
  13. delay(5); //ensure log messages have been printed out
  14. }
  15. void wokeUp() {
  16. tmElements_t wokeUpTime;
  17. rtc::getTime(wokeUpTime);
  18. VERBOSE_FORMAT("wokeUp", "%d:%d:%d", wokeUpTime.Hour, wokeUpTime.Minute, wokeUpTime.Second);
  19. hardware::sim808::simSerial.listen();
  20. }
  21. }
  22. void interrupt() {
  23. detachInterrupt(digitalPinToInterrupt(RTC_WAKE));
  24. }
  25. void interruptIn(uint16_t seconds) {
  26. rtc::setAlarm(seconds);
  27. pinMode(RTC_WAKE, INPUT);
  28. attachInterrupt(digitalPinToInterrupt(RTC_WAKE), interrupt, FALLING);
  29. }
  30. void deepSleep(uint16_t seconds) {
  31. NOTICE_FORMAT("deepSleep", "%d seconds", seconds);
  32. interruptIn(seconds);
  33. details::prepareSleep();
  34. LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  35. details::wokeUp();
  36. }
  37. }