Skip to content

Commit

Permalink
workaround strange std::chrono::steady_clock behaviours
Browse files Browse the repository at this point in the history
going back to millis()
updating tests
  • Loading branch information
PBrunot committed May 24, 2024
1 parent c6743fc commit 13421e1
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 96 deletions.
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
26 changes: 17 additions & 9 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,12 +113,12 @@ namespace fabomatic::Tasks
class Scheduler
{
public:
auto addTask(Task &task) -> void;
auto removeTask(const Task &task) -> void;
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;
Expand All @@ -119,10 +127,10 @@ namespace fabomatic::Tasks
[[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>>;
[[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; // Vector containing references to the tasks, not the tasks themselves

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
4 changes: 2 additions & 2 deletions src/BoardLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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 +147,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 13421e1

Please sign in to comment.