Skip to content

Commit

Permalink
driver: SDL based display emulation driver
Browse files Browse the repository at this point in the history
This driver introduces an emulated LCD display for the native POSIX
board. The emulated display driver makes use of SDL2 to render the
displays frame buffer into a dedicated desktop window.

Signed-off-by: Jan Van Winkel <[email protected]>
  • Loading branch information
vanwinkeljan committed Dec 1, 2018
1 parent 1d3e0ea commit 384c4c7
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 7 deletions.
9 changes: 9 additions & 0 deletions boards/posix/native_posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ zephyr_library_sources(
cmdline.c
)

if(CONFIG_HAS_SDL)
find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)
zephyr_include_directories(${SDL2_INCLUDE_DIRS})
zephyr_link_libraries(${SDL2_LIBRARIES})
zephyr_compile_options(${SDL2_CFLAGS_OTHER})
zephyr_library_sources(sdl_events.c)
endif()

zephyr_ld_options(
-lm
)
Expand Down
5 changes: 5 additions & 0 deletions boards/posix/native_posix/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ config NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME
case the zephyr kernel and application cannot tell the difference unless they
interact with some other driver/device which runs at real time.

config HAS_SDL
bool
help
This option specifies that the target board has SDL support

endif
7 changes: 7 additions & 0 deletions boards/posix/native_posix/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ config UART_CONSOLE

endif #CONSOLE

if DISPLAY

config SDL_DISPLAY
default y

endif # DISPLAY

endif # BOARD_NATIVE_POSIX
17 changes: 17 additions & 0 deletions boards/posix/native_posix/doc/board.rst
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,23 @@ The following peripherals are currently provided with this board:
must be powered down and support Bluetooth Low Energy (i.e. support the
Bluetooth specification version 4.0 or greater).

**Display driver**:
A display driver is provided that creates a window on the host machine to
render display content.

This driver requires a 32-bit version of the `SDL2`_ library on the host
machine and ``pkg-config`` settings to correctly pickup the SDL2 install path
and compiler flags.

On a Ubuntu 18.04 host system, for example, install the ``pkg-config`` and
``libsdl2-dev:i386`` packages, and configure the pkg-config search path with
these commands::

$ sudo apt-get install pkg-config libsdl2-dev:i386
$ export PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig

.. _SDL2:
https://www.libsdl.org/download-2.0.php

UART
*****
Expand Down
32 changes: 26 additions & 6 deletions boards/posix/native_posix/hw_models_top.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
#include <stdint.h>
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>
#include <pthread.h>
#include "hw_models_top.h"
#include "timer_model.h"
#include "irq_ctrl.h"
#include "posix_board_if.h"
#include "posix_soc_if.h"
#include "posix_arch_internal.h"
#include "sdl_events.h"
#include <misc/util.h>


static u64_t simu_time; /* The actual time as known by the HW models */
Expand All @@ -26,21 +30,33 @@ static u64_t end_of_time = NEVER; /* When will this device stop */
/* List of HW model timers: */
extern u64_t hw_timer_timer; /* When should this timer_model be called */
extern u64_t irq_ctrl_timer;

static enum { HWTIMER = 0, IRQCNT, NUMBER_OF_TIMERS, NONE }
next_timer_index = NONE;
#ifdef CONFIG_HAS_SDL
extern u64_t sdl_event_timer;
#endif

static enum {
HWTIMER = 0,
IRQCNT,
#ifdef CONFIG_HAS_SDL
SDLEVENTTIMER,
#endif
NUMBER_OF_TIMERS,
NONE
} next_timer_index = NONE;

static u64_t *Timer_list[NUMBER_OF_TIMERS] = {
&hw_timer_timer,
&irq_ctrl_timer
&irq_ctrl_timer,
#ifdef CONFIG_HAS_SDL
&sdl_event_timer,
#endif
};

static u64_t next_timer_time;

/* Have we received a SIGTERM or SIGINT */
static volatile sig_atomic_t signaled_end;


