refactor(settings): Debounce BLE/endpoint settings saves

This commit is contained in:
Nick 2020-12-02 17:00:57 -06:00 committed by Pete Johanson
parent 69d48c5715
commit 2204a5dce4
2 changed files with 40 additions and 4 deletions

View file

@ -228,6 +228,23 @@ int zmk_ble_clear_bonds() {
int zmk_ble_active_profile_index() { return active_profile; } int zmk_ble_active_profile_index() { return active_profile; }
#if IS_ENABLED(CONFIG_SETTINGS)
static void ble_save_profile_work(struct k_work *work) {
settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile));
}
static struct k_delayed_work ble_save_work;
#endif
static int ble_save_profile() {
#if IS_ENABLED(CONFIG_SETTINGS)
k_delayed_work_cancel(&ble_save_work);
return k_delayed_work_submit(&ble_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
#else
return 0;
#endif
}
int zmk_ble_prof_select(uint8_t index) { int zmk_ble_prof_select(uint8_t index) {
LOG_DBG("profile %d", index); LOG_DBG("profile %d", index);
if (active_profile == index) { if (active_profile == index) {
@ -235,7 +252,7 @@ int zmk_ble_prof_select(uint8_t index) {
} }
active_profile = index; active_profile = index;
settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile)); ble_save_profile();
update_advertising(); update_advertising();
@ -526,6 +543,8 @@ static int zmk_ble_init(const struct device *_arg) {
return err; return err;
} }
k_delayed_work_init(&ble_save_work, ble_save_profile_work);
settings_load_subtree("ble"); settings_load_subtree("ble");
settings_load_subtree("bt"); settings_load_subtree("bt");

View file

@ -29,6 +29,23 @@ static enum zmk_endpoint preferred_endpoint =
static void update_current_endpoint(); static void update_current_endpoint();
#if IS_ENABLED(CONFIG_SETTINGS)
static void endpoints_save_preferred_work(struct k_work *work) {
settings_save_one("endpoints/preferred", &preferred_endpoint, sizeof(preferred_endpoint));
}
static struct k_delayed_work endpoints_save_work;
#endif
static int endpoints_save_preferred() {
#if IS_ENABLED(CONFIG_SETTINGS)
k_delayed_work_cancel(&endpoints_save_work);
return k_delayed_work_submit(&endpoints_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
#else
return 0;
#endif
}
int zmk_endpoints_select(enum zmk_endpoint endpoint) { int zmk_endpoints_select(enum zmk_endpoint endpoint) {
LOG_DBG("Selected endpoint %d", endpoint); LOG_DBG("Selected endpoint %d", endpoint);
@ -38,9 +55,7 @@ int zmk_endpoints_select(enum zmk_endpoint endpoint) {
preferred_endpoint = endpoint; preferred_endpoint = endpoint;
#if IS_ENABLED(CONFIG_SETTINGS) endpoints_save_preferred();
settings_save_one("endpoints/preferred", &preferred_endpoint, sizeof(preferred_endpoint));
#endif
update_current_endpoint(); update_current_endpoint();
@ -166,6 +181,8 @@ static int zmk_endpoints_init(const struct device *_arg) {
return err; return err;
} }
k_delayed_work_init(&endpoints_save_work, endpoints_save_preferred_work);
settings_load_subtree("endpoints"); settings_load_subtree("endpoints");
#endif #endif