Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify OnTick_t typedef for ESP8266 platform #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion TimeAlarms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void TimeAlarmsClass::serviceAlarms()
Alarm[servicedAlarmId].updateNextTrigger();
}
if (TickHandler != NULL) {
(*TickHandler)(); // call the handler
TickHandler(); // call the handler
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request was not merged because of this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, even though I'm not the author of the pull request, I'm still wondering why line 245 is preventing the PR from being merged. Would you mind elaborating? To me the proposed extension seem to be implemented exactly as it is in other libraries (e.g. the ubiquitous "PubSubClient" MQTT client). The syntax change of line 245 simply ensures that the calling syntax is compatible with both cases: plain old function pointer and std::function.

I'd quite like to see this extension implemented in TimeAlarms. It allows for more elegant OOP design.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like you can have a try of my fork of the TimeAlarm. I rewrote it using the latest ESP 2.4 that is using Newlib C time functions.
https://github.com/jhagberg/ESP8266TimeAlarms

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out! Personally I try as much as possible to use the libraries available through platform.io's package manager and treat those as the "canonical" versions. That's why I'm interested in contributing some minor ESP8266-relatex fixes. A second consideration is to use platform-agnostic libraries (AVR vs ESP) whenever possible.
But then again, having a rebased library specifically for the ESP8266's C library is quite nice, too. Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this code is not reviewed by anyone yet. But it will handle Timezones and Daylight savings very nicely without any extra library. And works for me for the moment at least!

}
}
}
Expand Down
5 changes: 5 additions & 0 deletions TimeAlarms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
typedef std::function<void()> OnTick_t;
#else
typedef void (*OnTick_t)(); // alarm callback function typedef
#endif

// class defining an alarm instance, only used by dtAlarmsClass
class AlarmClass
Expand Down
91 changes: 91 additions & 0 deletions examples/TimeAlarm_ESP8266/TimeAlarm_ESP8266.ino
Original file line number Diff line number Diff line change
@@ -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"
Copy link

@pierangelof pierangelof Jul 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi gmag11,
thank you very much for your job.

The line 21 should be corrected as follows:

-#define YOUR_WIFI_SSID "Your_Pw"
+#define YOUR_WIFI_PASSWD "Your_Pw"


#include <TimeLib.h>
#include <NtpClientLib.h>
#include <ESP8266WiFi.h>
#include <TimeAlarms.h>

#include <functional>
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);
}