From 10d77ae3a5b1dc25a467524d704d0ee6a21cc672 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Mon, 28 Aug 2023 11:21:11 +0200 Subject: [PATCH] Initial commit with first untested code. --- MIDI-Host-Adapter.ino | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 MIDI-Host-Adapter.ino diff --git a/MIDI-Host-Adapter.ino b/MIDI-Host-Adapter.ino new file mode 100644 index 0000000..40c1573 --- /dev/null +++ b/MIDI-Host-Adapter.ino @@ -0,0 +1,79 @@ +#include +#include +#include + +#define STROBE_TM 9 +#define CLOCK_TM 8 +#define DIO_TM 7 +#define MIDI_CH_BIT3 5 +#define MIDI_CH_BIT2 4 +#define MIDI_CH_BIT1 3 +#define MIDI_CH_BIT0 2 + +bool swap_nibbles = false; +bool high_freq = false; + +#define BRIGHTNESS 6 +#define BUTTON_DEBOUNCE_TIME_MS 50 + +// Constructor object +TM1638plus_Model2 tm(STROBE_TM, CLOCK_TM, DIO_TM, swap_nibbles, high_freq); +elapsedMillis button_debounce_timer; + +void setup() { + Serial.begin(9600); + delay(100); + + Serial.println(""); + + tm.displayBegin(); + tm.brightness(BRIGHTNESS); + tm.DisplayStr("MIDIHOST", 0); + + pinMode(MIDI_CH_BIT3, OUTPUT); + pinMode(MIDI_CH_BIT2, OUTPUT); + pinMode(MIDI_CH_BIT1, OUTPUT); + pinMode(MIDI_CH_BIT0, OUTPUT); + digitalWrite(MIDI_CH_BIT3, LOW); + digitalWrite(MIDI_CH_BIT2, LOW); + digitalWrite(MIDI_CH_BIT1, LOW); + digitalWrite(MIDI_CH_BIT0, LOW); + + delay(500); + + Serial.println(""); +} + +void loop() { + uint8_t button; + + button = tm.ReadKey16(); + if (button > 0 && button_debounce_timer > BUTTON_DEBOUNCE_TIME_MS) { + Serial.print("Button: "); + Serial.println(button, DEC); + button_debounce_timer = 0; + tm.DisplayDecNum(button, 0, false, TMAlignTextRight); + + SetMidiChannel(button); + } +} + +void SetMidiChannel(uint8_t channel) { + Serial.print("Setting MIDI channel: "); + Serial.print(channel, DEC); + Serial.print(" ("); + printBinary(channel, 4); + Serial.println(")"); + + digitalWrite(MIDI_CH_BIT0, channel & 0x00); + digitalWrite(MIDI_CH_BIT1, channel & 0x01); + digitalWrite(MIDI_CH_BIT2, channel & 0x02); + digitalWrite(MIDI_CH_BIT3, channel & 0x08); +} + +void printBinary(uint32_t value, uint8_t len) { + for (len; len >= 0; len--) { + Serial.print((char)('0' + ((value >> len) & 1))); + } + Serial.println(); +} \ No newline at end of file