diff --git a/MIDI-Host-Adapter.ino b/MIDI-Host-Adapter.ino index 55559d8..127b2cb 100644 --- a/MIDI-Host-Adapter.ino +++ b/MIDI-Host-Adapter.ino @@ -1,6 +1,27 @@ +/* + MIDI-Host-Adapter + + (c)2023 H. Wirtz + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + #include #include #include +#include #define STROBE_TM 9 #define CLOCK_TM 8 @@ -18,16 +39,18 @@ bool high_freq = false; #define BUTTON_DEBOUNCE_TIME_MS 50 #define BUTTON_DOUBLE_PRESS_MS 200 #define BLINK_FREQUENCY_MS 500 +#define EEPROM_ADDRESS 0x42 // Constructor object 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; void setup() { Serial.begin(9600); - delay(100); + delay(50); Serial.println(""); @@ -35,6 +58,16 @@ void setup() { tm.brightness(BRIGHTNESS); tm.DisplayStr("MIDIHOST", 0); + uint8_t tmp_channel = EEPROM.read(EEPROM_ADDRESS); + if (tmp_channel & 0xf0 != 0xf0) { + actual_channel = 1; + Serial.println("Setting channel to 1"); + } else { + actual_channel = 0x0f & tmp_channel; + Serial.print("Reading channel from EEPROM: "); + Serial.println(actual_channel, DEC); + } + pinMode(MIDI_CH_BIT3, OUTPUT); pinMode(MIDI_CH_BIT2, OUTPUT); pinMode(MIDI_CH_BIT1, OUTPUT); @@ -44,19 +77,15 @@ void setup() { delay(500); + tm.DisplayDecNum(actual_channel, 0, false, TMAlignTextRight); + Serial.println(""); } void loop() { uint8_t button; - static uint8_t actual_channel; static uint8_t new_channel; - if (actual_channel == 0) { - actual_channel = 1; - tm.DisplayDecNum(actual_channel, 0, false, TMAlignTextRight); - } - button = tm.ReadKey16(); if (button > 0 && button_debounce_timer > BUTTON_DEBOUNCE_TIME_MS && double_press_timer > BUTTON_DOUBLE_PRESS_MS) { @@ -72,6 +101,7 @@ void loop() { Serial.print("Button twice: "); Serial.println(button, DEC); actual_channel = new_channel; + EEPROM.update(EEPROM_ADDRESS, 0xf0 & actual_channel); SetMidiChannel(actual_channel); double_press_timer = 0; } else if (actual_channel != new_channel) @@ -84,7 +114,12 @@ void loop() { } void SetMidiChannel(uint8_t channel) { - Serial.print("Setting MIDI channel: "); + if (channel < 1 || channel > 16) { + Serial.print("ERROR: MIDI channel number out of range: "); + Serial.print(channel, DEC); + return; + } + Serial.print("Setting MIDI channel to: "); Serial.print(channel, DEC); channel--; Serial.print(" (");