|
| 1 | +/* |
| 2 | + * Copyright 2023 Fabian Blatz <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT zephyr_lvgl_button_input |
| 8 | + |
| 9 | +#include "lvgl_common_input.h" |
| 10 | + |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | + |
| 13 | +LOG_MODULE_DECLARE(lvgl); |
| 14 | + |
| 15 | +struct lvgl_button_input_config { |
| 16 | + struct lvgl_common_input_config common_config; /* Needs to be first member */ |
| 17 | + const uint16_t *input_codes; |
| 18 | + uint8_t num_codes; |
| 19 | + const lv_coord_t *coordinates; |
| 20 | +}; |
| 21 | + |
| 22 | +static void lvgl_button_process_event(const struct device *dev, struct input_event *evt) |
| 23 | +{ |
| 24 | + struct lvgl_common_input_data *data = dev->data; |
| 25 | + const struct lvgl_button_input_config *cfg = dev->config; |
| 26 | + uint8_t i; |
| 27 | + |
| 28 | + for (i = 0; i < cfg->num_codes; i++) { |
| 29 | + if (evt->code == cfg->input_codes[i]) { |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if (i == cfg->num_codes) { |
| 35 | + LOG_DBG("Ignored input event: %u", evt->code); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + data->pending_event.btn_id = i; |
| 40 | + data->pending_event.state = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; |
| 41 | + |
| 42 | + if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) { |
| 43 | + LOG_WRN("Could not put input data into queue"); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +static int lvgl_button_input_init(const struct device *dev) |
| 48 | +{ |
| 49 | + struct lvgl_common_input_data *data = dev->data; |
| 50 | + const struct lvgl_button_input_config *cfg = dev->config; |
| 51 | + int ret; |
| 52 | + |
| 53 | + ret = lvgl_input_register_driver(LV_INDEV_TYPE_BUTTON, dev); |
| 54 | + if (ret < 0) { |
| 55 | + return ret; |
| 56 | + } |
| 57 | + |
| 58 | + lv_indev_set_button_points(data->indev, (const lv_point_t *)cfg->coordinates); |
| 59 | + |
| 60 | + return ret; |
| 61 | +} |
| 62 | + |
| 63 | +#define ASSERT_PROPERTIES(inst) \ |
| 64 | + BUILD_ASSERT(DT_INST_PROP_LEN(inst, input_codes) * 2 == \ |
| 65 | + DT_INST_PROP_LEN(inst, coordinates), \ |
| 66 | + "Property coordinates must contain one coordinate per input-code.") |
| 67 | + |
| 68 | +#define LVGL_BUTTON_INPUT_DEFINE(inst) \ |
| 69 | + ASSERT_PROPERTIES(inst); \ |
| 70 | + LVGL_INPUT_DEFINE(inst, button, CONFIG_LV_Z_BUTTON_INPUT_MSGQ_COUNT, \ |
| 71 | + lvgl_button_process_event); \ |
| 72 | + static const uint16_t lvgl_button_input_codes_##inst[] = DT_INST_PROP(inst, input_codes); \ |
| 73 | + static const lv_coord_t lvgl_button_coordinates_##inst[] = \ |
| 74 | + DT_INST_PROP(inst, coordinates); \ |
| 75 | + static const struct lvgl_button_input_config lvgl_button_input_config_##inst = { \ |
| 76 | + .common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, button), \ |
| 77 | + .input_codes = lvgl_button_input_codes_##inst, \ |
| 78 | + .num_codes = DT_INST_PROP_LEN(inst, input_codes), \ |
| 79 | + .coordinates = lvgl_button_coordinates_##inst, \ |
| 80 | + }; \ |
| 81 | + static struct lvgl_common_input_data lvgl_common_input_data_##inst; \ |
| 82 | + DEVICE_DT_INST_DEFINE(inst, lvgl_button_input_init, NULL, &lvgl_common_input_data_##inst, \ |
| 83 | + &lvgl_button_input_config_##inst, APPLICATION, \ |
| 84 | + CONFIG_LV_Z_INPUT_INIT_PRIORITY, NULL); |
| 85 | + |
| 86 | +DT_INST_FOREACH_STATUS_OKAY(LVGL_BUTTON_INPUT_DEFINE) |
0 commit comments