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 Jun 4, 2021
1 parent 4f13a98 commit ac224ea
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
68 changes: 47 additions & 21 deletions src/ext_sensors/ext_sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void accelerometer_trigger_handler(const struct device *dev,
{
int err = 0;
struct sensor_value data[ACCELEROMETER_CHANNELS];
struct ext_sensor_evt evt;
struct ext_sensor_evt evt = {0};

switch (trig->type) {
case SENSOR_TRIG_THRESHOLD:
Expand Down Expand Up @@ -110,62 +110,73 @@ static void accelerometer_trigger_handler(const struct device *dev,

int ext_sensors_init(ext_sensor_handler_t handler)
{
struct ext_sensor_evt evt = {0};

if (handler == NULL) {
LOG_INF("External sensor handler NULL!");
LOG_ERR("External sensor handler NULL!");
return -EINVAL;
}

evt_handler = handler;

if (!device_is_ready(temp_sensor.dev)) {
LOG_ERR("Temperature sensor device is not ready");
return -ENODEV;
evt.type = EXT_SENSOR_EVT_TEMPERATURE_ERROR;
evt_handler(&evt);
}

if (!device_is_ready(humid_sensor.dev)) {
LOG_ERR("Humidity sensor device is not ready");
return -ENODEV;
evt.type = EXT_SENSOR_EVT_HUMIDITY_ERROR;
evt_handler(&evt);
}

if (!device_is_ready(accel_sensor.dev)) {
LOG_ERR("Accelerometer device is not ready");
return -ENODEV;
}

evt.type = EXT_SENSOR_EVT_ACCELEROMETER_ERROR;
evt_handler(&evt);
} else {
#if defined(CONFIG_EXTERNAL_SENSORS_ACTIVITY_DETECTION_AUTO)
struct sensor_trigger trig = {
.chan = SENSOR_CHAN_ACCEL_XYZ,
.type = SENSOR_TRIG_THRESHOLD
};
struct sensor_trigger trig = {
.chan = SENSOR_CHAN_ACCEL_XYZ,
.type = SENSOR_TRIG_THRESHOLD
};

int err = sensor_trigger_set(accel_sensor.dev, &trig, accelerometer_trigger_handler);
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;
}
if (err) {
LOG_ERR("Could not set trigger for device %s, error: %d",
accel_sensor.dev->name, err);
return err;
}
#endif

evt_handler = handler;
}

return 0;
}

int ext_sensors_temperature_get(double *ext_temp)
{
int err;
struct sensor_value data;
struct sensor_value data = {0};
struct ext_sensor_evt evt = {0};

err = sensor_sample_fetch_chan(temp_sensor.dev, SENSOR_CHAN_ALL);
if (err) {
LOG_ERR("Failed to fetch data from %s, error: %d",
temp_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_TEMPERATURE_ERROR;
evt_handler(&evt);
return -ENODATA;
}

err = sensor_channel_get(temp_sensor.dev, temp_sensor.channel, &data);
if (err) {
LOG_ERR("Failed to fetch data from %s, error: %d",
temp_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_TEMPERATURE_ERROR;
evt_handler(&evt);
return -ENODATA;
}

Expand All @@ -179,19 +190,24 @@ int ext_sensors_temperature_get(double *ext_temp)
int ext_sensors_humidity_get(double *ext_hum)
{
int err;
struct sensor_value data;
struct sensor_value data = {0};
struct ext_sensor_evt evt = {0};

err = sensor_sample_fetch_chan(humid_sensor.dev, SENSOR_CHAN_ALL);
if (err) {
LOG_ERR("Failed to fetch data from %s, error: %d",
humid_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_HUMIDITY_ERROR;
evt_handler(&evt);
return -ENODATA;
}

err = sensor_channel_get(humid_sensor.dev, humid_sensor.channel, &data);
if (err) {
LOG_ERR("Failed to fetch data from %s, error: %d",
humid_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_HUMIDITY_ERROR;
evt_handler(&evt);
return -ENODATA;
}

Expand All @@ -207,6 +223,7 @@ int ext_sensors_mov_thres_set(double threshold_new)
int err, input_value;
double range_max_m_s2 = ADXL362_RANGE_MAX_M_S2;
double threshold_new_copy;
struct ext_sensor_evt evt = {0};

if (threshold_new > range_max_m_s2) {
LOG_ERR("Invalid threshold value");
Expand Down Expand Up @@ -241,6 +258,8 @@ int ext_sensors_mov_thres_set(double threshold_new)
LOG_ERR("Failed to set accelerometer x-axis threshold value");
LOG_ERR("Device: %s, error: %d",
accel_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_ACCELEROMETER_ERROR;
evt_handler(&evt);
return err;
}

Expand All @@ -250,6 +269,8 @@ int ext_sensors_mov_thres_set(double threshold_new)
LOG_ERR("Failed to set accelerometer y-axis threshold value");
LOG_ERR("Device: %s, error: %d",
accel_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_ACCELEROMETER_ERROR;
evt_handler(&evt);
return err;
}

Expand All @@ -259,6 +280,8 @@ int ext_sensors_mov_thres_set(double threshold_new)
LOG_ERR("Failed to set accelerometer z-axis threshold value");
LOG_ERR("Device: %s, error: %d",
accel_sensor.dev->name, err);
evt.type = EXT_SENSOR_EVT_ACCELEROMETER_ERROR;
evt_handler(&evt);
return err;
}

Expand All @@ -274,13 +297,16 @@ int ext_sensors_accelerometer_trigger_callback_set(bool enable)
.chan = SENSOR_CHAN_ACCEL_XYZ,
.type = SENSOR_TRIG_THRESHOLD
};
struct ext_sensor_evt evt = {0};

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);
evt.type = EXT_SENSOR_EVT_ACCELEROMETER_ERROR;
evt_handler(&evt);
return err;
}

Expand Down
5 changes: 5 additions & 0 deletions src/ext_sensors/ext_sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ extern "C" {
/** @brief Enum containing callback events from library. */
enum ext_sensor_evt_type {
EXT_SENSOR_EVT_ACCELEROMETER_TRIGGER,

/** Events propagated when an error associated with a sensor device occurs. */
EXT_SENSOR_EVT_ACCELEROMETER_ERROR,
EXT_SENSOR_EVT_TEMPERATURE_ERROR,
EXT_SENSOR_EVT_HUMIDITY_ERROR
};

/** @brief Structure containing external sensor data. */
Expand Down
26 changes: 11 additions & 15 deletions src/modules/sensor_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ static void accelerometer_callback_set(bool enable)
err = ext_sensors_accelerometer_trigger_callback_set(enable);
if (err) {
LOG_ERR("ext_sensors_accelerometer_trigger_callback_set, error: %d", err);
SEND_ERROR(sensor, SENSOR_EVT_ERROR, err);
}
}

Expand All @@ -155,13 +154,22 @@ static void ext_sensor_handler(const struct ext_sensor_evt *const evt)
case EXT_SENSOR_EVT_ACCELEROMETER_TRIGGER:
movement_data_send(evt);
break;
case EXT_SENSOR_EVT_ACCELEROMETER_ERROR:
LOG_ERR("EXT_SENSOR_EVT_ACCELEROMETER_ERROR");
break;
case EXT_SENSOR_EVT_TEMPERATURE_ERROR:
LOG_ERR("EXT_SENSOR_EVT_TEMPERATURE_ERROR");
break;
case EXT_SENSOR_EVT_HUMIDITY_ERROR:
LOG_ERR("EXT_SENSOR_EVT_HUMIDITY_ERROR");
break;
default:
break;
}
}
#endif

static int environmental_data_get(void)
static void environmental_data_get(void)
{
struct sensor_module_event *sensor_module_event;
#if defined(CONFIG_EXTERNAL_SENSORS)
Expand All @@ -172,13 +180,11 @@ static int environmental_data_get(void)
err = ext_sensors_temperature_get(&temp);
if (err) {
LOG_ERR("temperature_get, error: %d", err);
return err;
}

err = ext_sensors_humidity_get(&hum);
if (err) {
LOG_ERR("temperature_get, error: %d", err);
return err;
}

sensor_module_event = new_sensor_module_event();
Expand All @@ -202,8 +208,6 @@ static int environmental_data_get(void)
sensor_module_event->type = SENSOR_EVT_ENVIRONMENTAL_NOT_SUPPORTED;
#endif
EVENT_SUBMIT(sensor_module_event);

return 0;
}

static int setup(void)
Expand Down Expand Up @@ -246,7 +250,6 @@ static void on_state_init(struct sensor_msg_data *msg)
LOG_WRN("Passed in threshold value not valid");
} else if (err) {
LOG_ERR("Failed to set threshold, error: %d", err);
SEND_ERROR(sensor, SENSOR_EVT_ERROR, err);
}
#endif
state_set(STATE_RUNNING);
Expand All @@ -268,7 +271,6 @@ static void on_state_running(struct sensor_msg_data *msg)
LOG_WRN("Passed in threshold value not valid");
} else if (err) {
LOG_ERR("Failed to set threshold, error: %d", err);
SEND_ERROR(sensor, SENSOR_EVT_ERROR, err);
}
#endif
}
Expand All @@ -280,13 +282,7 @@ static void on_state_running(struct sensor_msg_data *msg)
return;
}

int err;

err = environmental_data_get();
if (err) {
LOG_ERR("environmental_data_get, error: %d", err);
SEND_ERROR(sensor, SENSOR_EVT_ERROR, err);
}
environmental_data_get();
}
}

Expand Down

0 comments on commit ac224ea

Please sign in to comment.