From a2e9b93deb8fe3366647519b45d9ca0d7f86a929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Fri, 25 Jun 2021 23:32:14 +0200 Subject: [PATCH] feat: add mzapo sdl --- Makefile | 14 ++++-- file-browser/Makefile | 2 +- image-viewer/Makefile | 2 +- image-viewer/src/main.c | 12 +++++ lib-pheripherals/include/xwin_sdl.h | 48 -------------------- lib-pheripherals/src/display_utils.c | 31 +------------ lib-pheripherals/src/mzapo_parlcd.c | 20 +++++++- lib-pheripherals/src/mzapo_phys.c | 6 ++- lib-pheripherals/src/mzapo_rgb_led.c | 2 - lib-pheripherals/src/xwin_sdl.c | 68 ---------------------------- text-viewer/Makefile | 2 +- text-viewer/src/main.c | 10 ++++ 12 files changed, 60 insertions(+), 157 deletions(-) delete mode 100644 lib-pheripherals/include/xwin_sdl.h delete mode 100644 lib-pheripherals/src/xwin_sdl.c diff --git a/Makefile b/Makefile index 5613a5c..30cd117 100644 --- a/Makefile +++ b/Makefile @@ -25,11 +25,15 @@ LIB_PHERIPHERALS=$(BIN_DIR)/libmzapo-pheripherals.so LIB_GUI=$(BIN_DIR)/libmzapo-gui.so TEXT_VIEWER=$(BIN_DIR)/text-viewer FILE_BROWSER=$(BIN_DIR)/file-browser +MZAPO_SDL=$(BIN_DIR)/libmzapo_sdl.so ifdef COMPUTER DEPENDENCIES=./.computer +COMPUTER_SDL=mzapo-sdl +INHERIT_INCLUDES += -I$(ROOT_DIR)/mzapo-sdl/include else DEPENDENCIES=./.arm +COMPUTER_SDL= endif all: $(DEPENDENCIES) image-viewer text-viewer file-browser @@ -39,14 +43,15 @@ lib-pheripherals: $(LIB_PHERIPHERALS) lib-gui: $(LIB_GUI) text-viewer: $(TEXT_VIEWER) file-browser: $(FILE_BROWSER) +mzapo-sdl: $(MZAPO_SDL) -$(IMAGE_VIEWER): $(DEPENDENCIES) lib-pheripherals FORCE +$(IMAGE_VIEWER): $(DEPENDENCIES) lib-pheripherals $(COMPUTER_SDL) FORCE @make -C image-viewer -$(TEXT_VIEWER): $(DEPENDENCIES) lib-pheripherals lib-gui FORCE +$(TEXT_VIEWER): $(DEPENDENCIES) lib-pheripherals lib-gui $(COMPUTER_SDL) FORCE @make -C text-viewer -$(FILE_BROWSER): $(DEPENDENCIES) lib-pheripherals lib-gui FORCE +$(FILE_BROWSER): $(DEPENDENCIES) lib-pheripherals lib-gui $(COMPUTER_SDL) FORCE @make -C file-browser $(DEPENDENCIES): @@ -59,6 +64,9 @@ $(LIB_PHERIPHERALS): $(DEPENDENCIES) FORCE $(LIB_GUI): $(DEPENDENCIES) FORCE @make -C lib-gui +$(MZAPO_SDL): FORCE + @make -C mzapo-sdl + copy-executable: all ssh $(SSH_OPTIONS) -t $(TARGET_USER)@$(TARGET_IP) killall gdbserver 1>/dev/null 2>/dev/null || true ssh $(SSH_OPTIONS) $(TARGET_USER)@$(TARGET_IP) mkdir -p $(TARGET_DIR) diff --git a/file-browser/Makefile b/file-browser/Makefile index 8d7458c..b26f258 100644 --- a/file-browser/Makefile +++ b/file-browser/Makefile @@ -16,7 +16,7 @@ CFLAGS =-g -std=gnu99 -O1 -Wall -D ILI9481 CXXFLAGS = -g -std=gnu++11 -O1 -Wall ifdef COMPUTER -LDFLAGS = -lmzapo-pheripherals -lmzapo-gui -lmagic $(shell sdl2-config --libs) -lSDL2_image +LDFLAGS = -lmzapo-pheripherals -lmzapo-gui -lmagic $(shell sdl2-config --libs) -lSDL2_image -lmzapo_sdl CFLAGS += -DCOMPUTER $(shell sdl2-config --cflags) else LDFLAGS = -lmzapo-pheripherals -lmzapo-gui -l :libmagic.so.1 diff --git a/image-viewer/Makefile b/image-viewer/Makefile index a100445..5e9c997 100644 --- a/image-viewer/Makefile +++ b/image-viewer/Makefile @@ -16,7 +16,7 @@ CFLAGS =-g -std=gnu99 -O1 -Wall -D ILI9481 CXXFLAGS = -g -std=gnu++11 -O1 -Wall ifdef COMPUTER -LDFLAGS = -lmzapo-pheripherals -lmagic -ljpeg -lpng $(shell sdl2-config --libs) -lSDL2_image +LDFLAGS = -lmzapo-pheripherals -lmagic -ljpeg -lpng $(shell sdl2-config --libs) -lSDL2_image -lmzapo_sdl CFLAGS += -DCOMPUTER $(shell sdl2-config --cflags) else LDFLAGS = -lmzapo-pheripherals -l :libmagic.so.1 -l :libjpeg.so.62 -l :libz.so -l :libpng16.so.16 diff --git a/image-viewer/src/main.c b/image-viewer/src/main.c index 9590135..26832ed 100644 --- a/image-viewer/src/main.c +++ b/image-viewer/src/main.c @@ -24,6 +24,10 @@ #include "serialize_lock.h" #include "mzapo_rgb_led.h" +#ifdef COMPUTER +#include "mzapo_sdl.h" +#endif + typedef enum { SUCCESS, TOO_FEW_ARGUMENTS, @@ -32,6 +36,10 @@ typedef enum { int main(int argc, char *argv[]) { +#ifdef COMPUTER + mzapo_sdl_init(); +#endif + /* Try to acquire lock the first */ if (serialize_lock(1) <= 0) { printf("System is occupied\n"); @@ -107,5 +115,9 @@ int main(int argc, char *argv[]) argv[1]); ledstrip_clear(&ledstrip); + +#ifdef COMPUTER + mzapo_sdl_deinit(); +#endif return SUCCESS; } diff --git a/lib-pheripherals/include/xwin_sdl.h b/lib-pheripherals/include/xwin_sdl.h deleted file mode 100644 index dd75a6f..0000000 --- a/lib-pheripherals/include/xwin_sdl.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef __XWIN_SDL_H__ -#define __XWIN_SDL_H__ - -#include -#include -#include - -/** - * @brief Init SDL window - * - * @param w width - * @param h height - * @return int - */ -int xwin_init(uint16_t w, uint16_t h); - -/** - * @brief Close SDL window - * - */ -void xwin_close(); - -/** - * @brief Redraw SDL window with img data in format rgb888 stacked together - * - * @param w width of img - * @param h height of img - * @param img data with rgb888 stacked together - */ -void xwin_redraw(uint16_t w, uint16_t h, uint8_t *img); - -/** - * @brief Poll event to show OS window is responsive - * - * @param event - * @return true event was polled - * @return false no event found - */ -bool xwin_poll_event(SDL_Event *event); - -/** - * @brief Save image of the window to file - * - * @param output_name where to save the image - */ -void xwin_save_image(char *output_name); - -#endif diff --git a/lib-pheripherals/src/display_utils.c b/lib-pheripherals/src/display_utils.c index 347f585..0bbfa0f 100644 --- a/lib-pheripherals/src/display_utils.c +++ b/lib-pheripherals/src/display_utils.c @@ -2,10 +2,6 @@ #include "mzapo_parlcd.h" #include -#ifdef COMPUTER -#include "xwin_sdl.h" -#endif - const display_pixel_t BLACK_PIXEL = {.bits = 0}; const display_pixel_t WHITE_PIXEL = {.bits = 0xFFFF}; @@ -13,14 +9,6 @@ const raw_pixel_t DISPLAY_PIXEL_MAX = {.red = (uint16_t)DISPLAY_MAX_RED, .green = (uint16_t)DISPLAY_MAX_GREEN, .blue = (uint16_t)DISPLAY_MAX_BLUE}; -#ifdef COMPUTER -static void display_pixel_to_rgb(display_pixel_t pixel, uint8_t *target) { - *(target++) = ((float)pixel.fields.r / DISPLAY_MAX_RED) * 255; - *(target++) = ((float)pixel.fields.g / DISPLAY_MAX_GREEN) * 255; - *(target++) = ((float)pixel.fields.b / DISPLAY_MAX_BLUE) * 255; -} -#endif - display_pixel_t raw_pixel_onebit_convert_to_display(raw_pixel_onebit_t pixel, raw_pixel_onebit_t max) { display_pixel_t new = { @@ -52,33 +40,17 @@ display_t display_init(display_data_t data) { }; if (data.base_address != NULL) { - parlcd_hx8357_init(data.base_address); + //parlcd_hx8357_init(data.base_address); display_clear(&display, true); } - #ifdef COMPUTER - xwin_init(DISPLAY_WIDTH, DISPLAY_HEIGHT); - #endif - return display; } void display_deinit(display_t *display) { -#ifdef COMPUTER - xwin_close(); -#else display_clear(display, true); -#endif } void display_render(display_t *display) { - #ifdef COMPUTER - uint8_t data[DISPLAY_WIDTH * DISPLAY_HEIGHT * 3]; - for (int i = 0; i < DISPLAY_WIDTH * DISPLAY_HEIGHT; i++) { - display_pixel_to_rgb(display->pixels[i], data + i * 3); - } - - xwin_redraw(DISPLAY_WIDTH, DISPLAY_HEIGHT, data); -#else if (display->data.base_address == NULL) { return; } @@ -88,7 +60,6 @@ void display_render(display_t *display) { for (int i = 0; i < count; i++) { parlcd_write_data(display->data.base_address, display->pixels[i].bits); } - #endif } void display_clear(display_t *display, bool render) { diff --git a/lib-pheripherals/src/mzapo_parlcd.c b/lib-pheripherals/src/mzapo_parlcd.c index 1a1abef..535439f 100644 --- a/lib-pheripherals/src/mzapo_parlcd.c +++ b/lib-pheripherals/src/mzapo_parlcd.c @@ -23,24 +23,40 @@ #include "mzapo_parlcd.h" #include "mzapo_regs.h" +#ifdef COMPUTER +#include "mzapo_sdl.h" +#endif + void parlcd_write_cr(unsigned char *parlcd_mem_base, uint16_t data) { - *(volatile uint16_t*)(parlcd_mem_base + PARLCD_REG_CR_o) = data; +#ifndef COMPUTER + *(volatile uint16_t *)(parlcd_mem_base + PARLCD_REG_CR_o) = data; +#endif } void parlcd_write_cmd(unsigned char *parlcd_mem_base, uint16_t cmd) { - *(volatile uint16_t*)(parlcd_mem_base + PARLCD_REG_CMD_o) = cmd; +#ifdef COMPUTER + ((mzapo_sdl_display*)(parlcd_mem_base))->cmd(cmd); +#else + *(volatile uint16_t *)(parlcd_mem_base + PARLCD_REG_CMD_o) = cmd; +#endif } void parlcd_write_data(unsigned char *parlcd_mem_base, uint16_t data) { +#ifdef COMPUTER + ((mzapo_sdl_display *)(parlcd_mem_base))->data(data); +#else *(volatile uint16_t*)(parlcd_mem_base + PARLCD_REG_DATA_o) = data; +#endif } void parlcd_write_data2x(unsigned char *parlcd_mem_base, uint32_t data) { +#ifndef COMPUTER *(volatile uint32_t*)(parlcd_mem_base + PARLCD_REG_DATA_o) = data; +#endif } void parlcd_delay(int msec) diff --git a/lib-pheripherals/src/mzapo_phys.c b/lib-pheripherals/src/mzapo_phys.c index 48109e1..3bed643 100644 --- a/lib-pheripherals/src/mzapo_phys.c +++ b/lib-pheripherals/src/mzapo_phys.c @@ -23,12 +23,16 @@ #include "mzapo_phys.h" +#ifdef COMPUTER +#include "mzapo_sdl.h" +#endif + const char *map_phys_memdev="/dev/mem"; void *map_phys_address(off_t region_base, size_t region_size, int opt_cached) { #ifdef COMPUTER - return NULL; + return mzapo_sdl_map_phys(region_base, region_size); #else unsigned long mem_window_size; unsigned long pagesize; diff --git a/lib-pheripherals/src/mzapo_rgb_led.c b/lib-pheripherals/src/mzapo_rgb_led.c index e8a405c..c4cccc3 100644 --- a/lib-pheripherals/src/mzapo_rgb_led.c +++ b/lib-pheripherals/src/mzapo_rgb_led.c @@ -60,8 +60,6 @@ void rgb_led_update(mzapo_rgb_led_t *rgb_led) { struct timespec set_time = rgb_led->states[i].set_time; uint32_t diff = (((now.tv_sec - set_time.tv_sec) * 1000) + (now.tv_nsec - set_time.tv_nsec) / 1000000); - printf("%u\r\n", diff); - if (diff >= rgb_led->states[i].timeout_ms) { rgb_led->states[i].timeout_ms = 0; rgb_led_clear(rgb_led, i); diff --git a/lib-pheripherals/src/xwin_sdl.c b/lib-pheripherals/src/xwin_sdl.c deleted file mode 100644 index 9d91597..0000000 --- a/lib-pheripherals/src/xwin_sdl.c +++ /dev/null @@ -1,68 +0,0 @@ -#ifdef COMPUTER -#include - -#include -#include - -#include "SDL_surface.h" -#include "SDL_video.h" -#include "xwin_sdl.h" - -static SDL_Window *win = NULL; - -int xwin_init(uint16_t w, uint16_t h) { - int r = 0; - r = SDL_Init(SDL_INIT_VIDEO); - assert(win == NULL); - win = SDL_CreateWindow("APO Test program", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN); - assert(win != NULL); - SDL_SetWindowTitle(win, "APO Test program"); - SDL_Surface *surface = SDL_CreateRGBSurface( - 32, 32, 24, 32 * 3, 0xff, 0xff00, 0xff0000, 0x0000); - SDL_SetWindowIcon(win, surface); - SDL_FreeSurface(surface); - - SDL_SetWindowResizable(win, SDL_FALSE); - return r; -} - -void xwin_close() { - assert(win != NULL); - SDL_DestroyWindow(win); - SDL_Quit(); -} - -void xwin_redraw(uint16_t w, uint16_t h, uint8_t *img) { - assert(img && win); - SDL_Surface *scr = SDL_GetWindowSurface(win); - - for (int y = 0; y < scr->h && y < h; ++y) { - for (int x = 0; x < scr->w && x < w; ++x) { - const int idx = (y * scr->w + x) * scr->format->BytesPerPixel; - Uint8 *px = (Uint8 *)scr->pixels + idx; - uint64_t position_in_img = (y * w + x) * 3; - *(px + scr->format->Rshift / 8) = *(img + position_in_img); - *(px + scr->format->Gshift / 8) = *(img + position_in_img + 1); - *(px + scr->format->Bshift / 8) = *(img + position_in_img + 2); - } - } - SDL_UpdateWindowSurface(win); - SDL_FreeSurface(scr); -} - -void xwin_save_image(char *output_name) { - IMG_Init(IMG_INIT_PNG); - SDL_Surface *scr = SDL_GetWindowSurface(win); - IMG_SavePNG(scr, output_name); - SDL_FreeSurface(scr); - IMG_Quit(); -} - -bool xwin_poll_event(SDL_Event *event) { - return SDL_PollEvent(event); -} - -/* end of xwin_sdl.c */ - -#endif diff --git a/text-viewer/Makefile b/text-viewer/Makefile index 378d27c..33acb50 100644 --- a/text-viewer/Makefile +++ b/text-viewer/Makefile @@ -16,7 +16,7 @@ CFLAGS =-g -std=gnu99 -O1 -Wall -D ILI9481 CXXFLAGS = -g -std=gnu++11 -O1 -Wall ifdef COMPUTER -LDFLAGS = -lmzapo-pheripherals -lmzapo-gui -lmagic -ljpeg -lpng $(shell sdl2-config --libs) -lSDL2_image +LDFLAGS = -lmzapo-pheripherals -lmzapo-gui -lmagic -ljpeg -lpng $(shell sdl2-config --libs) -lSDL2_image -lmzapo_sdl CFLAGS += -DCOMPUTER $(shell sdl2-config --cflags) else LDFLAGS = -lmzapo-pheripherals -lmzapo-gui -l :libmagic.so.1 -l :libjpeg.so.62 -l :libz.so -l :libpng16.so.16 diff --git a/text-viewer/src/main.c b/text-viewer/src/main.c index 52f1781..a4b6a71 100644 --- a/text-viewer/src/main.c +++ b/text-viewer/src/main.c @@ -10,6 +10,10 @@ #include "mzapo_pheripherals.h" #include "text_viewer.h" +#ifdef COMPUTER +#include "mzapo_sdl.h" +#endif + typedef enum { ERROR_SUCCESS, ERROR_NOT_ENOUGH_ARGUMENTS, @@ -18,6 +22,9 @@ typedef enum { } error_t; int main(int argc, char *argv[]) { + #ifdef COMPUTER + mzapo_sdl_init(); + #endif struct termios oldstdin; logger_t logger = @@ -89,5 +96,8 @@ int main(int argc, char *argv[]) { logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, "Application quit"); +#ifdef COMPUTER + mzapo_sdl_deinit(); +#endif return ERROR_SUCCESS; } -- 2.49.0