Skip to content

Commit

Permalink
Merge pull request #38 from fablab-bergamo/rfidDisablePeriod
Browse files Browse the repository at this point in the history
Trying to solve repeated start/stop use events in short periods
  • Loading branch information
PBrunot authored Aug 12, 2024
2 parents e092868 + 805305a commit 56df943
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 17 deletions.
3 changes: 3 additions & 0 deletions conf/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ namespace fabomatic
/// @brief Minimum time to confirm by long tap maintenance
static constexpr auto LONG_TAP_DURATION{10s};

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

} // namespace conf::machine

/// @brief Debug settings
Expand Down
3 changes: 3 additions & 0 deletions include/BaseRfidWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <optional>

#include "card.hpp"
#include "Tasks.hpp"

namespace fabomatic
{
Expand All @@ -21,6 +22,8 @@ namespace fabomatic
virtual auto readCardSerial() const -> std::optional<card::uid_t> = 0;
virtual auto selfTest() const -> bool = 0;
virtual auto reset() const -> void = 0;

virtual auto setDisabledUntil(std::optional<Tasks::time_point> t) -> void = 0;
};
} // namespace fabomatic
#endif // BASERFIDWRAPPER_HPP_
1 change: 0 additions & 1 deletion include/BoardLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ namespace fabomatic
FabBackend server;
std::optional<std::reference_wrapper<BaseRFIDWrapper>> rfid{std::nullopt}; // Configured at runtime
std::optional<std::reference_wrapper<LCDWrapper>> lcd{std::nullopt}; // Configured at runtime
bool ready_for_a_new_card{true};
bool led_status{false};

Machine machine;
Expand Down
5 changes: 4 additions & 1 deletion include/RFIDWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace fabomatic
{
private:
std::unique_ptr<Driver> driver;
std::optional<fabomatic::Tasks::time_point> disabledUntil;

public:
RFIDWrapper();
Expand Down Expand Up @@ -46,14 +47,16 @@ namespace fabomatic

[[nodiscard]] auto getUid() const -> card::uid_t override;

[[nodiscard]] auto setDisabledUntil(std::optional<Tasks::time_point> delay) -> void override;

/// @brief Returns the driver object for testing/simulation
Driver &getDriver();

RFIDWrapper(const RFIDWrapper &) = delete; // copy constructor
RFIDWrapper &operator=(const RFIDWrapper &x) = delete; // copy assignment
RFIDWrapper(RFIDWrapper &&) = delete; // move constructor
RFIDWrapper &operator=(RFIDWrapper &&) = delete; // move assignment
~RFIDWrapper() override{}; // Default destructor
~RFIDWrapper() override {}; // Default destructor
};
} // namespace fabomatic

