Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34
New RTC library implementation#231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // RTC.h | ||
| #pragma once | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/drivers/counter.h> | ||
| #include <zephyr/drivers/rtc.h> | ||
| #include <time.h> | ||
| // Alarm callback types | ||
| typedef void (*RTCAlarmCallback)(void *user_data); | ||
| typedef void (*RTCUpdateCallback)(void *user_data); | ||
| class ArduinoRTC { | ||
| public: | ||
| ArduinoRTC(); | ||
| bool begin(); | ||
| ~ArduinoRTC(); | ||
| /* setters */ | ||
| bool setDayOfMonth(int day); | ||
| bool setMonthOfYear(int m); | ||
| bool setYear(int year); | ||
| bool setHour(int hour); | ||
| bool setMinute(int minute); | ||
| bool setSecond(int second); | ||
| /* Getters */ | ||
| int getDayOfMonth(); | ||
| int getMonth(); | ||
| int getYear(); | ||
| int getHour(); | ||
| int getMinutes(); | ||
| int getSeconds(); | ||
| int setTime(int year, int month, int day, int hour, int minute, int second); | ||
| int getTime(int &year, int &month, int &day, int &hour, int &minute, int &second); | ||
| #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) | ||
| int setAlarm(int year, int month, int day, int hour, int minute, int second, | ||
| RTCAlarmCallback cb = nullptr, void *user_data = nullptr); | ||
| int cancelAlarm(); | ||
| #elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME) | ||
| int setAlarm(int year, int month, int day, int hour, int minute, int second, | ||
| void (*callback)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data), | ||
| void *cb_user_data); | ||
| void cancelAlarm(); | ||
| #endif | ||
| #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) | ||
| // Optional APIs (only if supported) | ||
| int getAlarm(int &year, int &month, int &day, int &hour, int &minute, int &second); | ||
| bool isAlarmPending(); | ||
| int setUpdateCallback(RTCUpdateCallback cb, void *user_data); | ||
| int setCalibration(int32_t calibration); | ||
| int getCalibration(int32_t &calibration); | ||
| #endif | ||
| private: | ||
| #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) | ||
| const struct device *rtc_dev; | ||
| static void alarmCallbackWrapper(const struct device *dev, uint16_t id, void *user_data); | ||
| static void updateCallbackWrapper(const struct device *dev, void *user_data); | ||
| RTCAlarmCallback userAlarmCallback = nullptr; | ||
| void *userAlarmCallbackData = nullptr; | ||
| RTCUpdateCallback userUpdateCallback = nullptr; | ||
| void *userUpdateCallbackData = nullptr; | ||
| uint16_t alarmId = 0; // default to alarm ID 0 | ||
| #elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME) | ||
| const struct device *counter_dev; | ||
| time_t timeOffset; | ||
| // Alarm members | ||
| struct counter_alarm_cfg alarm_cfg; | ||
| void (*user_callback)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data); | ||
| void *user_data; | ||
| static void alarmHandler(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data); | ||
| time_t datetimeToEpoch(int year, int month, int day, int hour, int minute, int second); | ||
| void epochToDatetime(time_t t, int &year, int &month, int &day, int &hour, int &minute, int &second); | ||
| #endif | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * This sketch sets an alarm 10 seconds in the future and handles it via a callback. | ||
| */ | ||
| #include "RTC.h" | ||
| #include <stdio.h> | ||
| ArduinoRTC rtc; | ||
| char printBuffer[30]; // declare a buffer of large enough size for the message we want to display | ||
| int year, month, day, hour, minute, second; | ||
| #if defined(ARDUINO_NICLA_SENSE_ME) || defined(ARDUINO_NANO33BLE) | ||
| void onAlarm(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data) { | ||
| char printBuffer[40]; | ||
| // Assuming user_data is a string or message you want to print | ||
| sprintf(printBuffer, "Alarm went off! Message: %s\n", (char *)user_data); | ||
| Serial.println(printBuffer); | ||
| } | ||
| #elif | ||
| void onAlarm(void *user_data) { | ||
| char printBuffer[40]; | ||
| sprintf(printBuffer, "Alarm went off! Message: %s\n", (char *)user_data); | ||
| Serial.println(printBuffer); | ||
| } | ||
| #endif | ||
| void setup() { | ||
| int ret = 0xDEADBEEFu; // Starting with a custom value for the return which will definitely lead to failure if not changed to zero (i.e. success) by the functions below | ||
| char printBuffer[60]; | ||
| Serial.begin(115200); | ||
| delay(1000); | ||
| if (!rtc.begin()) { | ||
| Serial.println("RTC not ready\n"); | ||
| return; | ||
| } | ||
| int year, month, day, hour, minute, second; | ||
| ret = rtc.getTime(year, month, day, hour, minute, second); | ||
| if(ret != 0) | ||
| { | ||
| rtc.setTime(2025, 10, 21, 12, 0, 0); | ||
| } | ||
| sprintf(printBuffer, "Current Time: %04d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second); | ||
| Serial.println(printBuffer); | ||
| // Set alarm 10 seconds into the future | ||
| second += 5; | ||
| // Correct for minute rollover if necessary | ||
| if (second >= 60) { | ||
| second -= 60; | ||
| minute += 1; | ||
| } | ||
| // The method also allows for registering a function callback which will be called when the alarm sets off and a display message which will be shown in the console | ||
| ret = rtc.setAlarm(year, month, day, hour, minute, second, onAlarm, (void *)"Wake up!!!"); | ||
| if (ret == 0) { | ||
| sprintf(printBuffer, "Alarm set for: %02d:%02d:%02d\n", hour, minute, second); | ||
| Serial.println(printBuffer); | ||
| } else { | ||
| sprintf(printBuffer, "Failed to set alarm (%d)\n", ret); | ||
| Serial.println(printBuffer); | ||
| } | ||
| } | ||
| void loop() { | ||
| char printBuffer[30]; // declare a buffer of large enough size for the message we want to display | ||
| int y, m, d, h, min, s; | ||
| int status = rtc.getTime(y, m, d, h, min, s); | ||
| // Because the print() and println() functions do not support formatted output directly, we can use this trick to prepare a buffer with the string we want to show | ||
| sprintf(printBuffer, "Time is: %04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, min, s); | ||
| Serial.println(printBuffer); | ||
| delay(1000); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include "RTC.h" | ||
| #include <stdio.h> | ||
| // Doesn't work on the Opta for some reason. | ||
| ArduinoRTC rtc; | ||
| void setup() { | ||
| char printBuffer[30]; | ||
| delay(1000); | ||
| Serial.begin(115200); | ||
| if (!rtc.begin()) { | ||
| printf("RTC not ready\n"); | ||
| return; | ||
| } | ||
| int calib = 0; | ||
| if (rtc.getCalibration(calib) == 0) { | ||
| sprintf(printBuffer, "Current calibration: %d\n", calib); | ||
| Serial.println(printBuffer); | ||
| } else { | ||
| Serial.println("Failed to get calibration"); | ||
| } | ||
| // Apply a small positive calibration (e.g., +1) | ||
| // This value is hardware-dependent, in a real application you should check the microcontroller's datasheet for the correct amount. | ||
| int32_t new_calib = calib + 1; | ||
| if (rtc.setCalibration(new_calib) == 0) { | ||
| sprintf(printBuffer, "Calibration updated to: %d\n", new_calib); | ||
| Serial.println(printBuffer); | ||
| } else { | ||
| Serial.println("Failed to set calibration"); | ||
| } | ||
| } | ||
| void loop() { | ||
| delay(5000); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "RTC.h" | ||
| ArduinoRTC rtc; | ||
| // 7-segment style representation for digits 0–9 and colon ":" | ||
| const char* bigDigits[11][3] = { | ||
| {" _ ", "| |", "|_|"}, // 0 | ||
| {" ", " |", " |"}, // 1 | ||
| {" _ ", " _|", "|_ "}, // 2 | ||
| {" _ ", " _|", " _|"}, // 3 | ||
| {" ", "|_|", " |"}, // 4 | ||
| {" _ ", "|_ ", " _|"}, // 5 | ||
| {" _ ", "|_ ", "|_|"}, // 6 | ||
| {" _ ", " |", " |"}, // 7 | ||
| {" _ ", "|_|", "|_|"}, // 8 | ||
| {" _ ", "|_|", " _|"}, // 9 | ||
| {" ", " . ", " . "} // colon ":" | ||
| }; | ||
| void printBigTime(int h, int m, int s) { | ||
| // Format time as HH:MM:SS string | ||
| char timeStr[9]; | ||
| memset(timeStr, 0, sizeof(timeStr)); | ||
| char bigDigitsPrint[40]; | ||
| memset(bigDigitsPrint, 0, sizeof(bigDigitsPrint)); | ||
| snprintf(timeStr, sizeof(timeStr), "%02d:%02d:%02d", h, m, s); // This (and further such manipulations) is necessary because Serial.print() does not support formatted output like printf() | ||
| Serial.println(timeStr); | ||
| // Print each of the 3 lines row by row | ||
| for (int row = 0; row < 3; row++) { | ||
| for (int i = 0; timeStr[i] != '\0'; i++) { | ||
| char c = timeStr[i]; | ||
| if (c >= '0' && c <= '9') { | ||
| sprintf(bigDigitsPrint, "%s ", bigDigits[c - '0'][row]); | ||
| Serial.print(bigDigitsPrint); | ||
| } else if (c == ':') { | ||
| sprintf(bigDigitsPrint, "%s ", bigDigits[10][row]); | ||
| Serial.print(bigDigitsPrint); | ||
| } else { | ||
| Serial.print(" "); // Space or unknown | ||
| } | ||
| } | ||
| Serial.println(); | ||
| } | ||
| Serial.println(); | ||
| } | ||
| void setup() { | ||
| Serial.begin(115200); | ||
| rtc.begin(); | ||
| bool status = rtc.setTime(2025, 9, 25, 7, 46, 0); // Initial time | ||
| } | ||
| void loop() { | ||
| int y, m, d, h, min, s; | ||
| char printBuffer[60]; | ||
| int status = rtc.getTime(y, m, d, h, min, s); | ||
| // Clear screen (optional line, works on many terminals, tested on Tera Term. Does not take effect in Arduino IDE console unfortunately) | ||
| Serial.println("\033[2J\033[H"); | ||
| // Print date and time in plain format | ||
| sprintf(printBuffer, "Date: %04d-%02d-%02d\n", y, m, d); | ||
| Serial.println(printBuffer); | ||
| Serial.println("Time:"); | ||
| // Print time in big digits | ||
| printBigTime(h, min, s); | ||
| delay(1000); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include "RTC.h" | ||
| ArduinoRTC rtc; | ||
| int year, month, day, hour, minute, second; | ||
| int previousSecond = 0; | ||
| char printBuffer[30]; // Allocate large enough buffer to hold the 28 characters of the output format "Time is: 2025-09-25 11:49:26" | ||
| void setup() { | ||
| Serial.begin(115200); | ||
| if (!rtc.begin()) { | ||
| Serial.println("RTC not ready\n"); | ||
| return; | ||
| } | ||
| rtc.setTime(2025, 10, 21, 12, 0, 0); | ||
| } | ||
| void loop() { | ||
| rtc.getTime(year, month, day, hour, minute, second); // Read back time from hardware | ||
| sprintf(printBuffer, "Time is: %04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second); | ||
| Serial.println(printBuffer); | ||
| delay(1000); | ||
| } |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.