Skip to content

Working with Event Timers

Dave Glover edited this page Jan 31, 2022 · 3 revisions

Working with Event Timers


Examples

Code example, working with Event Timers


Functions

EventLoop *dx_timerGetEventLoop(void);
bool dx_timerChange(DX_TIMER_BINDING *timer, const struct timespec *period);
bool dx_timerOneShotSet(DX_TIMER_BINDING *timer, const struct timespec *delay);
bool dx_timerStart(DX_TIMER_BINDING *timer);
void dx_timerSetStart(DX_TIMER_BINDING *timerSet[], size_t timerCount);
void dx_timerSetStop(DX_TIMER_BINDING *timerSet[], size_t timerCount);
void dx_timerStop(DX_TIMER_BINDING *timer);
void dx_timerEventLoopStop(void);

Structures

GPIO context structure.

typedef struct {
    void (*handler)(EventLoopTimer *timer);
    struct timespec period;  // Deprecated
    struct timespec *delay;
    struct timespec *repeat;
    EventLoopTimer *eventLoopTimer;
    const char *name;
} DX_TIMER_BINDING;

Macros

DX_TIMER_HANDLER(name)
DX_TIMER_HANDLER_END
DX_DECLARE_TIMER_HANDLER(name)

Usage

Understanding the Azure Sphere application

Event-driven programming helps to simplify application design.

There are two types of timer events:

  • Periodic timers
  • One-shot timers

Event timers generate events that are bound to handler functions, which implement desired actions.

The illustration shows the event timers concept.

Periodic timers

The application declares a repeating measureSensorTimer event timer that expires every 6 seconds. When the timer expires, the MeasureSensorHandler handler function is called.

// Forward declaration of timer handler
DX_DECLARE_TIMER_HANDLER(MeasureSensorHandler)

static DX_TIMER measureSensorTimer = {
    .repeat = &(struct timespec){ 6, 0 },
    .name = "measureSensorTimer",
    .handler = MeasureSensorHandler };

The MeasureSensorHandler function will read the environment sensor, format the data into a JSON string, display the JSON data on the Output tab, and then send the telemetry to Azure IoT Hub.

/// <summary>
/// Read sensor and send to Azure IoT
/// </summary>
static DX_TIMER_HANDLER(MeasureSensorHandler))
{
    static int msgId = 0;
    static DX_ENVIRONMENT environment;

    if (dx_readTelemetry(&environment) &&
        snprintf(msgBuffer, JSON_MESSAGE_BYTES, msgTemplate,
            environment.temperature, environment.humidity, environment.pressure, msgId++) > 0)
    {
        Log_Debug("%s\n", msgBuffer);
        dx_azureMsgSendWithProperties(msgBuffer, telemetryMessageProperties, NELEMS(telemetryMessageProperties));
    }
}
DX_TIMER_HANDLER_END 

Oneshot timers

The sample declares a oneshot event timer. This timer will trigger when the oneshot delay timer expires. When the timer expires the oneShotHandler handler function is called.

// Forward declaration of timer handler
DX_DECLARE_TIMER_HANDLER(oneShotHandler)

static DX_TIMER oneShotTimer = {.delay = &(struct timespec){ 2 , 0}, .name = "oneShotTimer", .handler = oneShotHandler };

Starting the oneshot timer

The following is an example of setting the oneshot timer.

/// <summary>
///  Initialize peripherals, device twins, direct methods, timers.
/// </summary>
static void InitPeripheralsAndHandlers(void)
{
    dx_timerSetStart(timerSet, NELEMS(timerSet));
}

Oneshot timer handler

The following example shows the oneshot timer handler. In this example the last thing the function does is to set the oneshot timer for when this function will be next called.

/// <summary>
/// One shot timer handler example
/// </summary>
static DX_TIMER_HANDLER(oneShotHandler) 
{
    Log_Debug("Hello from the oneshot timer. Reloading the oneshot timer period\n");

    // The oneshot timer will trigger again in 2.5 seconds
    dx_timerOneShotSet(&oneShotTimer, &(struct timespec){2, 500 * oneMS}); 
}
DX_TIMER_HANDLER_END 
Clone this wiki locally