Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed May 25, 2024
2 parents d659806 + 9b68943 commit e33a228
Show file tree
Hide file tree
Showing 26 changed files with 468 additions and 343 deletions.
16 changes: 8 additions & 8 deletions conf/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
#include <string>
#include <chrono>

#include "MachineConfig.hpp"
#include "MachineID.hpp"

namespace fabomatic
{
using namespace std::chrono_literals;

namespace conf::default_config
{
static constexpr std::string_view mqtt_server = "fabpi2.local";
static constexpr std::string_view mqtt_switch_topic = "";
static constexpr std::string_view mqtt_server{"fabpi2.local"};
static constexpr std::string_view mqtt_switch_topic{""};
static constexpr std::string_view mqtt_switch_on_message{"on"};
static constexpr std::string_view mqtt_switch_off_message{"off"};
static constexpr MachineID machine_id{1};
static constexpr std::string_view machine_name = "MACHINE1";
static constexpr MachineType machine_type = MachineType::Printer3D;
static constexpr std::string_view hostname = "BOARD"; // Machine ID will be added to the hostname in order to form unique hostnames
static constexpr std::string_view machine_name{"MACHINE1"};
static constexpr MachineType machine_type{MachineType::Printer3D};
static constexpr std::string_view hostname{"BOARD"}; // Machine ID will be added to the hostname in order to form unique hostnames

} // namespace conf::default_config

Expand Down Expand Up @@ -60,10 +62,8 @@ namespace fabomatic

namespace conf::buzzer
{
static constexpr unsigned short LEDC_PWM_CHANNEL{2}; /* Esp32 pwm channel for beep generation */
static constexpr auto STANDARD_BEEP_DURATION{250ms}; /* Single beep duration, typical value 200ms. Set to 0 to disable beeps. */
static constexpr auto NB_BEEPS{3}; /* Number of beeps every time the function is callsed */
static constexpr unsigned int BEEP_HZ{660}; /* Beep frequency in Hz */

} // namespace conf::buzzer

Expand Down
4 changes: 2 additions & 2 deletions include/AuthProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace fabomatic
private:
WhiteList whitelist;
mutable CachedCards cache;
mutable size_t cache_idx = 0;
mutable size_t cache_idx{0};
[[nodiscard]] auto uidInWhitelist(card::uid_t uid) const -> std::optional<WhiteListEntry>;
[[nodiscard]] auto uidInCache(card::uid_t uid) const -> std::optional<CachedCard>;
[[nodiscard]] auto searchCache(card::uid_t candidate_uid) const -> std::optional<CachedCard>;
Expand All @@ -28,7 +28,7 @@ namespace fabomatic
AuthProvider() = delete;
AuthProvider(WhiteList whitelist);
[[nodiscard]] auto tryLogin(card::uid_t uid, FabBackend &server) const -> std::optional<FabUser>;
void setWhitelist(WhiteList list);
auto setWhitelist(WhiteList list) -> void;
auto saveCache() const -> bool;
auto loadCache() -> void;
};
Expand Down
1 change: 1 addition & 0 deletions include/BoardLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace fabomatic
[[nodiscard]] auto getMachineForTesting() -> Machine &;
[[nodiscard]] auto getMachine() const -> const Machine &;
[[nodiscard]] auto authorize(const card::uid_t uid) -> bool;
[[nodiscard]] auto getHostname() const -> const std::string;

// copy reference
BoardLogic &operator=(const BoardLogic &board) = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/FabBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace fabomatic

bool online{false};
bool answer_pending{false};
int32_t channel{-1};
int16_t channel{-1};

auto messageReceived(String &topic, String &payload) -> void;

Expand Down
6 changes: 3 additions & 3 deletions include/FabUser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ namespace fabomatic
FabAdmin,
};

card::uid_t card_uid = card::INVALID;
card::uid_t card_uid {card::INVALID};
std::string holder_name{""};
bool authenticated = false;
UserLevel user_level = UserLevel::Unknown;
bool authenticated {false};
UserLevel user_level {UserLevel::Unknown};

FabUser() = default;

Expand Down
22 changes: 11 additions & 11 deletions include/LCDWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace fabomatic
LCDWrapper(const pins_config::lcd_config &config);

using DisplayBuffer = std::array<std::array<char, conf::lcd::COLS>, conf::lcd::ROWS>;
bool begin() override;
void clear() override;
void showConnection(bool show) override;
void showPower(bool show) override;
void setRow(uint8_t row, const std::string_view &text) override;

