Timer

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.

ID

MblMwTiimer objects are identified by a numerical id; you can retrieve the id by calling mbl_mw_timer_get_id. The id is used to retrieve existing timers from the API with the mbl_mw_timer_lookup_id function.

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);

        auto adc_signal= mbl_mw_gpio_get_analog_input_data_signal(board, 0,
                MBL_MW_GPIO_ANALOG_READ_MODE_ADC);
        // read gpio adc data every time the timer fires an event
        mbl_mw_event_record_commands((MblMwEvent*) timer);
        mbl_mw_datasignal_read(adc_signal);
        mbl_mw_event_end_record((MblMwEvent*) timer, cmds_recorded);

        mbl_mw_timer_start(timer);
    };

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