diff --git a/TimeAlarms.cpp b/TimeAlarms.cpp index 8ff4017..afacbd9 100644 --- a/TimeAlarms.cpp +++ b/TimeAlarms.cpp @@ -242,7 +242,7 @@ void TimeAlarmsClass::serviceAlarms() Alarm[servicedAlarmId].updateNextTrigger(); } if (TickHandler != NULL) { - (*TickHandler)(); // call the handler + TickHandler(); // call the handler } } } diff --git a/TimeAlarms.h b/TimeAlarms.h index 0b4c928..e035daa 100644 --- a/TimeAlarms.h +++ b/TimeAlarms.h @@ -52,7 +52,12 @@ typedef AlarmID_t AlarmId; // Arduino friendly name #define dtINVALID_TIME (time_t)(-1) #define AlarmHMS(_hr_, _min_, _sec_) (_hr_ * SECS_PER_HOUR + _min_ * SECS_PER_MIN + _sec_) +#ifdef ARDUINO_ARCH_ESP8266 +#include +typedef std::function OnTick_t; +#else typedef void (*OnTick_t)(); // alarm callback function typedef +#endif // class defining an alarm instance, only used by dtAlarmsClass class AlarmClass diff --git a/examples/TimeAlarm_ESP8266/TimeAlarm_ESP8266.ino b/examples/TimeAlarm_ESP8266/TimeAlarm_ESP8266.ino new file mode 100644 index 0000000..6e9b3dd --- /dev/null +++ b/examples/TimeAlarm_ESP8266/TimeAlarm_ESP8266.ino @@ -0,0 +1,91 @@ +/* + * TimeAlarm_ESP8266.ino + * + * This example calls two repetitive timers, one every + * 5 seconds and one every 22 seconds. A single shot + * timer is triggered after 58 seconds too. It finishes + * 5 senconds timer + * + * An alarm is triggered 1 minute after current time. + * + * At startup the time is syncd to NTP server, so + * it requires NTP Client library from + * https://github.com/gmag11/NtpClient + * + * It uses a class to initalize triggers + * to show how to use class methods as callbacks. + * + */ + +#define YOUR_WIFI_SSID "Your_SSID" +#define YOUR_WIFI_SSID "Your_Pw" + +#include +#include +#include +#include + +#include +using namespace std; +using namespace placeholders; + +class AlarmManager{ +public: + void begin() { + while (timeStatus() != timeSet) { + // Wait for time sync. Make yourself sure it can connect to WiFi + Serial.print("."); + delay(1000); + } + Serial.println(); + + //Set alarms + Alarm.timerRepeat(0, 0, 22, std::bind(&AlarmManager::timer22,this)); + alarm5 = Alarm.timerRepeat(0, 0, 5, std::bind(&AlarmManager::timer5, this)); + Alarm.timerOnce(0, 0, 58, std::bind(&AlarmManager::timerOnce, this)); + time_t alarm = now() + 60; // Current time + 1 minute + Serial.printf("---- Set alarm at %s\r\n", NTP.getTimeDateString(alarm).c_str()); + Alarm.alarmOnce(hour(alarm), minute(alarm), second(alarm), std::bind(&AlarmManager::alarmOnce, this)); + } + +protected: + AlarmId alarm5; + + void timer22() { + static int i = 0; + Serial.printf("-- %02d. timerRepeat every 22 seconds. It's %s. Uptime: %s\r\n", i, NTP.getTimeDateString().c_str(), NTP.getUptimeString().c_str()); + i++; + } + + void timer5() { + static int i = 0; + Serial.printf("- %02d. timerRepeat every 5 seconds. It's %s. Uptime: %s\r\n", i, NTP.getTimeDateString().c_str(), NTP.getUptimeString().c_str()); + i++; + } + + void timerOnce() { + Serial.printf("--- TimerOnce at 58 seconds. It's %s. Uptime: %s\r\n", NTP.getTimeDateString().c_str(), NTP.getUptimeString().c_str()); + } + + void alarmOnce() { + Serial.printf("---- AlarmOnce. It's %s. Uptime: %s\r\n", NTP.getTimeDateString().c_str(), NTP.getUptimeString().c_str()); + Alarm.free(alarm5); + } + +}; + +AlarmManager manager; + +void setup() +{ + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(YOUR_WIFI_SSID, YOUR_WIFI_PASSWD); + NTP.begin("pool.ntp.org", 1, true); //Start NTP Client + manager.begin(); +} + +void loop() +{ + Alarm.delay(0); +}