Skip to content

Commit

Permalink
Make globals in headers inline constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed Dec 21, 2024
1 parent 0288d18 commit 535c285
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 145 deletions.
86 changes: 43 additions & 43 deletions conf/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,171 +15,171 @@ namespace fabomatic
namespace conf::default_config
{
/// @brief Default MachineID for backend. Can be overriden through WiFi Portal config
static constexpr MachineID machine_id{1};
inline constexpr MachineID machine_id{1};

/// @brief Default machine name for LCD. Will be overriden with Backend config data
static constexpr std::string_view machine_name{"MACHINE1"};
inline constexpr std::string_view machine_name{"MACHINE1"};

/// @brief Default machine type. No impact on code
static constexpr MachineType machine_type{MachineType::Printer3D};
inline constexpr MachineType machine_type{MachineType::Printer3D};

/// @brief Default hostname for the ESP32 board, Machine ID will be added to the hostname in order to form unique hostnames
static constexpr std::string_view hostname{"BOARD"};
inline constexpr std::string_view hostname{"BOARD"};

} // namespace conf::default_config

/// @brief RFID-related settings
namespace conf::rfid_tags
{
/// @brief Number of bytes in RFID cards UID, may depend on specific RFID chip
static constexpr uint8_t UID_BYTE_LEN{4};
inline constexpr uint8_t UID_BYTE_LEN{4};
/// @brief Number of cached UID, persisted in flash
static constexpr uint8_t CACHE_LEN{10};
inline constexpr uint8_t CACHE_LEN{10};

} // namespace conf::rfid_tags

/// @brief Configuration for LCD pannel
namespace conf::lcd
{
/// @brief Number of rows for LCD display
static constexpr uint8_t ROWS{2};
inline constexpr uint8_t ROWS{2};

/// @brief Number of cols for LCD display
static constexpr uint8_t COLS{16};
inline constexpr uint8_t COLS{16};

/// @brief How much time shall we wait for a short message on LCD for user
static constexpr auto SHORT_MESSAGE_DELAY{1s};
inline constexpr auto SHORT_MESSAGE_DELAY{1s};

} // namespace conf::lcd

/// @brief Configuration for connected machine
namespace conf::machine
{
/// @brief User will be log out after this delay. If 0h, no auto-logout. This may be overriden by backend data
static constexpr auto DEFAULT_AUTO_LOGOFF_DELAY{12h};
inline constexpr auto DEFAULT_AUTO_LOGOFF_DELAY{12h};

/// @brief Idle time before poweroff. If 0min, machine will stay on.
static constexpr auto DEFAULT_GRACE_PERIOD{90s};
inline constexpr auto DEFAULT_GRACE_PERIOD{90s};

/// @brief Beeps will be heard every 30s when the machine is in grace period
static constexpr auto DELAY_BETWEEN_BEEPS{30s};
inline constexpr auto DELAY_BETWEEN_BEEPS{30s};

/// @brief If true, machine needing maintenance will be blocked for normal users
static constexpr bool MAINTENANCE_BLOCK{true};
inline constexpr bool MAINTENANCE_BLOCK{true};

/// @brief Minimum time to confirm by long tap maintenance
static constexpr auto LONG_TAP_DURATION{10s};
inline constexpr auto LONG_TAP_DURATION{10s};

/// @brief Disabled RFID reading after a successfull read for X seconds.
static constexpr auto DELAY_BETWEEN_SWEEPS{2s};
inline constexpr auto DELAY_BETWEEN_SWEEPS{2s};

} // namespace conf::machine

/// @brief Debug settings
namespace conf::debug
{
/// @brief True to add logs to serial output
static constexpr bool ENABLE_LOGS{true};
inline constexpr bool ENABLE_LOGS{true};

/// @brief True to add many logs regarding tasks scheduling and statistics
static constexpr bool ENABLE_TASK_LOGS{false};
inline constexpr bool ENABLE_TASK_LOGS{false};

/// @brief Serial speed in bauds
static constexpr unsigned long SERIAL_SPEED_BDS{115200};
inline constexpr unsigned long SERIAL_SPEED_BDS{115200};

/// @brief True to force portal startup. May be useful to override saved configuration
static constexpr bool FORCE_PORTAL{false};
inline constexpr bool FORCE_PORTAL{false};

/// @brief True to force EEPROM settings to defaults, regardless of actual values.
static constexpr bool LOAD_EEPROM_DEFAULTS{false};
inline constexpr bool LOAD_EEPROM_DEFAULTS{false};

/// @brief True if important MQTT messages should be saved when network is down and replayed.
static constexpr bool ENABLE_BUFFERING{true};
inline constexpr bool ENABLE_BUFFERING{true};

} // namespace conf::debug