Expand Down
1 change: 0 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ board_build.f_flash = 80000000L
build_flags = ${env.build_flags}
-D ARDUINO_USB_CDC_ON_BOOT
build_src_flags = ${env.build_src_flags}
-D CORE_DEBUG_LEVEL=5
-D MQTT_SIMULATION=false
-D RFID_SIMULATION=false
-D PINS_HARDWARE_REV0
Expand Down
10 changes: 2 additions & 8 deletions src/BoardLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ namespace fabomatic
{
ESP_LOGD(TAG, "New card present");

if (!ready_for_a_new_card)
{
return;
}
ready_for_a_new_card = false;

if (machine.isFree())
{
// machine is free
Expand Down Expand Up @@ -556,13 +550,13 @@ namespace fabomatic
const auto &result = rfid.readCardSerial();
if (result)
{
// This function may block for several seconds if a long tap is required
onNewCard(result.value());
rfid.setDisabledUntil(Tasks::arduinoNow() + conf::machine::DELAY_BETWEEN_SWEEPS);
}
return;
}

// No new card present
ready_for_a_new_card = true;
if (machine.isFree())
{
changeStatus(Status::MachineFree);
Expand Down
21 changes: 20 additions & 1 deletion src/RFIDWrapper.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace fabomatic
{
/// @brief Constructor
template <typename Driver>
RFIDWrapper<Driver>::RFIDWrapper() : driver{std::make_unique<Driver>()} {};
RFIDWrapper<Driver>::RFIDWrapper() : driver{std::make_unique<Driver>()}, disabledUntil{std::nullopt} {};

/// @brief indicates if a new card is present in the RFID chip antenna area
/// @return true if a new card is present
Expand All @@ -24,15 +24,34 @@ namespace fabomatic
if (conf::debug::ENABLE_LOGS && result)
ESP_LOGD(TAG, "isNewCardPresent=%d", result);

if (disabledUntil && disabledUntil > fabomatic::Tasks::arduinoNow())
{
ESP_LOGD(TAG, "isNewCardPresent is disabled");
return false;
}

return result;
}

template <typename Driver>
[[nodiscard]] auto RFIDWrapper<Driver>::setDisabledUntil(std::optional<Tasks::time_point> delay) -> void
{
this->disabledUntil = delay;
}

/// @brief tries to read the card serial number
/// @return true if successfull, result can be read with getUid()
template <typename Driver>
auto RFIDWrapper<Driver>::readCardSerial() const -> std::optional<card::uid_t>
{
const auto &result = driver->PICC_ReadCardSerial();

if (disabledUntil && disabledUntil > fabomatic::Tasks::arduinoNow())
{
ESP_LOGD(TAG, "readCardSerial is disabled");
return std::nullopt;
}

if (result)
{
return getUid();
Expand Down
9 changes: 7 additions & 2 deletions test/test_logic/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,29 @@ namespace fabomatic::tests
std::optional<std::chrono::milliseconds> duration_tap)
{
constexpr auto DEFAULT_CYCLES = 3;

MockMrfc522 &driver = rfid.getDriver();

driver.resetUid();
rfid.setDisabledUntil(std::nullopt);

for (auto i = 0; i < DEFAULT_CYCLES; i++)
{
logic.checkRfid();
rfid.setDisabledUntil(std::nullopt);
}

if (uid.has_value())
{
driver.setUid(uid.value(), duration_tap);
TEST_ASSERT_TRUE_MESSAGE(uid == rfid.getUid(), "Card UID not equal");
auto start = fabomatic::Tasks::arduinoNow();

do
{
logic.checkRfid();
rfid.setDisabledUntil(std::nullopt);
delay(50);
} while (duration_tap.has_value() && fabomatic::Tasks::arduinoNow() - start < duration_tap);

}
else if (duration_tap)
{
Expand Down
8 changes: 5 additions & 3 deletions test/test_logic/test_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ namespace fabomatic::tests
const auto card_uid = get_test_uid(1);

simulate_rfid_card(rfid, logic, card_uid, std::nullopt);

Tasks::delay(fabomatic::conf::machine::DELAY_BETWEEN_SWEEPS);
TEST_ASSERT_TRUE_MESSAGE(rfid.isNewCardPresent(), "New card not present");
TEST_ASSERT_TRUE_MESSAGE(rfid.readCardSerial().has_value(), "Card serial not read");
TEST_ASSERT_TRUE_MESSAGE(rfid.readCardSerial().value() == card_uid, "Card serial not equal");
Expand Down Expand Up @@ -271,8 +273,8 @@ namespace fabomatic::tests
TEST_ASSERT_EQUAL_UINT16_MESSAGE(BoardLogic::Status::MaintenanceNeeded, logic.getStatus(), "Status not MaintenanceNeeded");

simulate_rfid_card(rfid, logic, std::nullopt);
simulate_rfid_card(rfid, logic, card_admin, conf::machine::LONG_TAP_DURATION + 10s); // Log in + Conferma manutenzione perché non ritorna prima della conclusione
simulate_rfid_card(rfid, logic, std::nullopt); // Card away
simulate_rfid_card(rfid, logic, card_admin, conf::machine::LONG_TAP_DURATION + 3s); // Log in + Conferma manutenzione perché non ritorna prima della conclusione
simulate_rfid_card(rfid, logic, std::nullopt); // Card away
TEST_ASSERT_EQUAL_UINT16_MESSAGE(BoardLogic::Status::MachineInUse, logic.getStatus(), "Status not MachineInUse by admin");
TEST_ASSERT_FALSE_MESSAGE(logic.getMachine().isMaintenanceNeeded(), "Maintenance not cleared by admin");

Expand Down Expand Up @@ -355,7 +357,7 @@ void setup()
{
delay(1000);
esp_log_level_set(TAG, LOG_LOCAL_LEVEL);

auto config = fabomatic::SavedConfig::LoadFromEEPROM();
UNITY_BEGIN();
RUN_TEST(fabomatic::tests::test_machine_defaults);
Expand Down

0 comments on commit 56df943

Please sign in to comment.