Gpio

All boards come with general purpose I/O pins allowing users to attach their own sensors. Functions for communicating with the gpio pins are in the gpio.h header file.

Analog Data

Analog input data comes in 2 forms, an ADC value or a absolute reference value. These two modes are distinguished with the MblMwGpioAnalogReadMode enum.

Call mbl_mw_gpio_read_analog_input to read the analog input signal. ADC values are represented as an unsigned integer and is simply a ratiometric value with no units. The absolute reference value is also represented as an unsigned integer and has units of milli volts.

#include "metawear/sensor/gpio.h"

void gpio_analog_stream(MblMwMetaWearBoard* board) {
    static auto adc_handler = [](const MblMwData* data) -> void {
        // Cast value to uint32_t*
        printf("gpio 0 adc= %d", *((uint32_t*) data->value));
    };
    static auto abs_handler = [](const MblMwData* data) -> void {
        // Cast value to uint32_t*
        printf("gpio 1 voltage= %dmV", *((uint32_t*) data->value));
    };

    auto adc_signal = mbl_mw_gpio_get_analog_input_data_signal(board, 0,
            MBL_MW_GPIO_ANALOG_READ_MODE_ADC);
    auto abs_ref_signal = mbl_mw_gpio_get_analog_input_data_signal(board, 1,
            MBL_MW_GPIO_ANALOG_READ_MODE_ABS_REF);

    mbl_mw_datasignal_subscribe(adc_signal, adc_handler);
    mbl_mw_datasignal_subscribe(abs_ref_signal, abs_handler);

    mbl_mw_gpio_read_analog_input(board, 0, MBL_MW_GPIO_ANALOG_READ_MODE_ADC);
    mbl_mw_gpio_read_analog_input(board, 1, MBL_MW_GPIO_ANALOG_READ_MODE_ABS_REF);
}

Digital Data

Digital input data is an input signal that is interpreted as a 1 or 0. As per the product specification section 6.1, a logical high is between 2.1 and 3.0 volts and low is between 0 and 0.9 volts. To ensure that your input signal resides within one of the valid ranges, set the pull mode with mbl_mw_gpio_set_pull_mode.

To read the data input value, issue a call to mbl_mw_gpio_read_digital_input. Digital data is interpreted as an unsigned integer.

#include "metawear/sensor/gpio.h"

void gpio_digital_stream(MblMwMetaWearBoard* board) {
    static auto di_handler = [](const MblMwData* data) -> void {
        // Cast value to uint32_t*
        printf("gpio 2 digial= %d", *((uint32_t*) data->value));
    };

    auto di_signal = mbl_mw_gpio_get_digital_input_data_signal(board, 2);
    mbl_mw_datasignal_subscribe(di_signal, di_handler);
    mbl_mw_gpio_read_digital_input(board, 2);
}

Input Monitoring

The firmware can also monitor the digital state of the input signal and alert the user if the state changes. Set the change type by calling mbl_mw_gpio_set_pin_change_type and then call mbl_mw_gpio_start_pin_monitoring to start the monitoring.

#include "metawear/sensor/gpio.h"

void gpio_digital_stream(MblMwMetaWearBoard* board) {
    auto state_handler = [](const MblMwData* data) -> void {
        // Cast value to uint32_t*
        printf("gpio 3 state= %d", *((uint32_t*) data->value));
    };

    auto state_signal = mbl_mw_gpio_get_pin_monitor_data_signal(board, 3);
    mbl_mw_datasignal_subscribe(state_signal, state_handler);

    // Send notifications when the state transitions from high to low
    mbl_mw_gpio_set_pin_change_type(board, 3, MBL_MW_GPIO_PIN_CHANGE_TYPE_FALLING);
    mbl_mw_gpio_start_pin_monitoring(board, 3);
}