A MetaWear timer can be thought of as an event that is fired at fixed intervals. These timers are represented by the MblMwTimer struct and can be safely typcased to a MblMwEvent struct. Timers can be used to schedule periodic tasks or setup a delayed task execution.

Task Scheduling

Before you can schedule tasks, you first need to create a timer, by calling either mbl_mw_timer_create or mbl_mw_timer_create_indefinite. These functions are asynchronous and will pass a pointer to the caller when the timer is created. When you have a valid MblMwTimer, you can use the command recording system outlined in Events section to program the board to respond to the periodic events. Upon recording timer task commands, call mbl_mw_timer_start to start the timer.

When you are done using a timer, you can remove it with mbl_mw_timer_remove.

#include "metawear/core/event.h"
#include "metawear/core/timer.h"

#include "metawear/sensor/gpio.h"

void timer_setup(MblMwMetaWearBoard* board) {
    static auto cmds_recorded = [](void) -> void {
        printf("timer task setup\n");
    static auto timer_created = [](MblMwTimer* timer) -> void {
        auto owner = mbl_mw_event_get_owner((MblMwEvent*) timer);

        // read gpio adc data every time the timer fires an event
        mbl_mw_event_record_commands((MblMwEvent*) timer);
        mbl_mw_gpio_read_analog_input(owner, 0, MBL_MW_GPIO_ANALOG_READ_MODE_ADC);
        mbl_mw_event_end_record((MblMwEvent*) timer, cmds_recorded);


    // create a timer that indefinitely fires events every 500ms
    mbl_mw_timer_create_indefinite(board, 500, 0, timer_created);