Add interrupt trigger and charging event
This commit is contained in:
parent
ba810177d5
commit
4990e079da
1 changed files with 41 additions and 21 deletions
|
@ -55,8 +55,31 @@ static int zmk_battery_update(const struct device *battery) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (last_state_of_charge != state_of_charge.val1) {
|
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
||||||
|
|
||||||
|
rc = sensor_sample_fetch_chan(battery, SENSOR_CHAN_CHARGING);
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_DBG("Failed to fetch charging value: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
struct sensor_value charging_state;
|
||||||
|
rc = sensor_channel_get(battery, SENSOR_CHAN_CHARGING, &charging_state);
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_DBG("Failed to get battery charging status: %d", rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (last_state_of_charge != state_of_charge.val1
|
||||||
|
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
||||||
|
|| charging != charging_state.val1
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
last_state_of_charge = state_of_charge.val1;
|
last_state_of_charge = state_of_charge.val1;
|
||||||
|
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
||||||
|
charging = charging_state.val1;
|
||||||
|
#endif
|
||||||
#if IS_ENABLED(CONFIG_BT_BAS)
|
#if IS_ENABLED(CONFIG_BT_BAS)
|
||||||
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);
|
||||||
|
|
||||||
|
@ -67,27 +90,10 @@ static int zmk_battery_update(const struct device *battery) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
rc = raise_zmk_battery_state_changed(
|
rc = raise_zmk_battery_state_changed((struct zmk_battery_state_changed){
|
||||||
(struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge});
|
.state_of_charge = last_state_of_charge, .charging = charging});
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
|
||||||
|
|
||||||
rc = sensor_sample_fetch_chan(battery, SENSOR_CHAN_CHARGING);
|
|
||||||
|
|
||||||
if (rc != 0) {
|
|
||||||
LOG_DBG("Failed to fetch battery values: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
struct sensor_value charging_state;
|
|
||||||
rc = sensor_channel_get(battery, SENSOR_CHAN_CHARGING, &charging_state);
|
|
||||||
if (rc != 0) {
|
|
||||||
LOG_DBG("Failed to get battery charging status: %d", rc);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
charging = charging_state.val1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +118,11 @@ static void zmk_battery_start_reporting() {
|
||||||
k_timer_start(&battery_timer, K_NO_WAIT, K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));
|
k_timer_start(&battery_timer, K_NO_WAIT, K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
||||||
|
static void handle_chg_trig(const struct device *dev, const struct sensor_trigger *trig) {
|
||||||
|
zmk_battery_update(dev);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
static int zmk_battery_init(void) {
|
static int zmk_battery_init(void) {
|
||||||
#if !DT_HAS_CHOSEN(zmk_battery)
|
#if !DT_HAS_CHOSEN(zmk_battery)
|
||||||
battery = device_get_binding("BATTERY");
|
battery = device_get_binding("BATTERY");
|
||||||
|
@ -129,6 +139,16 @@ static int zmk_battery_init(void) {
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
|
||||||
|
struct sensor_trigger trigger = {
|
||||||
|
.type = SENSOR_TRIG_DATA_READY,
|
||||||
|
.chan = SENSOR_CHAN_ALL,
|
||||||
|
};
|
||||||
|
if (sensor_trigger_set(battery, &trigger, handle_chg_trig) < 0) {
|
||||||
|
LOG_ERR("can't set batt chg trigger");
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
zmk_battery_start_reporting();
|
zmk_battery_start_reporting();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue