This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

CAF and sensor_manager

Hi

Is there some example, how to use CAF and sensor_manager. I try to use BME680 sensor with CAF, but i'm somehow stuck. I got the sensor manager working and I see sensor events, but I can't figure out how to read data from the sensor events. 

Thanks..

Tiit

  • Hi,

    There is available documentation here. I couldn't find any example that used the library, but maybe you can start with sharing your code?

    regards

    Jared 

  • Actually I try to create battery powered sensor device with CAF and first try to get some understand about the archidecture.

    I created config file for sensor_manager:

    #include <caf/sensor_manager.h>

    #include "math.h"
    static const struct sm_sampled_channel temp_ch[] = {
            {
                    .chan = SENSOR_CHAN_AMBIENT_TEMP,
                    .data_cnt = 1,
            },
    };
    static const struct sm_sampled_channel hum_ch[] = {
            {
                    .chan = SENSOR_CHAN_HUMIDITY,
                    .data_cnt = 1,
            },
    };
    static const struct sm_sampled_channel press_ch[] = {
            {
                    .chan = SENSOR_CHAN_PRESS,
                    .data_cnt = 1,
            },
    };
    static const struct sm_sensor_config sensor_configs[] = {
            {
                    .dev_name = "BME680",
                    .event_descr = "bme_temp",
                    .chans = temp_ch,
                    .chan_cnt = ARRAY_SIZE(temp_ch),
                    .sampling_period_ms = 5000,
                    .active_events_limit = 1,
            },
            {
                    .dev_name = "BME680",
                    .event_descr = "bme_hum",
                    .chans = hum_ch,
                    .chan_cnt = ARRAY_SIZE(hum_ch),
                    .sampling_period_ms = 5000,
                    .active_events_limit = 1,
            },
            {
                    .dev_name = "BME680",
                    .event_descr = "bme_press",
                    .chans = temp_ch,
                    .chan_cnt = ARRAY_SIZE(press_ch),
                    .sampling_period_ms = 5000,
                    .active_events_limit = 1,
            },
    };
     and then a module for event data management:
    /*
     * Copyright (c) 2021 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    #include <zephyr.h>
    #include <stdio.h>
    #define MODULE bme680_sensor
    #include <caf/events/module_state_event.h>
    #include <caf/events/sensor_event.h>
    #include <logging/log.h>
    LOG_MODULE_REGISTER(MODULE, CONFIG_CAF_BME680_SENSOR_LOG_LEVEL);
    float temp, hum, press;
    static bool event_handler(const struct event_header *eh)
    {
        if (is_sensor_event(eh)) {
           
            struct sensor_event *event = cast_sensor_event(eh);
            float * sensor_data = sensor_event_get_data_ptr(event);
               
        }
       
        if (is_module_state_event(eh)) {
            //const struct module_state_event *event = cast_module_state_event(eh);
            return false;
        }
       
        /* Event not handled but subscribed. */
        __ASSERT_NO_MSG(false);
        return false;
    }
    EVENT_LISTENER(MODULE, event_handler);
    EVENT_SUBSCRIBE(MODULE, module_state_event);
    EVENT_SUBSCRIBE(MODULE, sensor_event);
    Is that the right way to configure sensor and get data? I should get sensor data pointer from sensor_event_get_data_ptr(event) then?
     
    Sorry .. but I'm still very noob in Zephyr.
    Tiit
  • I got the values from sensor and I think I got it working.

Related