Skip to content

Commit

Permalink
Add Night Mode for the Display UI (#28)
Browse files Browse the repository at this point in the history
* Add Night Mode for the Display UI
* Various network related fixes to make it stable.
* Made SD Card support compile time option.
  • Loading branch information
dk307 committed Jun 10, 2023
1 parent af05c0a commit 37b0cf1
Show file tree
Hide file tree
Showing 35 changed files with 220 additions and 77 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"idf.openOcdConfigs": [
"board/esp32s3-bridge.cfg"
],
"idf.portWin": "COM4",
"idf.portWin": "COM3",
"idf.flashType": "UART",
"files.associations": {
"sdkconfig.h": "c",
Expand Down Expand Up @@ -89,7 +89,10 @@
"esp_system.h": "c",
"hap.h": "c",
"ui.h": "c",
"sps30_sensor_device.h": "c"
"sps30_sensor_device.h": "c",
"csetjmp": "cpp",
"unordered_set": "cpp",
"esp_http_server.h": "c"
},
"C_Cpp.errorSquiggles": "disabled"
}
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ gulp.task('display-fonts-big-font', function() {

gulp.task('display-fonts-big-font-dual', function() {
return font_create(
8, 118,
8, 117,
'./node_modules/@fontsource/montserrat/files/montserrat-all-400-normal.woff',
'big_panel_font_dual.c', '--symbols="0,1,2,3,4,5,6,7,8,9,-"');
});
Expand Down
6 changes: 5 additions & 1 deletion main/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ config SCD30_SENSOR_TEMPERATURE_OFFSET
int "Temperature offset for SCD30 in increment of 0.01C"
default 0
depends on SCD30_SENSOR_ENABLE


config ENABLE_SD_CARD_SUPPORT
bool "Enable SD Card Support"
default n

endmenu
13 changes: 9 additions & 4 deletions main/hardware/display/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ void display::begin()

ESP_LOGI(DISPLAY_TAG, "Display initialized width:%ld height:%ld", screenWidth, screenHeight);

current_brightness_ = display_device_.getBrightness();

ESP_LOGI(DISPLAY_TAG, "LV initialized");
const int buffer_size = 80;

Expand Down Expand Up @@ -140,6 +138,14 @@ void display::gui_task()

if (result == pdPASS)
{
if (notification_value & update_brightness_device_bit)
{
const auto night_brightness = 50;
ESP_LOGI(DISPLAY_TAG, "Setting display brightness to %d", current_brightness_);
ui_instance_.set_day_or_night_theme(current_brightness_ <= night_brightness);
display_device_.setBrightness(std::max<uint8_t>(10, current_brightness_));
}

if (notification_value & task_notify_wifi_changed_bit)
{
ui_instance_.wifi_changed();
Expand Down Expand Up @@ -212,8 +218,7 @@ void display::set_screen_brightness(uint8_t value)
{
if (current_brightness_ != value)
{
ESP_LOGI(DISPLAY_TAG, "Setting display brightness to %d", value);
display_device_.setBrightness(std::max<uint8_t>(30, value));
current_brightness_ = value;
xTaskNotify(lvgl_task_.handle(), update_brightness_device_bit, eSetBits);
}
}
3 changes: 2 additions & 1 deletion main/hardware/display/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class display final : public esp32::singleton<display>
LGFX display_device_;
esp32::task lvgl_task_;
ui_interface &ui_interface_;
uint8_t current_brightness_{128};

lv_disp_draw_buf_t draw_buf_{};
lv_disp_drv_t disp_drv_{};
Expand All @@ -39,7 +40,6 @@ class display final : public esp32::singleton<display>
ui ui_instance_;
esp32::default_event_subscriber instance_app_common_event_{
APP_COMMON_EVENT, ESP_EVENT_ANY_ID, [this](esp_event_base_t base, int32_t event, void *data) { app_event_handler(base, event, data); }};
uint8_t current_brightness_{0};

static void IRAM_ATTR display_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
static void IRAM_ATTR touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data);
Expand All @@ -51,4 +51,5 @@ class display final : public esp32::singleton<display>
constexpr static uint32_t task_notify_restarting_bit = BIT(total_sensors + 3);
constexpr static uint32_t config_changed_bit = BIT(total_sensors + 4);
constexpr static uint32_t idenitfy_device_bit = BIT(total_sensors + 5);
constexpr static uint32_t update_brightness_device_bit = BIT(total_sensors + 6);
};
2 changes: 2 additions & 0 deletions main/hardware/sd_card.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hardware/sd_card.h"
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
#include "logging/logging_tags.h"
#include "util/exceptions.h"
#include "util/helper.h"
Expand Down Expand Up @@ -101,3 +102,4 @@ std::string sd_card::get_info()
return "No card found";
}
}
#endif
4 changes: 3 additions & 1 deletion main/hardware/sd_card.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
#include "util/singleton.h"
#include <sdmmc_cmd.h>
#include <string>
Expand All @@ -25,4 +26,5 @@ class sd_card final : public esp32::singleton<sd_card>
friend class esp32::singleton<sd_card>;

