Skip to content

Commit

Permalink
Colors of my life.
Browse files Browse the repository at this point in the history
  • Loading branch information
ballaswag committed Apr 27, 2024
1 parent ddfb242 commit 1b4e472
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 14 deletions.
1 change: 1 addition & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ASSET_NAME=$1
cp ./build/bin/guppyscreen $RELEASES_DIR/guppyscreen
cp -r ./k1/k1_mods $RELEASES_DIR
cp -r ./k1/scripts $RELEASES_DIR
cp -r ./themes $RELEASES_DIR
cp ./installer.sh $RELEASES_DIR
cp ./update.sh $RELEASES_DIR
if [ -f ./custom_upgrade.sh ]; then
Expand Down
4 changes: 4 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ std::string Config::get_wifi_interface() {
.string();
}

std::string Config::get_path() {
return path;
}

json &Config::get_json(const std::string &json_path) {
return data[json::json_pointer(json_path)];
}
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Config {
std::string& df();
std::string get_thumbnail_path();
std::string get_wifi_interface();
std::string get_path();

static Config *get_instance();

Expand Down
46 changes: 43 additions & 3 deletions src/guppyscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#include "printer_select_panel.h"
#include "spdlog/spdlog.h"
#include "state.h"
#include "theme.h"

GuppyScreen *GuppyScreen::instance = NULL;
lv_style_t GuppyScreen::style_container;
lv_style_t GuppyScreen::style_imgbtn_default;
lv_style_t GuppyScreen::style_imgbtn_pressed;
lv_style_t GuppyScreen::style_imgbtn_disabled;
lv_theme_t GuppyScreen::th_new;
Expand Down Expand Up @@ -46,7 +48,7 @@ GuppyScreen *GuppyScreen::get() {
return instance;
}

GuppyScreen *GuppyScreen::init(std::function<void()> hal_init) {
GuppyScreen *GuppyScreen::init(std::function<void(lv_color_t, lv_color_t)> hal_init) {
hlog_disable();

// config
Expand All @@ -57,6 +59,22 @@ GuppyScreen *GuppyScreen::init(std::function<void()> hal_init) {
? "debug"
: conf->get<std::string>(ll_path));

auto selected_theme = conf->get_json("/theme").empty()
? "blue.json"
: conf->get<std::string>("/theme") + ".json";
auto theme_config = fs::canonical(conf->get_path()).parent_path() / "themes" / selected_theme;

ThemeConfig *theme_conf = ThemeConfig::get_instance();
theme_conf->init(theme_config);

auto primary_color = theme_conf->get_json("/primary_color").empty()
? lv_color_hex(0x2196F3)
: lv_color_hex(std::stoul(theme_conf->get<std::string>("/primary_color"), nullptr, 16));

auto secondary_color = theme_conf->get_json("/secondary_color").empty()
? lv_color_hex(0xF44336)
: lv_color_hex(std::stoul(theme_conf->get<std::string>("/secondary_color"), nullptr, 16));

#ifndef OS_ANDROID
auto console_sink = std::make_shared<spdlog::sinks::stdout_sink_mt>();
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
Expand Down Expand Up @@ -89,16 +107,20 @@ GuppyScreen *GuppyScreen::init(std::function<void()> hal_init) {
fbdev_unblank();
#endif // OS_ANDROID

hal_init();
hal_init(primary_color, secondary_color);
lv_png_init();

lv_style_init(&style_container);
lv_style_set_border_width(&style_container, 0);
lv_style_set_radius(&style_container, 0);

// lv_style_init(&style_imgbtn_default);
// lv_style_set_img_recolor_opa(&style_imgbtn_default, LV_OPA_100);
// lv_style_set_img_recolor(&style_imgbtn_default, lv_color_black());

lv_style_init(&style_imgbtn_pressed);
lv_style_set_img_recolor_opa(&style_imgbtn_pressed, LV_OPA_100);
lv_style_set_img_recolor(&style_imgbtn_pressed, lv_palette_main(LV_PALETTE_BLUE));
lv_style_set_img_recolor(&style_imgbtn_pressed, primary_color);

lv_style_init(&style_imgbtn_disabled);
lv_style_set_img_recolor_opa(&style_imgbtn_disabled, LV_OPA_100);
Expand Down Expand Up @@ -223,6 +245,7 @@ void GuppyScreen::new_theme_apply_cb(lv_theme_t *th, lv_obj_t *obj) {
}

if (lv_obj_check_type(obj, &lv_imgbtn_class)) {
// lv_obj_add_style(obj, &style_imgbtn_default, LV_STATE_DEFAULT);
lv_obj_add_style(obj, &style_imgbtn_pressed, LV_STATE_PRESSED);
lv_obj_add_style(obj, &style_imgbtn_disabled, LV_STATE_DISABLED);
}
Expand All @@ -241,6 +264,23 @@ void GuppyScreen::save_calibration_coeff(lv_tc_coeff_t coeff) {
conf->save();
}

void GuppyScreen::refresh_theme() {
lv_theme_t *th = lv_theme_default_get();
ThemeConfig *theme_conf = ThemeConfig::get_instance();
auto primary_color = theme_conf->get_json("/primary_color").empty()
? lv_color_hex(0x2196F3)
: lv_color_hex(std::stoul(theme_conf->get<std::string>("/primary_color"), nullptr, 16));

auto secondary_color = theme_conf->get_json("/secondary_color").empty()
? lv_color_hex(0xF44336)
: lv_color_hex(std::stoul(theme_conf->get<std::string>("/secondary_color"), nullptr, 16));

lv_disp_t *disp = lv_disp_get_default();
lv_theme_t * new_theme = lv_theme_default_init(disp, primary_color, secondary_color, true, th->font_normal);
lv_disp_set_theme(disp, new_theme);
lv_style_set_img_recolor(&style_imgbtn_pressed, primary_color);
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void) {
static uint64_t start_ms = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/guppyscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class GuppyScreen {
private:
static GuppyScreen *instance;
static lv_style_t style_container;
static lv_style_t style_imgbtn_default;
static lv_style_t style_imgbtn_pressed;
static lv_style_t style_imgbtn_disabled;
static lv_theme_t th_new;
Expand All @@ -40,11 +41,12 @@ class GuppyScreen {

void connect_ws(const std::string &url);
static GuppyScreen *get();
static GuppyScreen *init(std::function<void()> hal_init);
static GuppyScreen *init(std::function<void(lv_color_t, lv_color_t)> hal_init);
static void loop();
static void new_theme_apply_cb(lv_theme_t *th, lv_obj_t *obj);
static void handle_calibrated(lv_event_t *event);
static void save_calibration_coeff(lv_tc_coeff_t coeff);
static void refresh_theme();
};

#endif // __GUPPY_SCREEN_H__
14 changes: 7 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static int tick_thread(void *data);

#endif // SIMUALTOR

static void hal_init(void);
static void hal_init(lv_color_t p, lv_color_t s);

#include "guppyscreen.h"
#include "hv/hlog.h"
Expand Down Expand Up @@ -50,7 +50,7 @@ int main(void)

#ifndef SIMULATOR

static void hal_init(void) {
static void hal_init(lv_color_t primary, lv_color_t secondary) {
/*A small buffer for LittlevGL to draw the screen's content*/
static lv_color_t buf[DISP_BUF_SIZE];
static lv_color_t buf2[DISP_BUF_SIZE];
Expand Down Expand Up @@ -85,8 +85,8 @@ static void hal_init(void) {
spdlog::debug("resolution {} x {}", width, height);
lv_disp_t * disp = lv_disp_drv_register(&disp_drv);
lv_theme_t * th = height <= 480
? lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), true, &lv_font_montserrat_12)
: lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), true, &lv_font_montserrat_20);
? lv_theme_default_init(NULL, primary, secondary, true, &lv_font_montserrat_12)
: lv_theme_default_init(NULL, primary, secondary, true, &lv_font_montserrat_20);
lv_disp_set_theme(disp, th);

evdev_init();
Expand All @@ -113,7 +113,7 @@ static void hal_init(void) {
* Initialize the Hardware Abstraction Layer (HAL) for the LVGL graphics
* library
*/
static void hal_init(void)
static void hal_init(lv_color_t primary, lv_color_t secondary)
{
/* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/
sdl_init();
Expand Down Expand Up @@ -142,8 +142,8 @@ static void hal_init(void)

lv_disp_t * disp = lv_disp_drv_register(&disp_drv);
lv_theme_t * th = MONITOR_HOR_RES <= 480
? lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), true, &lv_font_montserrat_12)
: lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), true, &lv_font_montserrat_16);
? lv_theme_default_init(NULL, primary, secondary, true, &lv_font_montserrat_12)
: lv_theme_default_init(NULL, primary, secondary, true, &lv_font_montserrat_16);
lv_disp_set_theme(disp, th);

lv_group_t * g = lv_group_create();
Expand Down
2 changes: 1 addition & 1 deletion src/print_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ PrintPanel::PrintPanel(KWebSocketClient &websocket, std::mutex &lock, PrintStatu
lv_obj_set_flex_flow(file_table_btns, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(file_table_btns, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_END);

lv_obj_set_size(file_table, LV_PCT(100), LV_PCT(85));
lv_obj_set_size(file_table, LV_PCT(100), LV_PCT(100));
lv_table_set_col_width(file_table, 0, LV_PCT(100));
lv_table_set_col_cnt(file_table, 1);
lv_obj_add_event_cb(file_table, &PrintPanel::_handle_callback, LV_EVENT_ALL, this);
Expand Down
55 changes: 55 additions & 0 deletions src/sysinfo_panel.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include "sysinfo_panel.h"
#include "utils.h"
#include "config.h"
#include "theme.h"
#include "spdlog/spdlog.h"
#include "guppyscreen.h"

#include <algorithm>
#include <iterator>
#include <map>

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

LV_IMG_DECLARE(back);

#ifdef GUPPYSCREEN_VERSION
Expand All @@ -21,6 +27,15 @@ std::vector<std::string> SysInfoPanel::log_levels = {
"info"
};

std::vector<std::string> SysInfoPanel::themes = {
"blue",
"red",
"green",
"purple",
"pink",
"yellow"
};

static std::map<int32_t, uint32_t> sleepsec_to_dd_idx = {
{-1, 0}, // never
{300, 1}, // 5 min
Expand Down Expand Up @@ -62,6 +77,11 @@ SysInfoPanel::SysInfoPanel()
, z_icon_toggle_cont(lv_obj_create(left_cont))
, z_icon_toggle(lv_switch_create(z_icon_toggle_cont))

// log level
, theme_cont(lv_obj_create(left_cont))
, theme_dd(lv_dropdown_create(theme_cont))
, theme(0)

, back_btn(cont, &back, "Back", &SysInfoPanel::_handle_callback, this)
{
lv_obj_move_background(cont);
Expand Down Expand Up @@ -176,6 +196,30 @@ SysInfoPanel::SysInfoPanel()
lv_obj_add_event_cb(z_icon_toggle, &SysInfoPanel::_handle_callback,
LV_EVENT_VALUE_CHANGED, this);

// theme dropdown
lv_obj_set_size(theme_cont, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(theme_cont, 0, 0);
l = lv_label_create(theme_cont);
lv_label_set_text(l, "Theme Color");
lv_obj_align(l, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_align(theme_dd, LV_ALIGN_RIGHT_MID, 0, 0);

lv_dropdown_set_options(theme_dd, fmt::format("{}", fmt::join(themes, "\n")).c_str());

v = conf->get_json("/theme");
if (!v.is_null()) {
auto it = std::find(themes.begin(), themes.end(), v.template get<std::string>());
if (it != std::end(themes)) {
theme = std::distance(themes.begin(), it);
lv_dropdown_set_selected(theme_dd, theme);
}
} else {
lv_dropdown_set_selected(theme_dd, theme);
}
lv_obj_add_event_cb(theme_dd, &SysInfoPanel::_handle_callback,
LV_EVENT_VALUE_CHANGED, this);


lv_obj_add_flag(back_btn.get_container(), LV_OBJ_FLAG_FLOATING);
lv_obj_align(back_btn.get_container(), LV_ALIGN_BOTTOM_RIGHT, 0, 0);
}
Expand Down Expand Up @@ -249,6 +293,17 @@ void SysInfoPanel::handle_callback(lv_event_t *e)
bool inverted = lv_obj_has_state(z_icon_toggle, LV_STATE_CHECKED);
conf->set<bool>("/invert_z_icon", inverted);
conf->save();
} else if (obj == theme_dd) {
auto idx = lv_dropdown_get_selected(theme_dd);
if (idx != theme) {
theme = idx;
auto selected_theme = themes[theme];
conf->set<std::string>("/theme", selected_theme);
conf->save();
auto theme_config = fs::canonical(conf->get_path()).parent_path() / "themes" / (selected_theme + ".json");
ThemeConfig::get_instance()->init(theme_config);
GuppyScreen::refresh_theme();
}
}
}
}
8 changes: 6 additions & 2 deletions src/sysinfo_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ class SysInfoPanel {

lv_obj_t *z_icon_toggle_cont;
lv_obj_t *z_icon_toggle;


lv_obj_t *theme_cont;
lv_obj_t *theme_dd;
uint32_t theme;

ButtonContainer back_btn;

static std::vector<std::string> log_levels;

static std::vector<std::string> themes;
};

#endif //__SYSINFO_PANEL_H__
47 changes: 47 additions & 0 deletions src/theme.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "theme.h"
#include "platform.h"

#include <sys/stat.h>
#include <fstream>
#include <iomanip>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

ThemeConfig *ThemeConfig::instance{NULL};

ThemeConfig::ThemeConfig() {
}

ThemeConfig *ThemeConfig::get_instance() {
if (instance == NULL) {
instance = new ThemeConfig();
}
return instance;
}

void ThemeConfig::init(const std::string config_path) {
path = config_path;
struct stat buffer;

if (stat(config_path.c_str(), &buffer) == 0) {
data = json::parse(std::fstream(config_path));
} else {
data = {
{"primary_color", "0x2196F3"}, //blue
{"secondary_color", "0xF44336"} // red
};
}

std::ofstream o(config_path);
o << std::setw(2) << data << std::endl;
}

json &ThemeConfig::get_json(const std::string &json_path) {
return data[json::json_pointer(json_path)];
}

void ThemeConfig::save() {
std::ofstream o(path);
o << std::setw(2) << data << std::endl;
}
Loading

0 comments on commit 1b4e472

Please sign in to comment.