Skip to content

Commit 600397d

Browse files
authored
Merge pull request #28 from fbiego/alarms
add alarm checker
2 parents 30fdd81 + 2f47fdb commit 600397d

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ void setQr(int index, String qr);
6666
// alarms
6767
Alarm getAlarm(int index);
6868
void setAlarm(int index, Alarm alarm);
69+
bool isAlarmActive(int index);
70+
bool isAlarmActive(Alarm alarm);
71+
bool isAnyAlarmActive();
6972
7073
// control
7174
void sendCommand(uint8_t *command, size_t length);

examples/watch/watch.ino

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include <ChronosESP32.h>
3434

35+
#define LED_PIN 2
36+
3537
ChronosESP32 watch;
3638
// ChronosESP32 watch("Chronos Watch"); // set the bluetooth name
3739
// ChronosESP32 watch("Chronos Watch", CS_360x360_130_CTF); // set the bluetooth name and screen configuration
@@ -329,6 +331,8 @@ void setup()
329331
{
330332
Serial.begin(115200);
331333

334+
pinMode(LED_PIN, OUTPUT);
335+
332336
// set the callbacks before calling begin funtion
333337
watch.setConnectionCallback(connectionCallback);
334338
watch.setNotificationCallback(notificationCallback);
@@ -364,7 +368,7 @@ void loop()
364368

365369
String time = watch.getHourC() + watch.getTime(":%M ") + watch.getAmPmC();
366370
Serial.println(time);
367-
delay(5000);
371+
delay(500);
368372

369373
/*
370374
// access available notifications
@@ -389,7 +393,7 @@ void loop()
389393

390394
/*
391395
// read the alarms, 8 available
392-
// the alarms are only stored as received from the app, there is no function to trigger it yet
396+
// the alarms are only stored as received from the app
393397
for (int j = 0; j < 8; j++){
394398
Alarm a = watch.getAlarm(j);
395399
Serial.print("Alarm: ");
@@ -401,8 +405,23 @@ void loop()
401405
Serial.print("\tState: ");
402406
Serial.println(a.enabled ? "Enabled": "Disabled");
403407
}
408+
409+
// you need to save alarms after receiving them from the app and restore them during setup
410+
// otherwise they will be lost when the esp32 is restarted
411+
Alarm a1;
412+
a1.hour = 7;
413+
a1.minute = 30;
414+
a1.repeat = 0b0111110; // repeat from Monday to Friday
415+
a1.enabled = true;
416+
watch.setAlarm(0, a1); // save alarm at index 0
417+
418+
watch.isAlarmActive(0); // check if alarm at index 0 is active
419+
watch.isAlarmActive(watch.getAlarm(0)); // check if a specific alarm is active
420+
watch.isAnyAlarmActive(); // check if any alarm is active
404421
*/
405422

423+
digitalWrite(LED_PIN, watch.isAnyAlarmActive());
424+
406425
/* // access weather forecast details
407426
int n = watch.getWeatherCount();
408427
String updateTime = watch.getWeatherTime();

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ getQrAt KEYWORD2
2727
setQr KEYWORD2
2828
getAlarm KEYWORD2
2929
setAlarm KEYWORD2
30+
isAlarmActive KEYWORD2
31+
isAlarmActive KEYWORD2
32+
isAnyAlarmActive KEYWORD2
3033
sendCommand KEYWORD2
3134
musicControl KEYWORD2
3235
setVolume KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ChronosESP32",
3-
"version": "1.8.1",
3+
"version": "1.8.2",
44
"keywords": "Arduino, ESP32, Time, BLE, Watch",
55
"description": "A library for ESP32 to interface with Chronos app over BLE",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ChronosESP32
2-
version=1.8.1
2+
version=1.8.2
33
author=fbiego
44
maintainer=fbiego
55
sentence=Setup your ESP32 as a smartwatch and connect to Chronos app over BLE.

src/ChronosESP32.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,53 @@ void ChronosESP32::setAlarm(int index, Alarm alarm)
415415
_alarms[index % ALARM_SIZE] = alarm;
416416
}
417417

418+
/*!
419+
@brief check whether the alarm at the index is active
420+
@param index
421+
position of the alarm to be checked
422+
*/
423+
bool ChronosESP32::isAlarmActive(int index)
424+
{
425+
return isAlarmActive(_alarms[index % ALARM_SIZE]);
426+
}
427+
428+
/*!
429+
@brief check whether the alarm is active
430+
@param alarm
431+
the alarm object to be checked
432+
*/
433+
bool ChronosESP32::isAlarmActive(Alarm alarm)
434+
{
435+
if (!alarm.enabled)
436+
return false;
437+
438+
if (alarm.hour != this->getHour(true) || alarm.minute != this->getMinute())
439+
return false;
440+
441+
if (alarm.repeat == 0x80 || alarm.repeat == 0x7F)
442+
return true;
443+
444+
int day = this->getDayofWeek(); // 0=Sun, ... 6=Sat
445+
446+
if (day == 0) // Sunday → bit 6
447+
return (alarm.repeat & (1 << 6)) != 0;
448+
else // Mon–Sat → bits 0–5
449+
return (alarm.repeat & (1 << (day - 1))) != 0;
450+
}
451+
452+
/*!
453+
@brief check whether any alarm is active
454+
*/
455+
bool ChronosESP32::isAnyAlarmActive()
456+
{
457+
for (int i = 0; i < ALARM_SIZE; i++)
458+
{
459+
if (isAlarmActive(_alarms[i]))
460+
return true;
461+
}
462+
return false;
463+
}
464+
418465
/*!
419466
@brief send a command to the app
420467
@param command

src/ChronosESP32.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
#define CHRONOSESP_VERSION_MAJOR 1
4141
#define CHRONOSESP_VERSION_MINOR 8
42-
#define CHRONOSESP_VERSION_PATCH 1
42+
#define CHRONOSESP_VERSION_PATCH 2
4343

4444
#define CHRONOSESP_VERSION F(CHRONOSESP_VERSION_MAJOR "." CHRONOSESP_VERSION_MINOR "." CHRONOSESP_VERSION_PATCH)
4545

@@ -301,9 +301,11 @@ class ChronosESP32 : public BLEServerCallbacks, public BLECharacteristicCallback
301301
// alarms
302302
Alarm getAlarm(int index);
303303
void setAlarm(int index, Alarm alarm);
304+
bool isAlarmActive(int index);
305+
bool isAlarmActive(Alarm alarm);
306+
bool isAnyAlarmActive();
304307
// TODO (alarms)
305308
// alarm active callback
306-
// isAlarmActive
307309
// getActiveAlarms
308310

309311
// control

0 commit comments

Comments
 (0)