Several small optimizations.

master
Holger Wirtz 4 years ago
parent beef61804a
commit 7746cacaf6
  1. 84
      OSC2MIDI.ino
  2. 4
      debug.h

@ -1,5 +1,5 @@
// Use from 0 to 4. Higher number, more debugging messages and memory usage. // Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _WIFIMGR_LOGLEVEL_ 4 #define _WIFIMGR_LOGLEVEL_ 1
#define DEBUG 1 #define DEBUG 1
#include <WiFi.h> #include <WiFi.h>
@ -22,10 +22,8 @@
#define AP_PASSWORD "osc2midi" #define AP_PASSWORD "osc2midi"
#define AP_SSID_CONFIG_NAME "OSC2MIDI-Config" #define AP_SSID_CONFIG_NAME "OSC2MIDI-Config"
#define AP_CONFIG_PASSWORD "osc2midi" #define AP_CONFIG_PASSWORD "osc2midi"
#define MDNS_NAME "osc2midi"
#define SOFT_SERIAL_RX 18 #define SOFT_SERIAL_RX 18
#define SOFT_SERIAL_TX 19 #define SOFT_SERIAL_TX 19
#define AP_TIMEOUT 120
#define AP_DATA_RESET_PIN 35 #define AP_DATA_RESET_PIN 35
#define AP_MODE_PIN 34 #define AP_MODE_PIN 34
#define LCD_I2C_ADDR 0x27 #define LCD_I2C_ADDR 0x27
@ -50,7 +48,7 @@ HardwareSerial midi1(2); // RX: 16, TX: 17
//#define TX (1) //#define TX (1)
#endif #endif
SoftwareSerial midi2; SoftwareSerial midi2;
uint32_t k_rate_last = millis(); uint32_t k_rate_last;
bool ap_mode_state = digitalRead(AP_MODE_PIN); bool ap_mode_state = digitalRead(AP_MODE_PIN);
MIDI_CREATE_INSTANCE(HardwareSerial, midi1, MIDI1); MIDI_CREATE_INSTANCE(HardwareSerial, midi1, MIDI1);
@ -62,44 +60,58 @@ void setup()
Serial.begin(115200); Serial.begin(115200);
Serial.setDebugOutput(true); Serial.setDebugOutput(true);
Serial.println("<start>"); Serial.println(F("OSC2MIDI (c)2020 H. Wirtz <wirtz@parasitstudio.de>"));
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
lcd.clear(); lcd.clear();
lcd.noCursor(); lcd.noCursor();
lcd.setCursor(0, 0); lcd.setCursor(2, 0);
lcd.print("* OSC2MIDI *"); lcd.print(F("* OSC2MIDI *"));
lcd.setCursor(0, 1);
lcd.print(F("(c)parasiTstudio"));
delay(1000); delay(1000);
if (ap_mode_state == LOW) if (ap_mode_state == LOW)
{ {
Serial.println("Mode Access-Point"); DEBUG_MSG("Mode Access-Point\n");
WiFi.softAP(AP_SSID_NAME, AP_PASSWORD);
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.clear();
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Mode AP"); lcd.print(F("Mode AP"));
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print(WiFi.softAPIP()); lcd.print(WiFi.softAPIP());
} }
else else
{ {
Serial.println("Mode Client"); DEBUG_MSG("Mode Client\n");
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP 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; WiFiManager wm;
if (digitalRead(AP_DATA_RESET_PIN) != LOW) if (digitalRead(AP_DATA_RESET_PIN) != LOW)
{ {
wm.resetSettings(); wm.resetSettings();
Serial.println("Resetting AP data"); DEBUG_MSG("Resetting AP data\n");
lcd.clear(); lcd.clear();
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Resetting AP data"); lcd.print(F("Resetting AP data"));
delay(2000); delay(2000);
//ESP.restart();
} }
// Automatically connect using saved credentials, // 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()) // 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 // 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.clear();
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Mode Config-AP"); lcd.print(F("Mode Config-AP"));
lcd.setCursor(0, 1); 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)) if (!wm.autoConnect(AP_SSID_CONFIG_NAME, AP_CONFIG_PASSWORD))
{ {
Serial.println("Failed to connect"); DEBUG_MSG("Failed to connect\n");
lcd.print("Failed"); lcd.print(F("Failed"));
delay(2000); delay(1000);
lcd.print(F("- restart"));
delay(1000);
ESP.restart(); ESP.restart();
} }
else { else {
//if you get here you have connected to the WiFi //if you get here you have connected to the WiFi
Serial.println("Connected"); DEBUG_MSG("Connected\n");
if (!MDNS.begin(MDNS_NAME)) if (!MDNS.begin(MDNS_NAME))
{ {
Serial.println("Error setting up MDNS responder!"); DEBUG_MSG("Error setting up MDNS responder!\n");
} }
else else
{ {
Serial.println("mDNS started."); DEBUG_MSG("mDNS started.\n");
} }
} }
lcd.clear(); lcd.clear();
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Mode WiFi client"); lcd.print(F("Mode WiFi client"));
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print(WiFi.localIP()); lcd.print(WiFi.localIP());
} }
udp.begin(UDP_RECV_PORT); udp.begin(UDP_RECV_PORT);
Serial.print("Listening for UDP packets on port "); DEBUG_MSG("Listening for UDP packets on port %d\n", UDP_RECV_PORT);
Serial.println(UDP_RECV_PORT);
midi1.begin(31250); // 16,17 midi1.begin(31250); // 16,17
midi2.begin(31250, SWSERIAL_8N1, D5, D6, false, 95, 11); midi2.begin(31250, SWSERIAL_8N1, D5, D6, false, 95, 11);
@ -154,6 +165,8 @@ void setup()
MIDI1.begin(MIDI_CHANNEL_OMNI); MIDI1.begin(MIDI_CHANNEL_OMNI);
MIDI1.setHandleControlChange(MidiCCToOSC); MIDI1.setHandleControlChange(MidiCCToOSC);
MIDI1.turnThruOff(); MIDI1.turnThruOff();
k_rate_last = millis();
} }
void loop() void loop()
@ -188,8 +201,7 @@ void loop()
if (clientIP == tmpIP) if (clientIP == tmpIP)
{ {
clientIP = tmpIP; clientIP = tmpIP;
Serial.print("Connection from: "); DEBUG_MSG("Connection from: %s", clientIP);
Serial.print(clientIP);
} }
} }
@ -212,18 +224,24 @@ void loop()
{ {
while (midi2.available() > 0) while (midi2.available() > 0)
{ {
Serial.print("MIDI-IN[2]: "); DEBUG_MSG("MIDI-IN[2]: %03d\n", midi2.peek());
Serial.println(midi2.peek(), DEC);
midi1.write(midi2.read()); midi1.write(midi2.read());
} }
} }
// Do something at control rate // 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)) if (ap_mode_state != digitalRead(AP_MODE_PIN))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Changing mode...");
delay(500);
ESP.restart(); ESP.restart();
} }
}
} }
void OSCToMidiCC(OSCMessage & msg, int offset) void OSCToMidiCC(OSCMessage & msg, int offset)

@ -1,7 +1,7 @@
#ifndef DEBUG_H #ifndef DEBUG_H
#define DEBUG_H #define DEBUG_H
#ifdef DEBUG #if defined(DEBUG) && DEBUG > 0
#define DEBUG_ESP_PORT Serial #define DEBUG_ESP_PORT Serial
#define DEBUG_MSG(...) Serial.printf( __VA_ARGS__ ) #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.isFloat(i)) { Serial.printf("f:%f\t", msg.getFloat(i)); } \
if (msg.isInt(i)) { Serial.printf("i:%d\t", msg.getInt(i)); } \ if (msg.isInt(i)) { Serial.printf("i:%d\t", msg.getInt(i)); } \
} \ } \
Serial.printf("\n"); \ Serial.print(F("\n")); \
} while(0); } while(0);
#else #else

Loading…
Cancel
Save