sdmmc_card_t *sd_card_{nullptr};
};
};
#endif
10 changes: 10 additions & 0 deletions main/logging/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ class esp32_hook
std::vector<logger_hook_sink *> sinks_;
};

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
logger::logger(sd_card &sd_card) : sd_card_(sd_card)
#else
logger::logger()
#endif
{
esp_register_shutdown_handler(logging_shutdown_handler);
}
Expand All @@ -106,12 +110,15 @@ void logger::logging_shutdown_handler()
std::lock_guard<esp32::semaphore> lock(logger::get_instance().hook_mutex_);
ESP_LOGI(LOGGING_TAG, "Flushing custom loggers");

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
if (logger::get_instance().sd_card_sink_instance_)
{
logger::get_instance().sd_card_sink_instance_->flush();
}
#endif
}

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
bool logger::enable_sd_logging()
{
std::lock_guard<esp32::semaphore> lock(hook_mutex_);
Expand All @@ -136,6 +143,7 @@ bool logger::enable_sd_logging()

return true;
}
#endif

bool logger::enable_web_logging(const std::function<void(std::unique_ptr<std::string>)> &callbackP)
{
Expand Down Expand Up @@ -174,11 +182,13 @@ template <class T> void logger::remove_sink(std::unique_ptr<T> &p)
}
}

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
void logger::disable_sd_logging()
{
remove_sink(sd_card_sink_instance_);
ESP_LOGI(LOGGING_TAG, "Disabled sd card logging");
}
#endif

void logger::disable_web_logging()
{
Expand Down
11 changes: 9 additions & 2 deletions main/logging/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class sd_card;
class logger : public esp32::singleton<logger>
{
public:
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
bool enable_sd_logging();
void disable_sd_logging();
#endif
bool enable_web_logging(const std::function<void(std::unique_ptr<std::string>)> &callbackP);
void disable_web_logging();

Expand All @@ -31,15 +33,20 @@ class logger : public esp32::singleton<logger>
}

private:
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
logger(sd_card &sd_card);
friend class esp32::singleton<logger>;

sd_card &sd_card_;
#else
logger();
#endif
friend class esp32::singleton<logger>;

static void logging_shutdown_handler();

esp32::semaphore hook_mutex_;
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
std::unique_ptr<sd_card_sink> sd_card_sink_instance_;
#endif
std::unique_ptr<web_callback_sink> web_callback_sink_instance_;
std::unique_ptr<esp32_hook> hook_instance_{nullptr};
std::vector<std::string> logging_tags;
Expand Down
4 changes: 4 additions & 0 deletions main/logging/sd_card_sink.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT

#include "hardware/sd_card.h"
#include "logger_hook_sink.h"
#include "logging/logging_tags.h"
Expand Down Expand Up @@ -122,3 +124,5 @@ class sd_card_sink final : public logger_hook_sink
std::vector<char, esp32::psram::allocator<char>> fs_buffer_;
esp32::task background_log_task_;
};

#endif
2 changes: 1 addition & 1 deletion main/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@
#endif /*LV_USE_THEME_DEFAULT*/

/*A very simple theme that is a good starting point for a custom theme*/
#define LV_USE_THEME_BASIC 1
#define LV_USE_THEME_BASIC 0

/*A theme designed for monochrome displays*/
#define LV_USE_THEME_MONO 0
Expand Down
18 changes: 16 additions & 2 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "freertos/task.h"
#include "hardware/display/display.h"
#include "hardware/hardware.h"
#include "hardware/sd_card.h"
#include "homekit/homekit_integration.h"
#include "logging/logger.h"
#include "logging/logging_tags.h"
Expand All @@ -18,6 +17,10 @@
#include <nvs_flash.h>
#include <stdio.h>

#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
#include "hardware/sd_card.h"
#endif

ESP_EVENT_DEFINE_BASE(APP_COMMON_EVENT);

extern "C" void app_main(void)
Expand All @@ -38,21 +41,32 @@ extern "C" void app_main(void)
CHECK_THROW_ESP(esp_event_loop_create_default());

