-
Notifications
You must be signed in to change notification settings - Fork 7
Working with Event Timers
Code example, working with Event Timers
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);
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;
DX_TIMER_HANDLER(name)
DX_TIMER_HANDLER_END
DX_DECLARE_TIMER_HANDLER(name)
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 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
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 };
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));
}
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
AzureSphereDevX Examples Wiki
- Home
- Build Tools
- Adding the DevX library
- Azure IoT Hub Messaging
- Azure IoT Hub Device Twins
- Azure IoT Hub Direct Methods
- Avnet IoT Connect messaging
- Handling multithreaded async events
- Working with GPIO
- Working with UARTS
- Working with PWM
- Working with Event Timers
- Intercore Messaging
- Application termination
- Deferring updates
- Utility functions
- Tools and scripts
- Hardware Definitions