From 36c8443b40f23c2e6122fdaaa3e16e9f6b66c380 Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Sun, 12 Nov 2023 20:30:19 +0900 Subject: [PATCH] refactor: rework polling system and expose warm_up time tunable --- .../bindings/kscan/zmk,kscan-hhkb-pro2.yaml | 7 +++- config/zephyr/kscan/kscan_hhkb_pro2.c | 40 ++++++++----------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/config/dts/bindings/kscan/zmk,kscan-hhkb-pro2.yaml b/config/dts/bindings/kscan/zmk,kscan-hhkb-pro2.yaml index 6d430e7..9572d24 100644 --- a/config/dts/bindings/kscan/zmk,kscan-hhkb-pro2.yaml +++ b/config/dts/bindings/kscan/zmk,kscan-hhkb-pro2.yaml @@ -11,6 +11,9 @@ properties: gpios: type: phandle-array required: true + matrix-warm-up-ms: + type: int + default: 5 matrix-relax-us: type: int default: 6 @@ -19,10 +22,10 @@ properties: default: 5 active-polling-interval-ms: type: int - default: 8 + default: 3 idle-polling-interval-ms: type: int - default: 30 + default: 35 sleep-polling-interval-ms: type: int default: 3000 diff --git a/config/zephyr/kscan/kscan_hhkb_pro2.c b/config/zephyr/kscan/kscan_hhkb_pro2.c index 4b7943b..9cbe527 100644 --- a/config/zephyr/kscan/kscan_hhkb_pro2.c +++ b/config/zephyr/kscan/kscan_hhkb_pro2.c @@ -27,6 +27,7 @@ struct kscan_hhkb_pro2_config struct gpio_dt_spec key; struct gpio_dt_spec hys; struct gpio_dt_spec strobe; + const uint16_t matrix_warm_up_ms; const uint16_t matrix_relax_us; const uint16_t adc_read_settle_us; const uint16_t active_polling_interval_ms; @@ -37,8 +38,8 @@ struct kscan_hhkb_pro2_config struct kscan_hhkb_pro2_data { kscan_callback_t callback; - struct k_timer poll_timer; - struct k_work poll; + uint16_t poll_interval; + struct k_work_delayable poll; bool matrix_state[MATRIX_CELLS]; const struct device *dev; }; @@ -60,10 +61,7 @@ static int kscan_hhkb_pro2_enable(const struct device *dev) { LOG_DBG("KSCAN API enable"); struct kscan_hhkb_pro2_data *data = dev->data; - const struct kscan_hhkb_pro2_config *cfg = dev->config; - k_timer_start(&data->poll_timer, - K_MSEC(cfg->active_polling_interval_ms), - K_MSEC(cfg->active_polling_interval_ms)); + k_work_schedule(&data->poll, K_MSEC(data->poll_interval)); return 0; } @@ -71,17 +69,10 @@ static int kscan_hhkb_pro2_disable(const struct device *dev) { LOG_DBG("KSCAN API disable"); struct kscan_hhkb_pro2_data *data = dev->data; - k_timer_stop(&data->poll_timer); + k_work_cancel_delayable(&data->poll); return 0; } -static void kscan_hhkb_pro2_timer_handler(struct k_timer *timer) -{ - struct kscan_hhkb_pro2_data *data = - CONTAINER_OF(timer, struct kscan_hhkb_pro2_data, poll_timer); - k_work_submit(&data->poll); -} - static void kscan_hhkb_pro2_work_handler(struct k_work *work) { struct kscan_hhkb_pro2_data *data = CONTAINER_OF(work, struct kscan_hhkb_pro2_data, poll); @@ -93,8 +84,8 @@ static void kscan_hhkb_pro2_work_handler(struct k_work *work) gpio_pin_configure(cfg->key.port, cfg->key.pin, GPIO_INPUT | cfg->key.dt_flags); gpio_pin_set(cfg->strobe.port, cfg->strobe.pin, 1); gpio_pin_set(cfg->power.port, cfg->power.pin, 1); - // Topre controller board needs 5 ms to be operational - k_sleep(K_MSEC(5)); + // The board needs some time to be operational after powering up + k_sleep(K_MSEC(cfg->matrix_warm_up_ms)); for (int r = 0; r < MATRIX_ROWS; ++r) { for (int c = 0; c < MATRIX_COLS; ++c) @@ -146,6 +137,7 @@ static void kscan_hhkb_pro2_work_handler(struct k_work *work) } } } + k_work_schedule(&data->poll, K_MSEC(data->poll_interval)); } static int kscan_hhkb_pro2_activity_event_handler(const struct device *dev, const zmk_event_t *eh) @@ -157,24 +149,22 @@ static int kscan_hhkb_pro2_activity_event_handler(const struct device *dev, cons } struct kscan_hhkb_pro2_data *data = dev->data; const struct kscan_hhkb_pro2_config *cfg = dev->config; - uint16_t poll_interval; switch (ev->state) { case ZMK_ACTIVITY_ACTIVE: - poll_interval = cfg->active_polling_interval_ms; + data->poll_interval = cfg->active_polling_interval_ms; break; case ZMK_ACTIVITY_IDLE: - poll_interval = cfg->idle_polling_interval_ms; + data->poll_interval = cfg->idle_polling_interval_ms; break; case ZMK_ACTIVITY_SLEEP: - poll_interval = cfg->sleep_polling_interval_ms; + data->poll_interval = cfg->sleep_polling_interval_ms; break; default: LOG_WRN("Unhandled activity state: %d", ev->state); return -EINVAL; } - LOG_DBG("Setting poll interval to %d", poll_interval); - k_timer_start(&data->poll_timer, K_MSEC(poll_interval), K_MSEC(poll_interval)); + LOG_DBG("Setting poll interval to %d", data->poll_interval); return 0; } @@ -196,8 +186,9 @@ static int kscan_hhkb_pro2_init(const struct device *dev) gpio_pin_configure(cfg->hys.port, cfg->hys.pin, GPIO_OUTPUT_INACTIVE | cfg->hys.dt_flags); gpio_pin_configure(cfg->strobe.port, cfg->strobe.pin, GPIO_OUTPUT_INACTIVE | cfg->strobe.dt_flags); - k_timer_init(&data->poll_timer, kscan_hhkb_pro2_timer_handler, NULL); - k_work_init(&data->poll, kscan_hhkb_pro2_work_handler); + data->poll_interval = cfg->active_polling_interval_ms; + k_work_init_delayable(&data->poll, kscan_hhkb_pro2_work_handler); + k_work_schedule(&data->poll, K_MSEC(data->poll_interval)); return 0; } @@ -223,6 +214,7 @@ static const struct kscan_driver_api kscan_hhkb_pro2_api = { .key = GPIO_DT_SPEC_INST_GET_BY_IDX(inst, gpios, 1), \ .hys = GPIO_DT_SPEC_INST_GET_BY_IDX(inst, gpios, 2), \ .strobe = GPIO_DT_SPEC_INST_GET_BY_IDX(inst, gpios, 9), \ + .matrix_warm_up_ms = DT_INST_PROP(inst, matrix_warm_up_ms), \ .matrix_relax_us = DT_INST_PROP(inst, matrix_relax_us), \ .adc_read_settle_us = DT_INST_PROP(inst, adc_read_settle_us), \ .active_polling_interval_ms = DT_INST_PROP(inst, active_polling_interval_ms), \