Fix buttons unit tests after ADC update

pull/59/head
D.R.racer 2021-07-12 17:50:42 +02:00 committed by DRracer
parent edf0804fd8
commit eed1c3f07a
2 changed files with 33 additions and 28 deletions

View File

@ -1,11 +1,9 @@
#include "catch2/catch.hpp"
#include "buttons.h"
#include "../stubs/stub_adc.h"
#include "../stubs/stub_timebase.h"
#include "buttons.h"
using Catch::Matchers::Equals;
uint16_t millis = 0;
static constexpr const uint16_t adcMaxValue = 1023U;
bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversampleFactor, uint8_t testedButtonIndex, uint8_t otherButton1, uint8_t otherButton2) {
for (uint8_t i = 0; i < oversampleFactor; ++i) {
@ -17,9 +15,20 @@ bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversample
CHECK(!b.ButtonPressed(otherButton2));
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // reset to waiting
b.Step();
modules::time::IncMillis();
}
// just before the debounce trigger
CHECK(!b.ButtonPressed(testedButtonIndex));
CHECK(!b.ButtonPressed(otherButton1));
CHECK(!b.ButtonPressed(otherButton2));
// Tune the alg to overcome an edge case in debouncing timing - just in the unit test
// This is very brittle, needs some work @TODO to clean up
modules::time::IncMillis(4);
b.Step(); // reset to waiting
CHECK(b.ButtonPressed(testedButtonIndex));
CHECK(!b.ButtonPressed(otherButton1));
CHECK(!b.ButtonPressed(otherButton2));
@ -37,12 +46,12 @@ bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversample
/// This test verifies the behaviour of a single button. The other buttons must remain intact.
bool Step_Basic_One_Button(hal::adc::TADCData &&d, uint8_t testedButtonIndex) {
using namespace modules::buttons;
namespace mb = modules::buttons;
modules::time::ReinitTimebase();
Buttons b;
mb::Buttons b;
// need to oversample the data as debouncing takes 100 cycles to accept a pressed button
constexpr uint8_t oversampleFactor = 100;
constexpr uint8_t oversampleFactor = config::buttonsDebounceMs;
hal::adc::ReinitADC(config::buttonsADCIndex, std::move(d), oversampleFactor);
uint8_t otherButton1 = 1, otherButton2 = 2;
@ -61,17 +70,11 @@ bool Step_Basic_One_Button(hal::adc::TADCData &&d, uint8_t testedButtonIndex) {
}
TEST_CASE("buttons::Step-basic-button", "[buttons]") {
{
hal::adc::TADCData d({ 5, 6, 1023 });
CHECK(Step_Basic_One_Button(std::move(d), 0));
}
{
hal::adc::TADCData d({ 321, 359, 1023 });
CHECK(Step_Basic_One_Button(std::move(d), 1));
}
{
hal::adc::TADCData d({ 501, 529, 1023 });
CHECK(Step_Basic_One_Button(std::move(d), 2));
for (uint8_t i = 0; i < config::buttonCount; ++i) {
CHECK(Step_Basic_One_Button({ config::buttonADCLimits[i][0],
config::buttonADCLimits[i][1],
adcMaxValue },
i));
}
}
@ -79,12 +82,14 @@ TEST_CASE("buttons::Step-basic-button", "[buttons]") {
/// and the Buttons class should press first button and release, then the second one and then the third one
/// without being reinitialized.
TEST_CASE("buttons::Step-basic-button-one-after-other", "[buttons]") {
using namespace modules::buttons;
hal::adc::TADCData d({ 5, 6, 1023, 321, 359, 1023, 501, 529, 1023 });
Buttons b;
namespace mb = modules::buttons;
hal::adc::TADCData d({ config::buttonADCLimits[0][0], config::buttonADCLimits[0][0] + 1, adcMaxValue,
config::buttonADCLimits[1][0], config::buttonADCLimits[1][0] + 1, adcMaxValue,
config::buttonADCLimits[2][0], config::buttonADCLimits[2][0] + 1, adcMaxValue });
mb::Buttons b;
// need to oversample the data as debouncing takes 100 cycles to accept a pressed button
constexpr uint8_t oversampleFactor = 100;
constexpr uint8_t oversampleFactor = config::buttonsDebounceMs;
hal::adc::ReinitADC(config::buttonsADCIndex, std::move(d), oversampleFactor);
CHECK(Step_Basic_One_Button_Test(b, oversampleFactor, 0, 1, 2));
@ -94,17 +99,17 @@ TEST_CASE("buttons::Step-basic-button-one-after-other", "[buttons]") {
/// This test tries to simulate a bouncing effect on data from ADC on the first button
TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
using namespace modules::buttons;
namespace mb = modules::buttons;
// make a bounce event on the first press
hal::adc::TADCData d({ 5, 1023, 5, 9, 6, 7, 8, 1023, 1023 });
hal::adc::TADCData d({ 5, adcMaxValue, 5, 9, 6, 7, 8, adcMaxValue, adcMaxValue });
// need to oversample the data as debouncing takes 100 cycles to accept a pressed button
constexpr uint8_t oversampleFactor = 25;
hal::adc::ReinitADC(config::buttonsADCIndex, std::move(d), oversampleFactor);
modules::time::ReinitTimebase();
Buttons b;
mb::Buttons b;
// 5
for (uint8_t i = 0; i < oversampleFactor; ++i) {

View File

@ -5,8 +5,8 @@
namespace hal {
namespace adc {
static TADCData values2Return[2];
static TADCData::const_iterator rdptr[2] = { values2Return[0].cbegin(), values2Return[1].cbegin() };
static TADCData values2Return[6];
static TADCData::const_iterator rdptr[6] = { values2Return[0].cbegin(), values2Return[1].cbegin() };
static uint8_t oversampleFactor = 1;
static uint8_t oversample = 1; ///< current count of oversampled values returned from the ADC - will get filled with oversampleFactor once it reaches zero