@@ -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; | |||
@@ -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(); | |||
@@ -5,20 +5,14 @@ | |||
#include <SIM808.h> | |||
#include <SIM808_Types.h> | |||
#include <Wire.h> | |||
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(); | |||
} | |||
} | |||
} |
@@ -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(); | |||
} | |||
} |
@@ -4,6 +4,5 @@ | |||
namespace network { | |||
/*inline */void powerOn() { hardware::sim808::networkPowerOn(); } | |||
/*inline */void powerOff() { hardware::sim808::networkPowerOff(); } | |||
} |
@@ -4,6 +4,6 @@ | |||
namespace network { | |||
/*inline */void powerOn(); | |||
/*inline */void powerOff(); | |||
inline void powerOn() { hardware::sim808::networkPowerOn(); } | |||
inline void powerOff() { hardware::sim808::networkPowerOff(); } | |||
} |
@@ -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 | |||
@@ -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); | |||
@@ -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); | |||
} |
@@ -4,6 +4,4 @@ | |||
#define LOGGER_NAME "Storage" | |||
namespace storage { | |||
void powerOn() {} | |||
void powerOff() {} | |||
} |
@@ -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(); | |||
} | |||
} |
@@ -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 */ |