From 7746cacaf667421ace8c90203f2d155e7a284ac4 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Fri, 9 Oct 2020 09:09:49 +0200 Subject: [PATCH] Several small optimizations. --- OSC2MIDI.ino | 116 +++++++++++++++++++++++++++++---------------------- debug.h | 4 +- 2 files changed, 69 insertions(+), 51 deletions(-) diff --git a/OSC2MIDI.ino b/OSC2MIDI.ino index 0ccb6f0..4a83c7a 100644 --- a/OSC2MIDI.ino +++ b/OSC2MIDI.ino @@ -1,6 +1,6 @@ // Use from 0 to 4. Higher number, more debugging messages and memory usage. -#define _WIFIMGR_LOGLEVEL_ 4 -#define DEBUG 1 +#define _WIFIMGR_LOGLEVEL_ 1 +#define DEBUG 1 #include #include @@ -17,23 +17,21 @@ #include #include -#define MDNS_NAME "osc2midi" -#define AP_SSID_NAME "OSC2MIDI" -#define AP_PASSWORD "osc2midi" -#define AP_SSID_CONFIG_NAME "OSC2MIDI-Config" -#define AP_CONFIG_PASSWORD "osc2midi" -#define MDNS_NAME "osc2midi" -#define SOFT_SERIAL_RX 18 -#define SOFT_SERIAL_TX 19 -#define AP_TIMEOUT 120 -#define AP_DATA_RESET_PIN 35 -#define AP_MODE_PIN 34 -#define LCD_I2C_ADDR 0x27 -#define LCD_COL 16 -#define LCD_ROW 2 -#define UDP_RECV_PORT 8000 -#define UDP_SEND_PORT 9000 -#define K_RATE 200 +#define MDNS_NAME "osc2midi" +#define AP_SSID_NAME "OSC2MIDI" +#define AP_PASSWORD "osc2midi" +#define AP_SSID_CONFIG_NAME "OSC2MIDI-Config" +#define AP_CONFIG_PASSWORD "osc2midi" +#define SOFT_SERIAL_RX 18 +#define SOFT_SERIAL_TX 19 +#define AP_DATA_RESET_PIN 35 +#define AP_MODE_PIN 34 +#define LCD_I2C_ADDR 0x27 +#define LCD_COL 16 +#define LCD_ROW 2 +#define UDP_RECV_PORT 8000 +#define UDP_SEND_PORT 9000 +#define K_RATE 200 void OSCToMidiCC(OSCMessage &msg, int offset); void MidiCCToOSC(uint8_t channel, uint8_t number, uint8_t value); @@ -50,7 +48,7 @@ HardwareSerial midi1(2); // RX: 16, TX: 17 //#define TX (1) #endif SoftwareSerial midi2; -uint32_t k_rate_last = millis(); +uint32_t k_rate_last; bool ap_mode_state = digitalRead(AP_MODE_PIN); MIDI_CREATE_INSTANCE(HardwareSerial, midi1, MIDI1); @@ -62,44 +60,58 @@ void setup() Serial.begin(115200); Serial.setDebugOutput(true); - Serial.println(""); + Serial.println(F("OSC2MIDI (c)2020 H. Wirtz ")); lcd.init(); lcd.backlight(); lcd.clear(); lcd.noCursor(); - lcd.setCursor(0, 0); - lcd.print("* OSC2MIDI *"); + lcd.setCursor(2, 0); + lcd.print(F("* OSC2MIDI *")); + lcd.setCursor(0, 1); + lcd.print(F("(c)parasiTstudio")); delay(1000); if (ap_mode_state == LOW) { - Serial.println("Mode Access-Point"); - WiFi.softAP(AP_SSID_NAME, AP_PASSWORD); + DEBUG_MSG("Mode Access-Point\n"); + + if (!WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0))) { + DEBUG_MSG("AP Config Failed\n"); + } + + if (!WiFi.softAP(AP_SSID_NAME, AP_PASSWORD)) + { + DEBUG_MSG("Failed to start AP\n"); + lcd.print(F("Failed ")); + delay(1000); + lcd.print(F("- restart")); + delay(1000); + ESP.restart(); + } + lcd.clear(); lcd.setCursor(0, 0); - lcd.print("Mode AP"); + lcd.print(F("Mode AP")); lcd.setCursor(0, 1); lcd.print(WiFi.softAPIP()); } else { - Serial.println("Mode Client"); + DEBUG_MSG("Mode Client\n"); WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP - //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around WiFiManager wm; if (digitalRead(AP_DATA_RESET_PIN) != LOW) { wm.resetSettings(); - Serial.println("Resetting AP data"); + DEBUG_MSG("Resetting AP data\n"); lcd.clear(); lcd.setCursor(0, 0); - lcd.print("Resetting AP data"); + lcd.print(F("Resetting AP data")); delay(2000); - //ESP.restart(); } // Automatically connect using saved credentials, @@ -107,45 +119,44 @@ void setup() // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect()) // then goes into a blocking loop awaiting configuration and will return success result - // res = wm.autoConnect(); // auto generated AP name from chipid - // res = wm.autoConnect("AutoConnectAP"); // anonymous ap lcd.clear(); lcd.setCursor(0, 0); - lcd.print("Mode Config-AP"); + lcd.print(F("Mode Config-AP")); lcd.setCursor(0, 1); - lcd.print("192.168.4.1"); + lcd.print(F("192.168.4.1")); if (!wm.autoConnect(AP_SSID_CONFIG_NAME, AP_CONFIG_PASSWORD)) { - Serial.println("Failed to connect"); - lcd.print("Failed"); - delay(2000); + DEBUG_MSG("Failed to connect\n"); + lcd.print(F("Failed")); + delay(1000); + lcd.print(F("- restart")); + delay(1000); ESP.restart(); } else { //if you get here you have connected to the WiFi - Serial.println("Connected"); + DEBUG_MSG("Connected\n"); if (!MDNS.begin(MDNS_NAME)) { - Serial.println("Error setting up MDNS responder!"); + DEBUG_MSG("Error setting up MDNS responder!\n"); } else { - Serial.println("mDNS started."); + DEBUG_MSG("mDNS started.\n"); } } lcd.clear(); lcd.setCursor(0, 0); - lcd.print("Mode WiFi client"); + lcd.print(F("Mode WiFi client")); lcd.setCursor(0, 1); lcd.print(WiFi.localIP()); } udp.begin(UDP_RECV_PORT); - Serial.print("Listening for UDP packets on port "); - Serial.println(UDP_RECV_PORT); + DEBUG_MSG("Listening for UDP packets on port %d\n", UDP_RECV_PORT); midi1.begin(31250); // 16,17 midi2.begin(31250, SWSERIAL_8N1, D5, D6, false, 95, 11); @@ -154,6 +165,8 @@ void setup() MIDI1.begin(MIDI_CHANNEL_OMNI); MIDI1.setHandleControlChange(MidiCCToOSC); MIDI1.turnThruOff(); + + k_rate_last = millis(); } void loop() @@ -188,8 +201,7 @@ void loop() if (clientIP == tmpIP) { clientIP = tmpIP; - Serial.print("Connection from: "); - Serial.print(clientIP); + DEBUG_MSG("Connection from: %s", clientIP); } } @@ -212,17 +224,23 @@ void loop() { while (midi2.available() > 0) { - Serial.print("MIDI-IN[2]: "); - Serial.println(midi2.peek(), DEC); + DEBUG_MSG("MIDI-IN[2]: %03d\n", midi2.peek()); midi1.write(midi2.read()); } } // Do something at control rate - if (k_rate_last < (millis() - K_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(); + } } } diff --git a/debug.h b/debug.h index 7d620b5..ab8dfca 100644 --- a/debug.h +++ b/debug.h @@ -1,7 +1,7 @@ #ifndef DEBUG_H #define DEBUG_H -#ifdef DEBUG +#if defined(DEBUG) && DEBUG > 0 #define DEBUG_ESP_PORT Serial #define DEBUG_MSG(...) Serial.printf( __VA_ARGS__ ) @@ -15,7 +15,7 @@ if (msg.isFloat(i)) { Serial.printf("f:%f\t", msg.getFloat(i)); } \ if (msg.isInt(i)) { Serial.printf("i:%d\t", msg.getInt(i)); } \ } \ - Serial.printf("\n"); \ + Serial.print(F("\n")); \ } while(0); #else