DS_CAP_TIMERS_COUNT_TICK - denis-stepanov/esp-ds-system GitHub Wiki

DS_CAP_TIMERS_COUNT_TICKER — Support for Countdown Timer Using Ticker

Description

This capability adds support for countdown timers, that is, timers firing periodically. This flavor of a countdown timer is implemented using Ticker (part of ES8266 Arduino Core, which, in turn, uses ESP8266-specific API for software timers). Unlike for countdown absolute timers, here all the times are relative, so the notion of time is not required.

The following methods, complementary to the generic timer methods, are available:

TimerCountdownTick::TimerCountdownTick(const String action = "undefined", const float interval = 1,
  Ticker::callback_function_t callback = nullptr, const bool armed = true, const bool recurrent = true,
  const bool transient = false, const int id = -1);          // Constructor
float TimerCountdownTick::getInterval() const;               // Return timer interval (s)
void TimerCountdownTick::setInterval(const float interval);  // Set timer interval (s)

One can also compare countdown countdown ticker timers among themselves.

Methods getInterval() and setInterval() allow setting timer period in seconds. Fractions of a second are permitted. The rest of the timer methods are the same as for a generic timer.

The field callback defines a handler function to be called when the timer fires. Unlike for countdown absolute timers, here the handler is specified per timer instance, so it is not strictly necessary to define action, unless the handler is reused for several different timers. The handler will be executed in the loop() context (however, see Bugs).

Requires

Cooperates With

None.

Conflicts With

None.

Usage

MySystem.h:

#define DS_CAP_TIMERS_COUNT_TICK // Enable countdown timers via ticker

#include "System.h"         // System global definitions

sketch.ino:

#include "MySystem.h"

using namespace ds;

// Timer handler
void handle_lamp_toggle() {
  static auto lamp_on = false;
  if (lamp_on) {

    // Turn the lamp off here
    // ...
  } else {

    // Turn the lamp on here
    // ...
  }
  lamp_on = !lamp_on;
}

// Countdown timer to flip a lamp with 5 seconds period
TimerCountdownTick timer_lamp_toggle("lamp toggle", 5, handle_lamp_toggle);

void setup() { 
}

void loop() {
}

Mandatory Calls

System::begin() Not required
System::update() Not required

Examples

Bugs

  • Ticker documentation warns against doing lengthy or blocking operations in the handler;
  • In principle, timer firing should be logged in syslog. However, doing so would require wrapping a user-provided timer handler into a ds::System-provided one.

Availability

Version 1.1 or later.

See Also

⚠️ **GitHub.com Fallback** ⚠️