void update(const BoardInfo &boardinfo, bool forced = false) override;
auto begin() -> bool override;
auto clear() -> void override;
auto showConnection(bool show) -> void override;
auto showPower(bool show) -> void override;
auto setRow(uint8_t row, const std::string_view &text) -> void override;
auto update(const BoardInfo &boardinfo, bool forced = false) -> void override;

private:
static constexpr auto HEIGHT_PX = 8;
Expand All @@ -41,7 +41,7 @@ namespace fabomatic
static constexpr std::array<uint8_t, HEIGHT_PX> powered_off_char{0x0a, 0x04, 0x0a, 0x00, 0x1f, 0x1f, 0x0a, 0x0a};
static constexpr std::array<uint8_t, HEIGHT_PX> powering_off_char{0x0e, 0x15, 0x15, 0x15, 0x17, 0x11, 0x11, 0x0e};

const pins_config::lcd_config config;
const pins_config::lcd_config &config;

LcdDriver lcd;
bool show_connection_status;
Expand All @@ -51,11 +51,11 @@ namespace fabomatic
DisplayBuffer current;
BoardInfo boardInfo;

void backlightOn() const;
void backlightOff() const;
void prettyPrint(const DisplayBuffer &buffer, const BoardInfo &bi) const;
auto backlightOn() const -> void;
auto backlightOff() const -> void;
auto prettyPrint(const DisplayBuffer &buffer, const BoardInfo &bi) const -> void;
[[nodiscard]] auto needsUpdate(const BoardInfo &bi) const -> bool;
void createChar(uint8_t char_idx, const std::array<uint8_t, HEIGHT_PX> &values);
auto createChar(uint8_t char_idx, const std::array<uint8_t, HEIGHT_PX> &values) -> void;
};
} // namespace fabomatic

