Skip to content

Commit

Permalink
Fixing emulator bug hd-zero#391
Browse files Browse the repository at this point in the history
  • Loading branch information
lukolszewski committed Jan 28, 2024
1 parent 6233610 commit ff96bf0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/core/SDLaccess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _SDLaccess_H
#define _SDLaccess_H

#include <SDL2/SDL.h>

extern SDL_mutex *global_sdl_mutex;

#endif
11 changes: 10 additions & 1 deletion src/core/input_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <minIni.h>

#ifdef EMULATOR_BUILD
#include "SDLaccess.h"
#include <SDL2/SDL.h>
#endif

Expand Down Expand Up @@ -482,7 +483,9 @@ static void *thread_input_device(void *ptr) {

while (true) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
SDL_LockMutex(global_sdl_mutex);
while (SDL_PollEvent(&event)) {
SDL_UnlockMutex(global_sdl_mutex);
switch (event.type) {
case SDL_QUIT:
exit(0);
Expand Down Expand Up @@ -539,7 +542,10 @@ static void *thread_input_device(void *ptr) {
}
break;
}
SDL_LockMutex(global_sdl_mutex);
}
SDL_UnlockMutex(global_sdl_mutex);
usleep(50000); // Sorry, this will break windows, but it's not like it is working now anyway :-(
}
#endif
}
Expand All @@ -561,5 +567,8 @@ void input_device_init() {
}
app_state_push(APP_STATE_MAINMENU);
#endif
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Error initializing SDL: %s\n", SDL_GetError());
}
pthread_create(&input_device_pid, NULL, thread_input_device, NULL);
}
19 changes: 16 additions & 3 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include <lvgl/lvgl.h>
#include <minIni.h>

#ifdef EMULATOR_BUILD
#include "SDLaccess.h"
#include <SDL2/SDL.h>
SDL_mutex *global_sdl_mutex;
#endif

#include "bmi270/accel_gyro.h"
#include "core/app_state.h"
#include "core/common.hh"
Expand All @@ -23,6 +29,7 @@
#include "core/sleep_mode.h"
#include "core/thread.h"
#include "driver/TP2825.h"
#include "driver/beep.h"
#include "driver/dm5680.h"
#include "driver/esp32.h"
#include "driver/fans.h"
Expand All @@ -34,7 +41,6 @@
#include "driver/mcp3021.h"
#include "driver/oled.h"
#include "driver/rtc.h"
#include "driver/beep.h"
#include "ui/page_power.h"
#include "ui/page_scannow.h"
#include "ui/page_source.h"
Expand Down Expand Up @@ -141,6 +147,13 @@ void lvgl_init() {
int main(int argc, char *argv[]) {
pthread_mutex_init(&lvgl_mutex, NULL);

#ifdef EMULATOR_BUILD
global_sdl_mutex = SDL_CreateMutex();
if (global_sdl_mutex == NULL) {
// Handle error: SDL_CreateMutex failed
}
#endif

// 1. Recall configuration
settings_init();
settings_load();
Expand Down Expand Up @@ -188,10 +201,10 @@ int main(int argc, char *argv[]) {

// 8. Synthetic counter for gif refresh
gif_cnt = 0;

// 8.1 set initial analog module power state
Analog_Module_Power(0);

// 10. Execute main loop
g_init_done = 1;
for (;;) {
Expand Down
9 changes: 8 additions & 1 deletion src/ui/ui_porting.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "lvgl/lvgl.h"

#ifdef EMULATOR_BUILD
#include "SDLaccess.h"
#include <SDL2/SDL.h>
static void *fb1, *fb2;
SDL_Window *window = NULL;
Expand Down Expand Up @@ -55,6 +56,7 @@ static void hdz_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_

fb_sync(&fbdev);
#else
SDL_LockMutex(global_sdl_mutex);
SDL_Rect
src = {
.x = 0,
Expand All @@ -76,6 +78,7 @@ static void hdz_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_
src.h = dst.h;
SDL_RenderCopy(renderer, texture, &src, &dst);
SDL_RenderPresent(renderer);
SDL_UnlockMutex(global_sdl_mutex);
#endif

if (disp_orbit_state & ORBIT_FLUSH) {
Expand Down Expand Up @@ -127,8 +130,11 @@ int lvgl_init_porting() {

lv_disp_drv_init(&disp_drv);
#else
SDL_InitSubSystem(SDL_INIT_VIDEO);
if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
SDL_InitSubSystem(SDL_INIT_VIDEO);
}

SDL_LockMutex(global_sdl_mutex);
window = SDL_CreateWindow(WINDOW_NAME,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
DISP_HOR_RES_FHD, DISP_VER_RES_FHD, 0);
Expand All @@ -140,6 +146,7 @@ int lvgl_init_porting() {
SDL_TEXTUREACCESS_STREAMING,
DRAW_HOR_RES_FHD,
DRAW_VER_RES_FHD);
SDL_UnlockMutex(global_sdl_mutex);

fb1 = malloc(DRAW_HOR_RES_FHD * DRAW_VER_RES_FHD * ((LV_COLOR_DEPTH + 7) / 8));
fb2 = malloc(DRAW_HOR_RES_FHD * DRAW_VER_RES_FHD * ((LV_COLOR_DEPTH + 7) / 8));
Expand Down

0 comments on commit ff96bf0

Please sign in to comment.