diff --git a/OSC2MIDI.ino b/OSC2MIDI.ino index 4a83c7a..88c3da4 100644 --- a/OSC2MIDI.ino +++ b/OSC2MIDI.ino @@ -16,6 +16,7 @@ #include #include #include +#include #define MDNS_NAME "osc2midi" #define AP_SSID_NAME "OSC2MIDI" @@ -31,10 +32,15 @@ #define LCD_ROW 2 #define UDP_RECV_PORT 8000 #define UDP_SEND_PORT 9000 -#define K_RATE 200 +#define KRATE_MODE 200 +#define KRATE_STATE 2000 void OSCToMidiCC(OSCMessage &msg, int offset); void MidiCCToOSC(uint8_t channel, uint8_t number, uint8_t value); +void change_midi_state(uint8_t midichannel, uint8_t cc, uint8_t value); +void show_midi_state(void); +void set_midi_state(void); +void check_mode(void); WiFiUDP udp; IPAddress clientIP; @@ -48,8 +54,10 @@ HardwareSerial midi1(2); // RX: 16, TX: 17 //#define TX (1) #endif SoftwareSerial midi2; -uint32_t k_rate_last; bool ap_mode_state = digitalRead(AP_MODE_PIN); +int8_t state[16][128]; + +looper sched; MIDI_CREATE_INSTANCE(HardwareSerial, midi1, MIDI1); @@ -155,6 +163,8 @@ void setup() lcd.print(WiFi.localIP()); } + memset(state, -1, 16 * 128); + udp.begin(UDP_RECV_PORT); DEBUG_MSG("Listening for UDP packets on port %d\n", UDP_RECV_PORT); @@ -166,7 +176,10 @@ void setup() MIDI1.setHandleControlChange(MidiCCToOSC); MIDI1.turnThruOff(); - k_rate_last = millis(); + //set_midi_state(); + + sched.addJob(check_mode, KRATE_MODE); + sched.addJob(show_midi_state, KRATE_STATE); } void loop() @@ -229,19 +242,7 @@ void loop() } } - // Do something at control rate - if ((millis() - K_RATE) > k_rate_last) - { - k_rate_last = millis(); - if (ap_mode_state != digitalRead(AP_MODE_PIN)) - { - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print("Changing mode..."); - delay(500); - ESP.restart(); - } - } + sched.scheduler(); } void OSCToMidiCC(OSCMessage & msg, int offset) @@ -252,7 +253,6 @@ void OSCToMidiCC(OSCMessage & msg, int offset) msg.getAddress(address, offset, sizeof(address)); midichannel = getMIDIChannel(address); - //midichannel--; if (msg.size() == 1 && msg.isFloat(0)) { @@ -262,6 +262,7 @@ void OSCToMidiCC(OSCMessage & msg, int offset) value = value > 127 ? 127 : value; DEBUG_MSG("MSG: % s\tChannel: % u\t\tCC: % u\tValue: % u\n", address, midichannel, cc, value); MIDI1.sendControlChange(cc, value, midichannel); + change_midi_state(midichannel, cc, value); } else if (msg.size() == 2 && msg.isFloat(0) && msg.isFloat(1)) { @@ -271,11 +272,13 @@ void OSCToMidiCC(OSCMessage & msg, int offset) value = value > 127 ? 127 : value; DEBUG_MSG("MSG: % s\tChannel: % u\t\tCC: % u\tValue: % u\n", address, midichannel, cc, value); MIDI1.sendControlChange(cc, value, midichannel); + change_midi_state(midichannel, cc, value); cc = getVar(address, 2); value = round(msg.getFloat(1)); value = value > 127 ? 127 : value; DEBUG_MSG("MSG: % s\tChannel: % u\t\tCC: % u\tValue: % u\n", address, midichannel, cc, value); MIDI1.sendControlChange(cc, value, midichannel); + change_midi_state(midichannel, cc, value); } else { @@ -301,3 +304,64 @@ void MidiCCToOSC(uint8_t channel, uint8_t number, uint8_t val) udp.endPacket(); } } + +void change_midi_state(uint8_t midichannel, uint8_t cc, uint8_t value) +{ + state[midichannel - 1][cc - 1] = value; +} + +void show_midi_state(void) +{ + uint8_t m, c; + + DEBUG_MSG("Current MIDI state:\n"); + + for (m = 0; m < 16; m++) + { + DEBUG_MSG("MIDI-Channel %d\n", m + 1); + for (c = 0; c < 128; c++) + { + if (state[m][c] >= 0) + { + DEBUG_MSG("\tCC: %03d = %03d\n", c, state[m][c]); + } + } + } +} + +/* + void set_midi_state(void) + { + uint8_t tmp; + uint8_t m; + uint16_t i; + + DEBUG_MSG("Send MIDI state:\n"); + + for (m = 0; m < 16; m++); + { + if (state[m].size() > 0) + { + DEBUG_MSG("MIDI-Channel %d\n"); + for (i = 0; i < state[m].size(); i++) + { + tmp = state[m].get(i); + MIDI1.sendControlChange(tmp >> 4, tmp & 0x0f, m); + DEBUG_MSG("\tSet state of CC: %03d = %03d\n", tmp >> 4, tmp & 0x0f); + } + } + } + } +*/ + +void check_mode(void) +{ + if (ap_mode_state != digitalRead(AP_MODE_PIN)) + { + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Changing mode..."); + delay(500); + ESP.restart(); + } +}