From bbaeb25b54745692a2050da6ccb19dc066b5f889 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Wed, 6 Sep 2023 15:51:36 +0200 Subject: [PATCH] Some optimizations. --- MIDI-Host-Adapter.ino | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/MIDI-Host-Adapter.ino b/MIDI-Host-Adapter.ino index 6ccaa51..6610658 100644 --- a/MIDI-Host-Adapter.ino +++ b/MIDI-Host-Adapter.ino @@ -45,7 +45,6 @@ bool high_freq = false; TM1638plus_Model2 tm(STROBE_TM, CLOCK_TM, DIO_TM, swap_nibbles, high_freq); elapsedMillis button_debounce_timer; elapsedMillis blink_timer; -elapsedMillis double_press_timer; uint8_t actual_channel; uint8_t new_channel; @@ -89,31 +88,33 @@ void loop() { button = tm.ReadKey16(); - if (button > 0 && button_debounce_timer > BUTTON_DEBOUNCE_TIME_MS && double_press_timer > BUTTON_DOUBLE_PRESS_MS) { - // Button pressed once - Serial.print("Button once: "); - Serial.println(button, DEC); - button_debounce_timer = 0; - double_press_timer = 0; - blink_timer = 0; - new_channel = button; - tm.DisplayDecNumNibble(new_channel, actual_channel, 0, false, TMAlignTextRight); - } else if (button > 0 && button_debounce_timer > BUTTON_DEBOUNCE_TIME_MS && double_press_timer < BUTTON_DOUBLE_PRESS_MS && button == new_channel) { - // Button pressed twice - Serial.print("Button twice: "); - Serial.println(button, DEC); - actual_channel = new_channel; - EEPROM.update(EEPROM_ADDRESS, 0xf0 | actual_channel); - SetMidiChannel(actual_channel); - } else if (actual_channel != new_channel) { + if (button > 0 && button != actual_channel && button_debounce_timer > BUTTON_DEBOUNCE_TIME_MS) { + if (button != new_channel) { + // Button pressed once + Serial.print("Button once: "); + Serial.println(button, DEC); + button_debounce_timer = 0; + blink_timer = 0; + new_channel = button; + tm.DisplayDecNumNibble(new_channel, actual_channel, 0, false, TMAlignTextRight); + } else { + // Button pressed twice + Serial.print("Button twice: "); + Serial.println(button, DEC); + actual_channel = new_channel; + EEPROM.update(EEPROM_ADDRESS, 0xf0 | actual_channel); + SetMidiChannel(actual_channel); + } + } + + if (actual_channel != new_channel) { if (blink_timer > BLINK_FREQUENCY_MS) { blink_timer = 0; tm.DisplayDecNum(actual_channel, 0, false, TMAlignTextRight); } else if (blink_timer > BLINK_FREQUENCY_MS / 2) { tm.DisplayDecNumNibble(new_channel, actual_channel, 0, false, TMAlignTextRight); } - } else - tm.DisplayDecNum(actual_channel, 0, false, TMAlignTextRight); + } else tm.DisplayDecNum(actual_channel, 0, false, TMAlignTextRight); } void SetMidiChannel(uint8_t channel) {