From 2f47fdb85893343d8bdaf7fe858496914ad105ac Mon Sep 17 00:00:00 2001 From: Felix LVGL Date: Sun, 14 Sep 2025 14:26:54 +0300 Subject: [PATCH] add alarm checker --- README.md | 3 +++ examples/watch/watch.ino | 23 ++++++++++++++++++-- keywords.txt | 3 +++ library.json | 2 +- library.properties | 2 +- src/ChronosESP32.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ src/ChronosESP32.h | 6 +++-- 7 files changed, 80 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 11f96e1..892a804 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,9 @@ void setQr(int index, String qr); // alarms Alarm getAlarm(int index); void setAlarm(int index, Alarm alarm); +bool isAlarmActive(int index); +bool isAlarmActive(Alarm alarm); +bool isAnyAlarmActive(); // control void sendCommand(uint8_t *command, size_t length); diff --git a/examples/watch/watch.ino b/examples/watch/watch.ino index 686b1ce..75ed852 100644 --- a/examples/watch/watch.ino +++ b/examples/watch/watch.ino @@ -32,6 +32,8 @@ #include +#define LED_PIN 2 + ChronosESP32 watch; // ChronosESP32 watch("Chronos Watch"); // set the bluetooth name // ChronosESP32 watch("Chronos Watch", CS_360x360_130_CTF); // set the bluetooth name and screen configuration @@ -329,6 +331,8 @@ void setup() { Serial.begin(115200); + pinMode(LED_PIN, OUTPUT); + // set the callbacks before calling begin funtion watch.setConnectionCallback(connectionCallback); watch.setNotificationCallback(notificationCallback); @@ -364,7 +368,7 @@ void loop() String time = watch.getHourC() + watch.getTime(":%M ") + watch.getAmPmC(); Serial.println(time); - delay(5000); + delay(500); /* // access available notifications @@ -389,7 +393,7 @@ void loop() /* // read the alarms, 8 available - // the alarms are only stored as received from the app, there is no function to trigger it yet + // the alarms are only stored as received from the app for (int j = 0; j < 8; j++){ Alarm a = watch.getAlarm(j); Serial.print("Alarm: "); @@ -401,8 +405,23 @@ void loop() Serial.print("\tState: "); Serial.println(a.enabled ? "Enabled": "Disabled"); } + + // you need to save alarms after receiving them from the app and restore them during setup + // otherwise they will be lost when the esp32 is restarted + Alarm a1; + a1.hour = 7; + a1.minute = 30; + a1.repeat = 0b0111110; // repeat from Monday to Friday + a1.enabled = true; + watch.setAlarm(0, a1); // save alarm at index 0 + + watch.isAlarmActive(0); // check if alarm at index 0 is active + watch.isAlarmActive(watch.getAlarm(0)); // check if a specific alarm is active + watch.isAnyAlarmActive(); // check if any alarm is active */ + digitalWrite(LED_PIN, watch.isAnyAlarmActive()); + /* // access weather forecast details int n = watch.getWeatherCount(); String updateTime = watch.getWeatherTime(); diff --git a/keywords.txt b/keywords.txt index 2ac2479..69eddfd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -27,6 +27,9 @@ getQrAt KEYWORD2 setQr KEYWORD2 getAlarm KEYWORD2 setAlarm KEYWORD2 +isAlarmActive KEYWORD2 +isAlarmActive KEYWORD2 +isAnyAlarmActive KEYWORD2 sendCommand KEYWORD2 musicControl KEYWORD2 setVolume KEYWORD2 diff --git a/library.json b/library.json index 9fd76c2..80b928c 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ChronosESP32", - "version": "1.8.1", + "version": "1.8.2", "keywords": "Arduino, ESP32, Time, BLE, Watch", "description": "A library for ESP32 to interface with Chronos app over BLE", "repository": diff --git a/library.properties b/library.properties index 0a6eb27..81d0e10 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ChronosESP32 -version=1.8.1 +version=1.8.2 author=fbiego maintainer=fbiego sentence=Setup your ESP32 as a smartwatch and connect to Chronos app over BLE. diff --git a/src/ChronosESP32.cpp b/src/ChronosESP32.cpp index b9cdd6c..a5b1fe6 100644 --- a/src/ChronosESP32.cpp +++ b/src/ChronosESP32.cpp @@ -415,6 +415,53 @@ void ChronosESP32::setAlarm(int index, Alarm alarm) _alarms[index % ALARM_SIZE] = alarm; } +/*! + @brief check whether the alarm at the index is active + @param index + position of the alarm to be checked +*/ +bool ChronosESP32::isAlarmActive(int index) +{ + return isAlarmActive(_alarms[index % ALARM_SIZE]); +} + +/*! + @brief check whether the alarm is active + @param alarm + the alarm object to be checked +*/ +bool ChronosESP32::isAlarmActive(Alarm alarm) +{ + if (!alarm.enabled) + return false; + + if (alarm.hour != this->getHour(true) || alarm.minute != this->getMinute()) + return false; + + if (alarm.repeat == 0x80 || alarm.repeat == 0x7F) + return true; + + int day = this->getDayofWeek(); // 0=Sun, ... 6=Sat + + if (day == 0) // Sunday → bit 6 + return (alarm.repeat & (1 << 6)) != 0; + else // Mon–Sat → bits 0–5 + return (alarm.repeat & (1 << (day - 1))) != 0; +} + +/*! + @brief check whether any alarm is active +*/ +bool ChronosESP32::isAnyAlarmActive() +{ + for (int i = 0; i < ALARM_SIZE; i++) + { + if (isAlarmActive(_alarms[i])) + return true; + } + return false; +} + /*! @brief send a command to the app @param command diff --git a/src/ChronosESP32.h b/src/ChronosESP32.h index 274217f..93f4512 100644 --- a/src/ChronosESP32.h +++ b/src/ChronosESP32.h @@ -39,7 +39,7 @@ #define CHRONOSESP_VERSION_MAJOR 1 #define CHRONOSESP_VERSION_MINOR 8 -#define CHRONOSESP_VERSION_PATCH 1 +#define CHRONOSESP_VERSION_PATCH 2 #define CHRONOSESP_VERSION F(CHRONOSESP_VERSION_MAJOR "." CHRONOSESP_VERSION_MINOR "." CHRONOSESP_VERSION_PATCH) @@ -301,9 +301,11 @@ class ChronosESP32 : public BLEServerCallbacks, public BLECharacteristicCallback // alarms Alarm getAlarm(int index); void setAlarm(int index, Alarm alarm); + bool isAlarmActive(int index); + bool isAlarmActive(Alarm alarm); + bool isAnyAlarmActive(); // TODO (alarms) // alarm active callback - // isAlarmActive // getActiveAlarms // control