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
// 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

View File

@ -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);
}

View File

@ -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

View File

@ -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();

View File

@ -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();