Terminal like serial console

This commit is contained in:
fvanroie 2020-11-14 14:04:16 +01:00
parent 68a38d0b38
commit 5d915c0641

View File

@ -15,9 +15,6 @@
#endif #endif
#include "hasp_hal.h" #include "hasp_hal.h"
#if HASP_USE_MQTT > 0
#include "hasp_mqtt.h"
#endif
#include "hasp_conf.h" #include "hasp_conf.h"
#include "hasp_debug.h" #include "hasp_debug.h"
@ -32,10 +29,6 @@
#define SERIAL_SPEED 115200 #define SERIAL_SPEED 115200
#endif #endif
#if HASP_USE_TELNET > 0
#include "hasp_telnet.h"
#endif
#if HASP_USE_SYSLOG > 0 #if HASP_USE_SYSLOG > 0
#include <WiFiUdp.h> #include <WiFiUdp.h>
@ -74,8 +67,8 @@ WiFiUDP * syslogClient;
#endif // USE_SYSLOG #endif // USE_SYSLOG
// Serial Settings // Serial Settings
uint8_t serialInputIndex = 0; // Empty buffer uint16_t serialInputIndex = 0; // Empty buffer
char serialInputBuffer[1024]; char serialInputBuffer[1024] = "";
uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10 uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10
bool debugSerialStarted = false; bool debugSerialStarted = false;
bool debugAnsiCodes = true; bool debugAnsiCodes = true;
@ -91,25 +84,26 @@ bool debugAnsiCodes = true;
#define TERM_COLOR_CYAN "\e[96m" #define TERM_COLOR_CYAN "\e[96m"
#define TERM_COLOR_WHITE "\e[97m" #define TERM_COLOR_WHITE "\e[97m"
#define TERM_COLOR_RESET "\e[0m" #define TERM_COLOR_RESET "\e[0m"
#define TERM_CLEAR_LINE "\e[1000D\e[0K"
unsigned long debugLastMillis = 0; unsigned long debugLastMillis = 0;
uint16_t debugTelePeriod = 300; uint16_t debugTelePeriod = 300;
/* Send the HASP header and version to the output device specified // Send the HASP header and version to the output device specified
*/
void debugHaspHeader(Print * output) void debugHaspHeader(Print * output)
{ {
if(debugAnsiCodes) output->println(TERM_COLOR_YELLOW); if(debugAnsiCodes) output->println(TERM_COLOR_YELLOW);
output->println(F("" output->print(F(""
" _____ _____ _____ _____\r\n" " _____ _____ _____ _____\r\n"
" | | | _ | __| _ |\r\n" " | | | _ | __| _ |\r\n"
" | | |__ | __|\r\n" " | | |__ | __|\r\n"
" |__|__|__|__|_____|__|\r\n" " |__|__|__|__|_____|__|\r\n"
" Home Automation Switch Plate")); " Home Automation Switch Plate\r\n"
char buffer[128]; " Open Hardware edition v"));
snprintf(buffer, sizeof(buffer), PSTR(" Open Hardware edition v%u.%u.%u\r\n"), HASP_VERSION_MAJOR, char buffer[32];
HASP_VERSION_MINOR, HASP_VERSION_REVISION); snprintf(buffer, sizeof(buffer), PSTR("%u.%u.%u"), HASP_VERSION_MAJOR, HASP_VERSION_MINOR, HASP_VERSION_REVISION);
output->println(buffer); output->println(buffer);
output->println();
} }
void debugStart() void debugStart()
@ -147,8 +141,7 @@ void debugSetup()
if(strlen(debugSyslogHost) > 0) { if(strlen(debugSyslogHost) > 0) {
syslogClient = new WiFiUDP(); syslogClient = new WiFiUDP();
if(syslogClient) { if(syslogClient) {
syslogClient->beginPacket(debugSyslogHost, debugSyslogPort); if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {
{
Log.registerOutput(2, syslogClient, LOG_LEVEL_VERBOSE, true); Log.registerOutput(2, syslogClient, LOG_LEVEL_VERBOSE, true);
} }
} }
@ -427,21 +420,21 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
// if(!syslogClient) return; // if(!syslogClient) return;
if(_logOutput == syslogClient && syslogClient) { if(_logOutput == syslogClient && syslogClient) {
syslogClient->beginPacket(debugSyslogHost, debugSyslogPort); if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {
// IETF Doc: https://tools.ietf.org/html/rfc5424 - The Syslog Protocol // IETF Doc: https://tools.ietf.org/html/rfc5424 - The Syslog Protocol
// BSD Doc: https://tools.ietf.org/html/rfc3164 - The BSD syslog Protocol // BSD Doc: https://tools.ietf.org/html/rfc3164 - The BSD syslog Protocol
syslogClient->print('<'); syslogClient->print(F("<"));
syslogClient->print((16 + debugSyslogFacility) * 8 + level); syslogClient->print((16 + debugSyslogFacility) * 8 + level);
syslogClient->print('>'); syslogClient->print(F(">"));
if(debugSyslogProtocol == SYSLOG_PROTO_IETF) { if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
syslogClient->print(F("1 - ")); syslogClient->print(F("1 - "));
} }
syslogClient->print(mqttGetNodename()); syslogClient->print(mqttGetNodename());
syslogClient->print(' '); syslogClient->print(F(" "));
debugPrintTag(tag, _logOutput); debugPrintTag(tag, _logOutput);
if(debugSyslogProtocol == SYSLOG_PROTO_IETF) { if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
@ -450,8 +443,10 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
syslogClient->print(F(": ")); syslogClient->print(F(": "));
} }
} }
}
#endif #endif
debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput);
debugPrintTimestamp(level, _logOutput); debugPrintTimestamp(level, _logOutput);
debugPrintHaspMemory(level, _logOutput); debugPrintHaspMemory(level, _logOutput);
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
@ -465,17 +460,20 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
void debugPrintSuffix(uint8_t tag, int level, Print * _logOutput) void debugPrintSuffix(uint8_t tag, int level, Print * _logOutput)
{ {
#if HASP_USE_SYSLOG > 0
if(_logOutput == syslogClient && syslogClient) {
syslogClient->endPacket();
return;
}
#endif
if(debugAnsiCodes) if(debugAnsiCodes)
_logOutput->println(F(TERM_COLOR_RESET)); _logOutput->println(F(TERM_COLOR_RESET));
else else
_logOutput->println(); _logOutput->println();
if(debugAnsiCodes) _logOutput->print(F(TERM_COLOR_MAGENTA));
#if HASP_USE_SYSLOG > 0 _logOutput->print("hasp > ");
if(_logOutput == syslogClient && syslogClient) { _logOutput->print(serialInputBuffer);
syslogClient->endPacket();
}
#endif
// syslogSend(level, debugOutput); // syslogSend(level, debugOutput);
} }
@ -548,21 +546,128 @@ void debugLoop()
{ {
while(Serial.available()) { while(Serial.available()) {
char ch = Serial.read(); char ch = Serial.read();
Serial.print(ch); // Serial.println((byte)ch);
if(ch == 13 || ch == 10) { switch(ch) {
serialInputBuffer[serialInputIndex] = 0; case 1: // ^A = goto begin
if(serialInputIndex > 0) dispatchTextLine(serialInputBuffer);
serialInputIndex = 0; serialInputIndex = 0;
} else { break;
if(serialInputIndex < sizeof(serialInputBuffer) - 1) { case 3: // ^C
serialInputIndex = 0;
break;
case 5: // ^E = goto end
serialInputIndex = strlen(serialInputBuffer);
break;
case 8: // Backspace
{
if(serialInputIndex > 0) {
serialInputIndex--;
size_t len = strlen(serialInputBuffer);
char * currchar = serialInputBuffer + serialInputIndex;
memmove(currchar, currchar + 1, len - serialInputIndex);
}
} break;
case 9: // Delete
{
size_t len = strlen(serialInputBuffer);
char * nextchar = serialInputBuffer + serialInputIndex;
char * remainingchars = serialInputBuffer + serialInputIndex + 1;
memmove(nextchar, remainingchars, len - serialInputIndex);
} break;
case 10 ... 13: // LF, VT, FF, CR
Serial.println();
if(serialInputBuffer[0] != 0) dispatchTextLine(serialInputBuffer);
serialInputIndex = 0;
serialInputBuffer[0] = 0;
break;
case 27:
/*if(Serial.peek() >= 0)*/ {
char nextchar = Serial.read();
if(nextchar == 91 /*&& Serial.peek() >= 0*/) {
nextchar = Serial.read();
switch(nextchar) {
case 51: // Del
/*if(Serial.peek() >= 0)*/ {
nextchar = Serial.read();
}
if(nextchar == 126) {
size_t len = strlen(serialInputBuffer);
char * nextchar = serialInputBuffer + serialInputIndex;
char * remainingchars = serialInputBuffer + serialInputIndex + 1;
memmove(nextchar, remainingchars, len - serialInputIndex);
}
break;
case 53: // Page Up
/*if(Serial.peek() >= 0)*/ {
nextchar = Serial.read();
}
if(nextchar == 126) {
dispatchPagePrev();
}
break;
case 54: // Page Down
/*if(Serial.peek() >= 0)*/ {
nextchar = Serial.read();
if(nextchar == 126) {
dispatchPageNext();
}
}
break;
case 65:
break;
case 66:
break;
case 68: // Left
if(serialInputIndex > 0) {
serialInputIndex--;
}
break;
case 67: // Right
if(serialInputIndex < strlen(serialInputBuffer)) {
serialInputIndex++;
}
break;
// default:
// Serial.println((byte)nextchar);
}
}
/* } else { // ESC, clear buffer
serialInputIndex = 0;
serialInputBuffer[serialInputIndex] = 0;*/
}
break;
case 32 ... 127:
Serial.print(ch);
if(serialInputIndex < sizeof(serialInputBuffer) - 2) {
if((size_t)1 + serialInputIndex >= strlen(serialInputBuffer))
serialInputBuffer[serialInputIndex + 1] = 0;
serialInputBuffer[serialInputIndex++] = ch; serialInputBuffer[serialInputIndex++] = ch;
} }
break;
case 177: // DEL
break;
// default:
// if(strcmp(serialInputBuffer, "jsonl=") == 0) { // if(strcmp(serialInputBuffer, "jsonl=") == 0) {
// dispatchJsonl(Serial); // dispatchJsonl(Serial);
// serialInputIndex = 0; // serialInputIndex = 0;
// } // }
} }
serialInputBuffer[serialInputIndex] = 0;
// Print current input - string
Serial.print(F(TERM_CLEAR_LINE)); // Move all the way left + Clear the line
Serial.print("hasp > ");
Serial.print(serialInputBuffer);
Serial.print("\e[1000D"); // Move all the way left again
/*if(serialInputIndex > 0)*/ {
Serial.print("\e[");
Serial.print(serialInputIndex + 7); // Move cursor too index
Serial.print("C");
}
// Serial.flush();
} }
} }