/// @brief Configuration for buzzer
namespace conf::buzzer
{
/// @brief Single beep duration, typical value 200ms. Set to 0 to disable beeps.
static constexpr auto STANDARD_BEEP_DURATION{250ms};
inline constexpr auto STANDARD_BEEP_DURATION{250ms};

/// @brief Number of beeps every time the function is called
static constexpr auto NB_BEEPS{3};
inline constexpr auto NB_BEEPS{3};

} // namespace conf::buzzer

/// @brief Configuration related to tasks scheduling
namespace conf::tasks
{
/// @brief Task period to check for RFID badge (should be fast: 150ms)
static constexpr auto RFID_CHECK_PERIOD{150ms};
inline constexpr auto RFID_CHECK_PERIOD{150ms};

/// @brief Performs RFID self check and reset chip if necessary (default: 60s)
static constexpr auto RFID_SELFTEST_PERIOD{60s};
inline constexpr auto RFID_SELFTEST_PERIOD{60s};

/// @brief Query the MQTT broker for machine state at given period (default: 30s)
static constexpr auto MQTT_REFRESH_PERIOD{30s};
inline constexpr auto MQTT_REFRESH_PERIOD{30s};

/// @brief Timeout for hardware watchdog, set to 0s to disable (default: 60s)
static constexpr auto WATCHDOG_TIMEOUT{60s};
inline constexpr auto WATCHDOG_TIMEOUT{60s};

/// @brief Period of the watchdog signaling task (default 1s)
static constexpr auto WATCHDOG_PERIOD{1s};
inline constexpr auto WATCHDOG_PERIOD{1s};

/// @brief Timeout for portal configuration (default: 5min)
static constexpr auto PORTAL_CONFIG_TIMEOUT{5min};
inline constexpr auto PORTAL_CONFIG_TIMEOUT{5min};

/// @brief Board announcement on the MQTT server (default: 2min)
static constexpr auto MQTT_ALIVE_PERIOD{2min};
inline constexpr auto MQTT_ALIVE_PERIOD{2min};

} // namespace conf::tasks

/// @brief Configuration regarding MQTT broker, topics
namespace conf::mqtt
{
/// @brief Initial part of the topic, machine ID will be added
static constexpr std::string_view topic{"machine"};
inline constexpr std::string_view topic{"machine"};

/// @brief Backend reply (sub-topic of the full machine topic)
static constexpr std::string_view response_topic{"/reply"};
inline constexpr std::string_view response_topic{"/reply"};

/// @brief Backend requests (sub-topic of the full machine topic)
static constexpr std::string_view request_topic{"/request"};
inline constexpr std::string_view request_topic{"/request"};

/// @brief Number of tries to get a reply from the backend
static constexpr auto MAX_TRIES{1};
inline constexpr auto MAX_TRIES{1};

/// @brief Timeout for a single backend reply request.
static constexpr auto TIMEOUT_REPLY_SERVER{2s};
inline constexpr auto TIMEOUT_REPLY_SERVER{2s};

/// @brief Once backend is unresponsive, wait at least this period before to try again
static constexpr auto FAIL_FAST_PERIOD {45s};
inline constexpr auto FAIL_FAST_PERIOD {45s};

/// @brief MQTT port for broker
static constexpr auto PORT_NUMBER{1883};
inline constexpr auto PORT_NUMBER{1883};

/// @brief Name of the default server for Backend. Will be resolved through mDNS
static constexpr std::string_view mqtt_server{"fabpi2.local"};
inline constexpr std::string_view mqtt_server{"fabpi2.local"};

/// @brief In case Shelly is used, name of the topic on MQTT Broker
static constexpr std::string_view mqtt_switch_topic{""};
inline constexpr std::string_view mqtt_switch_topic{""};

/// @brief What value shall be written on the topic to switch on the Shelly device
static constexpr std::string_view mqtt_switch_on_message{"on"};
inline constexpr std::string_view mqtt_switch_on_message{"on"};

/// @brief What value shall be written on the topic to switch off the Shelly device
static constexpr std::string_view mqtt_switch_off_message{"off"};
inline constexpr std::string_view mqtt_switch_off_message{"off"};
} // namespace conf::mqtt

/// @brief Other compile-time settings
namespace conf::common
{
/// @brief Maximum length of saved string in WiFiManager portal.
static constexpr auto STR_MAX_LENGTH{40U};
inline constexpr auto STR_MAX_LENGTH{40U};

/// @brief Maximum length of saved integer in WiFiManager portal.
static constexpr auto INT_MAX_LENGTH{5U};
inline constexpr auto INT_MAX_LENGTH{5U};
}

