Skip to content

Commit

Permalink
- Add light sensor to sensor summary
Browse files Browse the repository at this point in the history
- Add light sensor configuration to project configuration
- Change data type for "sensors_summary_ui_set_*" functions to "float" because all other functions are using float
- Replace "sensor_value_to_double" in "zsw_env_sensor.c" with "sensor_value_to_float" because the data types are float
- Replace "sensor_value_to_double" in "zsw_magnetometer.c" with "sensor_value_to_float" because the data types are float
- Rename "bme680" in  to "bme688"
- Add missing include "zsw_env_sensor.h" to "zsw_env_sensor.c"

Signed-off-by: Daniel Kampert <[email protected]>
  • Loading branch information
Kampi committed Oct 9, 2023
1 parent fac4e23 commit bf246a4
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 90 deletions.
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ target_sources(app PRIVATE src/zsw_clock.c)
target_sources(app PRIVATE src/zsw_charger.c)
target_sources(app PRIVATE src/zsw_cpu_freq.c)
target_sources(app PRIVATE src/zsw_env_sensor.c)
target_sources(app PRIVATE src/zsw_light_sensor.c)
target_sources(app PRIVATE src/zsw_gatt_sensor_server.c)
target_sources(app PRIVATE src/zsw_imu.c)
target_sources(app PRIVATE src/zsw_magnetometer.c)
Expand All @@ -52,7 +53,6 @@ target_sources(app PRIVATE src/zsw_phone_app_publisher.c)
target_sources(app PRIVATE src/zsw_power_manager.c)
target_sources(app PRIVATE src/zsw_pressure_sensor.c)

target_sources(app PRIVATE src/zsw_light_sensor.c)
target_sources(app PRIVATE src/zsw_flash.c)

if (DFU_BUILD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
apds9306: apds9306@52 {
compatible = "avago,apds9306";
reg = <0x52>;
status = "okay";
};
};