/**
* Handler for SIGTERM and SIGINT
*/
Expand Down Expand Up @@ -95,7 +111,6 @@ static void hwm_sleep_until_next_timer(void)
if (signaled_end || (simu_time > end_of_time)) {
posix_print_trace("\nStopped at %.3Lfs\n",
((long double)simu_time)/1.0e6);

posix_exit(0);
}
}
Expand Down Expand Up @@ -134,6 +149,11 @@ void hwm_main_loop(void)
case IRQCNT:
hw_irq_ctrl_timer_triggered();
break;
#ifdef CONFIG_HAS_SDL
case SDLEVENTTIMER:
sdl_handle_events();
break;
#endif
default:
/* LCOV_EXCL_START */
posix_print_error_and_exit(
Expand Down
75 changes: 75 additions & 0 deletions boards/posix/native_posix/sdl_events.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2018 Jan Van Winkel <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <SDL.h>
#include "posix_board_if.h"
#include "posix_trace.h"
#include "posix_arch_internal.h"
#include "soc.h"
#include "hw_models_top.h"

u64_t sdl_event_timer;

static void sdl_handle_window_event(const SDL_Event *event)
{
SDL_Window *window;
SDL_Renderer *renderer;

switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:

window = SDL_GetWindowFromID(event->window.windowID);
if (window == NULL) {
return;
}

renderer = SDL_GetRenderer(window);
if (renderer == NULL) {
return;
}
SDL_RenderPresent(renderer);
break;
default:
break;
}
}

void sdl_handle_events(void)
{
SDL_Event event;

sdl_event_timer = hwm_get_time() + 10000;

while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
sdl_handle_window_event(&event);
break;
case SDL_QUIT:
posix_exit(0);
break;
default:
break;
}
}

}

static void sdl_init(void)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
posix_print_error_and_exit("Error on SDL_Init (%s)\n",
SDL_GetError());
}
}

static void sdl_cleanup(void)
{
SDL_Quit();
}

NATIVE_TASK(sdl_init, PRE_BOOT_2, 1);
NATIVE_TASK(sdl_cleanup, ON_EXIT, 2);
14 changes: 14 additions & 0 deletions boards/posix/native_posix/sdl_events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2018 Jan Van Winkel <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_BOARD_POSIX_NATIVE_POSIX_SDL_EVENTS_H
#define ZEPHYR_BOARD_POSIX_NATIVE_POSIX_SDL_EVENTS_H

#include <zephyr/types.h>

void sdl_handle_events(void);

#endif /* ZEPHYR_BOARD_POSIX_NATIVE_POSIX_SDL_EVENTS_H */

3 changes: 3 additions & 0 deletions drivers/display/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ zephyr_sources_ifdef(CONFIG_ILI9340_LCD_ADAFRUIT_1480
)
zephyr_sources_ifdef(CONFIG_SSD1306 ssd1306.c)
zephyr_sources_ifdef(CONFIG_SSD1673 ssd1673.c)

zephyr_sources_ifdef(CONFIG_SDL_DISPLAY display_sdl.c)

2 changes: 1 addition & 1 deletion drivers/display/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ source "subsys/logging/Kconfig.template.log_config"
source "drivers/display/Kconfig.grove"

source "drivers/display/Kconfig.microbit"

source "drivers/display/Kconfig.ili9340"
source "drivers/display/Kconfig.sdl"

source "drivers/display/Kconfig.ssd1306"

Expand Down
30 changes: 30 additions & 0 deletions drivers/display/Kconfig.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Kconfig - SDL based emulated display configuration options

#
# Copyright (c) 2018 Jan Van Winkel <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig SDL_DISPLAY
bool "SDL based emulated display"
depends on BOARD_NATIVE_POSIX
select HAS_SDL
help
Enable SDL based emulated display compliant with display driver API.

if SDL_DISPLAY

config SDL_DISPLAY_DEV_NAME
string "SDL display device name"
default "SDL_DISPLAY"

config SDL_DISPLAY_X_RES
int "X resolution for SDL display"
default 320

config SDL_DISPLAY_Y_RES
int "Y resolution for SDL display"
default 240

endif #SDL_DISPLAY
Loading

0 comments on commit 384c4c7

Please sign in to comment.