Created
October 20, 2020 21:48
-
-
Save cowboy/21de5923a35ae02c7481595e8b58a1bf to your computer and use it in GitHub Desktop.
MIDI Merge for ATmega4809 (Curiosity Nano)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MIDI Merge for ATmega4809 | |
* Copyright (c) 2020 Benjamin Alman | |
* MIT License | |
*/ | |
#include <MIDI.h> | |
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midi_1); | |
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midi_2); | |
MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, midi_3); | |
// https://camo.githubusercontent.com/60339c7a6884cc766b6b80761017eeca1edab842/68747470733a2f2f692e696d6775722e636f6d2f626d64664346532e6a7067 | |
#define LED_TX 39 | |
#define LED_RX1 38 | |
#define LED_RX2 37 | |
#define LED_RX3 36 | |
void setup() { | |
pinMode(LED_TX, OUTPUT); | |
pinMode(LED_RX1, OUTPUT); | |
pinMode(LED_RX2, OUTPUT); | |
pinMode(LED_RX3, OUTPUT); | |
midi_1.begin(MIDI_CHANNEL_OMNI); | |
midi_2.begin(MIDI_CHANNEL_OMNI); | |
midi_3.begin(MIDI_CHANNEL_OMNI); | |
// Serial3.begin(57600); | |
} | |
const int LED_DURATION = 500; | |
const int MIDI_INPUT_COUNT = 3; | |
midi::MidiInterface<midi::SerialMIDI<HardwareSerial>> midi_tx = midi_2; | |
midi::MidiInterface<midi::SerialMIDI<HardwareSerial>> midi_rx[MIDI_INPUT_COUNT] = {midi_1, midi_2, midi_3}; | |
int led_rx_pin[MIDI_INPUT_COUNT] = {LED_RX1, LED_RX2, LED_RX3}; | |
int led_rx_count[MIDI_INPUT_COUNT] = {0, 0, 0}; | |
bool show_led(int type) { | |
return type != 254; // active sensing | |
} | |
void loop() { | |
int read_count = 0; | |
int off_count = 0; | |
for (int i = 0; i < MIDI_INPUT_COUNT; i++) { | |
if (midi_rx[i].read()) { | |
int type = midi_rx[i].getType(); | |
int data1 = midi_rx[i].getData1(); | |
int data2 = midi_rx[i].getData2(); | |
int channel = midi_rx[i].getChannel(); | |
midi_tx.send(type, data1, data2, channel); | |
// Serial3.printf("[MIDI %i] ch=%2i type=%3i data1=%3i data2=%3i\n", i + 1, channel, type, data1, data2); | |
if (show_led(type)) { | |
digitalWrite(led_rx_pin[i], HIGH); | |
read_count++; | |
led_rx_count[i] = 0; | |
} | |
} | |
if (++led_rx_count[i] >= LED_DURATION) { | |
led_rx_count[i] = LED_DURATION; | |
digitalWrite(led_rx_pin[i], LOW); | |
off_count++; | |
} | |
} | |
if (off_count == MIDI_INPUT_COUNT) { | |
digitalWrite(LED_TX, LOW); | |
} else if (read_count != 0) { | |
digitalWrite(LED_TX, HIGH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment