Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/Application/IrrigationSimple/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
/**
* Set system and TimeAlarm clock from compile datetime or RTC
*/
void setSystemClock(String compileDate, String compileTime)
void setSystemClock(String buildDate, String buildTime)
{
// Retrieve clock time from compile date...
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
// ... ore use the one from integrated RTC.
auto rtcTime = time(NULL);

// Remember to connect at least the CR2032 battery
// to keep the RTC running.
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;

// Set both system time and the alarms one
set_time(actualTime);
setTime(actualTime);

Serial.print("Compile Date and Time: ");
Serial.println(getLocaltime(buildTime));
Serial.println(getLocaltime(buildDateTime));
Serial.print("RTC Date and Time: ");
Serial.println(getLocaltime(rtcTime));
Serial.print("System Clock: ");
Expand Down
2 changes: 1 addition & 1 deletion examples/Application/IrrigationSimple/TimeHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "TimeHelpers.h"

// Convert compile time to system time
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
{
char s_month[5];
int year;
Expand Down
4 changes: 2 additions & 2 deletions examples/Application/IrrigationSimple/TimeHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <mbed_mktime.h>

// Convert compile time to system time
time_t compileDateTimeToSystemTime(const String, const String, bool local_time, int tz);
time_t buildDateTimeToSystemTime(const String, const String, bool local_time, int tz);
String getLocaltime();
String getLocaltime(const time_t &build_time);
String getLocaltime(const char *fmt, bool local_time, int);
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);
8 changes: 4 additions & 4 deletions examples/Application/IrrigationSimpleLCD/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
/**
* Set system and TimeAlarm clock from compile datetime or RTC
*/
void setSystemClock(String compileDate, String compileTime)
void setSystemClock(String buildDate, String buildTime)
{
// Retrieve clock time from compile date...
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
// ... ore use the one from integrated RTC.
auto rtcTime = time(NULL);

// Remember to connect at least the CR2032 battery
// to keep the RTC running.
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;

// Set both system time and the alarms one
set_time(actualTime);
setTime(actualTime);

Serial.print("Compile Date and Time: ");
Serial.println(getLocaltime(buildTime));
Serial.println(getLocaltime(buildDateTime));
Serial.print("RTC Date and Time: ");
Serial.println(getLocaltime(rtcTime));
Serial.print("System Clock: ");
Expand Down
2 changes: 1 addition & 1 deletion examples/Application/IrrigationSimpleLCD/TimeHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "TimeHelpers.h"

// Convert compile time to system time
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
{
char s_month[5];
int year;
Expand Down
4 changes: 2 additions & 2 deletions examples/Application/IrrigationSimpleLCD/TimeHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <mbed_mktime.h>

// Convert compile time to system time
time_t compileDateTimeToSystemTime(const String, const String, bool local_time, int tz);
time_t buildDateTimeToSystemTime(const String, const String, bool local_time, int tz);
String getLocaltime();
String getLocaltime(const time_t &build_time);
String getLocaltime(const char *fmt, bool local_time, int);
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);
String getLocaltime(const time_t build_time, const char *fmt, bool local_time, int tz);
10 changes: 5 additions & 5 deletions examples/Application/LowPowerDataLogger/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int getAverageInputRead(int pin, const size_t loops)
}

// Convert compile time to system time
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
{
char s_month[5];
int year;
Expand Down Expand Up @@ -108,22 +108,22 @@ String getLocaltime(const time_t& build_time)
/**
* Set system clock from compile datetime or RTC
*/
void setSystemClock(String compileDate, String compileTime)
void setSystemClock(String buildDate, String buildTime)
{
// Retrieve clock time from compile date...
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
// ... ore use the one from integrated RTC.
auto rtcTime = time(NULL);

// Remember to connect at least the CR2032 battery
// to keep the RTC running.
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;

// Set both system time
set_time(actualTime);

DebugSerial.print("Compile Date and Time: ");
DebugSerial.println(getLocaltime(buildTime));
DebugSerial.println(getLocaltime(buildDateTime));
DebugSerial.print("RTC Date and Time: ");
DebugSerial.println(getLocaltime(rtcTime));
DebugSerial.print("System Clock: ");
Expand Down
10 changes: 5 additions & 5 deletions examples/Application/RTCClock/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


// Convert compile time to system time
time_t compileDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
time_t buildDateTimeToSystemTime(const String date, const String time, bool local_time = true, int tz = 0)
{
char s_month[5];
int year;
Expand Down Expand Up @@ -56,22 +56,22 @@ String getLocaltime(const time_t& build_time)
/**
* Set system clock from compile datetime or RTC
*/
void setSystemClock(String compileDate, String compileTime)
void setSystemClock(String buildDate, String buildTime)
{
// Retrieve clock time from compile date...
auto buildTime = compileDateTimeToSystemTime(compileDate, compileTime, true, 2);
auto buildDateTime = buildDateTimeToSystemTime(buildDate, buildTime, true, 2);
// ... ore use the one from integrated RTC.
auto rtcTime = time(NULL);

// Remember to connect at least the CR2032 battery
// to keep the RTC running.
auto actualTime = rtcTime > buildTime ? rtcTime : buildTime;
auto actualTime = rtcTime > buildDateTime ? rtcTime : buildDateTime;

// Set both system time
set_time(actualTime);

Serial.print("Compile Date and Time: ");
Serial.println(getLocaltime(buildTime));
Serial.println(getLocaltime(buildDateTime));
Serial.print("RTC Date and Time: ");
Serial.println(getLocaltime(rtcTime));
Serial.print("System Clock: ");
Expand Down
132 changes: 132 additions & 0 deletions examples/Basic/RealTimeClock/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#pragma once

#include <Arduino.h>
#include <Arduino_EdgeControl.h>
#include <mbed_mktime.h>

// Convert build time to UNIX time
time_t getBuildDateTime(bool local_time = true, int tz = 0)
{
char s_month[5];
int year;

tm t;
time_t seconds;

static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";

sscanf(__DATE__, "%s %d %d", s_month, &t.tm_mday, &year);
sscanf(__TIME__, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec);

// Find where is s_month in month_names. Deduce month value.
t.tm_mon = (strstr(month_names, s_month) - month_names) / 3 + 1;
t.tm_year = year - 1900;
_rtc_maketime(&t, &seconds, RTC_FULL_LEAP_YEAR_SUPPORT);

if (!local_time) {
if (tz > 200) {
tz = 0x100 - tz; // Handle negative values
seconds += (3600UL) * tz;
} else {
seconds -= (3600UL) * tz;
}
}

return seconds;
}

String getRTCDate()
{
// APIs to get date fields.
auto years = RealTimeClock.getYears();
auto months = RealTimeClock.getMonths();
auto days = RealTimeClock.getDays();

char buf[12] {};

snprintf(buf, 11, "20%02d-%02d-%02d", years, months, days);

return String(buf);
}

String getRTCTime()
{
// APIs to get time fields.
auto hours = RealTimeClock.getHours();
auto minutes = RealTimeClock.getMinutes();
auto seconds = RealTimeClock.getSeconds();

char buf[11] {};

snprintf(buf, 10, "%02d:%02d:%02d", hours, minutes, seconds);

return String(buf);
}

String getRTCDateTime()
{
auto date = getRTCDate();
auto time = getRTCTime();

auto dateTime = date + ' ' + time;

return dateTime;
}

String getLocaltime()
{
char buffer[32];
tm t;
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
return String(buffer);
}

String getLocaltime(const time_t& build_time)
{
char buffer[32];
tm t;
_rtc_localtime(build_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
return String(buffer);
}

String getLocaltime(const char* fmt, bool local_time = true, int tz = 0)
{
char buffer[64];
time_t tmp_time = time(NULL);
tm t;

if (!local_time) {
if (tz > 200) {
tz = 0x100 - tz; // Handle negative values
tmp_time -= (3600UL) * tz;
} else {
tmp_time += (3600UL) * tz;
}
}

_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
strftime(buffer, 64, fmt, &t);
return String(buffer);
}

String getLocaltime(const time_t build_time, const char* fmt, bool local_time = true, int tz = 0)
{
char buffer[64];
time_t tmp_time = build_time;
tm t;

if (!local_time) {
if (tz > 200) {
tz = 0x100 - tz; // Handle negative values
tmp_time -= (3600UL) * tz;
} else {
tmp_time += (3600UL) * tz;
}
}

_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
strftime(buffer, 64, fmt, &t);
return String(buffer);
}
67 changes: 67 additions & 0 deletions examples/Basic/RealTimeClock/RealTimeClock.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Arduino Edge Control - RTC Example

This sketch shows how to use the RTC (PCF8563T) on the Arduino
Edge Control and how to configure the RTC's time registers.

Circuit:
- Arduino Edge Control
- CR2032 Battery
- Optional: Arduino Edge Control LCD + Button brekout

*/

#include "Helpers.h"
#include <Arduino_EdgeControl.h>

void setup()
{
pinMode(POWER_ON, INPUT);

Serial.begin(9600);

for (auto timeout = millis() + 2500l; !Serial && millis() < timeout; delay(250))
;

Serial.println("Hello, Arduino Edge Control!");

EdgeControl.begin();

Power.on(PWR_3V3);
Power.on(PWR_VBAT);

Wire.begin();
delay(500);

Serial.print("Initializating the Real Time Clock...");
while (!RealTimeClock.begin()) {
Serial.println(" failed! Retrying...");
delay(250);
}
Serial.println(" done!");

// APIs to set date's fields: years, months, days, hours, minutes and seconds
// The RTC time can be set as epoch, using one of the following two options:
// - Calendar time: RealTimeClock.setEpoch(years, months, days, hours, minutes, seconds);
// - UTC time: RealTimeClock.setEpoch(date_in_seconds);

// Set the RTC only when LCD's PowerOn button is pressed.
// YMMV.
if (digitalRead(POWER_ON) == LOW) {
Serial.println("Resetting the RTC to Sketch Build Datetime!");
auto buildDateTime = getBuildDateTime();
RealTimeClock.setEpoch(buildDateTime);
Serial.print("Build ");
}
}

void loop()
{
Serial.print("Date: ");
Serial.println(getRTCDateTime());

Serial.print("Unix time: ");
Serial.println(time(NULL));

delay(1000);
}
Loading