From 268feec514ab6f9f75028e858e9de2c1749161c3 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Wed, 30 Aug 2023 13:35:15 +0200 Subject: [PATCH] Several fixes. --- MIDI-Host-Adapter.ino | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/MIDI-Host-Adapter.ino b/MIDI-Host-Adapter.ino index 127b2cb..4a082d6 100644 --- a/MIDI-Host-Adapter.ino +++ b/MIDI-Host-Adapter.ino @@ -31,13 +31,13 @@ #define MIDI_CH_BIT1 3 #define MIDI_CH_BIT0 2 -bool swap_nibbles = false; +bool swap_nibbles = true; bool high_freq = false; #define BRIGHTNESS 6 #define INITIAL_MIDI_CHANNEL 1 -#define BUTTON_DEBOUNCE_TIME_MS 50 -#define BUTTON_DOUBLE_PRESS_MS 200 +#define BUTTON_DEBOUNCE_TIME_MS 150 +#define BUTTON_DOUBLE_PRESS_MS 250 #define BLINK_FREQUENCY_MS 500 #define EEPROM_ADDRESS 0x42 @@ -47,6 +47,7 @@ elapsedMillis button_debounce_timer; elapsedMillis blink_timer; elapsedMillis double_press_timer; uint8_t actual_channel; +uint8_t new_channel; void setup() { Serial.begin(9600); @@ -67,6 +68,7 @@ void setup() { Serial.print("Reading channel from EEPROM: "); Serial.println(actual_channel, DEC); } + new_channel = actual_channel; pinMode(MIDI_CH_BIT3, OUTPUT); pinMode(MIDI_CH_BIT2, OUTPUT); @@ -84,7 +86,6 @@ void setup() { void loop() { uint8_t button; - static uint8_t new_channel; button = tm.ReadKey16(); @@ -93,6 +94,7 @@ void loop() { 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); @@ -101,16 +103,17 @@ void loop() { Serial.print("Button twice: "); Serial.println(button, DEC); actual_channel = new_channel; - EEPROM.update(EEPROM_ADDRESS, 0xf0 & actual_channel); + EEPROM.update(EEPROM_ADDRESS, 0xf0 | actual_channel); SetMidiChannel(actual_channel); - double_press_timer = 0; - } else if (actual_channel != new_channel) + } else 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); } void SetMidiChannel(uint8_t channel) { @@ -133,8 +136,7 @@ void SetMidiChannel(uint8_t channel) { } void printBinary(uint32_t value, uint8_t len) { - for (len; len >= 0; len--) { - Serial.print((char)('0' + ((value >> len) & 1))); + for (uint8_t b = len; b > 0; --b) { + Serial.print((char)('0' + ((value >> (b - 1)) & 1))); } - Serial.println(); } \ No newline at end of file