Implement setting time through serial
This commit is contained in:
parent
97bb522189
commit
2fd64c8611
113
ds3231/main.cpp
113
ds3231/main.cpp
@ -1,5 +1,7 @@
|
||||
#include "clock.hpp"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ds3231/ds3231.hpp"
|
||||
#include "uart/uart.hpp"
|
||||
|
||||
@ -62,28 +64,113 @@ static inline void printLocalTime(const rtc::DateTime &dateTime)
|
||||
serial << dateTime << (dst ? F(" +2") : F(" +1")) << F("\r\n");
|
||||
}
|
||||
|
||||
static inline bool operator==(const rtc::DateTime &lhs, const rtc::DateTime &rhs)
|
||||
{
|
||||
if (lhs.second != rhs.second)
|
||||
return false;
|
||||
if (lhs.minute != rhs.minute)
|
||||
return false;
|
||||
if (lhs.hour != rhs.hour)
|
||||
return false;
|
||||
if (lhs.day != rhs.day)
|
||||
return false;
|
||||
if (lhs.month != rhs.month)
|
||||
return false;
|
||||
if (lhs.year != rhs.year)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool operator!=(const rtc::DateTime &lhs, const rtc::DateTime &rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
static inline size_t receiveLine(char *buffer, const size_t maxLength)
|
||||
{
|
||||
uart_t serial;
|
||||
size_t received = 0;
|
||||
|
||||
while (received < maxLength) {
|
||||
if (serial.rxByte(*reinterpret_cast<uint8_t *>(&buffer[received]))) {
|
||||
++received;
|
||||
if (buffer[received - 1] == '\r' || buffer[received - 1] == '\n')
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return received;
|
||||
}
|
||||
|
||||
static inline rtc::DateTime receiveTime()
|
||||
{
|
||||
uart_t serial;
|
||||
|
||||
rtc::DateTime dateTime;
|
||||
constexpr auto BUF_LEN = 8;
|
||||
char receiveBuffer[BUF_LEN];
|
||||
|
||||
serial << F("Enter year: ");
|
||||
auto receivedLen = receiveLine(receiveBuffer, BUF_LEN);
|
||||
receiveBuffer[receivedLen] = '\0';
|
||||
dateTime.year = atoi(receiveBuffer);
|
||||
|
||||
serial << F("\r\nEnter month: ");
|
||||
receivedLen = receiveLine(receiveBuffer, BUF_LEN);
|
||||
receiveBuffer[receivedLen] = '\0';
|
||||
dateTime.month = atoi(receiveBuffer);
|
||||
|
||||
serial << F("\r\nEnter date: ");
|
||||
receivedLen = receiveLine(receiveBuffer, BUF_LEN);
|
||||
receiveBuffer[receivedLen] = '\0';
|
||||
dateTime.day = atoi(receiveBuffer);
|
||||
|
||||
serial << F("\r\nEnter hour: ");
|
||||
receivedLen = receiveLine(receiveBuffer, BUF_LEN);
|
||||
receiveBuffer[receivedLen] = '\0';
|
||||
dateTime.hour = atoi(receiveBuffer);
|
||||
|
||||
serial << F("\r\nEnter minute: ");
|
||||
receivedLen = receiveLine(receiveBuffer, BUF_LEN);
|
||||
receiveBuffer[receivedLen] = '\0';
|
||||
dateTime.minute = atoi(receiveBuffer);
|
||||
|
||||
serial << F("\r\nEnter second: ");
|
||||
receivedLen = receiveLine(receiveBuffer, BUF_LEN);
|
||||
receiveBuffer[receivedLen] = '\0';
|
||||
dateTime.second = atoi(receiveBuffer);
|
||||
serial << F("\r\n");
|
||||
|
||||
return dateTime;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uart_t cSerial;
|
||||
uart_t serial;
|
||||
rtc::DS3231<i2c::I2c<i2c::Hardware<100'000>>, false> ds3231;
|
||||
|
||||
ds3231.init();
|
||||
cSerial.init();
|
||||
serial.init();
|
||||
|
||||
rtc::DateTime date;
|
||||
date.year = 2020;
|
||||
date.month = 01;
|
||||
date.day = 01;
|
||||
date.hour = 19;
|
||||
date.minute = 18;
|
||||
date.second = 50;
|
||||
|
||||
ds3231.setDateTime(date);
|
||||
auto oldDate = ds3231.getDateTime();
|
||||
|
||||
while (true) {
|
||||
const auto date = ds3231.getDateTime();
|
||||
printLocalTime(date);
|
||||
_delay_ms(1000);
|
||||
|
||||
if (oldDate != date) {
|
||||
oldDate = date;
|
||||
printLocalTime(date);
|
||||
}
|
||||
|
||||
uint8_t receivedByte;
|
||||
|
||||
if (serial.rxByte(receivedByte)) {
|
||||
if (receivedByte == 's') {
|
||||
const auto newDate = receiveTime();
|
||||
ds3231.setDateTime(newDate);
|
||||
} else
|
||||
serial << F("Invalid input: ") << static_cast<char>(receivedByte) << F("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user