diff --git a/lib/graphics/src/graphics.cpp b/lib/graphics/src/graphics.cpp index 4886c4b2..9cdff7e3 100644 --- a/lib/graphics/src/graphics.cpp +++ b/lib/graphics/src/graphics.cpp @@ -55,6 +55,8 @@ void graphics::setBrightness(uint16_t value, const bool temp) brightness = value; } + libsystem::log("Brightness: " + std::to_string(value)); + #ifdef ESP_PLATFORM static uint16_t oldValue = 0; diff --git a/lib/gui/src/elements/Image.cpp b/lib/gui/src/elements/Image.cpp index de5ce499..58cc8711 100644 --- a/lib/gui/src/elements/Image.cpp +++ b/lib/gui/src/elements/Image.cpp @@ -56,7 +56,8 @@ namespace gui::ImagesList { for (auto img = images.begin(); img != images.end();) { - if (img->surface.use_count() == 1) + // TODO: Refactor this logic + if (img->surface.unique()) { img = images.erase(img); //std::cout << "[Image] image deleted" << std::endl; diff --git a/lib/system/FileConfig.hpp b/lib/system/FileConfig.hpp index 5e0e6eda..815302ff 100644 --- a/lib/system/FileConfig.hpp +++ b/lib/system/FileConfig.hpp @@ -17,8 +17,13 @@ namespace libsystem { /** + * @brief Class to store user preferences in a binary format. + * * File config is a binary file format (.bfc). * It is used to store simple data like user preferences (E.g. screen brightness). + * Should be faster than JSON. + * + * @todo Add lists support. */ class FileConfig { // Need to match "FileConfig::Type" ! diff --git a/lib/system/libsystem.cpp b/lib/system/libsystem.cpp index 7ae0478e..955c8a19 100644 --- a/lib/system/libsystem.cpp +++ b/lib/system/libsystem.cpp @@ -22,11 +22,9 @@ #include "base64.hpp" -namespace libsystem { - std::vector bootErrors; - DeviceMode deviceMode = NORMAL; - auto systemConfig = FileConfig(storage::Path("system/config.bfc")); -} +std::vector bootErrors; +libsystem::DeviceMode deviceMode = libsystem::NORMAL; +auto systemConfig = libsystem::FileConfig(storage::Path("system/config.bfc")); class Restart final : public std::exception { public: @@ -52,8 +50,10 @@ std::string hexToString(const uint32_t hex) { void libsystem::panic(const std::string &message, const bool restart) { setScreenOrientation(graphics::PORTRAIT); +#ifdef ESP_PLATFORM const uint16_t screenWidth = graphics::getScreenWidth(); const uint16_t screenHeight = graphics::getScreenHeight(); +#endif LGFX* lcd = graphics::getLCD(); #ifdef ESP_PLATFORM diff --git a/lib/system/libsystem.hpp b/lib/system/libsystem.hpp index 8ea424ee..9a2213aa 100644 --- a/lib/system/libsystem.hpp +++ b/lib/system/libsystem.hpp @@ -5,9 +5,9 @@ #ifndef LIBSYSTEM_HPP #define LIBSYSTEM_HPP -#include #include #include +#include #include @@ -20,49 +20,187 @@ #define OS_VERSION_BUILD "0" #define OS_VERSION_CODENAME "Red Panic" -#define OS_VERSION OS_VERSION_MAJOR "." OS_VERSION_MINOR "." OS_VERSION_PATCH "-" OS_VERSION_BUILD " (" OS_VERSION_CODENAME ")" +#define OS_VERSION \ + OS_VERSION_MAJOR "." OS_VERSION_MINOR "." OS_VERSION_PATCH "-" OS_VERSION_BUILD " (" OS_VERSION_CODENAME ")" +/** + * @brief System interactions. + * + * libsystem contains useful functions and classes to manage the system. + */ namespace libsystem { + /** + * @brief Delay the current thread. + * + * @param ms The duration of the delay in milliseconds. + */ void delay(uint64_t ms); - void panic(const std::string& message, bool restart = true); - - void log(const std::string& message); - void registerBootError(const std::string& message); + /** + * @brief Make the OS panic and show a screen with detailed data. + * + * @param message The message to show. + * @param restart If true, the device will restart safely. + */ + void panic(const std::string &message, bool restart = true); + + /** + * @brief Print a message in the console. + * + * @param message The log message. + * + * @todo Save logs. + */ + void log(const std::string &message); + + /** + * @defgroup boot_errors Error management at boot time. + * + * Manage and display errors before the OS is fully started. + * + * @{ + */ + + /** + * @brief Register a new boot error. + * + * @param message The boot error message. + */ + void registerBootError(const std::string &message); + + /** + * @brief Display boot errors. + * + * Display every boot errors registered, halts the OS. + */ void displayBootErrors(); + + /** + * @brief Returns true if a boot error has been registered. + * + * @return True if a boot error has been registered. + */ bool hasBootErrors(); + /** + * @} + */ + + /** + * @brief Restart the device. + * + * The "saveBacktrace" parameter doesn't restart the device safely, + * but makes it crash to force a backtrace creation. + * + * @param silent Unknown behavior. + * @param timeout Delay the restart, in milliseconds. + * @param saveBacktrace If true, a backtrace will be created. + */ void restart(bool silent = false, uint64_t timeout = 0, bool saveBacktrace = false); - enum DeviceMode { - NORMAL, - SLEEP - }; - + /** + * @brief Different device state, unique. + */ + enum DeviceMode { NORMAL, SLEEP }; + + /** + * @brief Set the device mode. + * + * Set the device mode and apply the feature of the mode. + * + * @param mode The new mode to apply. + */ void setDeviceMode(DeviceMode mode); + + /** + * @brief Get the current device mode. + * + * @return The current device mode. + */ [[nodiscard]] DeviceMode getDeviceMode(); + /** + * @brief Get the system FileConfig. + * + * Use this FileConfig to store system related preferences, like settings. + * Please use another FileConfig to store user data. + * + * @return The system FileConfig. + */ FileConfig getSystemConfig(); + /** + * @brief Wrapper for handled exceptions. + * + * Please use these exception instead of standard ones. + */ namespace exceptions { + /** + * @brief Wrapper for std::runtime_error. + * + * Standard exception, but calls an OS panic. + */ class RuntimeError final : public std::runtime_error { public: - explicit RuntimeError(const std::string& message); - explicit RuntimeError(const char* message); + /** + * @brief Throw a new std::runtime_error. + * + * @param message The message to display. + */ + explicit RuntimeError(const std::string &message); + + /** + * @brief Throw a new std::runtime_error. + * + * @param message The message to display. + */ + explicit RuntimeError(const char *message); }; + /** + * @brief Wrapper for std::out_of_range. + * + * Standard exception, but calls an OS panic. + */ class OutOfRange final : public std::out_of_range { public: - explicit OutOfRange(const std::string& message); - explicit OutOfRange(const char* message); + /** + * @brief Throw a new std::out_of_range. + * + * @param message The message to display. + */ + explicit OutOfRange(const std::string &message); + + /** + * @brief Throw a new std::out_of_range. + * + * @param message The message to display. + */ + explicit OutOfRange(const char *message); }; + /** + * @brief Wrapper for std::invalid_argument. + * + * Standard exception, but calls an OS panic. + */ class InvalidArgument final : public std::invalid_argument { public: - explicit InvalidArgument(const std::string& message); - explicit InvalidArgument(const char* message); + /** + * @brief Throw a new std::invalid_argument. + * + * @param message The message to display. + */ + explicit InvalidArgument(const std::string &message); + + /** + * @brief Throw a new std::invalid_argument. + * + * @param message The message to display. + */ + explicit InvalidArgument(const char *message); }; - } -} + } // namespace exceptions +} // namespace libsystem -#endif //LIBSYSTEM_HPP +#endif // LIBSYSTEM_HPP diff --git a/src/main.cpp b/src/main.cpp index 3afbcc39..45321897 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -168,12 +169,20 @@ void setup() ); #endif // ESP_PLATFORM - // Positionnement de l'écran en mode Portrait - graphics::setScreenOrientation(graphics::PORTRAIT); - // Init de la gestiuon des Threads ThreadManager::init(); + libsystem::FileConfig systemConfig = libsystem::getSystemConfig(); + + if (!systemConfig.has("settings.brightness")) { + systemConfig.set("settings.brightness", 69); + systemConfig.write(); + } + + libsystem::log("settings.brightness: " + std::to_string(systemConfig.get("settings.brightness"))); + + graphics::setBrightness(systemConfig.get("settings.brightness")); + // Init launcher applications::launcher::init();