Skip to content

Commit

Permalink
WIP fleshing out rewiring of menu / gui / main / cart to use BoxEmu c…
Browse files Browse the repository at this point in the history
…omponent instead of box-emu-hal component
  • Loading branch information
finger563 committed Jul 3, 2024
1 parent 4bd5033 commit bb54482
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 81 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ set(EMULATOR_COMPONENTS

set(
COMPONENTS
"main esptool_py esp_lcd esp_psram task format display display_drivers monitor timer ${EMULATOR_COMPONENTS} box-emu-hal gui menu"
"main esptool_py esp_lcd esp_psram task format display display_drivers monitor timer ${EMULATOR_COMPONENTS} box-emu gui menu"
CACHE STRING
"List of components to include"
)
Expand Down
9 changes: 0 additions & 9 deletions components/box-emu/src/box-emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,6 @@ uint8_t *BoxEmu::romdata() const {
// Gamepad
/////////////////////////////////////////////////////////////////////////////

extern "C" lv_indev_t *get_keypad_input_device() {
auto keypad = BoxEmu::get().keypad();
if (!keypad) {
fmt::print("cannot get keypad input device: keypad not initialized properly!\n");
return nullptr;
}
return keypad->get_input_device();
}

bool BoxEmu::initialize_gamepad() {
logger_.info("Initializing gamepad");
if (version_ == BoxEmu::Version::V0) {
Expand Down
2 changes: 1 addition & 1 deletion components/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ idf_component_register(
SRC_DIRS "src" "generated" "generated/screens" "generated/components"
INCLUDE_DIRS "include"
PRIV_INCLUDE_DIRS "generated"
REQUIRES lvgl task display logger jpeg rom_info box-emu-hal)
REQUIRES lvgl task display logger jpeg rom_info box-emu)
15 changes: 8 additions & 7 deletions components/gui/include/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "task.hpp"
#include "logger.hpp"

#include "box_emu_hal.hpp"
#include "box-emu.hpp"
#include "rom_info.hpp"

class Gui {
Expand Down Expand Up @@ -80,7 +80,7 @@ class Gui {
void set_mute(bool muted);

void toggle_mute() {
set_mute(!hal::is_muted());
set_mute(!espp::EspBox::get().is_muted());
}

void set_audio_level(int new_audio_level);
Expand Down Expand Up @@ -147,18 +147,19 @@ class Gui {
void toggle_usb();

void update_shared_state() {
set_mute(hal::is_muted());
set_audio_level(hal::get_audio_volume());
set_brightness(hal::get_display_brightness() * 100.0f);
set_video_setting(hal::get_video_setting());
auto &box = espp::EspBox::get();
set_mute(box.is_muted());
set_audio_level(box.volume());
set_brightness(box.brightness());
set_video_setting(BoxEmu::get().video_setting());
}

VideoSetting get_video_setting();

void on_rom_focused(lv_obj_t *new_focus);

void on_mute_button_pressed(const std::vector<uint8_t>& data) {
set_mute(hal::is_muted());
set_mute(espp::EspBox::get().is_muted());
}

void on_battery(const std::vector<uint8_t>& data);
Expand Down
35 changes: 18 additions & 17 deletions components/gui/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern "C" {
}

void Gui::set_mute(bool muted) {
hal::set_muted(muted);
espp::EspBox::get().mute(muted);
if (muted) {
lv_obj_add_state(ui_mutebutton, LV_STATE_CHECKED);
} else {
Expand All @@ -18,17 +18,17 @@ void Gui::set_mute(bool muted) {
void Gui::set_audio_level(int new_audio_level) {
new_audio_level = std::clamp(new_audio_level, 0, 100);
lv_bar_set_value(ui_volumebar, new_audio_level, LV_ANIM_ON);
hal::set_audio_volume(new_audio_level);
espp::EspBox::get().volume(new_audio_level);
}

void Gui::set_brightness(int new_brightness) {
new_brightness = std::clamp(new_brightness, 10, 100);
lv_bar_set_value(ui_brightnessbar, new_brightness, LV_ANIM_ON);
hal::set_display_brightness((float)new_brightness / 100.0f);
espp::EspBox::get().brightness((float)new_brightness);
}

void Gui::set_video_setting(VideoSetting setting) {
hal::set_video_setting(setting);
BoxEmu::get().video_setting(setting);
lv_dropdown_set_selected(ui_videosettingdropdown, (int)setting);
}

Expand Down Expand Up @@ -169,7 +169,7 @@ void Gui::init_ui() {
settings_screen_group_ = lv_group_create();

// get the KEYPAD indev
auto keypad = get_keypad_input_device();
auto keypad = BoxEmu::get().keypad()->get_input_device();
if (keypad)
lv_indev_set_group(keypad, rom_screen_group_);

Expand All @@ -188,7 +188,7 @@ void Gui::init_ui() {
lv_obj_set_flex_flow(ui_rompanel, LV_FLEX_FLOW_COLUMN);
lv_obj_set_scroll_snap_y(ui_rompanel, LV_SCROLL_SNAP_CENTER);

lv_bar_set_value(ui_volumebar, hal::get_audio_volume(), LV_ANIM_OFF);
lv_bar_set_value(ui_volumebar, espp::EspBox::get().volume(), LV_ANIM_OFF);

// rom screen navigation
lv_obj_add_event_cb(ui_settingsbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
Expand Down Expand Up @@ -306,12 +306,12 @@ void Gui::on_pressed(lv_event_t *e) {
// volume controls
bool is_volume_up_button = (target == ui_volumeupbutton);
if (is_volume_up_button) {
set_audio_level(hal::get_audio_volume() + 10);
set_audio_level(espp::EspBox::get().volume() + 10);
return;
}
bool is_volume_down_button = (target == ui_volumedownbutton);
if (is_volume_down_button) {
set_audio_level(hal::get_audio_volume() - 10);
set_audio_level(espp::EspBox::get().volume() - 10);
return;
}
bool is_mute_button = (target == ui_mutebutton);
Expand All @@ -322,13 +322,13 @@ void Gui::on_pressed(lv_event_t *e) {
// brightness controlsn
bool is_brightness_up_button = (target == ui_brightnessupbutton);
if (is_brightness_up_button) {
int brightness = hal::get_display_brightness() * 100.0f;
int brightness = espp::EspBox::get().brightness();
set_brightness(brightness + 10);
return;
}
bool is_brightness_down_button = (target == ui_brightnessdownbutton);
if (is_brightness_down_button) {
int brightness = hal::get_display_brightness() * 100.0f;
int brightness = espp::EspBox::get().brightness();
set_brightness(brightness - 10);
return;
}
Expand Down Expand Up @@ -375,7 +375,7 @@ void Gui::on_pressed(lv_event_t *e) {

void Gui::on_volume(const std::vector<uint8_t>& data) {
// the volume was changed, update our display of the volume
lv_bar_set_value(ui_volumebar, hal::get_audio_volume(), LV_ANIM_ON);
lv_bar_set_value(ui_volumebar, espp::EspBox::get().volume(), LV_ANIM_ON);
}

void Gui::on_battery(const std::vector<uint8_t>& data) {
Expand Down Expand Up @@ -417,14 +417,15 @@ void Gui::on_battery(const std::vector<uint8_t>& data) {

void Gui::toggle_usb() {
fmt::print("Toggling USB\n");
auto &emu = BoxEmu::get();
// toggle the usb
if (usb_is_enabled()) {
usb_deinit();
if (emu.is_usb_enabled()) {
emu.deinitialize_usb();
} else {
usb_init();
emu.initialize_usb();
}
// update the label
if (usb_is_enabled()) {
if (emu.is_usb_enabled()) {
lv_label_set_text(ui_usb_label, "Enabled");
} else {
lv_label_set_text(ui_usb_label, "Disabled");
Expand All @@ -445,7 +446,7 @@ void Gui::focus_rommenu() {
// focus the rom screen group
logger_.debug("Focusing rom screen group");
lv_group_focus_freeze(rom_screen_group_, false);
auto keypad = get_keypad_input_device();
auto keypad = BoxEmu::get().keypad()->get_input_device();
if (keypad)
lv_indev_set_group(keypad, rom_screen_group_);
}
Expand All @@ -456,7 +457,7 @@ void Gui::focus_settings() {
logger_.debug("Focusing settings screen group");
lv_group_focus_freeze(settings_screen_group_, false);
// NOTE: we don't set editing here since we use it to manage the dropdown
auto keypad = get_keypad_input_device();
auto keypad = BoxEmu::get().keypad()->get_input_device();
if (keypad)
lv_indev_set_group(keypad, settings_screen_group_);
}
Expand Down
2 changes: 1 addition & 1 deletion components/menu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ idf_component_register(
INCLUDE_DIRS "include"
SRC_DIRS "src" "generated" "generated/screens" "generated/components"
PRIV_INCLUDE_DIRS "generated"
REQUIRES lvgl timer display logger jpeg box-emu-hal)
REQUIRES lvgl timer display logger jpeg box-emu statistics)
16 changes: 9 additions & 7 deletions components/menu/include/menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include "high_resolution_timer.hpp"
#include "logger.hpp"

#include "box_emu_hal.hpp"
#include "box-emu.hpp"
#include "statistics.hpp"

class Menu {
public:
Expand Down Expand Up @@ -90,7 +91,7 @@ class Menu {
void set_mute(bool muted);

void toggle_mute() {
set_mute(!hal::is_muted());
set_mute(!espp::EspBox::get().is_muted());
}

void set_audio_level(int new_audio_level);
Expand Down Expand Up @@ -123,16 +124,17 @@ class Menu {
void update_fps_label(float fps);

void update_shared_state() {
set_mute(hal::is_muted());
set_audio_level(hal::get_audio_volume());
set_brightness(hal::get_display_brightness() * 100.0f);
set_video_setting(hal::get_video_setting());
auto &box = espp::EspBox::get();
set_mute(box.is_muted());
set_audio_level(box.volume());
set_brightness(box.brightness());
set_video_setting(BoxEmu::get().video_setting());
}

VideoSetting get_video_setting();

void on_mute_button_pressed(const std::vector<uint8_t>& data) {
set_mute(hal::is_muted());
set_mute(espp::EspBox::get().is_muted());
}

void update() {
Expand Down
20 changes: 10 additions & 10 deletions components/menu/src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void Menu::init_ui() {
lv_group_set_default(group_);

// get the KEYPAD indev
auto keypad = get_keypad_input_device();
auto keypad = BoxEmu::get().keypad()->get_input_device();
if (keypad)
lv_indev_set_group(keypad, group_);

Expand Down Expand Up @@ -218,7 +218,7 @@ void Menu::update_fps_label(float fps) {
}

void Menu::set_mute(bool muted) {
hal::set_muted(muted);
espp::EspBox::get().mute(muted);
if (muted) {
lv_obj_add_state(ui_volume_mute_btn, LV_STATE_CHECKED);
} else {
Expand All @@ -229,17 +229,17 @@ void Menu::set_mute(bool muted) {
void Menu::set_audio_level(int new_audio_level) {
new_audio_level = std::clamp(new_audio_level, 0, 100);
lv_bar_set_value(ui_Bar2, new_audio_level, LV_ANIM_ON);
hal::set_audio_volume(new_audio_level);
espp::EspBox::get().volume(new_audio_level);
}

void Menu::set_brightness(int new_brightness) {
new_brightness = std::clamp(new_brightness, 10, 100);
lv_bar_set_value(ui_brightness_bar, new_brightness, LV_ANIM_ON);
hal::set_display_brightness((float)new_brightness / 100.0f);
espp::EspBox::get().brightness((float)new_brightness);
}

void Menu::set_video_setting(VideoSetting setting) {
hal::set_video_setting(setting);
BoxEmu::get().video_setting(setting);
lv_dropdown_set_selected(ui_Dropdown2, (int)setting);
}

Expand Down Expand Up @@ -307,12 +307,12 @@ void Menu::on_pressed(lv_event_t *e) {
// volume controls
bool is_volume_up_button = (target == ui_volume_inc_btn);
if (is_volume_up_button) {
set_audio_level(hal::get_audio_volume() + 10);
set_audio_level(espp::EspBox::get().volume() + 10);
return;
}
bool is_volume_down_button = (target == ui_volume_dec_btn);
if (is_volume_down_button) {
set_audio_level(hal::get_audio_volume() - 10);
set_audio_level(espp::EspBox::get().volume() - 10);
return;
}
bool is_mute_button = (target == ui_volume_mute_btn);
Expand All @@ -323,19 +323,19 @@ void Menu::on_pressed(lv_event_t *e) {
// brightness controls
bool is_brightness_up_button = (target == ui_brightness_inc_btn);
if (is_brightness_up_button) {
set_brightness(hal::get_display_brightness() * 100.0f + 10);
set_brightness(espp::EspBox::get().brightness() + 10);
return;
}
bool is_brightness_down_button = (target == ui_brightness_dec_btn);
if (is_brightness_down_button) {
set_brightness(hal::get_display_brightness() * 100.0f - 10);
set_brightness(espp::EspBox::get().brightness() - 10);
return;
}
}

void Menu::on_volume(const std::vector<uint8_t>& data) {
// the volume was changed, update our display of the volume
lv_bar_set_value(ui_Bar2, hal::get_audio_volume(), LV_ANIM_ON);
lv_bar_set_value(ui_Bar2, espp::EspBox::get().volume(), LV_ANIM_ON);
}

void Menu::on_battery(const std::vector<uint8_t>& data) {
Expand Down
18 changes: 8 additions & 10 deletions main/cart.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "display.hpp"
#include "logger.hpp"

#include "box_emu_hal.hpp"
#include "box-emu.hpp"
#include "rom_info.hpp"
#include "menu.hpp"

Expand Down Expand Up @@ -39,8 +39,8 @@ class Cart {
// clear the screen
espp::St7789::clear(0,0,320,240);
// copy the romdata
rom_size_bytes_ = copy_romdata_to_cart_partition(get_rom_filename());
romdata_ = get_mmapped_romdata();
rom_size_bytes_ = BoxEmu::get().copy_file_to_romdata(get_rom_filename());
romdata_ = BoxEmu::get().romdata();
// create the menu
menu_ = std::make_unique<Menu>(Menu::Config{
.display = display_,
Expand Down Expand Up @@ -156,16 +156,14 @@ class Cart {
virtual bool run() {
running_ = true;
// handle touchpad so we can know if the user presses the menu
uint8_t _num_touches = 0, _btn_state = false;
uint16_t _x = 0 ,_y = 0;
touchpad_read(&_num_touches, &_x, &_y, &_btn_state);
auto touch = espp::EspBox::get().touchpad_data();
bool btn_state = touch.btn_state;
// also get the gamepad input state so we can know if the user presses the
// start/select buttons together to bring up the menu
InputState state;
hal::get_input_state(&state);
auto state = BoxEmu::get().gamepad_state();
// if the user presses the menu button or the start/select buttons, then
// pause the game and show the menu
bool show_menu = _btn_state || (state.start && state.select);
bool show_menu = btn_state || (state.start && state.select);
if (show_menu) {
logger_.info("Menu pressed!");
pre_menu();
Expand Down Expand Up @@ -258,7 +256,7 @@ class Cart {

virtual void handle_video_setting() {
logger_.info("Base handling video setting...");
switch (hal::get_video_setting()) {
switch (BoxEmu::get().video_setting()) {
case VideoSetting::ORIGINAL:
set_original_video_setting();
break;
Expand Down
Loading

0 comments on commit bb54482

Please sign in to comment.