Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
fix: update from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions committed Mar 27, 2021
1 parent c1d0245 commit df92504
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 98 deletions.
4 changes: 4 additions & 0 deletions src/events/app_module_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ static char *get_evt_type_str(enum app_module_event_type type)
return "APP_EVT_DATA_GET";
case APP_EVT_CONFIG_GET:
return "APP_EVT_CONFIG_GET";
case APP_EVT_ACTIVITY_DETECTION_ENABLE:
return "APP_EVT_ACTIVITY_DETECTION_ENABLE";
case APP_EVT_ACTIVITY_DETECTION_DISABLE:
return "APP_EVT_ACTIVITY_DETECTION_DISABLE";
case APP_EVT_DATA_GET_ALL:
return "APP_EVT_DATA_GET_ALL";
case APP_EVT_START:
Expand Down
28 changes: 19 additions & 9 deletions src/events/app_module_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {
/** @brief Application event types submitted by Application module. */
enum app_module_event_type {
/** Signal that the application has done necessary setup, and
* now started.
* now started.
*/
APP_EVT_START,

Expand All @@ -33,33 +33,43 @@ enum app_module_event_type {
APP_EVT_LTE_DISCONNECT,

/** Signal other modules to start sampling and report the data when
* it's ready.
* The event must also contain a list with requested data types,
* @ref app_module_data_type.
* it's ready.
* The event must also contain a list with requested data types,
* @ref app_module_data_type.
*/
APP_EVT_DATA_GET,

/** Create a list with all available sensor types in the system and
* distribute it as a APP_EVT_DATA_GET event.
* distribute it as a APP_EVT_DATA_GET event.
*/
APP_EVT_DATA_GET_ALL,

/** Request latest configuration from the cloud. */
APP_EVT_CONFIG_GET,

/** Application module is waiting for movement to trigger the next sample request. This
* event is used to signal the sensor module to enable activity detection.
*/
APP_EVT_ACTIVITY_DETECTION_ENABLE,

/** Application module does not depend on activity detection. This event is used to signal
* the sensor module to disable activity detection.
*/
APP_EVT_ACTIVITY_DETECTION_DISABLE,

/** The application module has performed all procedures to prepare for
* a shutdown of the system.
* a shutdown of the system.
*/
APP_EVT_SHUTDOWN_READY,

/** An error has occurred in the application module. Error details are
* attached in the event structure.
* attached in the event structure.
*/
APP_EVT_ERROR
};

/** @brief Data types that the application module requests samples for in
* @ref app_module_event_type APP_EVT_DATA_GET.
* @ref app_module_event_type APP_EVT_DATA_GET.
*/
enum app_module_data_type {
APP_DATA_ENVIRONMENTAL,
Expand All @@ -85,7 +95,7 @@ struct app_module_event {
size_t count;

/** The time each module has to fetch data before what is available
* is transmitted.
* is transmitted.
*/
int timeout;
};
Expand Down
7 changes: 7 additions & 0 deletions src/ext_sensors/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ menuconfig EXTERNAL_SENSORS

if EXTERNAL_SENSORS

config EXTERNAL_SENSORS_ACTIVITY_DETECTION_AUTO
bool "Start activity detection automatically"
help
Enable this option to start activity detection when the library is initialized.
If the option is disabled, the accelerometer trigger handler must manually be enabled to
detect activity.

module = EXTERNAL_SENSORS
module-str = External sensors
source "subsys/logging/Kconfig.template.log_config"
Expand Down
43 changes: 35 additions & 8 deletions src/ext_sensors/ext_sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ static struct env_sensor accel_sensor = {
};

static ext_sensor_handler_t evt_handler;
static bool initial_trigger;

static void accelerometer_trigger_handler(const struct device *dev,
struct sensor_trigger *trig)
{
int err = 0;
struct sensor_value data[ACCELEROMETER_CHANNELS];
struct ext_sensor_evt evt;
static bool initial_trigger;

switch (trig->type) {
case SENSOR_TRIG_THRESHOLD:
Expand Down Expand Up @@ -130,15 +130,20 @@ int ext_sensors_init(ext_sensor_handler_t handler)
return -ENODEV;
}

struct sensor_trigger trig = { .chan = SENSOR_CHAN_ACCEL_XYZ };
#if defined(CONFIG_EXTERNAL_SENSORS_ACTIVITY_DETECTION_AUTO)
struct sensor_trigger trig = {
.chan = SENSOR_CHAN_ACCEL_XYZ,
.type = SENSOR_TRIG_THRESHOLD
};

trig.type = SENSOR_TRIG_THRESHOLD;
if (sensor_trigger_set(accel_sensor.dev, &trig,
accelerometer_trigger_handler)) {
LOG_ERR("Could not set trigger for device %s",
accel_sensor.dev->name);
return -ENODATA;
int err = sensor_trigger_set(accel_sensor.dev, &trig, accelerometer_trigger_handler);

if (err) {
LOG_ERR("Could not set trigger for device %s, error: %d",
accel_sensor.dev->name, err);
return err;
}
#endif

evt_handler = handler;

Expand Down Expand Up @@ -261,3 +266,25 @@ int ext_sensors_mov_thres_set(double threshold_new)

return 0;
}

int ext_sensors_accelerometer_trigger_callback_set(bool enable)
{
int err;
struct sensor_trigger trig = {
.chan = SENSOR_CHAN_ACCEL_XYZ,
.type = SENSOR_TRIG_THRESHOLD
};

sensor_trigger_handler_t handler = enable ? accelerometer_trigger_handler : NULL;

err = sensor_trigger_set(accel_sensor.dev, &trig, handler);
if (err) {
LOG_ERR("Could not set trigger for device %s, error: %d",
accel_sensor.dev->name, err);
return err;
}

initial_trigger = false;

return 0;
}
9 changes: 9 additions & 0 deletions src/ext_sensors/ext_sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ int ext_sensors_humidity_get(double *humid);
*/
int ext_sensors_mov_thres_set(double threshold_new);

/**
* @brief Enable or disable accelerometer trigger handler.
*
* @param[in] enable Flag that enables or disables callback triggers from the accelerometer.
*
* @return 0 on success or negative error value on failure.
*/
int ext_sensors_accelerometer_trigger_callback_set(bool enable);

#ifdef __cplusplus
}
#endif
Expand Down
119 changes: 58 additions & 61 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ static struct cloud_data_cfg app_cfg;
*/
static void data_sample_timer_handler(struct k_timer *timer);

/* Timer callback used to signal when the application is expecting movement to trigger the next
* sample request.
*/
static void waiting_for_movement_handler(struct k_timer *timer);

/* Application module message queue. */
#define APP_QUEUE_ENTRY_COUNT 10
#define APP_QUEUE_BYTE_ALIGNMENT 4
Expand All @@ -95,7 +100,7 @@ K_TIMER_DEFINE(movement_timeout_timer, data_sample_timer_handler, NULL);
* lower power consumption by limiting how often GPS search is performed and
* data is sent on air.
*/
K_TIMER_DEFINE(movement_resolution_timer, NULL, NULL);
K_TIMER_DEFINE(movement_resolution_timer, waiting_for_movement_handler, NULL);

/* Module data structure to hold information of the application module, which
* opens up for using convenience functions available for modules.
Expand Down Expand Up @@ -272,7 +277,47 @@ static void data_sample_timer_handler(struct k_timer *timer)
SEND_EVENT(app, APP_EVT_DATA_GET_ALL);
}

static void waiting_for_movement_handler(struct k_timer *timer)
{
ARG_UNUSED(timer);
SEND_EVENT(app, APP_EVT_ACTIVITY_DETECTION_ENABLE);
}

/* Static module functions. */
static void passive_mode_timers_start_all(void)
{
LOG_INF("Device mode: Passive");
LOG_INF("Start movement timeout: %d seconds interval", app_cfg.movement_timeout);

LOG_INF("%d seconds until movement can trigger a new data sample/publication",
app_cfg.movement_resolution);

k_timer_start(&movement_resolution_timer,
K_SECONDS(app_cfg.movement_resolution),
K_SECONDS(0));

k_timer_start(&movement_timeout_timer,
K_SECONDS(app_cfg.movement_timeout),
K_SECONDS(app_cfg.movement_timeout));

k_timer_stop(&data_sample_timer);
}

static void active_mode_timers_start_all(void)
{
LOG_INF("Device mode: Active");
LOG_INF("Start data sample timer: %d seconds interval", app_cfg.active_wait_timeout);

k_timer_start(&data_sample_timer,
K_SECONDS(app_cfg.active_wait_timeout),
K_SECONDS(app_cfg.active_wait_timeout));

k_timer_stop(&movement_resolution_timer);
k_timer_stop(&movement_timeout_timer);

SEND_EVENT(app, APP_EVT_ACTIVITY_DETECTION_DISABLE);
}

static void data_get(void)
{
static bool first = true;
Expand Down Expand Up @@ -329,20 +374,9 @@ static void on_state_init(struct app_msg_data *msg)
app_cfg = msg->module.data.data.cfg;

if (app_cfg.active_mode) {
LOG_INF("Device mode: Active");
LOG_INF("Start data sample timer: %d seconds interval",
app_cfg.active_wait_timeout);
k_timer_start(&data_sample_timer,
K_SECONDS(app_cfg.active_wait_timeout),
K_SECONDS(app_cfg.active_wait_timeout));
active_mode_timers_start_all();
} else {
LOG_INF("Device mode: Passive");
LOG_INF("Start movement timeout: %d seconds interval",
app_cfg.movement_timeout);

k_timer_start(&movement_timeout_timer,
K_SECONDS(app_cfg.movement_timeout),
K_SECONDS(app_cfg.movement_timeout));
passive_mode_timers_start_all();
}

state_set(STATE_RUNNING);
Expand Down Expand Up @@ -371,54 +405,30 @@ void on_sub_state_passive(struct app_msg_data *msg)
app_cfg = msg->module.data.data.cfg;

if (app_cfg.active_mode) {
LOG_INF("Device mode: Active");
LOG_INF("Start data sample timer: %d seconds interval",
app_cfg.active_wait_timeout);
k_timer_start(&data_sample_timer,
K_SECONDS(app_cfg.active_wait_timeout),
K_SECONDS(app_cfg.active_wait_timeout));
k_timer_stop(&movement_timeout_timer);
active_mode_timers_start_all();
sub_state_set(SUB_STATE_ACTIVE_MODE);
return;
}

LOG_INF("Device mode: Passive");
LOG_INF("Start movement timeout: %d seconds interval",
app_cfg.movement_timeout);

k_timer_start(&movement_timeout_timer,
K_SECONDS(app_cfg.movement_timeout),
K_SECONDS(app_cfg.movement_timeout));
k_timer_stop(&data_sample_timer);
passive_mode_timers_start_all();
}

if ((IS_EVENT(msg, sensor, SENSOR_EVT_MOVEMENT_DATA_READY)) ||
(IS_EVENT(msg, ui, UI_EVT_BUTTON_DATA_READY))) {
if ((IS_EVENT(msg, ui, UI_EVT_BUTTON_DATA_READY)) ||
(IS_EVENT(msg, sensor, SENSOR_EVT_MOVEMENT_DATA_READY))) {

if (IS_EVENT(msg, ui, UI_EVT_BUTTON_DATA_READY) &&
msg->module.ui.data.ui.button_number != 2) {
return;
}

/* Trigger sample/publication cycle if there has been movement
* or button 2 has been pushed on the DK.
/* Trigger a sample request if button 2 has been pushed on the DK or activity has
* been detected. The data request can only be triggered if the movement
* resolution timer has timed out.
*/

if (k_timer_remaining_get(&movement_resolution_timer) == 0) {
/* Do an initial data sample. */
data_sample_timer_handler(NULL);

LOG_INF("%d seconds until movement can trigger",
app_cfg.movement_resolution);
LOG_INF("a new data sample/publication");

/* Start one shot timer. After the timer has expired,
* movement is the only event that triggers a new
* one shot timer.
*/
k_timer_start(&movement_resolution_timer,
K_SECONDS(app_cfg.movement_resolution),
K_SECONDS(0));
passive_mode_timers_start_all();
}
}
}
Expand All @@ -431,25 +441,12 @@ static void on_sub_state_active(struct app_msg_data *msg)
app_cfg = msg->module.data.data.cfg;

if (!app_cfg.active_mode) {
LOG_INF("Device mode: Passive");
LOG_INF("Start movement timeout: %d seconds interval",
app_cfg.movement_timeout);
k_timer_start(&movement_timeout_timer,
K_SECONDS(app_cfg.movement_timeout),
K_SECONDS(app_cfg.movement_timeout));
k_timer_stop(&data_sample_timer);
passive_mode_timers_start_all();
sub_state_set(SUB_STATE_PASSIVE_MODE);
return;
}

LOG_INF("Device mode: Active");
LOG_INF("Start data sample timer: %d seconds interval",
app_cfg.active_wait_timeout);

k_timer_start(&data_sample_timer,
K_SECONDS(app_cfg.active_wait_timeout),
K_SECONDS(app_cfg.active_wait_timeout));
k_timer_stop(&movement_timeout_timer);
active_mode_timers_start_all();
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/modules/data_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,11 +1018,6 @@ static void on_all_states(struct data_msg_data *msg)
}

if (IS_EVENT(msg, sensor, SENSOR_EVT_MOVEMENT_DATA_READY)) {
if (current_cfg.active_mode) {
/* Do not store movement data in active mode. */
return;
}

struct cloud_data_accelerometer new_movement_data = {
.values[0] = msg->module.sensor.data.accel.values[0],
.values[1] = msg->module.sensor.data.accel.values[1],
Expand Down
Loading

0 comments on commit df92504

Please sign in to comment.