Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions ports/zephyr-cp/bindings/zephyr_input/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT

#include <stdbool.h>
#include <stdint.h>

#include "py/obj.h"
#include "py/runtime.h"
#include "zephyr/input/input.h"

typedef struct {
int32_t x;
int32_t y;
bool pressed;
} zephyr_input_touch_state_t;
static zephyr_input_touch_state_t touch_state;
static zephyr_input_touch_state_t buffered_touch_state;

static void zephyr_input_callback(struct input_event *event, void *user_data) {
(void)user_data;
if (event->type == INPUT_EV_ABS && event->code == INPUT_ABS_X) {
touch_state.x = event->value;
} else if (event->type == INPUT_EV_ABS && event->code == INPUT_ABS_Y) {
touch_state.y = event->value;
} else if (event->type == INPUT_EV_KEY && event->code == INPUT_BTN_TOUCH) {
touch_state.pressed = event->value != 0;
}
// Like the GT911 driver, retain the latest complete touch sample.
if (event->sync) {
buffered_touch_state = touch_state;
}
}

INPUT_CALLBACK_DEFINE(NULL, zephyr_input_callback, NULL);

//| """Access input devices provided by Zephyr."""
//|
//| def touch() -> tuple[int, int] | None:
//| """Return the latest buffered touch coordinates, or ``None`` when released.
//|
//| On native_sim, SDL mouse button and motion events are reported as touch.
//| """
//| ...
static mp_obj_t zephyr_input_touch(void) {
if (!buffered_touch_state.pressed) {
return mp_const_none;
}
mp_obj_t coordinates[] = {
mp_obj_new_int(buffered_touch_state.x),
mp_obj_new_int(buffered_touch_state.y),
};
return mp_obj_new_tuple(2, coordinates);
}
static MP_DEFINE_CONST_FUN_OBJ_0(zephyr_input_touch_obj, zephyr_input_touch);

static const mp_rom_map_elem_t zephyr_input_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephyr_input) },
{ MP_ROM_QSTR(MP_QSTR_touch), MP_ROM_PTR(&zephyr_input_touch_obj) },
};
static MP_DEFINE_CONST_DICT(zephyr_input_module_globals, zephyr_input_module_globals_table);

const mp_obj_module_t zephyr_input_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&zephyr_input_module_globals,
};

MP_REGISTER_MODULE(MP_QSTR_zephyr_input, zephyr_input_module);
9 changes: 9 additions & 0 deletions ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CONFIG_IOBROKER_PACKAGE_ONE_TO_ONE=y
CONFIG_EMUL=y
CONFIG_GPIO=y
CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=n
CONFIG_SPEED_OPTIMIZATIONS=y

# So we can test safe mode
CONFIG_NATIVE_SIM_REBOOT=y
Expand All @@ -26,12 +27,20 @@ CONFIG_DISPLAY=y
CONFIG_SDL_DISPLAY=y
# Don't require hardware acceleration so the software renderer can be used.
CONFIG_SDL_DISPLAY_USE_HARDWARE_ACCELERATOR=n
# Keep the priority-5 CircuitPython audio thread above display/input work.
CONFIG_SDL_THREAD_PRIORITY=6
CONFIG_SDL_DISPLAY_THREAD_PRIORITY=6

# Route SDL mouse button/motion events through Zephyr's touch input driver.
CONFIG_INPUT_SDL_TOUCH=y
CONFIG_INPUT_MODE_SYNCHRONOUS=y

# LVGL-based displayio compositing core. LVGL renders on its own RTOS thread (this
# workqueue, also matching the RTOS deployment model); CircuitPython's compositor mirror
# mutations take the glue's LVGL mutex so they don't interleave with lv_timer_handler.
CONFIG_LVGL=y
CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE=y
CONFIG_LV_Z_LVGL_WORKQUEUE_PRIORITY=6
# LVGL allocates objects and draw buffers from its own heap sizing.
CONFIG_LV_Z_MEM_POOL_SIZE=16777216
# Cache decoded images (OnDiskBitmaps and other LVGL images) so they're only decoded
Expand Down
8 changes: 8 additions & 0 deletions ports/zephyr-cp/boards/nordic/nrf54l15tag/board.overlay
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// No app.overlay because it doesn't have USB.

// Keep the button nodes and their GPIOs available for board pin-name
// generation, but don't instantiate the GPIO-keys input driver. The
// zephyr_input module currently exposes touch input only, and the input
// subsystem otherwise consumes most of this board's remaining flash.
&{/buttons} {
status = "disabled";
};

// The nrf54l15tag Zephyr board doesn't enable a UART or set zephyr,console,
// unlike the nrf54l15dk. Set up UART30 as the console using available pins.
&uart30 {
Expand Down
4 changes: 4 additions & 0 deletions ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# USB OTG on STM32WBA requires VOS Range 1. Keep HCLK > 16 MHz.
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC=32000000

# GPIO was previously selected indirectly by the GPIO-keys input driver.
# CircuitPython's digitalio support needs it even with input nodes disabled.
CONFIG_GPIO=y

# Bluetooth doesn't start up for some reason.
# CONFIG_BT=y
# CONFIG_BT_PERIPHERAL=y
Expand Down
11 changes: 11 additions & 0 deletions ports/zephyr-cp/boards/st/stm32wba65i_dk1/board.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
status = "okay";
};

/* Keep the button/key nodes available for board pin-name generation, but
* don't instantiate the input drivers. zephyr_input currently exposes touch
* input only, so these key devices don't provide useful input through it. */
&{/gpio_keys} {
status = "disabled";
};

&{/adc-keys} {
status = "disabled";
};

/* The board's &rtc node is already status-okay in the upstream devicetree, so
* alias it so the rtc module picks it up like other RTC boards do. */
/ {
Expand Down
1 change: 1 addition & 0 deletions ports/zephyr-cp/cptools/build_circuitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"audiofreeverb",
"audiomixer",
"audiomp3",
"audiotap",
"synthio",
],
"busio": ["fourwire", "i2cdisplaybus", "sdcardio", "sharpdisplay"],
Expand Down
3 changes: 3 additions & 0 deletions ports/zephyr-cp/cptools/zephyr2cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,9 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig
if driver not in active_zephyr_devices:
active_zephyr_devices[driver] = []
active_zephyr_devices[driver].append(node.labels)
elif driver == "input":
board_info["zephyr_input"] = True
logger.info("Supported Zephyr input device: %s", underscored)
elif driver in EXCEPTIONAL_DRIVERS:
pass
elif driver in BUSIO_CLASSES:
Expand Down
4 changes: 4 additions & 0 deletions ports/zephyr-cp/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ CONFIG_ASSERT=n

CONFIG_EVENTS=y

# Enable the input subsystem so boards with status-okay input devices in their
# devicetree get a working zephyr_input module via zephyr2cp.
CONFIG_INPUT=y

# Enable the RTC driver layer so boards with a status-okay `rtc` devicetree
# alias (e.g. native_sim's rtc-emul, STM32, RP2040, SmartBond) get a working
# rtc module via common-hal/rtc. Boards without the alias only pay for the
Expand Down
Loading