Skip to content

Commit

Permalink
millis() instead of std::chrono::clocks
Browse files Browse the repository at this point in the history
Update test_tasks.cpp
Workaround strange std::chrono::steady_clock behaviours
going back to millis()
updating tests
Compiler flags tweaks for size opt

espressif/crosstool-NG#52

small changes

more constexpr

Update settings.json

Removed -mno-target-align
  • Loading branch information
PBrunot committed May 25, 2024
1 parent 03179c0 commit 3102ecc
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 106 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@
"set": "cpp",
"future": "cpp",
"variant": "cpp",
"any": "cpp"
"any": "cpp",
"bit": "cpp",
"compare": "cpp",
"concepts": "cpp",
"netfwd": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"stop_token": "cpp"
},
"cmake.sourceDirectory": "D:/GitHub/rfid-arduino/.pio/libdeps/desktop/ArduinoJson",
"cmake.configureOnOpen": false,
Expand Down
8 changes: 4 additions & 4 deletions include/AuthProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace fabomatic
WhiteList whitelist;
mutable CachedCards cache;
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>;
auto updateCache(card::uid_t candidate_uid, FabUser::UserLevel level) const -> void;
[[nodiscard]] constexpr auto uidInWhitelist(card::uid_t uid) const -> std::optional<WhiteListEntry>;
[[nodiscard]] constexpr auto uidInCache(card::uid_t uid) const -> std::optional<CachedCard>;
[[nodiscard]] constexpr auto searchCache(card::uid_t candidate_uid) const -> std::optional<CachedCard>;
constexpr auto updateCache(card::uid_t candidate_uid, FabUser::UserLevel level) const -> void;

public:
AuthProvider() = delete;
Expand Down
2 changes: 1 addition & 1 deletion include/CachedCards.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace fabomatic
return {cards[i], levels[i]};
}