// Checks on configured values
Expand Down
16 changes: 8 additions & 8 deletions conf/secrets.hpp.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
namespace fabomatic::secrets::cards
{
/// @brief Number of whitelisted card in the array below
static constexpr uint8_t LEN = 10U;
inline constexpr uint8_t LEN = 10U;

using WhiteListEntry = std::tuple<card::uid_t, FabUser::UserLevel, std::string_view>;
using WhiteList = std::array<WhiteListEntry, LEN>;

/// @brief Static list of whitelisted RFID cards.
/// @note If the network is down, these cards will be authorized. If network is up, backend prevails.
static constexpr WhiteList whitelist{
inline constexpr WhiteList whitelist{
std::make_tuple(0xAABBCCD1, FabUser::UserLevel::FabAdmin, "ABCDEFG"),
std::make_tuple(0xAABBCCD2, FabUser::UserLevel::FabAdmin, "PIPPO"),
std::make_tuple(0xAABBCCD3, FabUser::UserLevel::NormalUser, "USER1"),
Expand All @@ -38,18 +38,18 @@ namespace fabomatic::secrets::credentials
{

#if (PINS_WOKWI)
static constexpr std::string_view ssid = "Wokwi-GUEST"; /* WOKWI SSID */
static constexpr std::string_view password = ""; /* WOKWI password */
inline constexpr std::string_view ssid = "Wokwi-GUEST"; /* WOKWI SSID */
inline constexpr std::string_view password = ""; /* WOKWI password */
#else
static constexpr std::string_view ssid = "MYSSID"; /* Default SSID */
static constexpr std::string_view password = "MYPWD"; /* Default password */
inline constexpr std::string_view ssid = "MYSSID"; /* Default SSID */
inline constexpr std::string_view password = "MYPWD"; /* Default password */
#endif

/// @brief Username for MQTT broker auth.
static constexpr std::string_view mqtt_user = "user";
inline constexpr std::string_view mqtt_user = "user";

/// @brief Username for MQTT broker auth.
static constexpr std::string_view mqtt_password = "password";
inline constexpr std::string_view mqtt_password = "password";

// Checks
static_assert(secrets::credentials::ssid.size() < conf::common::STR_MAX_LENGTH, "SSID is too long");
Expand Down
2 changes: 1 addition & 1 deletion include/PinsConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace fabomatic
{
/// @brief Constant used to indicate the pin is not used
static constexpr uint8_t NO_PIN = -1;
inline constexpr uint8_t NO_PIN = -1;

struct pins_config
{
Expand Down
2 changes: 1 addition & 1 deletion include/card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace fabomatic::card
{
using uid_t = u_int64_t;
static constexpr uid_t INVALID = 0ULL;
inline constexpr uid_t INVALID = 0ULL;

/**
* @brief Returns a string representation of the UID
Expand Down
92 changes: 46 additions & 46 deletions include/language/en-US.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,70 @@

namespace fabomatic::strings::en_US
{
static constexpr auto S_LANG_ID = "en-US";
inline constexpr auto S_LANG_ID = "en-US";

static constexpr auto S_BUSY = "Busy";
static constexpr auto S_CANCELLED = "* CANCELLED *";
static constexpr auto S_CONFIRMED = "* CONFIRMED *";
static constexpr auto S_LONGTAP_PROMPT = "Record?";
inline constexpr auto S_BUSY = "Busy";
inline constexpr auto S_CANCELLED = "* CANCELLED *";
inline constexpr auto S_CONFIRMED = "* CONFIRMED *";
inline constexpr auto S_LONGTAP_PROMPT = "Record?";

static constexpr auto S_MACHINE_BLOCKED = "> BLOCKED <";
static constexpr auto S_MACHINE_MAINTENANCE = "Maintenance";
static constexpr auto S_CARD_PROMPT = "Pass your card";
static constexpr auto S_USED_BY = "In use by";
inline constexpr auto S_MACHINE_BLOCKED = "> BLOCKED <";
inline constexpr auto S_MACHINE_MAINTENANCE = "Maintenance";
inline constexpr auto S_CARD_PROMPT = "Pass your card";
inline constexpr auto S_USED_BY = "In use by";

static constexpr auto S_START_USE = "Use started";
static constexpr auto S_LOGIN_DENIED = "Unknown card";
static constexpr auto S_GOODBYE = "Goodbye";
inline constexpr auto S_START_USE = "Use started";
inline constexpr auto S_LOGIN_DENIED = "Unknown card";
inline constexpr auto S_GOODBYE = "Goodbye";

static constexpr auto S_CONNECTING_MQTT_1 = "Connecting";
static constexpr auto S_CONNECTING_MQTT_2 = "to MQTT server";
inline constexpr auto S_CONNECTING_MQTT_1 = "Connecting";
inline constexpr auto S_CONNECTING_MQTT_2 = "to MQTT server";

static constexpr auto S_CONNECTED = "Connected";
static constexpr auto S_HELLO = "Hello";
inline constexpr auto S_CONNECTED = "Connected";
inline constexpr auto S_HELLO = "Hello";

static constexpr auto S_WORKING = "Working...";
static constexpr auto S_OFFLINE_MODE = "OFFLINE MODE";
inline constexpr auto S_WORKING = "Working...";
inline constexpr auto S_OFFLINE_MODE = "OFFLINE MODE";

static constexpr auto S_BLOCKED_ADMIN_1 = "Blocked by";
static constexpr auto S_BLOCKED_ADMIN_2 = "admins";
inline constexpr auto S_BLOCKED_ADMIN_1 = "Blocked by";
inline constexpr auto S_BLOCKED_ADMIN_2 = "admins";

static constexpr auto S_VERIFYING_1 = "Verifying";
static constexpr auto S_VERIFYING_2 = "card...";
inline constexpr auto S_VERIFYING_1 = "Verifying";
inline constexpr auto S_VERIFYING_2 = "card...";

static constexpr auto S_BLOCKED_MAINTENANCE_1 = "Blocked for";
static constexpr auto S_BLOCKED_MAINTENANCE_2 = "maintenance";
inline constexpr auto S_BLOCKED_MAINTENANCE_1 = "Blocked for";
inline constexpr auto S_BLOCKED_MAINTENANCE_2 = "maintenance";

static constexpr auto S_PROMPT_MAINTENANCE_1 = "Maintenance";
static constexpr auto S_PROMPT_MAINTENANCE_2 = "record ?";
inline constexpr auto S_PROMPT_MAINTENANCE_1 = "Maintenance";
inline constexpr auto S_PROMPT_MAINTENANCE_2 = "record ?";

static constexpr auto S_MAINTENANCE_REGISTERED_1 = "Maintenance";
static constexpr auto S_MAINTENANCE_REGISTERED_2 = "recorded";
inline constexpr auto S_MAINTENANCE_REGISTERED_1 = "Maintenance";
inline constexpr auto S_MAINTENANCE_REGISTERED_2 = "recorded";

static constexpr auto S_GENERIC_ERROR = "Error";
static constexpr auto S_HW_ERROR = "HW error";
static constexpr auto S_PORTAL_ERROR = "Portal error";
static constexpr auto S_OTA_ERROR = "OTA error";
static constexpr auto S_STATUS_ERROR_1 = "Unhandled status";
static constexpr auto S_STATUS_ERROR_2 = "value";
inline constexpr auto S_GENERIC_ERROR = "Error";
inline constexpr auto S_HW_ERROR = "HW error";
inline constexpr auto S_PORTAL_ERROR = "Portal error";
inline constexpr auto S_OTA_ERROR = "OTA error";
inline constexpr auto S_STATUS_ERROR_1 = "Unhandled status";
inline constexpr auto S_STATUS_ERROR_2 = "value";

static constexpr auto S_PORTAL_SUCCESS = "WiFi conf. OK";
static constexpr auto S_OPEN_PORTAL = "Open portal";
inline constexpr auto S_PORTAL_SUCCESS = "WiFi conf. OK";
inline constexpr auto S_OPEN_PORTAL = "Open portal";

static constexpr auto S_BOOTING = "Booting...";
inline constexpr auto S_BOOTING = "Booting...";

static constexpr auto S_SHUTTING_DOWN = "Shutting down!";
inline constexpr auto S_SHUTTING_DOWN = "Shutting down!";

static constexpr auto UPDATE_OTA_1 = "Upgrading";
static constexpr auto UPDATE_OTA_2 = "OTA...";
inline constexpr auto UPDATE_OTA_1 = "Upgrading";
inline constexpr auto UPDATE_OTA_2 = "OTA...";

static constexpr auto FACTORY_RESET_DONE_1 = "Factory reset";
static constexpr auto FACTORY_RESET_DONE_2 = "Wait reboot";
inline constexpr auto FACTORY_RESET_DONE_1 = "Factory reset";
inline constexpr auto FACTORY_RESET_DONE_2 = "Wait reboot";

static constexpr auto PORTAL_TITLE = "FAB-O-MATIC";
static constexpr auto PORTAL_MACHINE_ID_PROMPT = "Machine ID";
static constexpr auto PORTAL_SHELLY_MQTT_PROMPT = "Shelly MQTT topic (can be empty)";
static constexpr auto PORTAL_MQTT_BROKER_PROMPT = "MQTT broker (IP or hostname)";
inline constexpr auto PORTAL_TITLE = "FAB-O-MATIC";
inline constexpr auto PORTAL_MACHINE_ID_PROMPT = "Machine ID";
inline constexpr auto PORTAL_SHELLY_MQTT_PROMPT = "Shelly MQTT topic (can be empty)";
inline constexpr auto PORTAL_MQTT_BROKER_PROMPT = "MQTT broker (IP or hostname)";

} // namespace fabomatic::strings::en_US

Expand Down
Loading

0 comments on commit 535c285

Please sign in to comment.