Expand Down
25 changes: 11 additions & 14 deletions app/drivers/sensor/apds9306/apds9306.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,17 @@ static int apds9306_channel_get(const struct device* p_Dev, enum sensor_channel
{
struct apds9306_data* Data = p_Dev->data;

switch(Channel)
if(Channel != SENSOR_CHAN_LIGHT)
{
case SENSOR_CHAN_LIGHT:
{
p_Value->val1 = Data->light;
p_Value->val2 = 0;

return 0;
}
default:
{
return -ENOTSUP;
}
return -ENOTSUP;
}

// TODO: Conversion to lux is missing here

p_Value->val1 = Data->light;
p_Value->val2 = 0;

return 0;
}

static int apds9306_sensor_setup(const struct device* p_Dev)
Expand Down Expand Up @@ -402,10 +399,10 @@ static int apds9306_init(const struct device* p_Dev)
{
switch(Action)
{
case PM_DEVICE_ACTION_TURN_ON:
case PM_DEVICE_ACTION_SUSPEND:
case PM_DEVICE_ACTION_RESUME:
case PM_DEVICE_ACTION_TURN_OFF:
case PM_DEVICE_ACTION_SUSPEND:
case PM_DEVICE_ACTION_TURN_ON:
{
break;
}
Expand Down
3 changes: 3 additions & 0 deletions app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,6 @@ CONFIG_WATCHFACE_BACKGROUND_SPACE=y
CONFIG_WATCHFACE_BACKGROUND_FLOWER=n
CONFIG_WATCHFACE_BACKGROUND_PLANET=n
CONFIG_WATCHFACE_BACKGROUND_NONE=n

# APDS9306 configuration
CONFIG_APDS9306_IS_APDS9306_065=y
14 changes: 12 additions & 2 deletions app/src/applications/sensors_summary/sensors_summary_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <zephyr/logging/log.h>
#include <zsw_pressure_sensor.h>
#include <math.h>
#include <zsw_env_sensor.h>

#include "zsw_env_sensor.h"
#include "zsw_light_sensor.h"

LOG_MODULE_REGISTER(sensors_summary_app, LOG_LEVEL_DBG);

Expand Down Expand Up @@ -63,12 +65,20 @@ static void timer_callback(lv_timer_t *timer)
float pressure;
float humidity;

if (zsw_env_sensor_fetch_all(&temperature, &pressure, &humidity) != 0) {
if (zsw_env_sensor_fetch_all(&temperature, &pressure, &humidity) != 0)
{
return;
}

zsw_pressure_sensor_fetch_pressure(&pressure);

#if DT_NODE_HAS_STATUS(DT_NODELABEL(apds9306), okay)
float light;

zsw_light_sensor_fetch(&light);
sensors_summary_ui_set_light(light);
#endif

sensors_summary_ui_set_pressure(pressure);
sensors_summary_ui_set_temp(temperature);
sensors_summary_ui_set_gas(0);
Expand Down
28 changes: 23 additions & 5 deletions app/src/applications/sensors_summary/sensors_summary_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static lv_obj_t *humidity_label;
static lv_obj_t *temp_label;
static lv_obj_t *rel_height_label;
static lv_obj_t *gas_label;
static lv_obj_t *light_label;

static void event_set_reference_button(lv_event_t *e)
{
Expand Down Expand Up @@ -79,6 +80,16 @@ static void create_ui(lv_obj_t *parent)
lv_obj_set_align(gas_label, LV_ALIGN_LEFT_MID);
lv_label_set_text(gas_label, "Gas:");

#if DT_NODE_HAS_STATUS(DT_NODELABEL(apds9306), okay)
light_label = lv_label_create(parent);
lv_obj_set_width(light_label, LV_SIZE_CONTENT);
lv_obj_set_height(light_label, LV_SIZE_CONTENT);
lv_obj_set_x(light_label, 5);
lv_obj_set_y(light_label, 15);
lv_obj_set_align(light_label, LV_ALIGN_LEFT_MID);
lv_label_set_text(light_label, "Light: ");
#endif

lv_obj_add_event_cb(set_ref_btn, event_set_reference_button, LV_EVENT_CLICKED, NULL);
}

Expand All @@ -104,27 +115,34 @@ void sensors_summary_ui_remove(void)
root_page = NULL;
}

void sensors_summary_ui_set_pressure(double pressure)
void sensors_summary_ui_set_pressure(float pressure)
{
lv_label_set_text_fmt(pressure_label, "Pressure:\t%.0f Pa", pressure);
}

void sensors_summary_ui_set_humidity(double humidity)
void sensors_summary_ui_set_humidity(float humidity)
{
lv_label_set_text_fmt(humidity_label, "Humidity:\t%.2f %%", humidity);
}

void sensors_summary_ui_set_temp(double temp)
void sensors_summary_ui_set_temp(float temp)
{
lv_label_set_text_fmt(temp_label, "Temp:\t%.2f C", temp);
}

void sensors_summary_ui_set_rel_height(double rel_height)
void sensors_summary_ui_set_rel_height(float rel_height)
{
lv_label_set_text_fmt(rel_height_label, "Rel. height:\t%.2f m", rel_height);
}

void sensors_summary_ui_set_gas(double gas)
void sensors_summary_ui_set_gas(float gas)
{
lv_label_set_text_fmt(gas_label, "Gas:\t%.2f", gas);
}

#if DT_NODE_HAS_STATUS(DT_NODELABEL(apds9306), okay)
void sensors_summary_ui_set_light(float light)
{
lv_label_set_text_fmt(light_label, "Light:\t%.2f", light);
}
#endif
16 changes: 11 additions & 5 deletions app/src/applications/sensors_summary/sensors_summary_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <inttypes.h>
#include <lvgl.h>

#include <zephyr/devicetree.h>

typedef void(*on_ui_close_cb_t)(void);

typedef void(*on_reference_set_cb_t)(void);
Expand All @@ -11,12 +13,16 @@ void sensors_summary_ui_show(lv_obj_t *root, on_ui_close_cb_t close_cb, on_refer

void sensors_summary_ui_remove(void);

void sensors_summary_ui_set_pressure(double pressure);
void sensors_summary_ui_set_pressure(float pressure);

void sensors_summary_ui_set_humidity(float humidity);

void sensors_summary_ui_set_humidity(double humidity);
void sensors_summary_ui_set_temp(float temp);

void sensors_summary_ui_set_temp(double temp);
void sensors_summary_ui_set_rel_height(float rel_height);

void sensors_summary_ui_set_rel_height(double rel_height);
void sensors_summary_ui_set_gas(float gas);

void sensors_summary_ui_set_gas(double gas);
#if DT_NODE_HAS_STATUS(DT_NODELABEL(apds9306), okay)
void sensors_summary_ui_set_light(float light);
#endif
28 changes: 14 additions & 14 deletions app/src/zsw_env_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@
#include <zephyr/drivers/sensor.h>
#include <math.h>

static const struct device *const bme680 = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(bme688));
#include "zsw_env_sensor.h"

static const struct device *const bme688 = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(bme688));

int zsw_env_sensor_fetch_all(float *temperature, float *pressure, float *humidity)
{
struct sensor_value temp_sensor_val;
double gas_res;
int rc;
float gas_res;

if (!device_is_ready(bme680)) {
if (!device_is_ready(bme688)) {
return -ENODEV;
}

rc = sensor_sample_fetch(bme680);
if (rc != 0) {
if (sensor_sample_fetch(bme688) != 0) {
return -ENODATA;
}

sensor_channel_get(bme680, SENSOR_CHAN_AMBIENT_TEMP, &temp_sensor_val);
*temperature = sensor_value_to_double(&temp_sensor_val);
sensor_channel_get(bme688, SENSOR_CHAN_AMBIENT_TEMP, &temp_sensor_val);
*temperature = sensor_value_to_float(&temp_sensor_val);

sensor_channel_get(bme680, SENSOR_CHAN_GAS_RES, &temp_sensor_val);
gas_res = sensor_value_to_double(&temp_sensor_val);
sensor_channel_get(bme688, SENSOR_CHAN_GAS_RES, &temp_sensor_val);
gas_res = sensor_value_to_float(&temp_sensor_val);

sensor_channel_get(bme680, SENSOR_CHAN_HUMIDITY, &temp_sensor_val);
*humidity = sensor_value_to_double(&temp_sensor_val);
sensor_channel_get(bme688, SENSOR_CHAN_HUMIDITY, &temp_sensor_val);
*humidity = sensor_value_to_float(&temp_sensor_val);

sensor_channel_get(bme680, SENSOR_CHAN_PRESS, &temp_sensor_val);
*pressure = sensor_value_to_double(&temp_sensor_val);
sensor_channel_get(bme688, SENSOR_CHAN_PRESS, &temp_sensor_val);
*pressure = sensor_value_to_float(&temp_sensor_val);

return 0;
}
2 changes: 2 additions & 0 deletions app/src/zsw_env_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef ZSW_ENV_SENSOR_H_
#define ZSW_ENV_SENSOR_H_

#include <stdint.h>

int zsw_env_sensor_fetch_all(float *temperature, float *pressure, float *humidity);

#endif
71 changes: 31 additions & 40 deletions app/src/zsw_light_sensor.c
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
#include <zsw_light_sensor.h>
/*
* This file is part of ZSWatch project <https://github.com/jakkra/ZSWatch/>.
* Copyright (c) 2023 Jakob Krantz.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <zsw_env_sensor.h>
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/logging/log.h>
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>

LOG_MODULE_REGISTER(light_sensor, LOG_LEVEL_DBG);
#include "zsw_light_sensor.h"

#define APDS_9306_065_ADDRESS 0x52
#define APDS_9306_065_REG_ID 0x06
#define APDS_9306_065_CHIP_ID 0xB3
static const struct device *const apds9306 = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(apds9306));

// TODO: Proper driver implementation
// Currently just reads ID to verify the HW

#define I2C_DEV DT_NODELABEL(i2c1)

int zsw_light_sensor_detect(void)
int zsw_light_sensor_fetch(float *light)
{
uint8_t id = 0;
const struct device *i2c_dev = DEVICE_DT_GET_OR_NULL(I2C_DEV);

if (i2c_dev == NULL) {
LOG_ERR("Error: no APDS device found.");
return false;
}
struct sensor_value light_sensor_val;

if (!device_is_ready(i2c_dev)) {
LOG_ERR("Error: Device \"%s\" is not ready; "
"check the driver initialization logs for errors.",
i2c_dev->name);
return false;
if((!device_is_ready(apds9306)))
{
return -ENODEV;
}

/* Verify sensor working by reading the ID */
int err = i2c_reg_read_byte(i2c_dev, APDS_9306_065_ADDRESS, APDS_9306_065_REG_ID, &id);
if (err) {
LOG_ERR("Failed reading device id from APDS");
return false;
if(sensor_sample_fetch(apds9306) != 0) {
return -ENODATA;
}

if (id == APDS_9306_065_CHIP_ID) {
LOG_INF("APDS id: %d", id);
return true;
} else {
LOG_ERR("Wrong APDS id: %d", id);
}
sensor_channel_get(apds9306, SENSOR_CHAN_LIGHT, &light_sensor_val);
*light = sensor_value_to_float(&light_sensor_val);

return false;
return 0;
}

SYS_INIT(zsw_light_sensor_detect, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
24 changes: 22 additions & 2 deletions app/src/zsw_light_sensor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
#pragma once
/*
* This file is part of ZSWatch project <https://github.com/jakkra/ZSWatch/>.
* Copyright (c) 2023 Jakob Krantz.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ZSW_LIGHT_SENSOR_H_
#define ZSW_LIGHT_SENSOR_H_

#include <stdint.h>

int zsw_light_sensor_detect(void);
int zsw_light_sensor_fetch(float *light);

#endif
Loading

0 comments on commit bf246a4

Please sign in to comment.