Remove old rtc lib and systime

This commit is contained in:
BlackMark 2020-05-16 19:20:32 +02:00
parent b8c7c9ec5c
commit 97bb522189
5 changed files with 49 additions and 322 deletions

@ -1 +1 @@
Subproject commit 16249914c2cc87d7802278d32f75ef3566b17d40 Subproject commit a9467469600d68ce7e386b2ce0a85e2e4c50870a

View File

@ -227,12 +227,6 @@
<Compile Include="ds3231\registers.hpp"> <Compile Include="ds3231\registers.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="ds3231\rtc.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="ds3231\rtc.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="flash\flash.hpp"> <Compile Include="flash\flash.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
@ -248,12 +242,6 @@
<Compile Include="main.cpp"> <Compile Include="main.cpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="systime.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="systime.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="uart\config.hpp"> <Compile Include="uart\config.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>

View File

@ -1,217 +1,90 @@
#include "clock.hpp" #include "clock.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <avr/interrupt.h>
#include "ds3231/ds3231.hpp" #include "ds3231/ds3231.hpp"
#include "uart/uart.hpp" #include "uart/uart.hpp"
#include "systime.h"
using uart_t = uart::Uart0<>; using uart_t = uart::Uart0<>;
REGISTER_UART0_INT_VECTORS(uart_t); REGISTER_UART0_INT_VECTORS(uart_t);
static inline bool receiveLine(char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n") static inline bool isEuDst(const rtc::DateTime &dateTime)
{ {
size_t sizeReceived = 0; constexpr auto calcDstBegin = [](const auto &year) {
const auto beginDay = (31 - ((((5 * year) / 4) + 4) % 7));
return beginDay;
};
while (sizeReceived < sizeBufferLength - 1) { constexpr auto calcDstEnd = [](const auto &year) {
uint8_t ui8ReceiveByte; const auto endDay = (31 - ((((5 * year) / 4) + 1) % 7));
return endDay;
};
while (!uart_t::rxByte(ui8ReceiveByte)) if (dateTime.month > 10 || dateTime.month < 3)
; return false;
szBuffer[sizeReceived++] = ui8ReceiveByte; else if (dateTime.month > 3 && dateTime.month < 10)
szBuffer[sizeReceived] = '\0'; return true;
else if (dateTime.month == 3) {
if (strstr(szBuffer, szLineTerminator)) { if (dateTime.day > calcDstBegin(dateTime.year)) {
szBuffer[sizeReceived - strlen(szLineTerminator)] = '\0';
return true; return true;
} } else if (dateTime.day == calcDstBegin(dateTime.year) && dateTime.hour >= 1)
return true;
return false;
} }
// month == 10
if (dateTime.day < calcDstEnd(dateTime.year)) {
return true;
} else if (dateTime.day == calcDstEnd(dateTime.year) && dateTime.hour < 1)
return true;
return false; return false;
} }
void setup() inline uart_t &operator<<(uart_t &serial, const rtc::DateTime &dateTime)
{ {
uart_t cSerial; serial.txNumber<uint16_t, 10, 4>(dateTime.year);
serial << '-';
if (SysTime::init()) { serial.txNumber<uint8_t, 10, 2>(dateTime.month);
cSerial << "DS3231 detected!" serial << '-';
<< "\r\n"; serial.txNumber<uint8_t, 10, 2>(dateTime.day);
} else { serial << ' ';
cSerial << "ERROR - No RTC detected!" serial.txNumber<uint8_t, 10, 2>(dateTime.hour);
<< "\r\n"; serial << ':';
} serial.txNumber<uint8_t, 10, 2>(dateTime.minute);
serial << ':';
serial.txNumber<uint8_t, 10, 2>(dateTime.second);
return serial;
} }
void setTime() static inline void printLocalTime(const rtc::DateTime &dateTime)
{ {
uart_t cSerial; const auto dst = isEuDst(dateTime);
tm sTime = {}; uart_t serial;
serial << dateTime << (dst ? F(" +2") : F(" +1")) << F("\r\n");
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
cSerial << "Set time:"
<< "\r\n"
<< "Year: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
sTime.tm_year = atoi(szBuffer) - 1900;
cSerial << "Set time:"
<< "\r\n"
<< "Month: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
sTime.tm_mon = atoi(szBuffer) - 1;
cSerial << "Set time:"
<< "\r\n"
<< "Day: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
sTime.tm_mday = atoi(szBuffer);
cSerial << "Set time:"
<< "\r\n"
<< "Hour: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
sTime.tm_hour = atoi(szBuffer);
cSerial << "Minute: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
sTime.tm_min = atoi(szBuffer);
cSerial << "Second: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
sTime.tm_sec = atoi(szBuffer);
mktime(&sTime);
SysTime::setTime(sTime);
}
void setAlarm()
{
uart_t cSerial;
rtc_tm *tmAlarm = rtc_get_alarm();
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
cSerial << "Set alarm:"
<< "\r\n"
<< "Hour: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
tmAlarm->hour = atoi(szBuffer);
cSerial << "Minute: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
tmAlarm->min = atoi(szBuffer);
cSerial << "Second: ";
receiveLine(szBuffer, BUFFER_SIZE, "\r");
cSerial << szBuffer << "\r\n";
tmAlarm->sec = atoi(szBuffer);
rtc_set_alarm(tmAlarm);
} }
int main() int main()
{ {
uart_t cSerial; uart_t cSerial;
rtc::DS3231<i2c::I2c<i2c::Hardware<100'000>>> ds3231; rtc::DS3231<i2c::I2c<i2c::Hardware<100'000>>, false> ds3231;
ds3231.init();
ds3231.init();
cSerial.init(); cSerial.init();
sei();
rtc::DateTime date; rtc::DateTime date;
date.year = 2020; date.year = 2020;
date.month = 5; date.month = 01;
date.day = 16; date.day = 01;
date.hour = 17; date.hour = 19;
date.minute = 24; date.minute = 18;
date.second = 10; date.second = 50;
ds3231.setDateTime(date); ds3231.setDateTime(date);
setup();
while (true) { while (true) {
const auto date = ds3231.getDateTime(); const auto date = ds3231.getDateTime();
cSerial.txNumber<uint16_t, 10, 4>(date.year); printLocalTime(date);
cSerial << '-';
cSerial.txNumber<uint8_t, 10, 2>(date.month);
cSerial << '-';
cSerial.txNumber<uint8_t, 10, 2>(date.day);
cSerial << ' ';
cSerial.txNumber<uint8_t, 10, 2>(date.hour);
cSerial << ':';
cSerial.txNumber<uint8_t, 10, 2>(date.minute);
cSerial << ':';
cSerial.txNumber<uint8_t, 10, 2>(date.second);
cSerial << F("\r\n");
_delay_ms(1000); _delay_ms(1000);
} }
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
while (true) {
if (!SysTime::checkSync()) {
SysTime::syncSysTime();
time_t timeNow = time(nullptr);
tm *psLocalTime = localtime(&timeNow);
strftime(szBuffer, BUFFER_SIZE, "%F %T %z", psLocalTime);
cSerial << "Time: " << szBuffer << "\r\n";
if (rtc_check_alarm()) {
cSerial << "ALARM!"
<< "\r\n";
}
}
uint8_t ui8Cmd;
if (cSerial.rxByte(ui8Cmd)) {
if (ui8Cmd == 's') {
setTime();
} else if (ui8Cmd == 'a') {
setAlarm();
} else if (ui8Cmd == 'p') {
uint8_t ui8Hour;
uint8_t ui8Minute;
uint8_t ui8Second;
rtc_get_alarm_s(&ui8Hour, &ui8Minute, &ui8Second);
sprintf(szBuffer, "Alarm set to: %02d:%02d:%02d", ui8Hour, ui8Minute, ui8Second);
cSerial << szBuffer << "\r\n";
} else {
cSerial << "Invalid char: '";
cSerial.txByte(ui8Cmd);
cSerial << "'"
<< "\r\n";
}
}
}
return 0; return 0;
} }

View File

@ -1,106 +0,0 @@
#include "systime.h"
//////////////////////////////////////////////////////////////////////////
// DST magic by Edgar Bonet
int SysTime::euDST( const time_t *pTime, int32_t *pZ )
{
static_cast<void>( pZ );
uint32_t t = *pTime;
if( static_cast<uint8_t>( t >> 24 ) >= 194 )
t -= 3029443200U;
t = ( t + 655513200 ) / 604800 * 28;
if( static_cast<uint16_t>( t % 1461 ) < 856 )
return ONE_HOUR;
return 0;
}
//////////////////////////////////////////////////////////////////////////
bool SysTime::init()
{
rtc_init();
if( !rtc_is_ds3231() )
return false;
set_zone( 1 * ONE_HOUR );
set_dst( euDST );
syncSysTime();
return true;
}
//////////////////////////////////////////////////////////////////////////
void SysTime::syncSysTime()
{
tm sTime = getTime();
set_system_time( mk_gmtime( &sTime ) );
}
//////////////////////////////////////////////////////////////////////////
bool SysTime::checkSync()
{
time_t timeNow = time( nullptr );
tm *ptmUTC = gmtime( &timeNow );
rtc_tm *ptmRtcTime = rtc_get_time();
if( ptmUTC->tm_sec != ptmRtcTime->sec || ptmUTC->tm_min != ptmRtcTime->min || ptmUTC->tm_hour != ptmRtcTime->hour )
return false;
if( ptmUTC->tm_mday != ptmRtcTime->mday || ( ptmUTC->tm_mon + 1 ) != ptmRtcTime->mon || ( ptmUTC->tm_year + 1900 ) != ptmRtcTime->year )
return false;
return true;
}
//////////////////////////////////////////////////////////////////////////
void SysTime::tick()
{
system_tick();
}
//////////////////////////////////////////////////////////////////////////
tm SysTime::getTime()
{
tm sTime;
rtc_tm *ptmTime = rtc_get_time();
sTime.tm_sec = ptmTime->sec;
sTime.tm_min = ptmTime->min;
sTime.tm_hour = ptmTime->hour;
sTime.tm_mday = ptmTime->mday;
sTime.tm_mon = ptmTime->mon - 1;
sTime.tm_year = ptmTime->year - 1900;
sTime.tm_isdst = 0;
time_t timeUTC = mk_gmtime( &sTime );
sTime = *( gmtime( &timeUTC ) );
return sTime;
}
//////////////////////////////////////////////////////////////////////////
void SysTime::setTime( const tm &sTime )
{
rtc_tm *pRtcTime = rtc_get_time();
pRtcTime->sec = sTime.tm_sec;
pRtcTime->min = sTime.tm_min;
pRtcTime->hour = sTime.tm_hour;
pRtcTime->mday = sTime.tm_mday;
pRtcTime->mon = sTime.tm_mon + 1;
pRtcTime->year = sTime.tm_year + 1900;
pRtcTime->wday = sTime.tm_wday + 1;
pRtcTime->am = ( sTime.tm_hour < 12 ) ? true : false;
pRtcTime->twelveHour = ( sTime.tm_hour == 0 ) ? 12 : ( ( sTime.tm_hour > 12 ) ? ( sTime.tm_hour - 12 ) : sTime.tm_hour );
rtc_set_time( pRtcTime );
syncSysTime();
}

View File

@ -1,28 +0,0 @@
/*
* Copyright (c) by BlackMark 2017
* Date 17/12/2016
* Version 1.0
*/
#ifndef SYSTIME_H
#define SYSTIME_H
#include <time.h>
#include "ds3231/rtc.h"
class SysTime
{
private:
static int euDST( const time_t *pTime, int32_t *pZ );
public:
static bool init();
static void syncSysTime();
static bool checkSync();
static void tick();
static tm getTime();
static void setTime( const tm &sTime );
};
#endif