auto &config = config::create_instance();
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
auto &sd_card = sd_card::create_instance();
#endif
auto &wifi_manager = wifi_manager::create_instance(config);
auto &ui_interface = ui_interface::create_instance();
auto &display = display::create_instance(config, ui_interface);
auto &hardware = hardware::create_instance(config, display);
auto &homekit_integration = homekit_integration::create_instance(config, hardware);
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
auto &logger = logger::create_instance(sd_card);
#else
auto &logger = logger::create_instance();
#endif
auto &web_server = web_server::create_instance(config, ui_interface, logger);

// update later to account for circular dependency
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
ui_interface.update(config, sd_card, hardware, wifi_manager, homekit_integration);

#else
ui_interface.update(config, hardware, wifi_manager, homekit_integration);
#endif
// order is important
config.begin();
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
sd_card.begin();
#endif
display.begin();
wifi_manager.begin();
hardware.begin();
Expand Down
22 changes: 22 additions & 0 deletions main/ui/ui2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void ui::load_boot_screen()
#endif
lv_disp_t *dispp = lv_disp_get_default();

// most of ui uses this theme except main & detail sensor screen
lv_theme_t *theme = lv_theme_default_init(dispp, lv_palette_main(LV_PALETTE_GREEN), lv_palette_main(LV_PALETTE_LIME), true, LV_FONT_DEFAULT);

lv_disp_set_theme(dispp, theme);
Expand Down Expand Up @@ -81,6 +82,22 @@ void ui::init()
ESP_LOGI(UI_TAG, "UI screens initialized");
}

void ui::set_day_or_night_theme(bool night_mode)
{
if (night_theme_ != night_mode)
{
ESP_LOGI(UI_TAG, "Setting theme :%d", night_mode);
night_theme_ = night_mode;
main_screen_.theme_changed();
sensor_detail_screen_.theme_changed();
settings_screen_.theme_changed();
launcher_screen_.theme_changed();
hardware_screen_.theme_changed();
homekit_screen_.theme_changed();
wifi_enroll_screen_.theme_changed();
}
}

void ui::init_no_wifi_image()
{
LV_IMG_DECLARE(nowifi_png_img);
Expand Down Expand Up @@ -191,6 +208,11 @@ void ui::wifi_changed()
}
}

bool ui::is_night_theme_enabled()
{
return night_theme_;
}

void ui::show_home_screen()
{
main_screen_.show_screen();
Expand Down
6 changes: 5 additions & 1 deletion main/ui/ui2.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "util/noncopyable.h"
#include <lvgl.h>

class ui : public ui_inter_screen_interface, esp32::noncopyable
class ui final : public ui_inter_screen_interface, esp32::noncopyable
{
public:
ui(config &config, ui_interface &ui_interface_) : config_(config), ui_interface_instance_(ui_interface_)
Expand All @@ -29,6 +29,7 @@ class ui : public ui_inter_screen_interface, esp32::noncopyable
void wifi_changed();

// ui_inter_screen_interface
bool is_night_theme_enabled() override;
void show_home_screen() override;
void show_setting_screen() override;
void show_sensor_detail_screen(sensor_id_index index) override;
Expand All @@ -37,9 +38,12 @@ class ui : public ui_inter_screen_interface, esp32::noncopyable
void show_homekit_screen() override;
void show_wifi_enroll_screen() override;

void set_day_or_night_theme(bool night_mode);

private:
config &config_;
ui_interface &ui_interface_instance_;
bool night_theme_{false};

constexpr static uint32_t top_message_timer_period = 10000;

Expand Down
1 change: 1 addition & 0 deletions main/ui/ui_inter_screen_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class ui_inter_screen_interface
{
public:
virtual bool is_night_theme_enabled() = 0;
virtual void show_home_screen() = 0;
virtual void show_setting_screen() = 0;
virtual void show_sensor_detail_screen(sensor_id_index index) = 0;
Expand Down
4 changes: 4 additions & 0 deletions main/ui/ui_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ ui_interface::information_table_type ui_interface::get_information_table(informa
switch (type)
{
case information_type::system:
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
configASSERT(sd_card_);
#endif
configASSERT(hardware_);
return {
{"Version", get_version()},
Expand All @@ -238,7 +240,9 @@ ui_interface::information_table_type ui_interface::get_information_table(informa
{"Uptime", get_up_time()},
{"Reset Reason", get_reset_reason_string()},
{"Mac Address", get_default_mac_address()},
#ifdef CONFIG_ENABLE_SD_CARD_SUPPORT
{"SD Card", sd_card_->get_info()},
#endif
{"SPS30 sensor status", hardware_->get_sps30_error_register_status()},
};

Expand Down
Loading

0 comments on commit 37b0cf1

Please sign in to comment.