auto find_uid(const card::uid_t &search_uid) const -> const std::optional<CachedCard>
constexpr auto find_uid(const card::uid_t &search_uid) const -> const std::optional<CachedCard>
{
if (search_uid == card::INVALID)
{
Expand Down
4 changes: 2 additions & 2 deletions include/Machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ namespace fabomatic
bool active{false};
FabUser current_user{};

std::optional<std::chrono::time_point<std::chrono::system_clock>> usage_start_timestamp{std::nullopt}; // When did the machine start?
std::optional<std::chrono::time_point<std::chrono::system_clock>> logoff_timestamp{std::nullopt}; // When did the last user log off?
std::optional<std::chrono::milliseconds> usage_start_timestamp{std::nullopt}; // When did the machine start?
std::optional<std::chrono::milliseconds> logoff_timestamp{std::nullopt}; // When did the last user log off?
PowerState power_state{PowerState::PoweredOff};

/// @brief If true, machine needs maintenance
Expand Down
29 changes: 19 additions & 10 deletions include/Tasks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

#include <chrono>
#include <functional>
#include "Arduino.h"
#include <list>

/// @brief This namespace contains the classes that implement a cooperative task scheduler
namespace fabomatic::Tasks
{
using milliseconds = std::chrono::milliseconds;
using time_point_sc = std::chrono::time_point<std::chrono::system_clock>;
using namespace std::chrono_literals;

inline auto arduinoNow() -> milliseconds
{
return milliseconds{::millis()};
}

class Scheduler;

// A task is a function that is executed periodically
Expand Down Expand Up @@ -87,15 +93,17 @@ namespace fabomatic::Tasks

/// @brief When shall the task be run again
/// @return time_point of the next run or time_point::max() if the task will not run.
[[nodiscard]] auto getNextRun() const -> time_point_sc;
[[nodiscard]] auto getNextRun() const -> milliseconds;

[[nodiscard]] auto toString() const -> const std::string;

private:
bool active;
const std::string id;
milliseconds period;
milliseconds delay;
time_point_sc last_run;
time_point_sc next_run;
milliseconds last_run;
milliseconds next_run;
milliseconds average_tardiness;
milliseconds total_runtime;
std::function<void()> callback;
Expand All @@ -105,24 +113,25 @@ namespace fabomatic::Tasks
class Scheduler
{
public:
auto addTask(Task &task) -> void;
auto removeTask(const Task &task) -> void;
constexpr Scheduler(){};
auto addTask(Task *task) -> void;
auto removeTask(const Task *task) -> void;

/// @brief Execute all tasks that are ready to run
/// @details Tasks will be ordered by next_run time ascending, then run sequentially
auto execute() const -> void;
auto execute() -> void;

/// @brief Recompute all the next run times for all the tasks
auto updateSchedules() const -> void;

/// @brief Gets the number of tasks in the scheduler
[[nodiscard]] auto taskCount() const -> size_t;

/// @brief Get a vector of references to the tasks
[[nodiscard]] auto getTasks() const -> const std::vector<std::reference_wrapper<Task>>;
/// @brief Get a copy vector of task pointers
[[nodiscard]] auto getTasks() const -> const std::vector<Task *>;

private:
std::vector<std::reference_wrapper<Task>> tasks; // Vector containing references to the tasks, not the tasks themselves
std::vector<Task *> tasks;

auto printStats() const -> void;
};
Expand Down
2 changes: 1 addition & 1 deletion include/mock/MockMrfc522.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace fabomatic
{
private:
std::optional<card::uid_t> uid{std::nullopt};
std::optional<std::chrono::time_point<std::chrono::system_clock>> stop_uid_simulate_time{std::nullopt};
std::optional<std::chrono::milliseconds> stop_uid_simulate_time{std::nullopt};
std::optional<card::uid_t> getSimulatedUid() const;

public:
Expand Down
6 changes: 3 additions & 3 deletions src/AuthProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace fabomatic
/// @param uid card id of the user
/// @param name name of the user to be cached
/// @param level priviledge level of the user
void AuthProvider::updateCache(card::uid_t uid, FabUser::UserLevel level) const
constexpr void AuthProvider::updateCache(card::uid_t uid, FabUser::UserLevel level) const
{
// Search for the card in the cache
const auto pos = std::find(cache.cards.cbegin(), cache.cards.cend(), uid);
Expand Down Expand Up @@ -128,7 +128,7 @@ namespace fabomatic
/// @brief Checks if the card ID is whitelisted
/// @param uid card ID
/// @return a whitelistentry object if the card is found in whitelist
auto AuthProvider::uidInWhitelist(card::uid_t candidate_uid) const -> std::optional<WhiteListEntry>
constexpr auto AuthProvider::uidInWhitelist(card::uid_t candidate_uid) const -> std::optional<WhiteListEntry>
{
if (candidate_uid == card::INVALID)
{
Expand All @@ -154,7 +154,7 @@ namespace fabomatic
/// @brief Checks if the card ID is whitelisted
/// @param uid card ID
/// @return a whitelistentry object if the card is found in whitelist
auto AuthProvider::uidInCache(card::uid_t candidate_uid) const -> std::optional<CachedCard>
constexpr auto AuthProvider::uidInCache(card::uid_t candidate_uid) const -> std::optional<CachedCard>
{
return cache.find_uid(candidate_uid);
}
Expand Down
5 changes: 3 additions & 2 deletions src/BoardLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef GIT_VERSION
#define GIT_VERSION "??????"
#endif
#include <driver/gpio.h>

namespace fabomatic
{
Expand Down Expand Up @@ -138,7 +139,7 @@ namespace fabomatic
getLcd().setRow(1, ss.str());
getLcd().update(bi);

const auto start = std::chrono::system_clock::now();
const auto start = fabomatic::Tasks::arduinoNow();
if (!getRfid().cardStillThere(card, delay_per_step))
{
getLcd().setRow(1, strings::S_CANCELLED);
Expand All @@ -147,7 +148,7 @@ namespace fabomatic
}

// cardStillThere may have returned immediately, so we need to wait a bit
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start);
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(fabomatic::Tasks::arduinoNow() - start);
if (delay_per_step - elapsed > 10ms)
{
Tasks::delay(delay_per_step - elapsed);
Expand Down
4 changes: 2 additions & 2 deletions src/FabBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace fabomatic
*/
bool FabBackend::waitForAnswer(std::chrono::milliseconds max_duration)
{
const auto start_time = std::chrono::system_clock::now();
const auto start_time = fabomatic::Tasks::arduinoNow();
const auto DELAY_MS = 25ms;
do
{
Expand All @@ -185,7 +185,7 @@ namespace fabomatic
{
return true;
}
} while (std::chrono::system_clock::now() < (start_time + max_duration));
} while (fabomatic::Tasks::arduinoNow() < (start_time + max_duration));

ESP_LOGE(TAG, "Failure, no answer from MQTT server (timeout:%lld ms)", max_duration.count());
return false;
Expand Down
12 changes: 6 additions & 6 deletions src/Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace fabomatic
active = true;
current_user = user;
power(true);
usage_start_timestamp = std::chrono::system_clock::now();
usage_start_timestamp = fabomatic::Tasks::arduinoNow();
return true;
}
return false;
Expand All @@ -93,7 +93,7 @@ namespace fabomatic
usage_start_timestamp = std::nullopt;

// Sets the countdown to power off
logoff_timestamp = std::chrono::system_clock::now();
logoff_timestamp = fabomatic::Tasks::arduinoNow();

if (config.value().grace_period == 0s)
{
Expand All @@ -115,7 +115,7 @@ namespace fabomatic
CHECK_CONFIGURED(bool);

return (power_state == PowerState::WaitingPowerOff &&
std::chrono::system_clock::now() - logoff_timestamp.value() > config.value().grace_period);
fabomatic::Tasks::arduinoNow() - logoff_timestamp.value() > config.value().grace_period);
}

/// @brief indicates if the machine is about to shudown and board should beep
Expand All @@ -128,7 +128,7 @@ namespace fabomatic
CHECK_CONFIGURED(bool);

return (power_state == PowerState::WaitingPowerOff &&
std::chrono::system_clock::now() - logoff_timestamp.value() > config.value().grace_period);
fabomatic::Tasks::arduinoNow() - logoff_timestamp.value() > config.value().grace_period);
}

/// @brief sets the machine power to on (true) or off (false)
Expand Down Expand Up @@ -227,7 +227,7 @@ namespace fabomatic
{
if (usage_start_timestamp.has_value())
{
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - usage_start_timestamp.value());
return std::chrono::duration_cast<std::chrono::seconds>(fabomatic::Tasks::arduinoNow() - usage_start_timestamp.value());
}
return 0s;
}
Expand Down Expand Up @@ -259,7 +259,7 @@ namespace fabomatic
sstream << ", MaintenanceNeeded:" << maintenanceNeeded;
sstream << ", " << config.value().toString();
sstream << ", Active:" << active;
sstream << ", Last logoff:" << (logoff_timestamp.has_value() ? logoff_timestamp.value().time_since_epoch().count() : 0);
sstream << ", Last logoff:" << (logoff_timestamp.has_value() ? logoff_timestamp.value().count() : 0);
sstream << ", GracePeriod (s):" << getGracePeriod().count();
sstream << ")";

Expand Down
5 changes: 3 additions & 2 deletions src/RFIDWrapper.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "card.hpp"
#include "conf.hpp"
#include "pins.hpp"
#include "Tasks.hpp"

namespace fabomatic
{
Expand Down Expand Up @@ -47,7 +48,7 @@ namespace fabomatic
template <typename Driver>
auto RFIDWrapper<Driver>::cardStillThere(const card::uid_t original, std::chrono::milliseconds max_delay) const -> bool
{
const auto start = std::chrono::system_clock::now();
const auto start = fabomatic::Tasks::arduinoNow();
do
{
// Detect Tag without looking for collisions
Expand All @@ -60,7 +61,7 @@ namespace fabomatic
return true;
}
delay(20);
} while (std::chrono::system_clock::now() - start < max_delay);
} while (fabomatic::Tasks::arduinoNow() - start < max_delay);

return false;
}
Expand Down
Loading

0 comments on commit 3102ecc

Please sign in to comment.