From a8b9d65327318e4c2ee52a09ce351c00498c6d56 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 21 Jan 2018 14:48:18 +0100 Subject: [PATCH] Started building major bricks and global algorithm --- .editorconfig | 2 +- .gitignore | 5 +++- GpsTracker/Core.cpp | 14 ++++++++++ GpsTracker/Core.h | 24 +++++++++++++++++ GpsTracker/Debug.cpp | 18 +++++++++---- GpsTracker/Debug.h | 15 ++++++++--- GpsTracker/Gps.cpp | 28 ++++++++++++++++++- GpsTracker/Gps.h | 19 ++++++++++++- GpsTracker/GpsTracker.h | 8 ++++-- GpsTracker/GpsTracker.ino | 19 ++++++++++++- GpsTracker/Network.cpp | 8 +++++- GpsTracker/Network.h | 5 ++++ GpsTracker/Rtc.cpp | 57 ++++++++++++++++++++++++++++++++++++++- GpsTracker/Rtc.h | 10 +++++++ GpsTracker/Storage.cpp | 8 ++++++ GpsTracker/Storage.h | 5 ++++ GpsTracker/Time.h | 10 +++++++ GpsTracker/Utils.cpp | 8 ++++++ GpsTracker/Utils.h | 7 +++++ 19 files changed, 252 insertions(+), 18 deletions(-) create mode 100644 GpsTracker/Core.cpp create mode 100644 GpsTracker/Core.h create mode 100644 GpsTracker/Time.h create mode 100644 GpsTracker/Utils.cpp create mode 100644 GpsTracker/Utils.h diff --git a/.editorconfig b/.editorconfig index 14f3232..07c88d9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,6 @@ root = true [*] -end_of_line = lf +end_of_line = crlf indent_style = tab trim_trailing_whitespace = true diff --git a/.gitignore b/.gitignore index 945c5c6..85b7e92 100644 --- a/.gitignore +++ b/.gitignore @@ -261,4 +261,7 @@ __pycache__/ *.pyc #VisualMicro -__vm/ \ No newline at end of file +__vm/ + +#Line endings unifier +.leu \ No newline at end of file diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp new file mode 100644 index 0000000..5222961 --- /dev/null +++ b/GpsTracker/Core.cpp @@ -0,0 +1,14 @@ +#include "Core.h" + +#define LOGGER_NAME "Core" + +namespace core { + + GPS_TRACKER_STATES state; + + void setCurrentState(GPS_TRACKER_STATES s) { + state = s; + VERBOSE_FORMAT("setCurrentState", "%d, Free RAM : %d", state debug::freeRam()); + } + +} \ No newline at end of file diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h new file mode 100644 index 0000000..9540eb1 --- /dev/null +++ b/GpsTracker/Core.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "Debug.h" + +enum class GPS_TRACKER_STATES : uint8_t +{ + SETUP = 0, + GET_POSITION = 1, + SAVE_POSITION = 2, + SEND_POSITION = 3, + SLEEP = 4 +}; + + + +namespace core { + + extern GPS_TRACKER_STATES state; + + void setCurrentState(GPS_TRACKER_STATES s); + +} \ No newline at end of file diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 267c120..6b6c83d 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -1,8 +1,16 @@ #include "Debug.h" -void waitForSerial() { -#ifdef _DEBUG - while (!Serial); - Serial.begin(115200); -#endif // _DEBUG +namespace debug { + + void waitForSerial() { + while (!Serial); + Serial.begin(115200); + } + + int freeRam() { + extern int __heap_start, *__brkval; + int v; + return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval); + } + } \ No newline at end of file diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index 382c368..f0266a3 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -1,21 +1,28 @@ #pragma once +#include + #ifdef _DEBUG #include #define VERBOSE(f) Log.verbose(F("[" LOGGER_NAME "::" f "]\n")) #define VERBOSE_MSG(f, msg) Log.verbose(F("[" LOGGER_NAME "::" f "] " msg "\n")) -#define VERBOSE_FORMAT(f, msg, ...) Log.verbose(F["[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) +#define VERBOSE_FORMAT(f, msg, ...) Log.verbose(F("[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) #else #define DISABLE_LOGGING 1 #define VERBOSE(f) -#define VERBOSE(f, msg) -#define VERBOSE(f, msg, ...) +#define VERBOSE_MSG(f, msg) +#define VERBOSE_FORMAT(f, msg, ...) #endif -void waitForSerial(); +namespace debug { + + void waitForSerial(); + int freeRam(); + +} diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index d81a872..c5f1e3f 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -1,3 +1,29 @@ #include "Gps.h" -#define LOGGER_NAME "Gps" \ No newline at end of file +#define LOGGER_NAME "Gps" + +namespace gps { + + void powerOn() { + + } + + void powerOff() { + + } + + SIM808_GPS_STATUS acquireCurrentPosition() { + SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; + + //TODO : do while (!timeout && < accurate_fix) + if (currentStatus > SIM808_GPS_STATUS::NO_FIX) { + lastStatus = currentStatus; + } + + return currentStatus; + } + + Time getTime() { + + } +} \ No newline at end of file diff --git a/GpsTracker/Gps.h b/GpsTracker/Gps.h index 7b9637e..faa26a3 100644 --- a/GpsTracker/Gps.h +++ b/GpsTracker/Gps.h @@ -1 +1,18 @@ -#pragma once \ No newline at end of file +#pragma once + +#include "Time.h" + +#define GPS_POSITION_SIZE 128 + +namespace gps { + + char lastPosition[GPS_POSITION_SIZE]; + SIM808_GPS_STATUS lastStatus; + + void powerOn(); + void powerOff(); + + SIM808_GPS_STATUS acquireCurrentPosition(); + Time getTime(); + +} \ No newline at end of file diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h index 40c6c59..0f81c6d 100644 --- a/GpsTracker/GpsTracker.h +++ b/GpsTracker/GpsTracker.h @@ -5,9 +5,13 @@ #include #include "Debug.h" +#include "Core.h" + #include "Gps.h" #include "Network.h" +#include "Rtc.h" #include "Storage.h" +#include "Utils.h" #define LOGGER_NAME "GpsTracker" @@ -16,9 +20,9 @@ #define SIM_PWR 9 #define SIM_STATUS 8 -#define IC_PWR A0 +#define RTC_PWR A0 +#define EEPROM_PWR A0 #define SD_SS SS #define SIM_RI 2 #define RTC_WAKE 3 - diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index ef1a468..38ef513 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -1,12 +1,29 @@ #include "GpsTracker.h" + void setup() { #ifdef _DEBUG - waitForSerial(); + debug::waitForSerial(); + Log.begin(LOG_LEVEL_VERBOSE, &Serial); #endif + + core::setCurrentState(GPS_TRACKER_STATES::SETUP); + + rtc::powerOn(); + rtc::setup(); + rtc::powerOff(); } void loop() { + gps::powerOn(); + SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(); + gps::powerOff(); + if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { + Time time = utils::parseTime(); + rtc::powerOn(); + rtc::setTime(time); + rtc::powerOff(); + } } diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 96b272f..e2dc62d 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -1,3 +1,9 @@ #include "Network.h" -#define LOGGER_NAME "Network" \ No newline at end of file +#define LOGGER_NAME "Network" + +namespace network { + + void powerOn() {} + void powerOff() {} +} \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index 6f70f09..30659f2 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -1 +1,6 @@ #pragma once + +namespace network { + void powerOn(); + void powerOff(); +} \ No newline at end of file diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 478c868..12e4628 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -1,3 +1,58 @@ #include "Rtc.h" -#define LOGGER_NAME "Rtc" \ No newline at end of file +#include +#include + +#define LOGGER_NAME "Rtc" + +namespace rtc { + + void powerOn() { + digitalWrite(RTC_PWR, HIGH); + pinMode(RTC_PWR, OUTPUT); + + Wire.begin(); + } + + void powerOff() { + pinMode(RTC_PWR, INPUT); + digitalWrite(RTC_PWR, LOW); + + //turn off i2c + TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA)); + + //disable i2c internal pull ups + digitalWrite(A4, LOW); + digitalWrite(A5, LOW); + } + + void setup() { + RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock + RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON + } + + Time getTime() { + RTC.readTime(); + + return { + RTC.yyyy, + RTC.mm, + RTC.dd, + RTC.h, + RTC.m, + RTC.s + }; + } + + 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; + + RTC.writeTime(); + } + +} \ No newline at end of file diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index 6f70f09..d4f07b4 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -1 +1,11 @@ #pragma once + +namespace rtc { + void powerOn(); + void powerOff(); + + void setup(); + + Time getTime(); + void setTime(Time time); +} \ No newline at end of file diff --git a/GpsTracker/Storage.cpp b/GpsTracker/Storage.cpp index e69de29..fd8ff34 100644 --- a/GpsTracker/Storage.cpp +++ b/GpsTracker/Storage.cpp @@ -0,0 +1,8 @@ +#include "Storage.h" + +#define LOGGER_NAME "Storage" + +namespace storage { + void powerOn() {} + void powerOff() {} +} \ No newline at end of file diff --git a/GpsTracker/Storage.h b/GpsTracker/Storage.h index 6f70f09..778be48 100644 --- a/GpsTracker/Storage.h +++ b/GpsTracker/Storage.h @@ -1 +1,6 @@ #pragma once + +namespace storage { + void powerOn(); + void powerOff(); +} \ No newline at end of file diff --git a/GpsTracker/Time.h b/GpsTracker/Time.h new file mode 100644 index 0000000..5c74788 --- /dev/null +++ b/GpsTracker/Time.h @@ -0,0 +1,10 @@ +#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 new file mode 100644 index 0000000..3657e16 --- /dev/null +++ b/GpsTracker/Utils.cpp @@ -0,0 +1,8 @@ +#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 new file mode 100644 index 0000000..c35e6cd --- /dev/null +++ b/GpsTracker/Utils.h @@ -0,0 +1,7 @@ +#pragma once + +#include "Time.h" + +namespace utils { + Time parseTime(); +} \ No newline at end of file