Skip to content

Commit 4a2bda5

Browse files
committed
Game selector with images.
1 parent 279a278 commit 4a2bda5

File tree

6 files changed

+2687
-80
lines changed

6 files changed

+2687
-80
lines changed

components/esp_nes/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
file(GLOB_RECURSE T_SOURCES *.c)
2+
file(GLOB_RECURSE IMAGES images/*.c)
23

34
idf_component_register(
4-
SRCS ${T_SOURCES}
5+
SRCS ${T_SOURCES} ${IMAGES}
56
INCLUDE_DIRS "."
6-
PRIV_REQUIRES driver nofrendo hid
7+
PRIV_REQUIRES driver nofrendo hid nvs_flash
78
)

components/esp_nes/images/img_donkeykong.c

Lines changed: 847 additions & 0 deletions
Large diffs are not rendered by default.

components/esp_nes/images/img_qbert.c

Lines changed: 847 additions & 0 deletions
Large diffs are not rendered by default.

components/esp_nes/images/img_supermario.c

Lines changed: 847 additions & 0 deletions
Large diffs are not rendered by default.

components/esp_nes/osd.c

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
/*******************************************************************************
88
* Includes
99
*******************************************************************************/
10+
1011
#include <freertos/FreeRTOS.h>
1112
#include <freertos/timers.h>
1213
#include <freertos/task.h>
1314
#include <freertos/queue.h>
1415
#include <string.h>
1516

17+
#include "esp_system.h"
18+
#include "nvs_flash.h"
19+
#include "esp_partition.h"
20+
#include "spi_flash_mmap.h"
21+
1622
static const char *TAG = "OSD";
1723

1824
#define LOBYTE(u16) ((uint8_t)(((uint16_t)(u16)) & 0xff))
@@ -40,6 +46,11 @@ static const char *TAG = "OSD";
4046
#define APP_DEFAULT_HEIGHT 240
4147
#define APP_DEFAULT_SCALE (BSP_LCD_V_RES/APP_DEFAULT_HEIGHT)
4248

49+
// LVGL image declare
50+
LV_IMG_DECLARE(img_supermario)
51+
LV_IMG_DECLARE(img_donkeykong)
52+
LV_IMG_DECLARE(img_qbert)
53+
4354
typedef struct {
4455
hid_host_device_handle_t hid_device_handle;
4556
hid_host_driver_event_t event;
@@ -71,11 +82,13 @@ static void app_osd_video_clear(uint8 color);
7182
static bitmap_t *app_osd_video_lock_write(void);
7283
static void app_osd_video_free_write(int num_dirties, rect_t *dirty_rects);
7384
static void app_osd_video_custom_blit(bitmap_t *primary, int num_dirties, rect_t *dirty_rects);
74-
7585
/* Audio functions */
7686

7787
/* USB functions */
7888
static void app_hid_init(void);
89+
90+
/* Game selection */
91+
static void app_lvgl_game_selection(void);
7992
/*******************************************************************************
8093
* Local variables
8194
*******************************************************************************/
@@ -105,10 +118,39 @@ static uint8_t * vid_buffer = NULL;
105118
static QueueHandle_t hid_queue = NULL;
106119
static gamepad_t app_gamepad;
107120
static gamepad_t app_gamepad_tmp;
121+
122+
/* GAME */
123+
static const char * APP_GAME_SUPERMARIO = "nesrom1";
124+
static const char * APP_GAME_DONKEYKONG = "nesrom2";
125+
static const char * APP_GAME_QBERT = "nesrom3";
126+
/* Selected partion with game */
127+
static const char * app_selected_game = NULL;
128+
static lv_obj_t * game_selection = NULL;
108129
/*******************************************************************************
109130
* Public API functions
110131
*******************************************************************************/
111132

133+
char *osd_getromdata()
134+
{
135+
/* Wait for select the game */
136+
while(app_selected_game == NULL)
137+
{
138+
vTaskDelay(pdMS_TO_TICKS(100));
139+
};
140+
141+
char* romdata;
142+
const esp_partition_t* part;
143+
spi_flash_mmap_handle_t hrom;
144+
esp_err_t err;
145+
nvs_flash_init();
146+
part=esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, app_selected_game);
147+
if (part==0) printf("Couldn't find rom part!\n");
148+
err=esp_partition_mmap(part, 0, 1024*1024, SPI_FLASH_MMAP_DATA, (const void**)&romdata, &hrom);
149+
if (err!=ESP_OK) printf("Couldn't map rom part!\n");
150+
printf("Initialized. ROM@%p\n", romdata);
151+
return (char*)romdata;
152+
}
153+
112154
void osd_getvideoinfo(vidinfo_t *info)
113155
{
114156
info->default_width = APP_DEFAULT_WIDTH;
@@ -139,6 +181,9 @@ int osd_init(void)
139181
lv_canvas_set_buffer(lvgl_video_canvas, vid_buffer, APP_DEFAULT_SCALE*APP_DEFAULT_WIDTH, APP_DEFAULT_SCALE*APP_DEFAULT_HEIGHT, LV_IMG_CF_TRUE_COLOR);
140182
lv_obj_center(lvgl_video_canvas);
141183
bsp_display_unlock();
184+
185+
/* Show selection of the game */
186+
app_lvgl_game_selection();
142187

143188
return 0;
144189
}
@@ -282,6 +327,103 @@ int osd_makesnapname(char *filename, int len)
282327
* Private API functions
283328
*******************************************************************************/
284329

330+
static void app_lvgl_btn_supermario(lv_event_t *e)
331+
{
332+
lv_obj_del(game_selection);
333+
app_selected_game = APP_GAME_SUPERMARIO;
334+
}
335+
336+
static void app_lvgl_btn_donkeykong(lv_event_t *e)
337+
{
338+
lv_obj_del(game_selection);
339+
app_selected_game = APP_GAME_DONKEYKONG;
340+
}
341+
342+
static void app_lvgl_btn_qbert(lv_event_t *e)
343+
{
344+
lv_obj_del(game_selection);
345+
app_selected_game = APP_GAME_QBERT;
346+
}
347+
348+
static void app_lvgl_btn_reset(lv_event_t *e)
349+
{
350+
esp_restart();
351+
}
352+
353+
static void app_lvgl_game_selection(void)
354+
{
355+
bsp_display_lock(0);
356+
lv_obj_set_style_bg_color(lv_scr_act(), lv_color_black(), 0);
357+
358+
lv_obj_t * btn_rst = lv_btn_create(lv_scr_act());
359+
lv_obj_t * lbl = lv_label_create(btn_rst);
360+
lv_label_set_text_static(lbl, LV_SYMBOL_PREV"");
361+
lv_obj_add_event_cb(btn_rst, app_lvgl_btn_reset, LV_EVENT_CLICKED, NULL);
362+
363+
game_selection = lv_obj_create(lv_scr_act());
364+
lv_obj_set_size(game_selection, BSP_LCD_H_RES, 2*(BSP_LCD_V_RES/3));
365+
lv_obj_set_flex_flow(game_selection, LV_FLEX_FLOW_COLUMN);
366+
lv_obj_set_style_bg_color(game_selection, lv_color_black(), 0);
367+
lv_obj_set_style_border_color(game_selection, lv_color_black(), 0);
368+
lv_obj_align(game_selection, LV_ALIGN_CENTER, 0, 0);
369+
lv_obj_set_style_pad_top(game_selection, 0, 0);
370+
lv_obj_set_style_pad_bottom(game_selection, 0, 0);
371+
lv_obj_set_style_pad_left(game_selection, 0, 0);
372+
lv_obj_set_style_pad_right(game_selection, 0, 0);
373+
lv_obj_set_flex_align(game_selection, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
374+
375+
lv_obj_t * label = lv_label_create(game_selection);
376+
lv_obj_set_style_text_color(label, lv_color_white(), 0);
377+
lv_label_set_text_static(label, "SELECT THE GAME");
378+
379+
lv_obj_t * games = lv_obj_create(game_selection);
380+
lv_obj_set_style_bg_color(games, lv_color_black(), 0);
381+
lv_obj_set_style_border_color(games, lv_color_black(), 0);
382+
lv_obj_set_size(games, BSP_LCD_H_RES-10, BSP_LCD_V_RES/2);
383+
lv_obj_align(games, LV_ALIGN_CENTER, 0, 0);
384+
lv_obj_set_flex_flow(games, LV_FLEX_FLOW_ROW);
385+
lv_obj_set_style_pad_top(games, 0, 0);
386+
lv_obj_set_style_pad_bottom(games, 0, 0);
387+
lv_obj_set_style_pad_left(games, 0, 0);
388+
lv_obj_set_style_pad_right(games, 0, 0);
389+
lv_obj_set_flex_align(games, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
390+
391+
lv_obj_t * btn = lv_btn_create(games);
392+
lv_obj_t *img = lv_img_create(btn);
393+
lv_img_set_src(img, &img_supermario);
394+
lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 0);
395+
lv_obj_set_style_radius(btn, 0, 0);
396+
lv_obj_set_style_pad_top(btn, 15, 0);
397+
lv_obj_set_style_pad_bottom(btn, 15, 0);
398+
lv_obj_set_style_pad_left(btn, 15, 0);
399+
lv_obj_set_style_pad_right(btn, 15, 0);
400+
lv_obj_add_event_cb(btn, app_lvgl_btn_supermario, LV_EVENT_CLICKED, NULL);
401+
402+
btn = lv_btn_create(games);
403+
img = lv_img_create(btn);
404+
lv_img_set_src(img, &img_donkeykong);
405+
lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 0);
406+
lv_obj_set_style_radius(btn, 0, 0);
407+
lv_obj_set_style_pad_top(btn, 15, 0);
408+
lv_obj_set_style_pad_bottom(btn, 15, 0);
409+
lv_obj_set_style_pad_left(btn, 15, 0);
410+
lv_obj_set_style_pad_right(btn, 15, 0);
411+
lv_obj_add_event_cb(btn, app_lvgl_btn_donkeykong, LV_EVENT_CLICKED, NULL);
412+
413+
btn = lv_btn_create(games);
414+
img = lv_img_create(btn);
415+
lv_img_set_src(img, &img_qbert);
416+
lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 0);
417+
lv_obj_set_style_radius(btn, 0, 0);
418+
lv_obj_set_style_pad_top(btn, 15, 0);
419+
lv_obj_set_style_pad_bottom(btn, 15, 0);
420+
lv_obj_set_style_pad_left(btn, 15, 0);
421+
lv_obj_set_style_pad_right(btn, 15, 0);
422+
lv_obj_add_event_cb(btn, app_lvgl_btn_qbert, LV_EVENT_CLICKED, NULL);
423+
424+
bsp_display_unlock();
425+
}
426+
285427
static int app_osd_video_init(int width, int height)
286428
{
287429
return 0;
@@ -375,7 +517,6 @@ static void app_usb_hid_host_interface_callback(hid_host_device_handle_t hid_dev
375517
case HID_HOST_INTERFACE_EVENT_INPUT_REPORT:
376518
data = hid_host_device_get_data(hid_device_handle, &data_length);
377519
if (dev->proto == HID_PROTOCOL_NONE) {
378-
gamepad_t *gamepad = (gamepad_t*)data;
379520
if (data_length < 8) {
380521
break;
381522
}

main/main.c

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,13 @@
44
* SPDX-License-Identifier: CC0-1.0
55
*/
66

7-
#include "esp_system.h"
8-
#include "nvs_flash.h"
97
#include "nofrendo.h"
10-
#include "esp_partition.h"
11-
#include "spi_flash_mmap.h"
128
#include "bsp/esp-bsp.h"
139
#include "usb/usb_host.h"
1410
#include "esp_log.h"
1511

1612
static const char *TAG = "NES";
1713

18-
static const char * APP_GAME_SUPERMARIO = "nesrom1";
19-
static const char * APP_GAME_DONKEYKONG = "nesrom2";
20-
static const char * APP_GAME_QBERT = "nesrom3";
21-
/* Selected partion with game */
22-
static const char * app_selected_game = NULL;
23-
24-
char *osd_getromdata()
25-
{
26-
/* Wait for select the game */
27-
while(app_selected_game == NULL)
28-
{
29-
vTaskDelay(pdMS_TO_TICKS(100));
30-
};
31-
32-
char* romdata;
33-
const esp_partition_t* part;
34-
spi_flash_mmap_handle_t hrom;
35-
esp_err_t err;
36-
nvs_flash_init();
37-
part=esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, app_selected_game);
38-
if (part==0) printf("Couldn't find rom part!\n");
39-
err=esp_partition_mmap(part, 0, 1024*1024, SPI_FLASH_MMAP_DATA, (const void**)&romdata, &hrom);
40-
if (err!=ESP_OK) printf("Couldn't map rom part!\n");
41-
printf("Initialized. ROM@%p\n", romdata);
42-
return (char*)romdata;
43-
}
44-
45-
4614
static void usb_host_task(void *args)
4715
{
4816
while (1) {
@@ -62,47 +30,6 @@ static void usb_host_task(void *args)
6230
vTaskDelete(NULL);
6331
}
6432

65-
static void app_lvgl_btn_supermario(lv_event_t *e)
66-
{
67-
app_selected_game = APP_GAME_SUPERMARIO;
68-
}
69-
70-
static void app_lvgl_btn_donkeykong(lv_event_t *e)
71-
{
72-
app_selected_game = APP_GAME_DONKEYKONG;
73-
}
74-
75-
static void app_lvgl_btn_qbert(lv_event_t *e)
76-
{
77-
app_selected_game = APP_GAME_QBERT;
78-
}
79-
80-
static void app_lvgl_game_selection(void)
81-
{
82-
bsp_display_lock(0);
83-
lv_obj_set_style_bg_color(lv_scr_act(), lv_color_black(), 0);
84-
85-
lv_obj_t * btn = lv_btn_create(lv_scr_act());
86-
lv_obj_t * label = lv_label_create(btn);
87-
lv_label_set_text_static(label, "Super Mario");
88-
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, 10);
89-
lv_obj_add_event_cb(btn, app_lvgl_btn_supermario, LV_EVENT_CLICKED, NULL);
90-
91-
btn = lv_btn_create(lv_scr_act());
92-
label = lv_label_create(btn);
93-
lv_label_set_text_static(label, "Donkey Kong");
94-
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, 60);
95-
lv_obj_add_event_cb(btn, app_lvgl_btn_donkeykong, LV_EVENT_CLICKED, NULL);
96-
97-
btn = lv_btn_create(lv_scr_act());
98-
label = lv_label_create(btn);
99-
lv_label_set_text_static(label, "Q-Bert");
100-
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, 110);
101-
lv_obj_add_event_cb(btn, app_lvgl_btn_qbert, LV_EVENT_CLICKED, NULL);
102-
103-
bsp_display_unlock();
104-
}
105-
10633
int app_main(void)
10734
{
10835
usb_host_config_t host_config = {
@@ -118,9 +45,6 @@ int app_main(void)
11845

11946
/* Set display brightness to 100% */
12047
bsp_display_backlight_on();
121-
122-
/* Show selection of the game */
123-
app_lvgl_game_selection();
12448

12549
printf("NoFrendo start!\n");
12650
nofrendo_main(0, NULL);

0 commit comments

Comments
 (0)