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

5644b289fe6fb9561ede6b58e64ac25d6c2f0553 — František Boháček 3 years ago 6d5b042
feat: add option to run on x86 computer with sdl
M image-viewer/Makefile => image-viewer/Makefile +12 -2
@@ 3,13 3,23 @@ OBJ_DIR=./obj
BIN_DIR?=./bin
INC_DIR=./include $(INHERIT_INCLUDES)

CC = arm-linux-gnueabihf-gcc
CXX = arm-linux-gnueabihf-g++
ifdef COMPUTER
	CC = clang
else
	CC = arm-linux-gnueabihf-gcc
	CXX = arm-linux-gnueabihf-g++
endif

CPPFLAGS = -I .
CFLAGS =-g -std=gnu99 -O1 -Wall
CXXFLAGS = -g -std=gnu++11 -O1 -Wall

ifdef COMPUTER
LDFLAGS = -lrt -lpthread -lmagic -ljpeg -lpng $(shell sdl2-config --libs) -lSDL2_image
CFLAGS += -DCOMPUTER $(shell sdl2-config --cflags)
else
LDFLAGS = -lrt -lpthread -l :libmagic.so.1 -l :libjpeg.so.62 -l :libz.so -l :libpng16.so.16
endif

NAME=image-viewer
BINARY=$(BIN_DIR)/$(NAME)

A image-viewer/include/xwin_sdl.h => image-viewer/include/xwin_sdl.h +14 -0
@@ 0,0 1,14 @@
#ifndef __XWIN_SDL_H__
#define __XWIN_SDL_H__

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

int xwin_init(uint16_t w, uint16_t h);
void xwin_close();
void xwin_redraw(uint16_t w, uint16_t h, uint8_t *img);
bool xwin_poll_event(SDL_Event *event);
void xwin_save_image(char *output_name);

#endif

M image-viewer/src/display_utils.c => image-viewer/src/display_utils.c +25 -1
@@ 2,6 2,9 @@
#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};



@@ 9,6 12,12 @@ 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};

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;
}

display_pixel_t raw_pixel_onebit_convert_to_display(raw_pixel_onebit_t pixel,
                                                    raw_pixel_onebit_t max) {
  display_pixel_t new = {


@@ 44,13 53,28 @@ display_t display_init(display_data_t data) {
    display_clear(&display);
  }

  #ifdef COMPUTER
  xwin_init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
  #endif
  return display;
}
void display_deinit(display_t *display) {
  display_clear(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;
  }

M image-viewer/src/input.c => image-viewer/src/input.c +9 -0
@@ 89,6 89,15 @@ int16_t commands_execute(commands_t * commands, input_type_t type, char filter,
}

void commands_update_rotation_encoders(rotation_encoders_t *encoders) {
  if (encoders->base_address == NULL) {
    for (int i = 0; i < ROTATION_ENCODERS_COUNT; i++) {
      encoders->encoders_state[i].absolute = 0;
      encoders->encoders_state[i].delta = 0;
      encoders->encoders_state[i].button = false;
      encoders->encoders_state[i].button_prev = false;
    }
    return;
  }
  uint8_t btns = *(volatile uint8_t*)(encoders->base_address + ROTATION_ENCODERS_COUNT);

  for (int i = 0; i < ROTATION_ENCODERS_COUNT; i++) {

M image-viewer/src/main.c => image-viewer/src/main.c +4 -2
@@ 52,8 52,10 @@ int main(int argc, char *argv[])
  };

  void *reg_knobs_base =
      map_phys_address(SPILED_REG_BASE_PHYS, SPILED_REG_SIZE, 0) +
      SPILED_REG_KNOBS_8BIT_o;
    map_phys_address(SPILED_REG_BASE_PHYS, SPILED_REG_SIZE, 0);
  if (reg_knobs_base != NULL) {
    reg_knobs_base += SPILED_REG_KNOBS_8BIT_o;
  }

  logger_debug(&logger, __FILE__, __FUNCTION__, __LINE__,
              "Initializing display...", argv[1]);

M image-viewer/src/mzapo_phys.c => image-viewer/src/mzapo_phys.c +4 -0
@@ 27,6 27,9 @@ 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;
  #else
  unsigned long mem_window_size;
  unsigned long pagesize;
  unsigned char *mm;


@@ 53,4 56,5 @@ void *map_phys_address(off_t region_base, size_t region_size, int opt_cached)
  }

  return (void *)mem;
  #endif
}

A image-viewer/src/xwin_sdl.c => image-viewer/src/xwin_sdl.c +71 -0
@@ 0,0 1,71 @@
/*
 * File name: xwin_sdl.c
 * Date:      2015/06/18 14:37
 * Author:    Jan Faigl
 */

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

Do not follow this link