From 5e7e47d6cd15f322ba27494ebf53b797081f2c3b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 24 Jan 2018 21:21:06 +0100 Subject: [PATCH] Inlined some functions and handle logical vs physical power management --- GpsTracker/Gps.cpp | 4 - GpsTracker/Gps.h | 6 +- GpsTracker/Hardware.cpp | 85 +++++++++++++++------ GpsTracker/Hardware.h | 11 +-- GpsTracker/Network.cpp | 3 +- GpsTracker/Network.h | 4 +- GpsTracker/Pins.h | 2 +- GpsTracker/Rtc.cpp | 26 ------- GpsTracker/Rtc.h | 15 +++- GpsTracker/Storage.cpp | 2 - GpsTracker/Storage.h | 11 ++- GpsTracker/Time2.cpp | 164 ++++++++++++++++++++-------------------- 12 files changed, 176 insertions(+), 157 deletions(-) diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index aab7808..25375b9 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -9,10 +9,6 @@ namespace gps { char lastPosition[GPS_POSITION_SIZE]; SIM808_GPS_STATUS lastStatus; - /*inline */void powerOn() { hardware::sim808::gpsPowerOn(); } - /*inline */void powerOff() { hardware::sim808::gpsPowerOff(); } - - SIM808_GPS_STATUS acquireCurrentPosition() { SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; diff --git a/GpsTracker/Gps.h b/GpsTracker/Gps.h index 6dd628c..c8d1743 100644 --- a/GpsTracker/Gps.h +++ b/GpsTracker/Gps.h @@ -12,9 +12,9 @@ namespace gps { extern char lastPosition[GPS_POSITION_SIZE]; extern SIM808_GPS_STATUS lastStatus; - void powerOn(); - void powerOff(); - + inline void powerOn() { hardware::sim808::gpsPowerOn(); } + inline void powerOff() { hardware::sim808::gpsPowerOff(); } + SIM808_GPS_STATUS acquireCurrentPosition(); timestamp_t getTime(); diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 582c2d2..11033ae 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -5,20 +5,14 @@ #include #include +#include + namespace hardware { namespace sim808 { SoftwareSerial simSerial = SoftwareSerial(SIM_TX, SIM_RX); SIM808 device = SIM808(SIM_RST, SIM_PWR, SIM_STATUS); - //idea : int powered - //gps::powerOn() => +1 - //network::powerOn() => +1 - //gps::powerOff() => -1 - //network::powerOff() => -1 - - //sim808:powerOff() => force powerOff of both - //gps/network::powerOff() => powered == 1 => sim808::powerOff() - //idea : gps power on = +1, network power on = +1 => powerOff forces power off of all, powerOff one will lead to actual powerOff if + void powerOn() { bool poweredOn = device.powerOnOff(true); if (!poweredOn) return; @@ -27,15 +21,7 @@ namespace hardware { } void powerOff() { - bool poweredOff = device.powerOnOff(false); - } - - void init() { - device.powerOnOff(true); - simSerial.begin(4800); - - device.begin(simSerial); - device.init(); + device.powerOnOff(false); } void powerOffIfUnused() { @@ -47,6 +33,14 @@ namespace hardware { } } + void init() { + device.powerOnOff(true); + simSerial.begin(4800); + + device.begin(simSerial); + device.init(); + } + void gpsPowerOn() { powerOn(); device.enableGps(); @@ -71,13 +65,54 @@ namespace hardware { } } - namespace rtc { - void powerOn(); - void powerOff(); - } + namespace i2c { + + #define DEVICE_RTC 1 + #define DEVICE_EEPROM 2 + + uint8_t powered = 0; + + void powerOn() { + digitalWrite(I2C_PWR, HIGH); + pinMode(I2C_PWR, OUTPUT); + + Wire.begin(); + } + + void powerOff() { + pinMode(I2C_PWR, INPUT); + digitalWrite(I2C_PWR, LOW); + + //turn off i2c + TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA)); - namespace eeprom { - void powerOn(); - void powerOff(); + //disable i2c internal pull ups + digitalWrite(A4, LOW); + digitalWrite(A5, LOW); + } + + inline void powerOffIfUnused() { + if (!powered) powerOff(); + } + + void rtcPowerOn() { + powerOn(); + powered |= DEVICE_RTC; + } + + void rtcPowerOff() { + powered &= ~DEVICE_RTC; + powerOffIfUnused(); + } + + void eepromPowerOn() { + powerOn(); + powered |= DEVICE_EEPROM; + } + + void eepromPowerOff() { + powered &= ~DEVICE_EEPROM; + powerOffIfUnused(); + } } } diff --git a/GpsTracker/Hardware.h b/GpsTracker/Hardware.h index 3a1f320..26c6bd0 100644 --- a/GpsTracker/Hardware.h +++ b/GpsTracker/Hardware.h @@ -19,13 +19,14 @@ namespace hardware { void networkPowerOff(); } - namespace rtc { + namespace i2c { void powerOn(); void powerOff(); - } - namespace eeprom { - void powerOn(); - void powerOff(); + void rtcPowerOn(); + void rtcPowerOff(); + + void eepromPowerOn(); + void eepromPowerOff(); } } \ No newline at end of file diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 6f92d2e..0372b00 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -4,6 +4,5 @@ namespace network { - /*inline */void powerOn() { hardware::sim808::networkPowerOn(); } - /*inline */void powerOff() { hardware::sim808::networkPowerOff(); } + } \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index c7324af..df89526 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -4,6 +4,6 @@ namespace network { - /*inline */void powerOn(); - /*inline */void powerOff(); + inline void powerOn() { hardware::sim808::networkPowerOn(); } + inline void powerOff() { hardware::sim808::networkPowerOff(); } } \ No newline at end of file diff --git a/GpsTracker/Pins.h b/GpsTracker/Pins.h index 40334eb..9903d55 100644 --- a/GpsTracker/Pins.h +++ b/GpsTracker/Pins.h @@ -6,7 +6,7 @@ #define SIM_PWR 9 #define SIM_STATUS 8 -#define RTC_PWR A0 +#define I2C_PWR A0 #define EEPROM_PWR A0 #define SD_SS SS diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index a9cca77..17b549a 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -38,27 +38,7 @@ 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_OFF); //INTCN OFF @@ -74,12 +54,6 @@ namespace rtc { RTC.writeTime(); } - void setAlarm(uint16_t seconds) { - timestamp_t t = getTime(); - t = t + seconds; - setAlarm(t); - } - void setAlarm(timestamp_t &time) { details::writeTimeToRegisters(time); RTC.writeAlarm1(DS3231_ALM_S); diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index a3ced7d..706c04f 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -1,16 +1,25 @@ #pragma once #include "Time2.h" +#include "Hardware.h" namespace rtc { - void powerOn(); - void powerOff(); + inline void powerOn() { + hardware::i2c::rtcPowerOn(); + } + + inline void powerOff() { + hardware::i2c::rtcPowerOff(); + } void setup(); timestamp_t getTime(); void setTime(timestamp_t &time); - void setAlarm(uint16_t seconds); + inline void setAlarm(uint16_t seconds) { + setAlarm(getTime() + seconds); + } + void setAlarm(timestamp_t &time); } \ No newline at end of file diff --git a/GpsTracker/Storage.cpp b/GpsTracker/Storage.cpp index ccf4616..dfab7b8 100644 --- a/GpsTracker/Storage.cpp +++ b/GpsTracker/Storage.cpp @@ -4,6 +4,4 @@ #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 778be48..982ab33 100644 --- a/GpsTracker/Storage.h +++ b/GpsTracker/Storage.h @@ -1,6 +1,13 @@ #pragma once +#include "Hardware.h" + namespace storage { - void powerOn(); - void powerOff(); + inline void powerOn() { + hardware::i2c::eepromPowerOn(); + } + + inline void powerOff() { + hardware::i2c::eepromPowerOff(); + } } \ No newline at end of file diff --git a/GpsTracker/Time2.cpp b/GpsTracker/Time2.cpp index 85d65d7..02208ca 100644 --- a/GpsTracker/Time2.cpp +++ b/GpsTracker/Time2.cpp @@ -35,96 +35,96 @@ // leap year calulator expects year argument as years offset from 1970 #define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) ) - static const uint8_t monthDays[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; // API starts months from 1, this array starts from 0 - - __attribute__((__optimize__("O2"))) - void breakTime(timestamp_t timeInput, tmElements_t &tm) { - // break the given timestamp_t into time components - // this is a more compact version of the C library localtime function - // note that year is offset from 1970 !!! - - uint8_t year; - uint8_t month, monthLength; - uint32_t time; - unsigned long days; - - time = (uint32_t)timeInput; - tm.Second = time % 60; - time /= 60; // now it is minutes - tm.Minute = time % 60; - time /= 60; // now it is hours - tm.Hour = time % 24; - time /= 24; // now it is days - tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1 - - year = 0; - days = 0; - while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { - year++; - } - tm.Year = year; // year is offset from 1970 - - days -= LEAP_YEAR(year) ? 366 : 365; - time -= days; // now it is days in this year, starting at 0 - - days = 0; - month = 0; - monthLength = 0; - for (month = 0; month<12; month++) { - if (month == 1) { // february - if (LEAP_YEAR(year)) { - monthLength = 29; - } - else { - monthLength = 28; - } - } - else { - monthLength = monthDays[month]; - } - - if (time >= monthLength) { - time -= monthLength; +static const uint8_t monthDays[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; // API starts months from 1, this array starts from 0 + +__attribute__((__optimize__("O2"))) +void breakTime(timestamp_t timeInput, tmElements_t &tm) { + // break the given timestamp_t into time components + // this is a more compact version of the C library localtime function + // note that year is offset from 1970 !!! + + uint8_t year; + uint8_t month, monthLength; + uint32_t time; + unsigned long days; + + time = (uint32_t)timeInput; + tm.Second = time % 60; + time /= 60; // now it is minutes + tm.Minute = time % 60; + time /= 60; // now it is hours + tm.Hour = time % 24; + time /= 24; // now it is days + tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1 + + year = 0; + days = 0; + while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { + year++; + } + tm.Year = year; // year is offset from 1970 + + days -= LEAP_YEAR(year) ? 366 : 365; + time -= days; // now it is days in this year, starting at 0 + + days = 0; + month = 0; + monthLength = 0; + for (month = 0; month<12; month++) { + if (month == 1) { // february + if (LEAP_YEAR(year)) { + monthLength = 29; } else { - break; + monthLength = 28; } } - tm.Month = month + 1; // jan is month 1 - tm.Day = time + 1; // day of month - } - - __attribute__((__optimize__("O2"))) - timestamp_t makeTime(tmElements_t &tm) { - // assemble time elements into timestamp_t - // note year argument is offset from 1970 (see macros in time.h to convert to other formats) - // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9 - - int i; - uint32_t seconds; - - // seconds from 1970 till 1 jan 00:00:00 of the given year - seconds = tm.Year*(SECS_PER_DAY * 365); - for (i = 0; i < tm.Year; i++) { - if (LEAP_YEAR(i)) { - seconds += SECS_PER_DAY; // add extra days for leap years + else { + monthLength = monthDays[month]; } - } - // add days for this year, months start from 1 - for (i = 1; i < tm.Month; i++) { - if ((i == 2) && LEAP_YEAR(tm.Year)) { - seconds += SECS_PER_DAY * 29; + if (time >= monthLength) { + time -= monthLength; } else { - seconds += SECS_PER_DAY * monthDays[i - 1]; //monthDay array starts from 0 + break; } } - seconds += (tm.Day - 1) * SECS_PER_DAY; - seconds += tm.Hour * SECS_PER_HOUR; - seconds += tm.Minute * SECS_PER_MIN; - seconds += tm.Second; - return (timestamp_t)seconds; + tm.Month = month + 1; // jan is month 1 + tm.Day = time + 1; // day of month +} + +__attribute__((__optimize__("O2"))) +timestamp_t makeTime(tmElements_t &tm) { + // assemble time elements into timestamp_t + // note year argument is offset from 1970 (see macros in time.h to convert to other formats) + // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9 + + int i; + uint32_t seconds; + + // seconds from 1970 till 1 jan 00:00:00 of the given year + seconds = tm.Year*(SECS_PER_DAY * 365); + for (i = 0; i < tm.Year; i++) { + if (LEAP_YEAR(i)) { + seconds += SECS_PER_DAY; // add extra days for leap years + } + } + + // add days for this year, months start from 1 + for (i = 1; i < tm.Month; i++) { + if ((i == 2) && LEAP_YEAR(tm.Year)) { + seconds += SECS_PER_DAY * 29; + } + else { + seconds += SECS_PER_DAY * monthDays[i - 1]; //monthDay array starts from 0 + } } - /*=====================================================*/ - /* Low level system time functions */ + seconds += (tm.Day - 1) * SECS_PER_DAY; + seconds += tm.Hour * SECS_PER_HOUR; + seconds += tm.Minute * SECS_PER_MIN; + seconds += tm.Second; + return (timestamp_t)seconds; +} +/*=====================================================*/ +/* Low level system time functions */