MblMwMetaWearBoard

Like the MetaWearBoard (Android) and MBLMetaWear (iOS) classes, the MblMwMetaWearBoard struct is the C++ API’s representation of a MetaWear board and is the central type for using the API. The metawearboard.h header file provides functions for instantiating and retrieving information about a board.

Instantiation

Before you can instantiate a MblMwMetaWearBoard struct, you must first create a MblMwBtleConnection as outlined in the Bluetooth LE Communication section. Once you have your struct properly setup, call mbl_mw_metawearboard_create to instantiate a MblMwMetaWearBoard struct.

#include "metawear/core/connection.h"
#include "metawear/core/metawearboard.h"

int main(int argc, char **argv) {
    MblMwBtleConnection btle_conn = { write_gatt_char, read_gatt_char };
    MblMwMetaWearBoard* board = mbl_mw_metawearboard_create(&btle_conn);
}

Initialize

After you have instantiated a MblMwMetaWearBoard, you then need to initialize its internal state by calling mbl_mw_metawearboard_initialize. Initializing can take a few seconds and will asynchronously alert the caller when it is finished. If initialization succeeded, MBL_MW_STATUS_OK will be passed to the callback function, otherwise MBL_MW_STATUS_ERROR_TIMEOUT will be used signifying the initialization timed out.

You can check if a MblMwMetaWearBoard has been initialized by calling mbl_mw_metawearboard_is_initialized.

mbl_mw_metawearboard_initialize(board, [](MblMwMetaWearBoard* board, int32_t status) -> void {
    if (!status) {
        printf("Error initializing board: %d\n", status);
    } else {
        printf("Board initialized\n");
    }
);

Initialization must be done everytime you connect to a board.

Tear Down

Calling mbl_mw_metawearboard_tear_down will remove all data processors, loggers, timers, and recorded events from both the board and the struct’s internal state. It does not reset the board so any configuration changes will be preserved.

Modules

Modules are sensors or features supported by the MetaWear firmware. While the C++ API does not partition the module functions in the same way the Android and iOS APIs do, it does provide the mbl_mw_metawearboard_lookup_module function. The output is used to determine what kind of module is present, i.e. which accelerometer is on the board, or if the module is even supported on the board e.g. barometers are not available on all boards.

Constants identifying the modules are defined in the module.h header file.

Freeing Memory

When you are done using the struct, call mbl_mw_metawearboard_free. This function does not affect the state of the MetaWear board, it only frees the memory allocated by the mbl_mw_metawearboard_create function.

Saving and Restoring State

The internal state of the MblMwMetaWearBoard object can be converted into a byte array, which can then be saved to the disk. You will need to free the allocated memory after you are done using the byte array.

uint32_t size;
uint8_t* state = mbl_mw_metawearboard_serialize(board, &size);

for (uint32_t i = 0; i < size; i++) {
    // write content to a stream
}

mbl_mw_memory_free(state);

To restore the board state, pass the byte array into mbl_mw_metawearboard_deserialize. You must still call mbl_mw_metawearboard_is_initialized after deserializing the state.

uint8_t* state;
uint32_t state_size;

// assign state and state_size

mbl_mw_metawearboard_deserialize(board, state, sizeof(state));
mbl_mw_metawearboard_initialize(board, [](MblMwMetaWearBoard* board, int32_t status) -> void {

});