Skip to content

Commit

Permalink
Fixed minor bug in drv component (bad enum value); updated GUI to hav…
Browse files Browse the repository at this point in the history
…e haptic setting with play button for testing it out.
  • Loading branch information
finger563 committed Nov 29, 2022
1 parent 63736ad commit 1a84c7f
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 24 deletions.
20 changes: 10 additions & 10 deletions components/drv2605/include/drv2605.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class Drv2605 {

/**
* @brief Function to write a byte to a register
* @param uint8_t register address to write to
* @param uint8_t data to write
* @param reg_addr register address to write to
* @param data Data to write
*/
typedef std::function<void(uint8_t, uint8_t)> write_fn;
typedef std::function<void(uint8_t reg_addr, uint8_t data)> write_fn;

/**
* @brief Function to read a byte from a register
* @param uint8_t register address to read from
* @param reg_addr register address to read from
* @return Byte read from the register
*/
typedef std::function<uint8_t(uint8_t)> read_fn;
typedef std::function<uint8_t(uint8_t reg_addr)> read_fn;

enum class Mode : uint8_t {
INTTRIG, ///< Internal Trigger (call star() to start playback)
Expand All @@ -42,7 +42,7 @@ class Drv2605 {
DOUBLE_CLICK = 10,
TRIPLE_CLICK = 12,
SOFT_FUZZ = 13,
STRONG_BUZZ = 13,
STRONG_BUZZ = 14,
ALERT_750MS = 15,
ALERT_1000MS = 16, // omg there are 123 of theese i'm not typing them out right now...
BUZZ1 = 47,
Expand All @@ -65,10 +65,10 @@ class Drv2605 {
};

struct Config {
write_fn write;
read_fn read;
MotorType motor_type{MotorType::ERM};
espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN};
write_fn write; /**< Function for writing a byte to a register on the Drv2605. */
read_fn read; /**< Function for reading a byte from a register on the Drv2605. */
MotorType motor_type{MotorType::ERM}; /**< MotorType that this driver is driving. */
espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN}; /**< Log verbosity for the Drv2605. */
};

Drv2605(const Config& config)
Expand Down
92 changes: 87 additions & 5 deletions components/gui/generated/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ lv_obj_t *ui_settingsscreen_Label3;
lv_obj_t *ui_fillpanel;
lv_obj_t *ui_settingsscreen_Label4;
lv_obj_t *ui_videosettingdropdown;
lv_obj_t *ui_hapticpanel;
lv_obj_t *ui_settingsscreen_Label5;
lv_obj_t *ui_settingsscreen_Panel1;
lv_obj_t *ui_hapticlabel;
lv_obj_t *ui_hapticdownbutton;
lv_obj_t *ui_settingsscreen_Label7;
lv_obj_t *ui_hapticupbutton;
lv_obj_t *ui_settingsscreen_Label6;
lv_obj_t *ui_hapticplaybutton;
lv_obj_t *ui_settingsscreen_Label8;

///////////////////// TEST LVGL SETTINGS ////////////////////
#if LV_COLOR_DEPTH != 16
Expand Down Expand Up @@ -164,6 +174,11 @@ lv_obj_set_height( ui_settingspanel, 165);
lv_obj_set_width( ui_settingspanel, lv_pct(100));
lv_obj_set_align( ui_settingspanel, LV_ALIGN_BOTTOM_MID );
lv_obj_add_flag( ui_settingspanel, LV_OBJ_FLAG_SCROLL_ON_FOCUS | LV_OBJ_FLAG_SCROLL_ONE ); /// Flags
lv_obj_set_scroll_dir(ui_settingspanel, LV_DIR_VER);
lv_obj_set_style_pad_left(ui_settingspanel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
lv_obj_set_style_pad_right(ui_settingspanel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
lv_obj_set_style_pad_top(ui_settingspanel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
lv_obj_set_style_pad_bottom(ui_settingspanel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

ui_volumepanel = lv_obj_create(ui_settingspanel);
lv_obj_set_height( ui_volumepanel, 50);
Expand All @@ -173,9 +188,9 @@ lv_obj_clear_flag( ui_volumepanel, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

ui_volumebar = lv_bar_create(ui_volumepanel);
lv_bar_set_value(ui_volumebar,25,LV_ANIM_OFF);
lv_obj_set_width( ui_volumebar, 150);
lv_obj_set_width( ui_volumebar, 130);
lv_obj_set_height( ui_volumebar, 10);
lv_obj_set_x( ui_volumebar, 20 );
lv_obj_set_x( ui_volumebar, 25 );
lv_obj_set_y( ui_volumebar, 0 );
lv_obj_set_align( ui_volumebar, LV_ALIGN_CENTER );

Expand Down Expand Up @@ -210,8 +225,6 @@ lv_label_set_text(ui_settingsscreen_Label2, LV_SYMBOL_VOLUME_MID);
ui_volumeupbutton = lv_btn_create(ui_volumepanel);
lv_obj_set_width( ui_volumeupbutton, 32);
lv_obj_set_height( ui_volumeupbutton, 32);
lv_obj_set_x( ui_volumeupbutton, -10 );
lv_obj_set_y( ui_volumeupbutton, 0 );
lv_obj_set_align( ui_volumeupbutton, LV_ALIGN_RIGHT_MID );
lv_obj_add_flag( ui_volumeupbutton, LV_OBJ_FLAG_SCROLL_ON_FOCUS ); /// Flags
lv_obj_clear_flag( ui_volumeupbutton, LV_OBJ_FLAG_SCROLLABLE ); /// Flags
Expand All @@ -226,7 +239,7 @@ ui_fillpanel = lv_obj_create(ui_settingspanel);
lv_obj_set_height( ui_fillpanel, 50);
lv_obj_set_width( ui_fillpanel, lv_pct(100));
lv_obj_set_x( ui_fillpanel, 0 );
lv_obj_set_y( ui_fillpanel, 50 );
lv_obj_set_y( ui_fillpanel, 60 );
lv_obj_set_align( ui_fillpanel, LV_ALIGN_TOP_MID );
lv_obj_clear_flag( ui_fillpanel, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

Expand All @@ -243,6 +256,75 @@ lv_obj_set_height( ui_videosettingdropdown, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_videosettingdropdown, LV_ALIGN_RIGHT_MID );
lv_obj_add_flag( ui_videosettingdropdown, LV_OBJ_FLAG_SCROLL_ON_FOCUS ); /// Flags

ui_hapticpanel = lv_obj_create(ui_settingspanel);
lv_obj_set_height( ui_hapticpanel, 50);
lv_obj_set_width( ui_hapticpanel, lv_pct(100));
lv_obj_set_x( ui_hapticpanel, 0 );
lv_obj_set_y( ui_hapticpanel, 120 );
lv_obj_set_align( ui_hapticpanel, LV_ALIGN_TOP_MID );
lv_obj_clear_flag( ui_hapticpanel, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

ui_settingsscreen_Label5 = lv_label_create(ui_hapticpanel);
lv_obj_set_width( ui_settingsscreen_Label5, LV_SIZE_CONTENT); /// 1
lv_obj_set_height( ui_settingsscreen_Label5, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_settingsscreen_Label5, LV_ALIGN_LEFT_MID );
lv_label_set_text(ui_settingsscreen_Label5,"Haptics");

ui_settingsscreen_Panel1 = lv_obj_create(ui_hapticpanel);
lv_obj_set_width( ui_settingsscreen_Panel1, 140);
lv_obj_set_height( ui_settingsscreen_Panel1, 50);
lv_obj_set_x( ui_settingsscreen_Panel1, -30 );
lv_obj_set_y( ui_settingsscreen_Panel1, 0 );
lv_obj_set_align( ui_settingsscreen_Panel1, LV_ALIGN_RIGHT_MID );
lv_obj_clear_flag( ui_settingsscreen_Panel1, LV_OBJ_FLAG_SCROLLABLE ); /// Flags
lv_obj_set_style_border_color(ui_settingsscreen_Panel1, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
lv_obj_set_style_border_opa(ui_settingsscreen_Panel1, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

ui_hapticlabel = lv_label_create(ui_settingsscreen_Panel1);
lv_obj_set_width( ui_hapticlabel, LV_SIZE_CONTENT); /// 1
lv_obj_set_height( ui_hapticlabel, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_hapticlabel, LV_ALIGN_CENTER );
lv_label_set_text(ui_hapticlabel,"128");

ui_hapticdownbutton = lv_btn_create(ui_settingsscreen_Panel1);
lv_obj_set_width( ui_hapticdownbutton, 32);
lv_obj_set_height( ui_hapticdownbutton, 32);
lv_obj_set_align( ui_hapticdownbutton, LV_ALIGN_LEFT_MID );
lv_obj_add_flag( ui_hapticdownbutton, LV_OBJ_FLAG_SCROLL_ON_FOCUS ); /// Flags
lv_obj_clear_flag( ui_hapticdownbutton, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

ui_settingsscreen_Label7 = lv_label_create(ui_hapticdownbutton);
lv_obj_set_width( ui_settingsscreen_Label7, LV_SIZE_CONTENT); /// 1
lv_obj_set_height( ui_settingsscreen_Label7, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_settingsscreen_Label7, LV_ALIGN_CENTER );
lv_label_set_text(ui_settingsscreen_Label7, LV_SYMBOL_MINUS);

ui_hapticupbutton = lv_btn_create(ui_settingsscreen_Panel1);
lv_obj_set_width( ui_hapticupbutton, 32);
lv_obj_set_height( ui_hapticupbutton, 32);
lv_obj_set_align( ui_hapticupbutton, LV_ALIGN_RIGHT_MID );
lv_obj_add_flag( ui_hapticupbutton, LV_OBJ_FLAG_SCROLL_ON_FOCUS ); /// Flags
lv_obj_clear_flag( ui_hapticupbutton, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

ui_settingsscreen_Label6 = lv_label_create(ui_hapticupbutton);
lv_obj_set_width( ui_settingsscreen_Label6, LV_SIZE_CONTENT); /// 1
lv_obj_set_height( ui_settingsscreen_Label6, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_settingsscreen_Label6, LV_ALIGN_CENTER );
lv_label_set_text(ui_settingsscreen_Label6, LV_SYMBOL_PLUS);

ui_hapticplaybutton = lv_btn_create(ui_hapticpanel);
lv_obj_set_width( ui_hapticplaybutton, 32);
lv_obj_set_height( ui_hapticplaybutton, 32);
lv_obj_set_align( ui_hapticplaybutton, LV_ALIGN_RIGHT_MID );
lv_obj_add_flag( ui_hapticplaybutton, LV_OBJ_FLAG_SCROLL_ON_FOCUS ); /// Flags
lv_obj_clear_flag( ui_hapticplaybutton, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

ui_settingsscreen_Label8 = lv_label_create(ui_hapticplaybutton);
lv_obj_set_width( ui_settingsscreen_Label8, LV_SIZE_CONTENT); /// 1
lv_obj_set_height( ui_settingsscreen_Label8, LV_SIZE_CONTENT); /// 1
lv_obj_set_align( ui_settingsscreen_Label8, LV_ALIGN_CENTER );
lv_label_set_text(ui_settingsscreen_Label8, LV_SYMBOL_PLAY);

lv_obj_add_event_cb(ui_closebutton, ui_event_closebutton, LV_EVENT_ALL, NULL);

}
Expand Down
10 changes: 10 additions & 0 deletions components/gui/generated/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ extern lv_obj_t *ui_settingsscreen_Label3;
extern lv_obj_t *ui_fillpanel;
extern lv_obj_t *ui_settingsscreen_Label4;
extern lv_obj_t *ui_videosettingdropdown;
extern lv_obj_t *ui_hapticpanel;
extern lv_obj_t *ui_settingsscreen_Label5;
extern lv_obj_t *ui_settingsscreen_Panel1;
extern lv_obj_t *ui_hapticlabel;
extern lv_obj_t *ui_hapticdownbutton;
extern lv_obj_t *ui_settingsscreen_Label7;
extern lv_obj_t *ui_hapticupbutton;
extern lv_obj_t *ui_settingsscreen_Label6;
extern lv_obj_t *ui_hapticplaybutton;
extern lv_obj_t *ui_settingsscreen_Label8;



Expand Down
67 changes: 66 additions & 1 deletion components/gui/include/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ extern "C" {

class Gui {
public:
typedef std::function<void(void)> play_haptic_fn;
typedef std::function<void(int)> set_waveform_fn;
typedef std::function<void(int, int)> set_haptic_slot_fn;

struct Config {
play_haptic_fn play_haptic;
set_waveform_fn set_waveform;
std::shared_ptr<espp::Display> display;
espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN};
};

enum class VideoSetting { ORIGINAL, FIT, FILL, MAX_UNUSED };

Gui(const Config& config) : display_(config.display), logger_({.tag="Gui", .level=config.log_level}) {
Gui(const Config& config)
: play_haptic_(config.play_haptic),
set_waveform_(config.set_waveform),
display_(config.display),
logger_({.tag="Gui", .level=config.log_level}) {
init_ui();
// now start the gui updater task
using namespace std::placeholders;
Expand Down Expand Up @@ -182,6 +192,30 @@ class Gui {
lv_img_set_src(ui_boxart, boxarts_[focused_rom_].c_str());
}

void set_haptic_waveform(int new_waveform) {
if (new_waveform > 123) {
new_waveform = 1;
} else if (new_waveform <= 0) {
new_waveform = 123;
}
haptic_waveform_ = new_waveform;
set_waveform_(haptic_waveform_);
update_haptic_waveform_label();
}

void next_haptic_waveform() {
set_haptic_waveform(haptic_waveform_ + 1);
}

void previous_haptic_waveform() {
set_haptic_waveform(haptic_waveform_ - 1);
}

void update_haptic_waveform_label() {
auto haptic_label = fmt::format("{}", haptic_waveform_);
lv_label_set_text(ui_hapticlabel, haptic_label.c_str());
}

protected:
void init_ui() {
ui_init();
Expand All @@ -201,11 +235,21 @@ class Gui {

lv_bar_set_value(ui_volumebar, audio_level_, LV_ANIM_OFF);

// rom screen navigation
lv_obj_add_event_cb(ui_settingsbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
lv_obj_add_event_cb(ui_playbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));

// volume settings
lv_obj_add_event_cb(ui_volumeupbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
lv_obj_add_event_cb(ui_volumedownbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
lv_obj_add_event_cb(ui_mutebutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));

// haptic settings
lv_obj_add_event_cb(ui_hapticdownbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
lv_obj_add_event_cb(ui_hapticupbutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
lv_obj_add_event_cb(ui_hapticplaybutton, &Gui::event_callback, LV_EVENT_PRESSED, static_cast<void*>(this));
// ensure the waveform is set and the ui is updated
set_haptic_waveform(haptic_waveform_);
}

void update(std::mutex& m, std::condition_variable& cv) {
Expand Down Expand Up @@ -251,6 +295,7 @@ class Gui {
// TODO: DO SOMETHING HERE!
return;
}
// volume controls
bool is_volume_up_button = (target == ui_volumeupbutton);
if (is_volume_up_button) {
set_audio_level(audio_level_ + 10);
Expand All @@ -266,6 +311,22 @@ class Gui {
toggle_mute();
return;
}
// haptic controls
bool is_haptic_up_button = (target == ui_hapticupbutton);
if (is_haptic_up_button) {
next_haptic_waveform();
return;
}
bool is_haptic_down_button = (target == ui_hapticdownbutton);
if (is_haptic_down_button) {
previous_haptic_waveform();
return;
}
bool is_hapticplay_button = (target == ui_hapticplaybutton);
if (is_hapticplay_button) {
play_haptic_();
return;
}
// or is it the play button?
bool is_play_button = (target == ui_playbutton);
if (is_play_button) {
Expand All @@ -290,6 +351,10 @@ class Gui {
lv_anim_t rom_label_animation_template_;
lv_style_t rom_label_style_;

play_haptic_fn play_haptic_;
set_waveform_fn set_waveform_;
std::atomic<int> haptic_waveform_{16}; // for the DRV2605, this is a 1s alert

std::atomic<bool> ready_to_play_{false};
std::atomic<bool> paused_{false};
std::shared_ptr<espp::Display> display_;
Expand Down
16 changes: 10 additions & 6 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ extern "C" void app_main(void) {
.write = write_drv,
.read = read_drv,
});

// we're using an ERM motor, so select an ERM library.
haptic_motor.select_library(1);
// we want strong click (for when user selects rom)
haptic_motor.set_waveform(0, Drv2605::Waveform::STRONG_CLICK);
haptic_motor.set_waveform(1, Drv2605::Waveform::END);
// let the user know we're booting up
haptic_motor.start();

auto play_haptic = [&haptic_motor]() {
haptic_motor.start();
};
auto set_waveform = [&haptic_motor](int waveform) {
haptic_motor.set_waveform(0, (Drv2605::Waveform)waveform);
haptic_motor.set_waveform(1, Drv2605::Waveform::END);
};

fmt::print("initializing gui...\n");
// initialize the gui
Gui gui({
.play_haptic = play_haptic,
.set_waveform = set_waveform,
.display = display,
.log_level = espp::Logger::Verbosity::WARN
});
Expand Down
Loading

0 comments on commit 1a84c7f

Please sign in to comment.