|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Basalte bv |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT zephyr_gpio_emul_sdl |
| 8 | + |
| 9 | +#include <zephyr/drivers/gpio/gpio_emul.h> |
| 10 | +#include <zephyr/logging/log.h> |
| 11 | + |
| 12 | +#include <SDL.h> |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(gpio_emul_sdl, CONFIG_GPIO_LOG_LEVEL); |
| 15 | + |
| 16 | +struct gpio_sdl_config { |
| 17 | + const struct device *emul; |
| 18 | + |
| 19 | + const SDL_Scancode *codes; |
| 20 | + uint8_t num_codes; |
| 21 | +}; |
| 22 | + |
| 23 | +static int sdl_filter(void *arg, SDL_Event *event) |
| 24 | +{ |
| 25 | + const struct device *port = arg; |
| 26 | + const struct gpio_sdl_config *config = port->config; |
| 27 | + int ret; |
| 28 | + |
| 29 | + gpio_pin_t pin = 0; |
| 30 | + |
| 31 | + /* Only handle keyboard events */ |
| 32 | + switch (event->type) { |
| 33 | + case SDL_KEYDOWN: |
| 34 | + case SDL_KEYUP: |
| 35 | + break; |
| 36 | + default: |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + /* Search for the corresponding scancode */ |
| 41 | + while (pin < config->num_codes) { |
| 42 | + if (config->codes[pin] == event->key.keysym.scancode) { |
| 43 | + break; |
| 44 | + } |
| 45 | + pin++; |
| 46 | + } |
| 47 | + |
| 48 | + if (pin == config->num_codes) { |
| 49 | + /* Not tracked */ |
| 50 | + return 1; |
| 51 | + } |
| 52 | + |
| 53 | + /* Lock the scheduler so we can't be preempted, |
| 54 | + * as the gpio_emul driver keeps a mutex locked |
| 55 | + * for as long as there are pending interrupts |
| 56 | + */ |
| 57 | + k_sched_lock(); |
| 58 | + |
| 59 | + /* Update the pin state */ |
| 60 | + ret = gpio_emul_input_set(config->emul, pin, event->type == SDL_KEYDOWN ? 1 : 0); |
| 61 | + |
| 62 | + k_sched_unlock(); |
| 63 | + if (ret < 0) { |
| 64 | + LOG_WRN("Failed to emulate input (%d)", ret); |
| 65 | + } |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +static int gpio_sdl_init(const struct device *dev) |
| 71 | +{ |
| 72 | + const struct gpio_sdl_config *config = dev->config; |
| 73 | + |
| 74 | + for (uint8_t pin = 0; pin < config->num_codes; ++pin) { |
| 75 | + if (config->codes[pin] != SDL_SCANCODE_UNKNOWN) { |
| 76 | + LOG_INF("GPIO %s:%u = %u", dev->name, pin, config->codes[pin]); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + SDL_AddEventWatch(sdl_filter, (void *)dev); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +#define GPIO_SDL_DEFINE(inst) \ |
| 86 | + BUILD_ASSERT(DT_NODE_HAS_COMPAT_STATUS(DT_INST_PARENT(inst), \ |
| 87 | + zephyr_gpio_emul, okay), \ |
| 88 | + "Enabled parent zephyr,gpio-emul node is required"); \ |
| 89 | + \ |
| 90 | + static const SDL_Scancode gpio_sdl_##inst##_codes[] \ |
| 91 | + = DT_INST_PROP(inst, scancodes); \ |
| 92 | + \ |
| 93 | + static const struct gpio_sdl_config gpio_sdl_##inst##_config = { \ |
| 94 | + .emul = DEVICE_DT_GET(DT_INST_PARENT(inst)), \ |
| 95 | + .codes = gpio_sdl_##inst##_codes, \ |
| 96 | + .num_codes = DT_INST_PROP_LEN(inst, scancodes), \ |
| 97 | + }; \ |
| 98 | + \ |
| 99 | + DEVICE_DT_INST_DEFINE(inst, gpio_sdl_init, NULL, NULL, \ |
| 100 | + &gpio_sdl_##inst##_config, POST_KERNEL, \ |
| 101 | + CONFIG_GPIO_INIT_PRIORITY, NULL); |
| 102 | + |
| 103 | +DT_INST_FOREACH_STATUS_OKAY(GPIO_SDL_DEFINE) |
0 commit comments