From c2325b687a8536525d625916000260d06e419fd9 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Mon, 8 Nov 2021 11:51:22 +0100 Subject: [PATCH] Unittests: set buttons more consistently --- src/modules/user_input.cpp | 6 +-- tests/unit/logic/helpers/helpers.ipp | 9 ++++ .../load_filament/test_load_filament.cpp | 29 +++------- .../unload_filament/test_unload_filament.cpp | 16 ++---- tests/unit/modules/buttons/test_buttons.cpp | 54 +++++++++---------- 5 files changed, 48 insertions(+), 66 deletions(-) diff --git a/src/modules/user_input.cpp b/src/modules/user_input.cpp index a23635b..a178f2c 100644 --- a/src/modules/user_input.cpp +++ b/src/modules/user_input.cpp @@ -8,11 +8,11 @@ namespace user_input { UserInput userInput; void UserInput::Step() { - if (buttons::buttons.ButtonPressed(0)) + if (buttons::buttons.ButtonPressed(mb::Left)) eventQueue.push(Event::Left); - if (buttons::buttons.ButtonPressed(1)) + if (buttons::buttons.ButtonPressed(mb::Middle)) eventQueue.push(Event::Middle); - if (buttons::buttons.ButtonPressed(2)) + if (buttons::buttons.ButtonPressed(mb::Right)) eventQueue.push(Event::Right); } diff --git a/tests/unit/logic/helpers/helpers.ipp b/tests/unit/logic/helpers/helpers.ipp index 3b75a5a..c22317f 100644 --- a/tests/unit/logic/helpers/helpers.ipp +++ b/tests/unit/logic/helpers/helpers.ipp @@ -143,3 +143,12 @@ void InvalidSlot(SM &logicSM, uint8_t activeSlot, uint8_t invSlot){ logicSM.Reset(invSlot); REQUIRE(VerifyState(logicSM, mg::FilamentLoadState::AtPulley, mi::Idler::IdleSlotIndex(), activeSlot, false, false, ml::off, ml::off, ErrorCode::INVALID_TOOL, ProgressCode::OK)); } + +template +void PressButtonAndDebounce(SM &sm, uint8_t btnIndex){ + hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[btnIndex][0] + 1); + while (!mb::buttons.ButtonPressed(btnIndex)) { + main_loop(); + sm.StepInner(); + } +} diff --git a/tests/unit/logic/load_filament/test_load_filament.cpp b/tests/unit/logic/load_filament/test_load_filament.cpp index 4e03350..a29f889 100644 --- a/tests/unit/logic/load_filament/test_load_filament.cpp +++ b/tests/unit/logic/load_filament/test_load_filament.cpp @@ -99,13 +99,7 @@ void FailedLoadToFindaResolveHelp(uint8_t slot, logic::LoadFilament &lf) { // - resolve the problem by hand - after pressing the button we shall check, that FINDA is off and we should do what? // In this case we check the first option - - // Perform press on button 0 + debounce - hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[0][0] + 1); - while (!mb::buttons.ButtonPressed(0)) { - main_loop(); - lf.Step(); - } + PressButtonAndDebounce(lf, mb::Left); REQUIRE(VerifyState(lf, mg::FilamentLoadState::InSelector, mi::Idler::IdleSlotIndex(), slot, false, true, ml::off, ml::blink0, ErrorCode::FINDA_DIDNT_SWITCH_ON, ProgressCode::ERREngagingIdler)); @@ -140,12 +134,9 @@ void FailedLoadToFindaResolveManual(uint8_t slot, logic::LoadFilament &lf) { // simulate the user fixed the issue himself // Perform press on button 2 + debounce + switch on FINDA - hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[2][0] + 1); hal::gpio::WritePin(FINDA_PIN, hal::gpio::Level::high); - while (!mb::buttons.ButtonPressed(2)) { - main_loop(); - lf.Step(); - } + PressButtonAndDebounce(lf, mb::Right); + // pulling filament back REQUIRE(VerifyState(lf, mg::FilamentLoadState::InSelector, mi::Idler::IdleSlotIndex(), slot, true, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::RetractingFromFinda)); @@ -168,22 +159,14 @@ void FailedLoadToFindaResolveManual(uint8_t slot, logic::LoadFilament &lf) { void FailedLoadToFindaResolveManualNoFINDA(uint8_t slot, logic::LoadFilament &lf) { // Perform press on button 2 + debounce + keep FINDA OFF (i.e. the user didn't solve anything) - hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[2][0] + 1); - while (!mb::buttons.ButtonPressed(2)) { - main_loop(); - lf.Step(); - } + PressButtonAndDebounce(lf, mb::Right); + // pulling filament back REQUIRE(VerifyState(lf, mg::FilamentLoadState::InSelector, mi::Idler::IdleSlotIndex(), slot, false, true, ml::off, ml::blink0, ErrorCode::FINDA_DIDNT_SWITCH_ON, ProgressCode::ERRWaitingForUser)); } void FailedLoadToFindaResolveTryAgain(uint8_t slot, logic::LoadFilament &lf) { - // Perform press on button 1 + debounce - hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[1][0] + 1); - while (!mb::buttons.ButtonPressed(1)) { - main_loop(); - lf.Step(); - } + PressButtonAndDebounce(lf, mb::Middle); // the state machine should have restarted REQUIRE(VerifyState(lf, mg::FilamentLoadState::InSelector, mi::Idler::IdleSlotIndex(), slot, false, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::FeedingToFinda)); diff --git a/tests/unit/logic/unload_filament/test_unload_filament.cpp b/tests/unit/logic/unload_filament/test_unload_filament.cpp index e9c8562..22ecf77 100644 --- a/tests/unit/logic/unload_filament/test_unload_filament.cpp +++ b/tests/unit/logic/unload_filament/test_unload_filament.cpp @@ -156,12 +156,8 @@ void FindaDidntTriggerResolveHelp(uint8_t slot, logic::UnloadFilament &uf) { // In this case we check the first option - // Perform press on button 1 + debounce - hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[0][0] + 1); - while (!mb::buttons.ButtonPressed(0)) { - main_loop(); - uf.StepInner(); - } + // Perform press on button 0 + debounce + PressButtonAndDebounce(uf, mb::Left); // we still think we have filament loaded at this stage // idler should have been disengaged @@ -239,13 +235,7 @@ void FindaDidntTriggerResolveTryAgain(uint8_t slot, logic::UnloadFilament &uf) { // - resolve the problem by hand - after pressing the button we shall check, that FINDA is off and we should do what? // In this case we check the second option - - // Perform press on button 2 + debounce - hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[1][0] + 1); - while (!mb::buttons.ButtonPressed(1)) { - main_loop(); - uf.StepInner(); - } + PressButtonAndDebounce(uf, mb::Middle); // we still think we have filament loaded at this stage // idler should have been disengaged diff --git a/tests/unit/modules/buttons/test_buttons.cpp b/tests/unit/modules/buttons/test_buttons.cpp index aef94ed..68a4a3f 100644 --- a/tests/unit/modules/buttons/test_buttons.cpp +++ b/tests/unit/modules/buttons/test_buttons.cpp @@ -115,79 +115,79 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") { b.Step(); // should detect the press but remain in detected state - wait for debounce mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 1023 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // reset to waiting mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 5 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // pressed again, still in debouncing state mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 9 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // no change mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 6 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // no change mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 7 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // one step from "pressed" mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 8 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // fifth set of samples - should report "pressed" finally mt::IncMillis(); } - CHECK(b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 1023 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // sixth set of samples - button released (no debouncing on release) mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); // 1023 for (uint8_t i = 0; i < oversampleFactor; ++i) { b.Step(); // seventh set of samples - still released mt::IncMillis(); } - CHECK(!b.ButtonPressed(0)); - CHECK(!b.ButtonPressed(1)); - CHECK(!b.ButtonPressed(2)); + CHECK(!b.ButtonPressed(mb::Left)); + CHECK(!b.ButtonPressed(mb::Middle)); + CHECK(!b.ButtonPressed(mb::Right)); }