From 6838690539cad237601fa19831ba37c124bbd77e Mon Sep 17 00:00:00 2001 From: Darryldh Date: Wed, 4 Aug 2021 21:30:12 -0400 Subject: [PATCH 1/5] Initial code dump of battery.c --- app/src/battery.c | 246 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 224 insertions(+), 22 deletions(-) diff --git a/app/src/battery.c b/app/src/battery.c index c63008e6..300cc608 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -25,40 +25,242 @@ static uint8_t last_state_of_charge = 0; uint8_t zmk_battery_state_of_charge() { return last_state_of_charge; } static int zmk_battery_update(const struct device *battery) { - struct sensor_value state_of_charge; + struct sensor_value state_of_charge, + state_of_health, + voltage, + current, + current_standby, + current_max_load, + full_charge_capacity, + remaining_charge_capacity, + nominal_available_capacity, + full_available_capacity, + avg_power, + int_temp; - int rc = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE); + int rc; + int status = 0; + int rv = 0; + + status = sensor_sample_fetch(battery); - if (rc != 0) { - LOG_DBG("Failed to fetch battery values: %d", rc); - return rc; + if (status < 0) { + LOG_ERR("Failed to fetch battery data from configured sensor. error: %d\n", status); + return status; } - rc = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE, &state_of_charge); + struct zmk_battery_state_changed bat_state_data; - if (rc != 0) { - LOG_DBG("Failed to get battery state of charge: %d", rc); - return rc; + /* ---------- get channel data for State of Charge ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE, &state_of_charge); + + if (status != 0 && status != -ENOTSUP) { + LOG_WRN("Failed to get battery state of charge: %d", rc); + rv++; } - if (last_state_of_charge != state_of_charge.val1) { - last_state_of_charge = state_of_charge.val1; + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: State of Charge"); + } else { + /* channel is supported by sensor */ + /* ensure channel value has actually changed before updating it */ + if (last_state_of_charge != state_of_charge.val1) { + last_state_of_charge = state_of_charge.val1; - LOG_DBG("Setting BAS GATT battery level to %d.", last_state_of_charge); + LOG_DBG("Setting BAS GATT battery level to %d%%", last_state_of_charge); - rc = bt_bas_set_battery_level(last_state_of_charge); + /* set BAS GATT battery level so it can be displayed at the host */ + rc = bt_bas_set_battery_level(last_state_of_charge); + + if (rc != 0) { + LOG_WRN("Failed to set BAS GATT battery level (err %d)", rc); + rv++; + } + + /* append this value to the battery state changed event */ + bat_state_data.state_of_charge = last_state_of_charge; + LOG_DBG("-----> battery State of Charge: %d%%", last_state_of_charge); - if (rc != 0) { - LOG_WRN("Failed to set BAS GATT battery level (err %d)", rc); - return rc; } - - rc = ZMK_EVENT_RAISE(new_zmk_battery_state_changed( - (struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge})); } - return rc; -} + /* ---------- get channel data for State of Health ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_HEALTH, &state_of_health); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery State of Health: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The configured battery sensor does not support the channel: State of Health"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.state_of_health = state_of_health.val1; + LOG_DBG("-----> battery State of Health: %d%%", state_of_health.val1); + } + + /* ---------- get channel data for Voltage ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_VOLTAGE, &voltage); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery voltage: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The configured battery sensor does not support the channel: Voltage"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.voltage = voltage.val1; + LOG_DBG("-----> battery voltage: %d.%dV", voltage.val1, voltage.val2); + } + + /* ---------- get channel data for Average Current ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_AVG_CURRENT, ¤t); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Average Current: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Average Current"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.current = current.val1; + LOG_DBG("-----> battery Average Current: %d.%dA", current.val1, current.val2); + } + + /* ---------- get channel data for Standby Current ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STDBY_CURRENT, ¤t_standby); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Standby Current: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Standby Current"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.current_standby = current_standby.val1; + LOG_DBG("-----> battery Standby Current: %d.%dA", current_standby.val1, + current_standby.val2); + } + + /* ---------- get channel data for Maximum Load Current ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT, ¤t_max_load); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Maximum Load Current: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Maximum Load Current"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.current_max_load = current_max_load.val1; + LOG_DBG("-----> battery Maximum Load Current: %d.%dA", current_max_load.val1, + current_max_load.val2); + } + + /* ---------- get channel data for Full Charge Capacity ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY, &full_charge_capacity); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Full Charge Capacity: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Full Charge Capacity"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.full_charge_capacity = full_charge_capacity.val1; + LOG_DBG("-----> battery Full Charge Capacity: %d.%dmAh", full_charge_capacity.val1, + full_charge_capacity.val2); + } + + /* ---------- get channel data for Remaining Charge Capacity ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY, &remaining_charge_capacity); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Remaining Charge Capacity: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Remaining Charge Capacity"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.remaining_charge_capacity = remaining_charge_capacity.val1; + LOG_DBG("-----> battery Remaining Charge Capacity: %d.%dmAh", remaining_charge_capacity.val1, + remaining_charge_capacity.val2); + } + + /* ---------- get channel data for Nominal Available Capacity ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY, &nominal_available_capacity); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Nominal Available Capacity: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Nominal Available Capacity"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.nominal_available_capacity = nominal_available_capacity.val1; + LOG_DBG("-----> battery Nominal Available Capacity: %d.%dmAh", nominal_available_capacity.val1, + nominal_available_capacity.val2); + } + + /* ---------- get channel data for Full Available Capacity ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY, &full_available_capacity);; + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Full Available Capacity: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Full Available Capacity"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.full_available_capacity = full_available_capacity.val1; + LOG_DBG("-----> battery Full Available Capacity: %d.%dmAh", full_available_capacity.val1, + full_available_capacity.val2); + } + + /* ---------- get channel data for Average Power Usage ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_AVG_POWER, &avg_power); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get battery Average Power Usage: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Average Power Usage"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.avg_power = avg_power.val1; + LOG_DBG("-----> battery Average Power Usage: %d.%dmW", avg_power.val1, avg_power.val2); + } + + /* ---------- get channel data for Internal IC Temperature ---------- */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_TEMP, &int_temp); + + if (status != 0 && status != -ENOTSUP) { + LOG_DBG("Failed to get Internal IC Temperature: %d", status); + rv++; + } + if (status == -ENOTSUP) { + LOG_DBG("The battery sensor does not support the channel: Internal IC Temperature"); + } else { + /* append this value to the battery state changed event */ + bat_state_data.int_temp = int_temp.val1; + LOG_DBG("-----> battery Internal IC Temperature: %d.%d C", int_temp.val1, int_temp.val2); + } + + /* trigger event to notify of battery sensor value changes */ + ZMK_EVENT_RAISE(new_zmk_battery_state_changed(bat_state_data)); + + return rv; + +} /* zmk_battery_update() */ static void zmk_battery_work(struct k_work *work) { int rc = zmk_battery_update(battery); @@ -85,7 +287,7 @@ static int zmk_battery_init(const struct device *_arg) { int rc = zmk_battery_update(battery); if (rc != 0) { - LOG_DBG("Failed to update battery value: %d.", rc); + LOG_WRN("Failed to update %d battery value(s) which the configured sensor supports.", rc); return rc; } From a73cab1e6524f9d35156b3b15c157ab83c2eac42 Mon Sep 17 00:00:00 2001 From: Darryldh Date: Thu, 5 Aug 2021 19:46:19 -0400 Subject: [PATCH 2/5] Update battery_state_changed.h New channels added to struct. --- .../zmk/events/battery_state_changed.h | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/include/zmk/events/battery_state_changed.h b/app/include/zmk/events/battery_state_changed.h index 6a003d8d..3f84760a 100644 --- a/app/include/zmk/events/battery_state_changed.h +++ b/app/include/zmk/events/battery_state_changed.h @@ -10,8 +10,40 @@ #include struct zmk_battery_state_changed { - // TODO: Other battery channels + + /* Remaining capacity as a %age */ uint8_t state_of_charge; + + /* State of Health */ + int16_t state_of_health; + + /* Current cell voltage in units of 1.25/16mV */ + uint32_t voltage; + + /* Average current in units of 1.5625uV / Rsense */ + uint32_t current; + + /* Standby current in mA? uA? */ + int32_t current_standby; + + /* Maximum Load Current in mA? uA? */ + int32_t current_max_load; + + /* Full charge capacity in 5/Rsense uA */ + int32_t full_charge_capacity; + + /* Remaining capacity in 5/Rsense uA */ + int32_t remaining_charge_capacity; + + int32_t nominal_available_capacity; + + int32_t full_available_capacity; + + /* Average power consumption in mA? uA? */ + int32_t avg_power; + + /* Internal temperature in units of 1/256 degrees C */ + int32_t int_temp; }; ZMK_EVENT_DECLARE(zmk_battery_state_changed); \ No newline at end of file From 9f995ed7440a81c49482ab2038decbd0e12e7362 Mon Sep 17 00:00:00 2001 From: Darryldh Date: Thu, 5 Aug 2021 20:00:13 -0400 Subject: [PATCH 3/5] Update battery.c added some comments to identify where help is needed --- app/src/battery.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/battery.c b/app/src/battery.c index 300cc608..71090934 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -110,7 +110,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The configured battery sensor does not support the channel: Voltage"); } else { /* append this value to the battery state changed event */ - bat_state_data.voltage = voltage.val1; + bat_state_data.voltage = voltage.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery voltage: %d.%dV", voltage.val1, voltage.val2); } @@ -125,7 +125,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Average Current"); } else { /* append this value to the battery state changed event */ - bat_state_data.current = current.val1; + bat_state_data.current = current.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Average Current: %d.%dA", current.val1, current.val2); } @@ -140,7 +140,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Standby Current"); } else { /* append this value to the battery state changed event */ - bat_state_data.current_standby = current_standby.val1; + bat_state_data.current_standby = current_standby.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Standby Current: %d.%dA", current_standby.val1, current_standby.val2); } @@ -156,7 +156,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Maximum Load Current"); } else { /* append this value to the battery state changed event */ - bat_state_data.current_max_load = current_max_load.val1; + bat_state_data.current_max_load = current_max_load.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Maximum Load Current: %d.%dA", current_max_load.val1, current_max_load.val2); } @@ -172,7 +172,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Full Charge Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.full_charge_capacity = full_charge_capacity.val1; + bat_state_data.full_charge_capacity = full_charge_capacity.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Full Charge Capacity: %d.%dmAh", full_charge_capacity.val1, full_charge_capacity.val2); } @@ -188,7 +188,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Remaining Charge Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.remaining_charge_capacity = remaining_charge_capacity.val1; + bat_state_data.remaining_charge_capacity = remaining_charge_capacity.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Remaining Charge Capacity: %d.%dmAh", remaining_charge_capacity.val1, remaining_charge_capacity.val2); } @@ -204,7 +204,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Nominal Available Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.nominal_available_capacity = nominal_available_capacity.val1; + bat_state_data.nominal_available_capacity = nominal_available_capacity.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Nominal Available Capacity: %d.%dmAh", nominal_available_capacity.val1, nominal_available_capacity.val2); } @@ -220,7 +220,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Full Available Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.full_available_capacity = full_available_capacity.val1; + bat_state_data.full_available_capacity = full_available_capacity.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Full Available Capacity: %d.%dmAh", full_available_capacity.val1, full_available_capacity.val2); } @@ -236,7 +236,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Average Power Usage"); } else { /* append this value to the battery state changed event */ - bat_state_data.avg_power = avg_power.val1; + bat_state_data.avg_power = avg_power.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Average Power Usage: %d.%dmW", avg_power.val1, avg_power.val2); } @@ -251,7 +251,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Internal IC Temperature"); } else { /* append this value to the battery state changed event */ - bat_state_data.int_temp = int_temp.val1; + bat_state_data.int_temp = int_temp.val1; /*combine val1" and "val2" here*/ LOG_DBG("-----> battery Internal IC Temperature: %d.%d C", int_temp.val1, int_temp.val2); } From 09c015b7f2e46fb5e1300c9589bc904fb31562b6 Mon Sep 17 00:00:00 2001 From: Darryldh Date: Thu, 5 Aug 2021 20:09:40 -0400 Subject: [PATCH 4/5] Update battery.c fixed the comments --- app/src/battery.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/battery.c b/app/src/battery.c index 71090934..dff2a244 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -110,7 +110,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The configured battery sensor does not support the channel: Voltage"); } else { /* append this value to the battery state changed event */ - bat_state_data.voltage = voltage.val1; /*combine val1" and "val2" here*/ + bat_state_data.voltage = voltage.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery voltage: %d.%dV", voltage.val1, voltage.val2); } @@ -125,7 +125,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Average Current"); } else { /* append this value to the battery state changed event */ - bat_state_data.current = current.val1; /*combine val1" and "val2" here*/ + bat_state_data.current = current.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Average Current: %d.%dA", current.val1, current.val2); } @@ -140,7 +140,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Standby Current"); } else { /* append this value to the battery state changed event */ - bat_state_data.current_standby = current_standby.val1; /*combine val1" and "val2" here*/ + bat_state_data.current_standby = current_standby.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Standby Current: %d.%dA", current_standby.val1, current_standby.val2); } @@ -156,7 +156,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Maximum Load Current"); } else { /* append this value to the battery state changed event */ - bat_state_data.current_max_load = current_max_load.val1; /*combine val1" and "val2" here*/ + bat_state_data.current_max_load = current_max_load.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Maximum Load Current: %d.%dA", current_max_load.val1, current_max_load.val2); } @@ -172,7 +172,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Full Charge Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.full_charge_capacity = full_charge_capacity.val1; /*combine val1" and "val2" here*/ + bat_state_data.full_charge_capacity = full_charge_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Full Charge Capacity: %d.%dmAh", full_charge_capacity.val1, full_charge_capacity.val2); } @@ -188,7 +188,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Remaining Charge Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.remaining_charge_capacity = remaining_charge_capacity.val1; /*combine val1" and "val2" here*/ + bat_state_data.remaining_charge_capacity = remaining_charge_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Remaining Charge Capacity: %d.%dmAh", remaining_charge_capacity.val1, remaining_charge_capacity.val2); } @@ -204,7 +204,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Nominal Available Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.nominal_available_capacity = nominal_available_capacity.val1; /*combine val1" and "val2" here*/ + bat_state_data.nominal_available_capacity = nominal_available_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Nominal Available Capacity: %d.%dmAh", nominal_available_capacity.val1, nominal_available_capacity.val2); } @@ -220,7 +220,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Full Available Capacity"); } else { /* append this value to the battery state changed event */ - bat_state_data.full_available_capacity = full_available_capacity.val1; /*combine val1" and "val2" here*/ + bat_state_data.full_available_capacity = full_available_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Full Available Capacity: %d.%dmAh", full_available_capacity.val1, full_available_capacity.val2); } @@ -236,7 +236,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Average Power Usage"); } else { /* append this value to the battery state changed event */ - bat_state_data.avg_power = avg_power.val1; /*combine val1" and "val2" here*/ + bat_state_data.avg_power = avg_power.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Average Power Usage: %d.%dmW", avg_power.val1, avg_power.val2); } @@ -251,7 +251,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: Internal IC Temperature"); } else { /* append this value to the battery state changed event */ - bat_state_data.int_temp = int_temp.val1; /*combine val1" and "val2" here*/ + bat_state_data.int_temp = int_temp.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Internal IC Temperature: %d.%d C", int_temp.val1, int_temp.val2); } From c551cf2d6840dab39c9c6e7931f903b419811b1c Mon Sep 17 00:00:00 2001 From: Darryldh Date: Fri, 13 Aug 2021 15:15:56 -0400 Subject: [PATCH 5/5] reverted code to work for Corne-ish Zen --- app/src/battery.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/app/src/battery.c b/app/src/battery.c index dff2a244..8f5fd6d5 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -42,7 +42,7 @@ static int zmk_battery_update(const struct device *battery) { int status = 0; int rv = 0; - status = sensor_sample_fetch(battery); + //status = sensor_sample_fetch(battery); if (status < 0) { LOG_ERR("Failed to fetch battery data from configured sensor. error: %d\n", status); @@ -52,7 +52,7 @@ static int zmk_battery_update(const struct device *battery) { struct zmk_battery_state_changed bat_state_data; /* ---------- get channel data for State of Charge ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE, &state_of_charge); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE); if (status != 0 && status != -ENOTSUP) { LOG_WRN("Failed to get battery state of charge: %d", rc); @@ -63,6 +63,7 @@ static int zmk_battery_update(const struct device *battery) { LOG_DBG("The battery sensor does not support the channel: State of Charge"); } else { /* channel is supported by sensor */ + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_CHARGE, &state_of_charge); /* ensure channel value has actually changed before updating it */ if (last_state_of_charge != state_of_charge.val1) { last_state_of_charge = state_of_charge.val1; @@ -85,7 +86,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for State of Health ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_HEALTH, &state_of_health); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_STATE_OF_HEALTH); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery State of Health: %d", status); @@ -94,13 +95,14 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The configured battery sensor does not support the channel: State of Health"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STATE_OF_HEALTH, &state_of_health); /* append this value to the battery state changed event */ bat_state_data.state_of_health = state_of_health.val1; LOG_DBG("-----> battery State of Health: %d%%", state_of_health.val1); } /* ---------- get channel data for Voltage ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_VOLTAGE, &voltage); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_VOLTAGE); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery voltage: %d", status); @@ -109,13 +111,14 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The configured battery sensor does not support the channel: Voltage"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_VOLTAGE, &voltage); /* append this value to the battery state changed event */ bat_state_data.voltage = voltage.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery voltage: %d.%dV", voltage.val1, voltage.val2); } /* ---------- get channel data for Average Current ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_AVG_CURRENT, ¤t); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_AVG_CURRENT); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Average Current: %d", status); @@ -124,13 +127,14 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Average Current"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_AVG_CURRENT, ¤t); /* append this value to the battery state changed event */ bat_state_data.current = current.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Average Current: %d.%dA", current.val1, current.val2); } /* ---------- get channel data for Standby Current ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STDBY_CURRENT, ¤t_standby); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_STDBY_CURRENT); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Standby Current: %d", status); @@ -139,6 +143,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Standby Current"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_STDBY_CURRENT, ¤t_standby); /* append this value to the battery state changed event */ bat_state_data.current_standby = current_standby.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Standby Current: %d.%dA", current_standby.val1, @@ -146,7 +151,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for Maximum Load Current ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT, ¤t_max_load); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Maximum Load Current: %d", status); @@ -155,6 +160,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Maximum Load Current"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT, ¤t_max_load); /* append this value to the battery state changed event */ bat_state_data.current_max_load = current_max_load.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Maximum Load Current: %d.%dA", current_max_load.val1, @@ -162,7 +168,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for Full Charge Capacity ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY, &full_charge_capacity); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Full Charge Capacity: %d", status); @@ -171,6 +177,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Full Charge Capacity"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY, &full_charge_capacity); /* append this value to the battery state changed event */ bat_state_data.full_charge_capacity = full_charge_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Full Charge Capacity: %d.%dmAh", full_charge_capacity.val1, @@ -178,7 +185,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for Remaining Charge Capacity ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY, &remaining_charge_capacity); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Remaining Charge Capacity: %d", status); @@ -187,6 +194,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Remaining Charge Capacity"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY, &remaining_charge_capacity); /* append this value to the battery state changed event */ bat_state_data.remaining_charge_capacity = remaining_charge_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Remaining Charge Capacity: %d.%dmAh", remaining_charge_capacity.val1, @@ -194,7 +202,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for Nominal Available Capacity ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY, &nominal_available_capacity); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Nominal Available Capacity: %d", status); @@ -203,6 +211,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Nominal Available Capacity"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY, &nominal_available_capacity); /* append this value to the battery state changed event */ bat_state_data.nominal_available_capacity = nominal_available_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Nominal Available Capacity: %d.%dmAh", nominal_available_capacity.val1, @@ -210,7 +219,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for Full Available Capacity ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY, &full_available_capacity);; + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Full Available Capacity: %d", status); @@ -219,6 +228,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Full Available Capacity"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY, &full_available_capacity); /* append this value to the battery state changed event */ bat_state_data.full_available_capacity = full_available_capacity.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Full Available Capacity: %d.%dmAh", full_available_capacity.val1, @@ -226,7 +236,7 @@ static int zmk_battery_update(const struct device *battery) { } /* ---------- get channel data for Average Power Usage ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_AVG_POWER, &avg_power); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get battery Average Power Usage: %d", status); @@ -235,13 +245,14 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Average Power Usage"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_AVG_POWER, &avg_power); /* append this value to the battery state changed event */ bat_state_data.avg_power = avg_power.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Average Power Usage: %d.%dmW", avg_power.val1, avg_power.val2); } /* ---------- get channel data for Internal IC Temperature ---------- */ - status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_TEMP, &int_temp); + status = sensor_sample_fetch_chan(battery, SENSOR_CHAN_GAUGE_TEMP); if (status != 0 && status != -ENOTSUP) { LOG_DBG("Failed to get Internal IC Temperature: %d", status); @@ -250,6 +261,7 @@ static int zmk_battery_update(const struct device *battery) { if (status == -ENOTSUP) { LOG_DBG("The battery sensor does not support the channel: Internal IC Temperature"); } else { + status = sensor_channel_get(battery, SENSOR_CHAN_GAUGE_TEMP, &int_temp); /* append this value to the battery state changed event */ bat_state_data.int_temp = int_temp.val1; /*combine val1 and val2 here*/ LOG_DBG("-----> battery Internal IC Temperature: %d.%d C", int_temp.val1, int_temp.val2);