Skip to content

Commit

Permalink
Cleaning and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Mahoudeau committed Sep 10, 2024
1 parent b79a4d5 commit b0a53d7
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 29 deletions.
2 changes: 2 additions & 0 deletions lib/graphics/src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lib/gui/src/elements/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions lib/system/FileConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" !
Expand Down
10 changes: 5 additions & 5 deletions lib/system/libsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@

#include "base64.hpp"

namespace libsystem {
std::vector<std::string> bootErrors;
DeviceMode deviceMode = NORMAL;
auto systemConfig = FileConfig(storage::Path("system/config.bfc"));
}
std::vector<std::string> bootErrors;
libsystem::DeviceMode deviceMode = libsystem::NORMAL;
auto systemConfig = libsystem::FileConfig(storage::Path("system/config.bfc"));

class Restart final : public std::exception {
public:
Expand All @@ -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
Expand Down
178 changes: 158 additions & 20 deletions lib/system/libsystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#ifndef LIBSYSTEM_HPP
#define LIBSYSTEM_HPP

#include <string>
#include <cstdint>
#include <stdexcept>
#include <string>

#include <FileConfig.hpp>

Expand All @@ -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
15 changes: 12 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <gsm.hpp>
#include <app.hpp>
#include <contacts.hpp>
#include <FileConfig.hpp>
#include <iostream>
#include <libsystem.hpp>
#include <GuiManager.hpp>
Expand Down Expand Up @@ -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<uint8_t>("settings.brightness", 69);
systemConfig.write();
}

libsystem::log("settings.brightness: " + std::to_string(systemConfig.get<uint8_t>("settings.brightness")));

graphics::setBrightness(systemConfig.get<uint8_t>("settings.brightness"));

// Init launcher
applications::launcher::init();

Expand Down

0 comments on commit b0a53d7

Please sign in to comment.