Skip to content

Commit

Permalink
Add interrupt trigger and charging event
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Mar 21, 2024
1 parent 8c27d6a commit ac51663
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions app/src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,12 @@ static int zmk_battery_update(const struct device *battery) {
return rc;
}

if (last_state_of_charge != state_of_charge.val1) {
last_state_of_charge = state_of_charge.val1;
#if IS_ENABLED(CONFIG_BT_BAS)
LOG_DBG("Setting BAS GATT battery level to %d.", last_state_of_charge);

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);
return rc;
}
#endif
rc = raise_zmk_battery_state_changed(
(struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge});
}

#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);
LOG_DBG("Failed to fetch charging value: %d", rc);
return rc;
}
struct sensor_value charging_state;
Expand All @@ -85,9 +69,31 @@ static int zmk_battery_update(const struct device *battery) {
LOG_DBG("Failed to get battery charging status: %d", rc);
return rc;
}
charging = charging_state.val1;
#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;
#if DT_NODE_HAS_PROP(DT_CHOSEN(zmk_battery), chg_gpios)
charging = charging_state.val1;
#endif
#if IS_ENABLED(CONFIG_BT_BAS)
LOG_DBG("Setting BAS GATT battery level to %d.", last_state_of_charge);

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);
return rc;
}
#endif
rc = raise_zmk_battery_state_changed((struct zmk_battery_state_changed){
.state_of_charge = last_state_of_charge, .charging = charging});
}

return rc;
}

Expand All @@ -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));
}
}

#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) {
#if !DT_HAS_CHOSEN(zmk_battery)
battery = device_get_binding("BATTERY");
Expand All @@ -129,6 +139,16 @@ static int zmk_battery_init(void) {
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();
return 0;
}
Expand Down

0 comments on commit ac51663

Please sign in to comment.