4 Application Code

This application initializes the sensor and periodically prints all the sensor values to terminal. The MTCH9010 implements the sensor API as Fetch and Get, meaning that the sensor data is loaded into an internal buffer, and the sensor values are decoded from the buffer later. There are four key elements in this program:

Firstly, the specific MTCH9010 device instance is required. There are multiple ways to obtain the instance, but this code uses DEVICE_DT_GET (DT_COMPAT_GET_ANY_STATUS_OKAY (microchip_mtch9010)) to get any device using the specified driver and convert it into a constant device pointer (const device*). The node identifier is the name defined in the Devicetree.

const struct device *my_sensor = DEVICE_DT_GET(
        DT_COMPAT_GET_ANY_STATUS_OKAY(microchip_mtch9010));

Next, verify that the device initialized correctly. The API call device_is_ready will indicate whether the device can be used. If this API fails, the device pointer should not be used.

if (!device_is_ready(my_sensor)) {
        printk("MTCH9010 is NOT Ready\n");
        return 0;
    }

Once the code is inside the main loop, start by requesting Zephyr to obtain a new device reading. Use the API call sensor_sample_fetch.

sensor_sample_fetch(my_sensor);

Finally, specific sensor data can be retrieved using the API sensor_channel_get. The sensor data is loaded in the sensor_value struct, in the variable val1.

    if (sensor_channel_get(mySensor, SENSOR_CHAN_MTCH9010_OUT_STATE,
                           &val) < 0) {
      printk("Unable to fetch SENSOR_CHAN_MTCH9010_OUT_STATE\r\n");
    } else {
      printk("SENSOR_CHAN_MTCH9010_OUT_STATE = %d\r\n", val.val1);
    }

The following are valid channel options for the MTCH9010 with this API.

Table 4-1. Channel Outputs of the MTCH9010 Driver
Sensor ChannelDescriptionOutput
SENSOR_CHAN_MTCH9010_OUT_STATEState of the OUT GPIO Int32_t; 1 if active, 0 if inactive, <0 if GPIO error
SENSOR_CHAN_MTCH9010_SW_OUT_STATEState of the OUT GPIO, as calculated in software
SENSOR_CHAN_MTCH9010_REFERENCE_VALUEReference value set for the sensor; does not change after device initializationUint16_t
SENSOR_CHAN_MTCH9010_THRESHOLD_VALUEThreshold value set for the sensor; does not change after device initializationUint16_t
SENSOR_CHAN_MTCH9010_MEAS_RESULTLast measured resultUint16_t
SENSOR_CHAN_MTCH9010_MEAS_DELTALast measured delta; can be calculated from the current result if delta output is not setInt16_t
SENSOR_CHAN_MTCH9010_HEARTBEAT_ERROR_STATEIs the heartbeat signal present?Bool; True or False