Change buttons' ADC limits into an array

as proposed by @vintagePC

cleaned-up some of the unit tests by this change
pull/46/head
D.R.racer 2021-07-02 09:04:46 +02:00 committed by DRracer
parent 552fec9abf
commit 43a423f299
5 changed files with 13 additions and 20 deletions

View File

@ -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 static constexpr const uint16_t findaADCDecisionLevel = 512; ///< ADC decision level when a FINDA is considered pressed/not pressed
// Buttons setup // 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 buttonsDebounceMs = 100;
static constexpr const uint16_t button0ADCMin = 0; static constexpr const uint16_t buttonADCLimits[buttonCount][2] = { { 0, 10 }, { 320, 360 }, { 500, 530 } };
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 uint8_t buttonsADCIndex = 0; ///< ADC index of buttons input static constexpr const uint8_t buttonsADCIndex = 0; ///< ADC index of buttons input
} // namespace config } // namespace config

View File

@ -14,19 +14,18 @@ int8_t Buttons::DecodeADC(uint16_t rawADC) {
// Button 3 - 516 // Button 3 - 516
// Doesn't handle multiple pressed buttons at once // Doesn't handle multiple pressed buttons at once
if (rawADC >= config::button0ADCMin && rawADC <= config::button0ADCMax) for (int8_t buttonIndex = 0; buttonIndex < config::buttonCount; ++buttonIndex) {
return 0; if (rawADC > config::buttonADCLimits[buttonIndex][0] && rawADC <= config::buttonADCLimits[buttonIndex][1])
else if (rawADC >= config::button1ADCMin && rawADC <= config::button1ADCMax) return buttonIndex;
return 1; }
else if (rawADC >= config::button2ADCMin && rawADC <= config::button2ADCMax)
return 2;
return -1; return -1;
} }
void Buttons::Step() { void Buttons::Step() {
uint16_t millis = modules::time::timebase.Millis(); uint16_t millis = modules::time::timebase.Millis();
int8_t currentState = DecodeADC(hal::adc::ReadADC(config::buttonsADCIndex)); 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 // this button was pressed if b == currentState, released otherwise
buttons[b].Step(millis, b == currentState); buttons[b].Step(millis, b == currentState);
} }

View File

@ -27,8 +27,6 @@ enum {
/// A model of the 3 buttons on the MMU unit /// A model of the 3 buttons on the MMU unit
class Buttons { class Buttons {
constexpr static const uint8_t N = 3; ///< number of buttons currently supported
public: public:
inline constexpr Buttons() = default; inline constexpr Buttons() = default;
@ -41,7 +39,7 @@ public:
/// @returns true if any of the button is pressed /// @returns true if any of the button is pressed
inline bool AnyButtonPressed() const { inline bool AnyButtonPressed() const {
for (uint8_t i = 0; i < N; ++i) { for (uint8_t i = 0; i < config::buttonCount; ++i) {
if (ButtonPressed(i)) if (ButtonPressed(i))
return true; return true;
} }
@ -49,7 +47,7 @@ public:
} }
private: private:
Button buttons[N]; Button buttons[config::buttonCount];
/// Decode ADC output into a button index /// Decode ADC output into a button index
/// @returns index of the button pressed or -1 in case no button is pressed /// @returns index of the button pressed or -1 in case no button is pressed

View File

@ -113,7 +113,7 @@ void FailedLoadToFindaResolveHelp(uint8_t slot, logic::LoadFilament &lf) {
// In this case we check the first option // In this case we check the first option
// Perform press on button 1 + debounce // 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)) { while (!mb::buttons.ButtonPressed(0)) {
main_loop(); main_loop();
lf.Step(); lf.Step();

View File

@ -183,7 +183,7 @@ void FindaDidntTriggerResolveHelp(uint8_t slot, logic::UnloadFilament &uf) {
// In this case we check the first option // In this case we check the first option
// Perform press on button 1 + debounce // 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)) { while (!mb::buttons.ButtonPressed(0)) {
main_loop(); main_loop();
uf.Step(); uf.Step();
@ -267,7 +267,7 @@ void FindaDidntTriggerResolveTryAgain(uint8_t slot, logic::UnloadFilament &uf) {
// In this case we check the second option // In this case we check the second option
// Perform press on button 2 + debounce // 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)) { while (!mb::buttons.ButtonPressed(1)) {
main_loop(); main_loop();
uf.Step(); uf.Step();