-
Notifications
You must be signed in to change notification settings - Fork 8.2k
drivers: gpio: Add SDL simulated GPIO support #50080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d67130
drivers: gpio: pca953x: select I2C
pdgendt 57d2614
drivers: gpio: Add SDL emulated GPIO support
pdgendt 14d1753
samples: subsys: display: lvgl: Add button to reset counter
pdgendt 380bfc0
samples: subsys: display: lvgl: Add native_posix overlay for SDL input
pdgendt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # GPIO emulation using SDL keyboard events | ||
| # | ||
| # Copyright (c) 2022, Basalte bv | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| config GPIO_EMUL_SDL | ||
| bool "SDL GPIO emulation" | ||
| default y | ||
| depends on DT_HAS_ZEPHYR_GPIO_EMUL_SDL_ENABLED | ||
| depends on GPIO_EMUL | ||
| depends on HAS_SDL | ||
| help | ||
| Enable GPIO emulation using SDL keyboard events. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright (c) 2022, Basalte bv | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #define DT_DRV_COMPAT zephyr_gpio_emul_sdl | ||
|
|
||
| #include <zephyr/drivers/gpio/gpio_emul.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| #include <SDL.h> | ||
|
|
||
| LOG_MODULE_REGISTER(gpio_emul_sdl, CONFIG_GPIO_LOG_LEVEL); | ||
|
|
||
| struct gpio_sdl_config { | ||
| const struct device *emul; | ||
|
|
||
| const SDL_Scancode *codes; | ||
| uint8_t num_codes; | ||
| }; | ||
|
|
||
| static int sdl_filter(void *arg, SDL_Event *event) | ||
| { | ||
| const struct device *port = arg; | ||
| const struct gpio_sdl_config *config = port->config; | ||
| int ret; | ||
|
|
||
| gpio_pin_t pin = 0; | ||
|
|
||
| /* Only handle keyboard events */ | ||
| switch (event->type) { | ||
| case SDL_KEYDOWN: | ||
| case SDL_KEYUP: | ||
| break; | ||
| default: | ||
| return 1; | ||
| } | ||
|
|
||
| /* Search for the corresponding scancode */ | ||
| while (pin < config->num_codes) { | ||
| if (config->codes[pin] == event->key.keysym.scancode) { | ||
| break; | ||
| } | ||
| pin++; | ||
| } | ||
|
|
||
| if (pin == config->num_codes) { | ||
| /* Not tracked */ | ||
| return 1; | ||
| } | ||
|
|
||
| /* Lock the scheduler so we can't be preempted, | ||
| * as the gpio_emul driver keeps a mutex locked | ||
| * for as long as there are pending interrupts | ||
| */ | ||
| k_sched_lock(); | ||
|
|
||
| /* Update the pin state */ | ||
| ret = gpio_emul_input_set(config->emul, pin, event->type == SDL_KEYDOWN ? 1 : 0); | ||
|
|
||
| k_sched_unlock(); | ||
| if (ret < 0) { | ||
| LOG_WRN("Failed to emulate input (%d)", ret); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int gpio_sdl_init(const struct device *dev) | ||
| { | ||
| const struct gpio_sdl_config *config = dev->config; | ||
|
|
||
| for (uint8_t pin = 0; pin < config->num_codes; ++pin) { | ||
| if (config->codes[pin] != SDL_SCANCODE_UNKNOWN) { | ||
| LOG_INF("GPIO %s:%u = %u", dev->name, pin, config->codes[pin]); | ||
| } | ||
| } | ||
|
|
||
| SDL_AddEventWatch(sdl_filter, (void *)dev); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| #define GPIO_SDL_DEFINE(inst) \ | ||
| BUILD_ASSERT(DT_NODE_HAS_COMPAT_STATUS(DT_INST_PARENT(inst), \ | ||
| zephyr_gpio_emul, okay), \ | ||
| "Enabled parent zephyr,gpio-emul node is required"); \ | ||
| \ | ||
| static const SDL_Scancode gpio_sdl_##inst##_codes[] \ | ||
| = DT_INST_PROP(inst, scancodes); \ | ||
| \ | ||
| static const struct gpio_sdl_config gpio_sdl_##inst##_config = { \ | ||
| .emul = DEVICE_DT_GET(DT_INST_PARENT(inst)), \ | ||
| .codes = gpio_sdl_##inst##_codes, \ | ||
| .num_codes = DT_INST_PROP_LEN(inst, scancodes), \ | ||
| }; \ | ||
| \ | ||
| DEVICE_DT_INST_DEFINE(inst, gpio_sdl_init, NULL, NULL, \ | ||
| &gpio_sdl_##inst##_config, POST_KERNEL, \ | ||
| CONFIG_GPIO_INIT_PRIORITY, NULL); | ||
|
|
||
| DT_INST_FOREACH_STATUS_OKAY(GPIO_SDL_DEFINE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright 2022, Basalte bv | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: | | ||
| SDL keyboard GPIO input Emulator | ||
|
|
||
| Simulate GPIO state/interrupts using SDL keyboard events. This node has | ||
| to be a child of a `zephyr,gpio-emul` compatible. | ||
| Add a list of scancodes for the desired keys to be mapped. | ||
|
|
||
| Refer to https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf | ||
| section Keyboard/Keypad (p53) for a list of scancode values. | ||
|
|
||
| The following example maps the first 3 numeric keys to GPIO pins: | ||
|
|
||
| /* gpio0 has to be a zephyr,gpio-emul device */ | ||
| &gpio0 { | ||
| ngpios = <3>; | ||
|
|
||
| sdl_gpio { | ||
| compatible = "zephyr,gpio-emul-sdl"; | ||
| scancodes = <30 31 32>; | ||
| }; | ||
| }; | ||
|
|
||
| keypad: keypad { | ||
| compatible = "gpio-keys"; | ||
| key1: key1 { | ||
| gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>; | ||
| }; | ||
| key1: key2 { | ||
| gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>; | ||
| }; | ||
| key3: key3 { | ||
| gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>; | ||
| }; | ||
| }; | ||
|
|
||
| The limitations of usage are: | ||
| - Only active high as we don't get events for keys that aren't pressed | ||
| - Pressing multiple keys is best effort, state will be kept but no events | ||
| are generated once the last key is released | ||
|
|
||
| compatible: "zephyr,gpio-emul-sdl" | ||
|
|
||
| include: base.yaml | ||
|
|
||
| properties: | ||
| scancodes: | ||
| type: array | ||
| required: true | ||
| description: | | ||
| An array of SDL scancodes mapped to its GPIO index |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| CONFIG_LV_COLOR_DEPTH_32=y | ||
| CONFIG_GPIO=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright (c) 2022, Basalte bv | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
|
||
| / { | ||
| aliases { | ||
| sw0 = &button0; | ||
| }; | ||
|
|
||
| keys { | ||
| compatible = "gpio-keys"; | ||
| button0: button0 { | ||
| /* gpio0 pin 0 is already aliased to led0 */ | ||
| gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| &gpio0 { | ||
| ngpios = <2>; | ||
|
|
||
| sdl_gpio { | ||
| status = "okay"; | ||
| compatible = "zephyr,gpio-emul-sdl"; | ||
| /* Skip pin 0 with the unknown code 0 */ | ||
| scancodes = <0 21>; | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| CONFIG_LV_COLOR_DEPTH_32=y | ||
| CONFIG_GPIO=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /* | ||
| * Copyright (c) 2022, Basalte bv | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #include "native_posix.overlay" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.