~ruther/CTU-FEE-B0B35APO-Semestral-project

a2e9b93deb8fe3366647519b45d9ca0d7f86a929 — František Boháček 4 years ago 33bc08c
feat: add mzapo sdl
M Makefile => Makefile +11 -3
@@ 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)

M file-browser/Makefile => file-browser/Makefile +1 -1
@@ 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

M image-viewer/Makefile => image-viewer/Makefile +1 -1
@@ 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

M image-viewer/src/main.c => image-viewer/src/main.c +12 -0
@@ 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;
}

D lib-pheripherals/include/xwin_sdl.h => lib-pheripherals/include/xwin_sdl.h +0 -48
@@ 1,48 0,0 @@
#ifndef __XWIN_SDL_H__
#define __XWIN_SDL_H__

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

/**
 * @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

M lib-pheripherals/src/display_utils.c => lib-pheripherals/src/display_utils.c +1 -30
@@ 2,10 2,6 @@
#include "mzapo_parlcd.h"
#include <stdlib.h>

#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) {

M lib-pheripherals/src/mzapo_parlcd.c => lib-pheripherals/src/mzapo_parlcd.c +18 -2
@@ 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)

M lib-pheripherals/src/mzapo_phys.c => lib-pheripherals/src/mzapo_phys.c +5 -1
@@ 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;

M lib-pheripherals/src/mzapo_rgb_led.c => lib-pheripherals/src/mzapo_rgb_led.c +0 -2
@@ 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);

D lib-pheripherals/src/xwin_sdl.c => lib-pheripherals/src/xwin_sdl.c +0 -68
@@ 1,68 0,0 @@
#ifdef COMPUTER
#include <assert.h>

#include <SDL.h>
#include <SDL_image.h>

#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

M text-viewer/Makefile => text-viewer/Makefile +1 -1
@@ 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

M text-viewer/src/main.c => text-viewer/src/main.c +10 -0
@@ 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;
}

Do not follow this link