diff --git a/OSC2MidiGateway.ino b/OSC2MidiGateway.ino index 34d3cfd..5ab1092 100644 --- a/OSC2MidiGateway.ino +++ b/OSC2MidiGateway.ino @@ -1,14 +1,3 @@ -// .../Arduino/hardware/espressif/esp32/cores/esp32/HardwareSerial.cpp: -/* - #ifndef RX1 - #define RX1 35 - #endif - - #ifndef TX1 - #define TX1 34 - #endif -*/ - #if !(defined(ESP32) ) #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif @@ -16,11 +5,36 @@ // Use from 0 to 4. Higher number, more debugging messages and memory usage. #define _WIFIMGR_LOGLEVEL_ 3 - #define DEBUG 1 -#define AP_TRIGGER_PIN 15 +#define MDNS_NAME "osc2midi" +#define AP_SSID_NAME "OSC2MIDI" +#define AP_SSID_CONFIG_NAME "OSC2MIDI-Config" +#define AP_PASSWORD "osc2midi" +#define MDNS_NAME "osc2midi" +#define SOFT_SERIAL_RX 18 +#define SOFT_SERIAL_TX 19 #define AP_TIMEOUT 120 -#define AP_NAME "OSC2MidiBridge" +#define AP_TRIGGER_PIN 15 +#define AP_DATA_RESET_PIN 15 +#define AP_MODE 23 +#define LCD_I2C_ADDR 0x27 +#define LCD_COL 16 +#define LCD_ROW 2 + +#define FileFS SPIFFS +#define FS_Name "SPIFFS" +#define MIN_AP_PASSWORD_SIZE 8 +#define SSID_MAX_LEN 32 +#define PASS_MAX_LEN 64 +#define NUM_WIFI_CREDENTIALS 2 +#define CONFIG_FILENAME F("/wifi_cred.dat") +// You only need to format the filesystem once +//#define FORMAT_FILESYSTEM +//#define RESET_AP_DATA +#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 100 +#define WIFI_MULTI_CONNECT_WAITING_MS 200 +#define WIFICHECK_INTERVAL 1000 +#define HEARTBEAT_INTERVAL 10000 #include #include @@ -49,23 +63,7 @@ #include "OSC2Midi.h" #include #include - -#define FileFS SPIFFS -#define FS_Name "SPIFFS" -#define MIN_AP_PASSWORD_SIZE 8 -#define SSID_MAX_LEN 32 -#define PASS_MAX_LEN 64 -#define NUM_WIFI_CREDENTIALS 2 -#define CONFIG_FILENAME F("/wifi_cred.dat") -// You only need to format the filesystem once -//#define FORMAT_FILESYSTEM -//#define RESET_AP_DATA -#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 100 -#define WIFI_MULTI_CONNECT_WAITING_MS 200 -#define WIFICHECK_INTERVAL 1000 -#define HEARTBEAT_INTERVAL 10000 -#define AP_DATA_RESET_PIN 15 -#define AP_MODE 23 +#include typedef struct { @@ -101,6 +99,7 @@ String AP_PASS; IPAddress stationIP = IPAddress(0, 0, 0, 0); IPAddress gatewayIP = IPAddress(192, 168, 2, 1); IPAddress netMask = IPAddress(255, 255, 255, 0); +LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_COL, LCD_ROW); /* UART RX IO TX IO CTS RTS @@ -110,30 +109,54 @@ IPAddress netMask = IPAddress(255, 255, 255, 0); */ HardwareSerial midi1(2); // RX: 16, TX: 17 -//HardwareSerial midi2(1); // RX: 35, TX: 34 (changed in .../Arduino/hardware/espressif/esp32/cores/esp32/HardwareSerial.cpp!) -#define D5 (18) -#define D6 (19) +#ifndef D5 +#define D5 (SOFT_SERIAL_RX) +#define D6 (SOFT_SERIAL_TX) +//#define D7 (23) +//#define D8 (5) +//#define TX (1) +#endif SoftwareSerial midi2; MIDI_CREATE_INSTANCE(HardwareSerial, midi1, MIDI1); -MIDI_CREATE_INSTANCE(SoftwareSerial, midi2, MIDI2); void setup() { + pinMode(AP_TRIGGER_PIN, INPUT_PULLUP); + pinMode(AP_DATA_RESET_PIN, INPUT_PULLUP); + pinMode(AP_MODE, INPUT_PULLUP); + + Serial.begin(115200); + Serial.setDebugOutput(true); + + DEBUG_MSG("\nOSC2Midi\n"); + + lcd.init(); + lcd.backlight(); + lcd.clear(); + lcd.noCursor(); + lcd.setCursor(0, 0); + lcd.print("OSC2MIDI"); + WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP Serial.print("\nStarting AutoConnectWithFeedBack using " + String(FS_Name)); Serial.println(" on " + String(ARDUINO_BOARD)); - pinMode(AP_DATA_RESET_PIN, INPUT_PULLUP); - pinMode(AP_MODE, INPUT_PULLUP); if (digitalRead(AP_MODE) == LOW) { - Serial.println("Mode: AP"); + Serial.println("Mode AccessPoint"); + WiFi.softAP(AP_SSID_NAME, AP_PASSWORD); + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Mode AP"); } else { + Serial.println("Mode: Client"); + lcd.clear(); + #if defined(FORMAT_FILESYSTEM) FileFS.format(); #endif @@ -192,8 +215,8 @@ void setup() } // SSID and PW for Config Portal - AP_SSID = "OSC2MIDI"; - AP_PASS = "osc2midi"; + AP_SSID = AP_SSID_CONFIG_NAME; + AP_PASS = AP_PASSWORD; // From v1.1.0, Don't permit NULL password if ( (Router_SSID == "") || (Router_Pass == "") ) @@ -205,9 +228,17 @@ void setup() // Starts an access point //if (!ESP_wifiManager.startConfigPortal((const char *) ssid.c_str(), password)) if ( !ESP_wifiManager.startConfigPortal(AP_SSID.c_str(), AP_PASS.c_str()) ) + { Serial.println("Not connected to WiFi but continuing anyway."); + lcd.setCursor(0, 0); + lcd.print("Mode WiFi Cfg-AP"); + lcd.setCursor(0, 1); + lcd.print("192.168.100.1"); + } else + { Serial.println("WiFi connected...yeey :)"); + } memset(&WM_config, 0, sizeof(WM_config)); @@ -279,7 +310,7 @@ void setup() else Serial.println(ESP_wifiManager.getStatus(WiFi.status())); - if (!MDNS.begin("osc2midi")) { + if (!MDNS.begin(MDNS_NAME)) { Serial.println("Error setting up MDNS responder!"); while (1) { delay(1000); @@ -289,6 +320,12 @@ void setup() { Serial.println("mDNS started."); } + + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Mode WiFi client"); + lcd.setCursor(0, 1); + lcd.print(WiFi.localIP()); } /* @@ -297,29 +334,20 @@ void setup() */ midi1.begin(31250); midi2.begin(31250, SWSERIAL_8N1, D5, D6, false, 95, 11); - - Serial.begin(115200); - Serial.setDebugOutput(true); - - DEBUG_MSG("\nOSC2Midi\n"); - - pinMode(AP_TRIGGER_PIN, INPUT_PULLUP); - - //WiFi.softAP("OSC2Midi", "1357924680"); + midi2.enableIntTx(false); udp.begin(8000); MIDI1.begin(MIDI_CHANNEL_OMNI); MIDI1.setHandleControlChange(MidiCCToOSC); MIDI1.turnThruOff(); - - MIDI2.begin(MIDI_CHANNEL_OMNI); - MIDI2.setHandleControlChange(MidiCCToOSC); - MIDI2.turnThruOff(); } void loop() { + if (digitalRead(AP_MODE) != LOW) + check_status(); + // check if we need to configure a AP if (digitalRead(AP_TRIGGER_PIN) == LOW) { @@ -330,7 +358,7 @@ void loop() wm.setConfigPortalTimeout(AP_TIMEOUT); - if (!wm.startConfigPortal(AP_NAME)) + if (!wm.startConfigPortal(AP_SSID_NAME)) { DEBUG_MSG("Failed to connect and hit timeout - restarting!\n"); delay(3000); @@ -375,36 +403,28 @@ void loop() //MIDI1.read(); if (MIDI1.read()) { - Serial.print("MIDI-IN[1] Type: "); - Serial.print(MIDI1.getType(), DEC); - Serial.print(" Data1: "); - Serial.print(MIDI1.getData1(), DEC); - Serial.print(" Data2: "); - Serial.print(MIDI1.getData2(), DEC); - Serial.print(" Channel: "); - Serial.print(MIDI1.getChannel(), DEC); - Serial.println(" Channel: "); + DEBUG_MSG("MIDI-IN[1] Type: "); + DEBUG_MSG("%3d", MIDI1.getType()); + DEBUG_MSG(" Data1: "); + DEBUG_MSG("%3d", MIDI1.getData1()); + DEBUG_MSG(" Data2: "); + DEBUG_MSG("%3d", MIDI1.getData2()); + DEBUG_MSG(" Channel: "); + DEBUG_MSG("%0d", MIDI1.getChannel()); + DEBUG_MSG("\n"); } - // MIDI-Merger from MIDI2 to MIDI1 - if (MIDI2.read()) + // MIDI-Merger from (Soft-)MIDI2 to MIDI1 + if (midi2.available() > 0) { - MIDI1.send(MIDI2.getType(), - MIDI2.getData1(), - MIDI2.getData2(), - MIDI2.getChannel()); - Serial.print("MIDI-IN[2] Type: "); - Serial.print(MIDI2.getType(), DEC); - Serial.print(" Data1: "); - Serial.print(MIDI2.getData1(), DEC); - Serial.print(" Data2: "); - Serial.print(MIDI2.getData2(), DEC); - Serial.print(" Channel: "); - Serial.print(MIDI2.getChannel(), DEC); - Serial.println(" Channel: "); + while (midi2.available() > 0) + { + Serial.print("->M:"); + Serial.println(midi2.peek(), DEC); + midi1.write(midi2.read()); + } } } - check_status(); } void OSCToMidiCC(OSCMessage & msg, int offset) @@ -423,7 +443,7 @@ void OSCToMidiCC(OSCMessage & msg, int offset) cc = getCC(address); value = round(msg.getFloat(0)); value = value > 127 ? 127 : value; - DEBUG_MSG("MSG: %s\tChannel: %u\t\tCC: %u\tValue: %u\n", address, midichannel, cc, value); + DEBUG_MSG("MSG: % s\tChannel: % u\t\tCC: % u\tValue: % u\n", address, midichannel, cc, value); MIDI1.sendControlChange(cc, value, midichannel); } else if (msg.size() == 2 && msg.isFloat(0) && msg.isFloat(1)) @@ -432,17 +452,17 @@ void OSCToMidiCC(OSCMessage & msg, int offset) cc = getVar(address, 1); value = round(msg.getFloat(0)); value = value > 127 ? 127 : value; - DEBUG_MSG("MSG: %s\tChannel: %u\t\tCC: %u\tValue: %u\n", address, midichannel, cc, value); + DEBUG_MSG("MSG: % s\tChannel: % u\t\tCC: % u\tValue: % u\n", address, midichannel, cc, value); MIDI1.sendControlChange(cc, value, midichannel); 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); + DEBUG_MSG("MSG: % s\tChannel: % u\t\tCC: % u\tValue: % u\n", address, midichannel, cc, value); MIDI1.sendControlChange(cc, value, midichannel); } else { - DEBUG_MSG("Cannot handle: %s\n", address); + DEBUG_MSG("Cannot handle: % s\n", address); } } @@ -450,9 +470,9 @@ void MidiCCToOSC(uint8_t channel, uint8_t number, uint8_t val) { char buffer[1024]; - snprintf(buffer, sizeof(buffer), "/midi/cc/%u/%u", channel, number); + snprintf(buffer, sizeof(buffer), " / midi / cc / % u / % u", channel, number); - DEBUG_MSG("MidiCCToOsc: %s %f\n", buffer, val * 1.0); + DEBUG_MSG("MidiCCToOsc: % s % f\n", buffer, val * 1.0); if (clientIP) { @@ -562,11 +582,11 @@ uint8_t connectMultiWiFi(void) { uint8_t status; - LOGERROR(F("ConnectMultiWiFi with :")); + LOGERROR(F("ConnectMultiWiFi with : ")); if ( (Router_SSID != "") && (Router_Pass != "") ) { - LOGERROR3(F("* Flash-stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass ); + LOGERROR3(F("* Flash - stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass ); } for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) @@ -601,8 +621,8 @@ uint8_t connectMultiWiFi(void) if ( status == WL_CONNECTED ) { LOGERROR1(F("WiFi connected after time: "), i); - LOGERROR3(F("SSID:"), WiFi.SSID(), F(",RSSI="), WiFi.RSSI()); - LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() ); + LOGERROR3(F("SSID: "), WiFi.SSID(), F(", RSSI = "), WiFi.RSSI()); + LOGERROR3(F("Channel: "), WiFi.channel(), F(", IP address: "), WiFi.localIP() ); } else LOGERROR(F("WiFi not connected"));