From d21ba433942d44797f57bb8eb8ed087038f1e33f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 21 Jan 2018 17:59:55 +0100 Subject: [PATCH] Implemented RTC alarm sleep and wake up --- GpsTracker/Gps.cpp | 6 +++- GpsTracker/Gps.h | 10 +++--- GpsTracker/GpsTracker.h | 22 +++++------- GpsTracker/GpsTracker.ino | 10 +++++- GpsTracker/MainUnit.cpp | 26 +++++++++++++++ GpsTracker/MainUnit.h | 9 +++++ GpsTracker/Pins.h | 13 ++++++++ GpsTracker/Positions.cpp | 22 ++++++++++++ GpsTracker/Positions.h | 8 +++++ GpsTracker/Rtc.cpp | 70 +++++++++++++++++++++++++++++---------- GpsTracker/Rtc.h | 10 ++++-- GpsTracker/Storage.cpp | 1 + GpsTracker/Time.h | 10 ------ GpsTracker/Utils.cpp | 8 ----- GpsTracker/Utils.h | 7 ---- 15 files changed, 167 insertions(+), 65 deletions(-) create mode 100644 GpsTracker/MainUnit.cpp create mode 100644 GpsTracker/MainUnit.h create mode 100644 GpsTracker/Pins.h create mode 100644 GpsTracker/Positions.cpp create mode 100644 GpsTracker/Positions.h delete mode 100644 GpsTracker/Time.h delete mode 100644 GpsTracker/Utils.cpp delete mode 100644 GpsTracker/Utils.h diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index c5f1e3f..f6983ec 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -1,9 +1,13 @@ #include "Gps.h" +#include "Debug.h" #define LOGGER_NAME "Gps" namespace gps { + char lastPosition[GPS_POSITION_SIZE]; + SIM808_GPS_STATUS lastStatus; + void powerOn() { } @@ -23,7 +27,7 @@ namespace gps { return currentStatus; } - Time getTime() { + time_t getTime() { } } \ No newline at end of file diff --git a/GpsTracker/Gps.h b/GpsTracker/Gps.h index faa26a3..282a912 100644 --- a/GpsTracker/Gps.h +++ b/GpsTracker/Gps.h @@ -1,18 +1,20 @@ #pragma once -#include "Time.h" +#include +#include +#include #define GPS_POSITION_SIZE 128 namespace gps { - char lastPosition[GPS_POSITION_SIZE]; - SIM808_GPS_STATUS lastStatus; + extern char lastPosition[GPS_POSITION_SIZE]; + extern SIM808_GPS_STATUS lastStatus; void powerOn(); void powerOff(); SIM808_GPS_STATUS acquireCurrentPosition(); - Time getTime(); + time_t getTime(); } \ No newline at end of file diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h index 0f81c6d..998af9d 100644 --- a/GpsTracker/GpsTracker.h +++ b/GpsTracker/GpsTracker.h @@ -3,26 +3,20 @@ #include #include #include +#include +#include #include "Debug.h" #include "Core.h" +#include "posmgr.h" #include "Gps.h" +#include "posmgr.h" +#include "MainUnit.h" #include "Network.h" #include "Rtc.h" -#include "Storage.h" -#include "Utils.h" +#include "Pins.h" +#include "Positions.h" -#define LOGGER_NAME "GpsTracker" - -#define SIM_RX 5 -#define SIM_TX 6 -#define SIM_PWR 9 -#define SIM_STATUS 8 -#define RTC_PWR A0 -#define EEPROM_PWR A0 -#define SD_SS SS - -#define SIM_RI 2 -#define RTC_WAKE 3 +#define LOGGER_NAME "GpsTracker" diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 38ef513..b017f5f 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -21,9 +21,17 @@ void loop() { gps::powerOff(); if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { - Time time = utils::parseTime(); + time_t time = gps::getTime(); rtc::powerOn(); rtc::setTime(time); rtc::powerOff(); + + positions::appendLast(); + } + + if (positions::needsToSend()) { + positions::send(); } + + mainunit::deepSleep(10); } diff --git a/GpsTracker/MainUnit.cpp b/GpsTracker/MainUnit.cpp new file mode 100644 index 0000000..f2e1f82 --- /dev/null +++ b/GpsTracker/MainUnit.cpp @@ -0,0 +1,26 @@ +#include "MainUnit.h" +#include "Rtc.h" +#include "Pins.h" + +namespace mainunit { + + void interrupt() { + detachInterrupt(digitalPinToInterrupt(RTC_WAKE)); + } + + void interruptIn(uint16_t seconds) { + rtc::setAlarm(seconds); + + pinMode(RTC_WAKE, INPUT); + attachInterrupt(digitalPinToInterrupt(RTC_WAKE), interrupt, FALLING); + } + + void sleep(period_t period) { + LowPower.powerDown(period, ADC_OFF, BOD_OFF); + } + + void deepSleep(uint16_t seconds) { + interruptIn(seconds); + LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); + } +} \ No newline at end of file diff --git a/GpsTracker/MainUnit.h b/GpsTracker/MainUnit.h new file mode 100644 index 0000000..8a7b633 --- /dev/null +++ b/GpsTracker/MainUnit.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +namespace mainunit { + void sleep(period_t period); + void deepSleep(uint16_t seconds); +} \ No newline at end of file diff --git a/GpsTracker/Pins.h b/GpsTracker/Pins.h new file mode 100644 index 0000000..a88edf0 --- /dev/null +++ b/GpsTracker/Pins.h @@ -0,0 +1,13 @@ +#pragma once + +#define SIM_RX 5 +#define SIM_TX 6 +#define SIM_PWR 9 +#define SIM_STATUS 8 + +#define RTC_PWR A0 +#define EEPROM_PWR A0 +#define SD_SS SS + +#define SIM_RI 2 +#define RTC_WAKE 3 diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp new file mode 100644 index 0000000..840e302 --- /dev/null +++ b/GpsTracker/Positions.cpp @@ -0,0 +1,22 @@ +#include "Positions.h" + +#include "Gps.h" +#include "Network.h" +#include "Storage.h" + +#define LOGGER_NAME "Positions" + +namespace positions { + + void appendLast() { + //write gps::lastPosition to eeprom and eventually to sd card + } + + bool needsToSend() { + return false; + } + + void send() { + + } +} \ No newline at end of file diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h new file mode 100644 index 0000000..d68680c --- /dev/null +++ b/GpsTracker/Positions.h @@ -0,0 +1,8 @@ +#pragma once + +namespace positions { + void appendLast(); + + bool needsToSend(); + void send(); +} \ No newline at end of file diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 12e4628..5c850b2 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -1,4 +1,5 @@ #include "Rtc.h" +#include "Pins.h" #include #include @@ -7,6 +8,38 @@ namespace rtc { + namespace details { + + time_t readTimeFromRegisters() { + tmElements_t tmElements = { + RTC.s, + RTC.m, + RTC.h, + RTC.dow, + RTC.dd, + RTC.mm, + CalendarYrToTm(RTC.yyyy) + }; + + return makeTime(tmElements); + } + + void writeTimeToRegisters(time_t &time) { + tmElements_t tmElements; + breakTime(time, tmElements); + + RTC.s = tmElements.Second; + RTC.m = tmElements.Minute; + RTC.h = tmElements.Hour; + RTC.dow = tmElements.Wday; + RTC.dd = tmElements.Day; + RTC.mm = tmElements.Month; + RTC.yyyy = tmYearToCalendar(tmElements.Year); + } + + } + + void powerOn() { digitalWrite(RTC_PWR, HIGH); pinMode(RTC_PWR, OUTPUT); @@ -28,31 +61,32 @@ namespace rtc { void setup() { RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock - RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON + RTC.control(DS3231_INT_ENABLE, DS3231_OFF); //INTCN OFF } - Time getTime() { + time_t getTime() { RTC.readTime(); + return details::readTimeFromRegisters(); + } - return { - RTC.yyyy, - RTC.mm, - RTC.dd, - RTC.h, - RTC.m, - RTC.s - }; + void setTime(time_t &time) { + details::writeTimeToRegisters(time); + RTC.writeTime(); } - void setTime(Time time) { - RTC.yyyy = time.yyyy; - RTC.mm = time.mm; - RTC.dd = time.dd; - RTC.h = time.h; - RTC.m = time.m; - RTC.s = time.s; + void setAlarm(uint16_t seconds) { + time_t t = getTime(); + t = t + seconds; + setAlarm(t); + } - RTC.writeTime(); + void setAlarm(time_t &time) { + details::writeTimeToRegisters(time); + RTC.writeAlarm1(DS3231_ALM_S); + + RTC.control(DS3231_A1_FLAG, DS3231_OFF); + RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON + RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON } } \ No newline at end of file diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index d4f07b4..e5b4c28 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -1,11 +1,17 @@ #pragma once +#include +#include + namespace rtc { void powerOn(); void powerOff(); void setup(); - Time getTime(); - void setTime(Time time); + time_t getTime(); + void setTime(time_t &time); + + void setAlarm(uint16_t seconds); + void setAlarm(time_t &time); } \ No newline at end of file diff --git a/GpsTracker/Storage.cpp b/GpsTracker/Storage.cpp index fd8ff34..ccf4616 100644 --- a/GpsTracker/Storage.cpp +++ b/GpsTracker/Storage.cpp @@ -1,4 +1,5 @@ #include "Storage.h" +#include "Gps.h" #define LOGGER_NAME "Storage" diff --git a/GpsTracker/Time.h b/GpsTracker/Time.h deleted file mode 100644 index 5c74788..0000000 --- a/GpsTracker/Time.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -struct Time { - uint16_t yyyy; - uint8_t mm; - uint8_t dd; - uint8_t h; - uint8_t m; - uint8_t s; -}; \ No newline at end of file diff --git a/GpsTracker/Utils.cpp b/GpsTracker/Utils.cpp deleted file mode 100644 index 3657e16..0000000 --- a/GpsTracker/Utils.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Utils.h" -#include "Gps.h" - -namespace utils { - - Time parseTime(); - -} \ No newline at end of file diff --git a/GpsTracker/Utils.h b/GpsTracker/Utils.h deleted file mode 100644 index c35e6cd..0000000 --- a/GpsTracker/Utils.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include "Time.h" - -namespace utils { - Time parseTime(); -} \ No newline at end of file