diff --git a/src/config/config.h b/src/config/config.h index 20da6ee..44b661d 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -21,13 +21,9 @@ static constexpr const uint8_t findaADCIndex = 1; ///< ADC index of FINDA input static constexpr const uint16_t findaADCDecisionLevel = 512; ///< ADC decision level when a FINDA is considered pressed/not pressed // Buttons setup +static constexpr const uint8_t buttonCount = 3; ///< number of buttons currently supported static constexpr const uint16_t buttonsDebounceMs = 100; -static constexpr const uint16_t button0ADCMin = 0; -static constexpr const uint16_t button0ADCMax = 10; -static constexpr const uint16_t button1ADCMin = 320; -static constexpr const uint16_t button1ADCMax = 360; -static constexpr const uint16_t button2ADCMin = 500; -static constexpr const uint16_t button2ADCMax = 530; +static constexpr const uint16_t buttonADCLimits[buttonCount][2] = { { 0, 10 }, { 320, 360 }, { 500, 530 } }; static constexpr const uint8_t buttonsADCIndex = 0; ///< ADC index of buttons input } // namespace config diff --git a/src/modules/buttons.cpp b/src/modules/buttons.cpp index 0d32fa7..5c3b6a1 100644 --- a/src/modules/buttons.cpp +++ b/src/modules/buttons.cpp @@ -14,19 +14,18 @@ int8_t Buttons::DecodeADC(uint16_t rawADC) { // Button 3 - 516 // Doesn't handle multiple pressed buttons at once - if (rawADC >= config::button0ADCMin && rawADC <= config::button0ADCMax) - return 0; - else if (rawADC >= config::button1ADCMin && rawADC <= config::button1ADCMax) - return 1; - else if (rawADC >= config::button2ADCMin && rawADC <= config::button2ADCMax) - return 2; + for (int8_t buttonIndex = 0; buttonIndex < config::buttonCount; ++buttonIndex) { + if (rawADC > config::buttonADCLimits[buttonIndex][0] && rawADC <= config::buttonADCLimits[buttonIndex][1]) + return buttonIndex; + } + return -1; } void Buttons::Step() { uint16_t millis = modules::time::timebase.Millis(); int8_t currentState = DecodeADC(hal::adc::ReadADC(config::buttonsADCIndex)); - for (uint_fast8_t b = 0; b < N; ++b) { + for (uint_fast8_t b = 0; b < config::buttonCount; ++b) { // this button was pressed if b == currentState, released otherwise buttons[b].Step(millis, b == currentState); } diff --git a/src/modules/buttons.h b/src/modules/buttons.h index d9be196..d0e423c 100644 --- a/src/modules/buttons.h +++ b/src/modules/buttons.h @@ -27,8 +27,6 @@ enum { /// A model of the 3 buttons on the MMU unit class Buttons { - constexpr static const uint8_t N = 3; ///< number of buttons currently supported - public: inline constexpr Buttons() = default; @@ -41,7 +39,7 @@ public: /// @returns true if any of the button is pressed inline bool AnyButtonPressed() const { - for (uint8_t i = 0; i < N; ++i) { + for (uint8_t i = 0; i < config::buttonCount; ++i) { if (ButtonPressed(i)) return true; } @@ -49,7 +47,7 @@ public: } private: - Button buttons[N]; + Button buttons[config::buttonCount]; /// Decode ADC output into a button index /// @returns index of the button pressed or -1 in case no button is pressed diff --git a/tests/unit/logic/load_filament/test_load_filament.cpp b/tests/unit/logic/load_filament/test_load_filament.cpp index 50ffe1b..36694d7 100644 --- a/tests/unit/logic/load_filament/test_load_filament.cpp +++ b/tests/unit/logic/load_filament/test_load_filament.cpp @@ -113,7 +113,7 @@ void FailedLoadToFindaResolveHelp(uint8_t slot, logic::LoadFilament &lf) { // In this case we check the first option // Perform press on button 1 + debounce - hal::adc::SetADC(config::buttonsADCIndex, 1); + hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[0][0] + 1); while (!mb::buttons.ButtonPressed(0)) { main_loop(); lf.Step(); diff --git a/tests/unit/logic/unload_filament/test_unload_filament.cpp b/tests/unit/logic/unload_filament/test_unload_filament.cpp index f1644c8..8b38fa9 100644 --- a/tests/unit/logic/unload_filament/test_unload_filament.cpp +++ b/tests/unit/logic/unload_filament/test_unload_filament.cpp @@ -183,7 +183,7 @@ 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, 1); + hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[0][0] + 1); while (!mb::buttons.ButtonPressed(0)) { main_loop(); uf.Step(); @@ -267,7 +267,7 @@ void FindaDidntTriggerResolveTryAgain(uint8_t slot, logic::UnloadFilament &uf) { // In this case we check the second option // Perform press on button 2 + debounce - hal::adc::SetADC(config::buttonsADCIndex, 340); + hal::adc::SetADC(config::buttonsADCIndex, config::buttonADCLimits[1][0] + 1); while (!mb::buttons.ButtonPressed(1)) { main_loop(); uf.Step();