Expand Down
8 changes: 4 additions & 4 deletions include/Led.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace fabomatic
class Led
{
public:
enum class Status : uint8_t
enum class Status
{
Off,
On,
Expand All @@ -21,9 +21,9 @@ namespace fabomatic

Led() = default;

void set(Status status);
void setColor(uint8_t r, uint8_t g, uint8_t b);
void update();
auto set(Status status) -> void;
auto setColor(uint8_t r, uint8_t g, uint8_t b) -> void;
auto update() -> void;

private:
Adafruit_NeoPixel pixel{1, pins.led.pin, pins.led.neopixel_config};
Expand Down
6 changes: 3 additions & 3 deletions include/MQTTtypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ namespace fabomatic::ServerMQTT
bool is_valid = false; /* True if the machine has a valid ID */
bool maintenance = true; /* True if the machine needs maintenance */
bool allowed = false; /* True if the machine can be used by anybody */
uint16_t logoff = 0; /* Timeout in minutes */
uint16_t logoff{0}; /* Timeout in minutes */
std::string name{""}; /* Name of the machine from server DB */
uint8_t type = 0; /* Type of the machine */
uint16_t grace = 0; /* Grace period in minutes */
uint8_t type{0}; /* Type of the machine */
uint16_t grace{0}; /* Grace period in minutes */
std::string description{""}; /* Description of the expired maintenance */
MachineResponse() = delete;
MachineResponse(bool rok) : Response(rok){};
Expand Down
44 changes: 10 additions & 34 deletions include/MachineConfig.hpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,26 @@
#ifndef MACHINECONFIG_HPP_
#define MACHINECONFIG_HPP_

#include "pins.hpp"
#include <chrono>
#include <cstdint>
#include <string>

#include "pins.hpp"
#include "MachineID.hpp"
#include "conf.hpp"

namespace fabomatic
{
enum class MachineType : uint8_t
{
Invalid = 0,
Printer3D = 1,
Laser = 2,
Cnc = 3,
PrinterResin = 4,
Other = 5
};

struct MachineID
{
uint16_t id;

constexpr MachineID() : id(0) {}
constexpr MachineID(uint16_t id) : id(id) {}
// Add conversion operator to uint16_t to use in print, streams...
constexpr operator uint16_t() const { return id; }
};
struct MachineConfig
{
MachineID machine_id{0};
MachineType machine_type{MachineType::Invalid};
std::string machine_name{""};
struct RelayConfig
{
const uint8_t pin{NO_PIN};
const bool active_low{false};
} relay_config;
struct MQTTConfig
{
const std::string topic{""};
const std::string on_message{"on"};
const std::string off_message{"off"};
} mqtt_config;
const pins_config::relay_config &relay_config;
const std::string mqtt_switch_topic{""};

/// @brief Time after which the active user on the machine shall be logged-off
std::chrono::seconds autologoff;
std::chrono::seconds autologoff{conf::machine::DEFAULT_AUTO_LOGOFF_DELAY};

/// @brief Time after which the active user on the machine shall be logged-off
std::chrono::seconds grace_period;
Expand All @@ -55,8 +30,9 @@ namespace fabomatic
std::chrono::seconds autologoff,
std::chrono::seconds grace_period) : machine_id(id), machine_type(type),
machine_name(name),
relay_config{relay.ch1_pin, relay.active_low},
mqtt_config{topic}, autologoff(autologoff),
relay_config{relay},
mqtt_switch_topic(topic),
autologoff(autologoff),
grace_period{grace_period} {};

[[nodiscard]] auto toString() const -> const std::string;
Expand Down
26 changes: 26 additions & 0 deletions include/MachineID.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef MACHINE_ID_HPP
#define MACHINE_ID_HPP

#include <cstdint>

enum class MachineType : uint8_t
{
Invalid = 0,
Printer3D = 1,
Laser = 2,
Cnc = 3,
PrinterResin = 4,
Other = 5
};

struct MachineID
{
uint16_t id;

constexpr MachineID() : id(0) {}
constexpr MachineID(uint16_t id) : id(id) {}
// Add conversion operator to uint16_t to use in print, streams...
constexpr operator uint16_t() const { return id; }
};

#endif // MACHINE_ID_HPP
24 changes: 24 additions & 0 deletions include/OTA.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef OTA_HPP
#define OTA_HPP

#include "ArduinoOTA.h"
#include <WiFiManager.h>

#include "secrets.hpp"

// For ArduinoOTA
const char *const ssid = fabomatic::secrets::credentials::ssid.data();
const char *const password = fabomatic::secrets::credentials::password.data();

namespace fabomatic
{

/// @brief Open the ArduinoOTA configuration portal
/// @param force_reset true to discard saved settings and restore compile-time settings
/// @param disable_portal true to skip portal opening (used at boot time)
auto openConfigPortal(bool force_reset, bool disable_portal) -> void;

auto setupOTA() -> void;

} // namespace fabomatic
#endif // #ifndef OTA_HPP
2 changes: 1 addition & 1 deletion include/card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace fabomatic::card
return retVal;
}

inline void print(uint64_t uid)
inline auto print(uint64_t uid) -> void
{
ESP_LOGI(TAG, "Card UID = %s", card::uid_str(uid).c_str());
}
Expand Down
22 changes: 0 additions & 22 deletions include/mock/MockLcdLibrary.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions include/mock/MockMQTTBroker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace fabomatic

private:
std::mutex mutex;
std::string topic = "";
std::string payload = "";
std::string topic{""};
std::string payload{""};
struct query
{
std::string source_topic{""};
Expand Down
11 changes: 6 additions & 5 deletions include/mock/MockMrfc522.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ namespace fabomatic

auto PICC_IsNewCardPresent() -> bool;
auto PICC_ReadCardSerial() -> bool;
void reset();
auto reset() -> void;
auto PCD_Init() -> bool;
auto PICC_WakeupA(byte *bufferATQA, byte &bufferSize) -> bool;
auto PCD_PerformSelfTest() -> bool;
auto getDriverUid() const -> MockMrfc522::UidDriver;
void PCD_SetAntennaGain(MFRC522Constants::PCD_RxGain gain);
void PCD_DumpVersionToSerial();
auto PCD_SetAntennaGain(MFRC522Constants::PCD_RxGain gain) -> void;
auto PCD_DumpVersionToSerial() -> void;

void setUid(const std::optional<card::uid_t> &uid, const std::optional<std::chrono::milliseconds> &max_delay);
void resetUid();
auto setUid(const std::optional<card::uid_t> &uid,
const std::optional<std::chrono::milliseconds> &max_delay) -> void;
auto resetUid() -> void;

static constexpr auto RxGainMax = MFRC522::PCD_RxGain::RxGain_max;
};
Expand Down
Loading

0 comments on commit e33a228

Please sign in to comment.