127 lines
2.0 KiB
C++
127 lines
2.0 KiB
C++
//
|
|
// Created by erki on 17.04.21.
|
|
//
|
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
#include "peripherals_button.hpp"
|
|
|
|
namespace
|
|
{
|
|
|
|
struct HAL
|
|
{
|
|
HAL()
|
|
{
|
|
millis = 1;
|
|
}
|
|
|
|
~HAL()
|
|
{
|
|
millis = 1;
|
|
}
|
|
|
|
static std::uint32_t millis;
|
|
|
|
static std::uint32_t GetMillis()
|
|
{
|
|
return millis;
|
|
}
|
|
};
|
|
|
|
std::uint32_t HAL::millis = 1;
|
|
|
|
struct Gpio
|
|
{
|
|
static bool set;
|
|
|
|
Gpio()
|
|
{
|
|
set = false;
|
|
}
|
|
|
|
bool Read()
|
|
{
|
|
return set;
|
|
}
|
|
};
|
|
|
|
bool Gpio::set = false;
|
|
|
|
}// namespace
|
|
|
|
using Button = Peripherals::Button<Gpio, HAL>;
|
|
|
|
TEST_CASE("Button reads no press as NOT_PRESSED.", "[peripherals],[button]")
|
|
{
|
|
Gpio g;
|
|
HAL hal;
|
|
|
|
Button button{g};
|
|
|
|
button.update();
|
|
|
|
HAL::millis = Button::TIMEOUT_LONG_PRESS + 10;
|
|
|
|
button.update();
|
|
|
|
REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED);
|
|
}
|
|
|
|
TEST_CASE("Button reads presses properly.", "[peripherals],[button]")
|
|
{
|
|
Gpio g;
|
|
HAL hal;
|
|
|
|
Button button{g};
|
|
|
|
Gpio::set = true;
|
|
button.update();
|
|
|
|
SECTION("Too short of a press is debounced.")
|
|
{
|
|
HAL::millis = Button::TIMEOUT_SHORT_PRESS - 1;
|
|
Gpio::set = false;
|
|
|
|
button.update();
|
|
|
|
REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED);
|
|
}
|
|
|
|
SECTION("Short press is identified properly.")
|
|
{
|
|
HAL::millis = Button::TIMEOUT_SHORT_PRESS + 2;
|
|
Gpio::set = false;
|
|
|
|
button.update();
|
|
|
|
REQUIRE(button.getState() == Peripherals::ButtonPress::SHORT_PRESS);
|
|
|
|
SECTION("Next state is NOT_PRESSED.")
|
|
{
|
|
HAL::millis += 1;
|
|
button.update();
|
|
|
|
REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED);
|
|
}
|
|
}
|
|
|
|
SECTION("Long press is identified properly.")
|
|
{
|
|
HAL::millis = Button::TIMEOUT_LONG_PRESS + 2;
|
|
Gpio::set = false;
|
|
|
|
button.update();
|
|
|
|
REQUIRE(button.getState() == Peripherals::ButtonPress::LONG_PRESS);
|
|
|
|
SECTION("Next state is NOT_PRESSED.")
|
|
{
|
|
HAL::millis += 1;
|
|
button.update();
|
|
|
|
REQUIRE(button.getState() == Peripherals::ButtonPress::NOT_PRESSED);
|
|
}
|
|
}
|
|
}
|