From d47ae2e4a4d67ddd1d3a2b08ad1c8a0795774a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 10 Jan 2019 11:25:01 +0100 Subject: [PATCH] Helper terminal methods terminalOK and terminalError --- code/espurna/config/arduino.h | 2 +- code/espurna/eeprom.ino | 12 ++++----- code/espurna/homeassistant.ino | 6 ++--- code/espurna/i2c.ino | 4 +-- code/espurna/light.ino | 12 ++++----- code/espurna/mqtt.ino | 2 +- code/espurna/nofuss.ino | 2 +- code/espurna/ota.ino | 4 +-- code/espurna/relay.ino | 4 +-- code/espurna/rfbridge.ino | 8 +++--- code/espurna/sensor.ino | 18 ++++++------- code/espurna/terminal.ino | 46 ++++++++++++++++++++-------------- code/espurna/wifi.ino | 12 ++++----- 13 files changed, 70 insertions(+), 62 deletions(-) diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 040ac3b9..8d2a95af 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -146,7 +146,7 @@ //#define SPIFFS_SUPPORT 1 //#define SSDP_SUPPORT 1 //#define TELNET_SUPPORT 0 -#define TERMINAL_SUPPORT 0 +//#define TERMINAL_SUPPORT 0 //#define THINGSPEAK_SUPPORT 0 //#define UART_MQTT_SUPPORT 1 //#define WEB_SUPPORT 0 diff --git a/code/espurna/eeprom.ino b/code/espurna/eeprom.ino index 36853e30..2c2e5da9 100644 --- a/code/espurna/eeprom.ino +++ b/code/espurna/eeprom.ino @@ -65,13 +65,13 @@ void _eepromInitCommands() { DEBUG_MSG_P(PSTR("[MAIN] Commits done: %lu\n"), _eeprom_commit_count); DEBUG_MSG_P(PSTR("[MAIN] Last result: %s\n"), _eeprom_last_commit_result ? "OK" : "ERROR"); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("EEPROM.COMMIT"), [](Embedis* e) { const bool res = _eepromCommit(); if (res) { - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); } else { DEBUG_MSG_P(PSTR("-ERROR\n")); } @@ -79,22 +79,22 @@ void _eepromInitCommands() { terminalRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) { EEPROMr.dump(terminalSerial()); - DEBUG_MSG_P(PSTR("\n+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("FLASH.DUMP"), [](Embedis* e) { if (e->argc < 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); return; } uint32_t sector = String(e->argv[1]).toInt(); uint32_t max = ESP.getFlashChipSize() / SPI_FLASH_SEC_SIZE; if (sector >= max) { - DEBUG_MSG_P(PSTR("-ERROR: Sector out of range\n")); + terminalError(F("Sector out of range")); return; } EEPROMr.dump(terminalSerial(), sector); - DEBUG_MSG_P(PSTR("\n+OK\n")); + terminalOK(); }); } diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index 300f237b..30a4722b 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -269,7 +269,7 @@ void _haInitCommands() { terminalRegisterCommand(F("HA.CONFIG"), [](Embedis* e) { DEBUG_MSG(_haGetConfig().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("HA.SEND"), [](Embedis* e) { @@ -278,7 +278,7 @@ void _haInitCommands() { #if WEB_SUPPORT wsSend(_haWebSocketOnSend); #endif - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("HA.CLEAR"), [](Embedis* e) { @@ -287,7 +287,7 @@ void _haInitCommands() { #if WEB_SUPPORT wsSend(_haWebSocketOnSend); #endif - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); } diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino index b9e6a84b..8e5a24ec 100644 --- a/code/espurna/i2c.ino +++ b/code/espurna/i2c.ino @@ -355,12 +355,12 @@ void i2cCommands() { terminalRegisterCommand(F("I2C.SCAN"), [](Embedis* e) { i2cScan(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("I2C.CLEAR"), [](Embedis* e) { i2cClearBus(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); } diff --git a/code/espurna/light.ino b/code/espurna/light.ino index b57af644..9ed554d2 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -989,12 +989,12 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("CHANNEL"), [](Embedis* e) { if (e->argc < 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); } int id = String(e->argv[1]).toInt(); if (e->argc > 2) { @@ -1003,7 +1003,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id)); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("COLOR"), [](Embedis* e) { @@ -1013,7 +1013,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("KELVIN"), [](Embedis* e) { @@ -1023,7 +1023,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("MIRED"), [](Embedis* e) { @@ -1033,7 +1033,7 @@ void _lightInitCommands() { lightUpdate(true, true); } DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); } diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 12e8b99e..4c8f3e56 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -353,7 +353,7 @@ void _mqttInitCommands() { terminalRegisterCommand(F("MQTT.RESET"), [](Embedis* e) { _mqttConfigure(); mqttDisconnect(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); } diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index ad9fd4d2..c6a33ad1 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -75,7 +75,7 @@ void _nofussConfigure() { void _nofussInitCommands() { terminalRegisterCommand(F("NOFUSS"), [](Embedis* e) { - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); nofussRun(); }); diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index bf366f8c..d52844b8 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -190,9 +190,9 @@ void _otaInitCommands() { terminalRegisterCommand(F("OTA"), [](Embedis* e) { if (e->argc < 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); } else { - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); String url = String(e->argv[1]); _otaFrom(url); } diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 77b8abe0..f91055f6 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -985,7 +985,7 @@ void _relayInitCommands() { terminalRegisterCommand(F("RELAY"), [](Embedis* e) { if (e->argc < 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); return; } int id = String(e->argv[1]).toInt(); @@ -1008,7 +1008,7 @@ void _relayInitCommands() { DEBUG_MSG_P(PSTR("Pulse time: %d\n"), _relays[id].pulse_ms); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); } diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index 3524fb06..4e854503 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -608,7 +608,7 @@ void _rfbInitCommands() { terminalRegisterCommand(F("LEARN"), [](Embedis* e) { if (e->argc < 3) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); return; } @@ -622,14 +622,14 @@ void _rfbInitCommands() { rfbLearn(id, status == 1); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("FORGET"), [](Embedis* e) { if (e->argc < 3) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); return; } @@ -643,7 +643,7 @@ void _rfbInitCommands() { rfbForget(id, status == 1); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 69478626..4dc84e7b 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -282,7 +282,7 @@ void _sensorInitCommands() { magnitude.global ); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); #if PZEM004T_SUPPORT terminalRegisterCommand(F("PZ.ADDRESS"), [](Embedis* e) { @@ -292,23 +292,23 @@ void _sensorInitCommands() { for(unsigned char dev = 0; dev < dev_count; dev++) { DEBUG_MSG_P(PSTR("Device %d/%s\n"), dev, pzem004t_sensor->getAddress(dev).c_str()); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); } else if(e->argc == 2) { IPAddress addr; if (addr.fromString(String(e->argv[1]))) { if(pzem004t_sensor->setDeviceAddress(&addr)) { - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); } } else { - DEBUG_MSG_P(PSTR("-ERROR: Invalid address argument\n")); + terminalError(F("Invalid address argument")); } } else { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); } }); terminalRegisterCommand(F("PZ.RESET"), [](Embedis* e) { if(e->argc > 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); } else { unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0; unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount(); @@ -318,12 +318,12 @@ void _sensorInitCommands() { setSetting("pzEneTotal", dev, offset); DEBUG_MSG_P(PSTR("Device %d/%s - Offset: %s\n"), dev, pzem004t_sensor->getAddress(dev).c_str(), String(offset).c_str()); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); } }); terminalRegisterCommand(F("PZ.VALUE"), [](Embedis* e) { if(e->argc > 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); } else { unsigned char init = e->argc == 2 ? String(e->argv[1]).toInt() : 0; unsigned char limit = e->argc == 2 ? init +1 : pzem004t_sensor->getAddressesCount(); @@ -337,7 +337,7 @@ void _sensorInitCommands() { String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_POWER_ACTIVE_INDEX)).c_str(), String(pzem004t_sensor->value(dev * PZ_MAGNITUDE_ENERGY_INDEX)).c_str()); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); } }); #endif diff --git a/code/espurna/terminal.ino b/code/espurna/terminal.ino index 6164f38c..e7b1eb66 100644 --- a/code/espurna/terminal.ino +++ b/code/espurna/terminal.ino @@ -83,17 +83,17 @@ void _terminalInitCommand() { terminalRegisterCommand(F("CRASH"), [](Embedis* e) { crashDump(); crashClear(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); #endif terminalRegisterCommand(F("COMMANDS"), [](Embedis* e) { _terminalHelpCommand(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("ERASE.CONFIG"), [](Embedis* e) { - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); resetReason(CUSTOM_RESET_TERMINAL); _eepromCommit(); ESP.eraseConfig(); @@ -102,17 +102,17 @@ void _terminalInitCommand() { terminalRegisterCommand(F("FACTORY.RESET"), [](Embedis* e) { resetSettings(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("GPIO"), [](Embedis* e) { if (e->argc < 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); return; } int pin = String(e->argv[1]).toInt(); //if (!gpioValid(pin)) { - // DEBUG_MSG_P(PSTR("-ERROR: Invalid GPIO\n")); + // terminalError(F("Invalid GPIO")); // return; //} if (e->argc > 2) { @@ -120,37 +120,37 @@ void _terminalInitCommand() { digitalWrite(pin, state); } DEBUG_MSG_P(PSTR("GPIO %d is %s\n"), pin, digitalRead(pin) == HIGH ? "HIGH" : "LOW"); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("HEAP"), [](Embedis* e) { infoMemory("Heap", getInitialFreeHeap(), getFreeHeap()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("STACK"), [](Embedis* e) { infoMemory("Stack", 4096, getFreeStack()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("HELP"), [](Embedis* e) { _terminalHelpCommand(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("INFO"), [](Embedis* e) { info(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("KEYS"), [](Embedis* e) { _terminalKeysCommand(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("GET"), [](Embedis* e) { if (e->argc < 2) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); + terminalError(F("Wrong arguments")); return; } @@ -165,28 +165,28 @@ void _terminalInitCommand() { DEBUG_MSG_P(PSTR("> %s => \"%s\"\n"), key.c_str(), value.c_str()); } - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("RELOAD"), [](Embedis* e) { espurnaReload(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("RESET"), [](Embedis* e) { - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); deferredReset(100, CUSTOM_RESET_TERMINAL); }); terminalRegisterCommand(F("RESET.SAFE"), [](Embedis* e) { EEPROMr.write(EEPROM_CRASH_COUNTER, SYSTEM_CHECK_MAX); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); deferredReset(100, CUSTOM_RESET_TERMINAL); }); terminalRegisterCommand(F("UPTIME"), [](Embedis* e) { DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime()); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("CONFIG"), [](Embedis* e) { @@ -196,7 +196,7 @@ void _terminalInitCommand() { String output; root.printTo(output); DEBUG_MSG(output.c_str()); - DEBUG_MSG_P(PSTR("\n+OK\n")); + }); #if not SETTINGS_AUTOSAVE @@ -249,6 +249,14 @@ void terminalRegisterCommand(const String& name, void (*call)(Embedis*)) { Embedis::command(name, call); }; +void terminalOK() { + DEBUG_MSG_P(PSTR("+OK\n")); +} + +void terminalError(const String& error) { + DEBUG_MSG_P(PSTR("-ERROR: %s\n"), error.c_str()); +} + void terminalSetup() { _serial.callback([](uint8_t ch) { diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 78dd79ab..272b0151 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -387,37 +387,37 @@ void _wifiInitCommands() { terminalRegisterCommand(F("WIFI"), [](Embedis* e) { wifiDebug(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("WIFI.RESET"), [](Embedis* e) { _wifiConfigure(); wifiDisconnect(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); terminalRegisterCommand(F("WIFI.AP"), [](Embedis* e) { wifiStartAP(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); #if defined(JUSTWIFI_ENABLE_WPS) terminalRegisterCommand(F("WIFI.WPS"), [](Embedis* e) { wifiStartWPS(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); #endif // defined(JUSTWIFI_ENABLE_WPS) #if defined(JUSTWIFI_ENABLE_SMARTCONFIG) terminalRegisterCommand(F("WIFI.SMARTCONFIG"), [](Embedis* e) { wifiStartSmartConfig(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG) terminalRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) { _wifiScan(); - DEBUG_MSG_P(PSTR("+OK\n")); + terminalOK(); }); }