Add history

This commit is contained in:
fvanroie 2020-11-18 19:12:10 +01:00
parent 889e8aa779
commit 5ac8b4c16d

View File

@ -67,11 +67,12 @@ WiFiUDP * syslogClient;
#endif // USE_SYSLOG #endif // USE_SYSLOG
// Serial Settings // Serial Settings
uint16_t serialInputIndex = 0; // Empty buffer uint16_t serialInputIndex = 0; // Empty buffer
char serialInputBuffer[1024] = ""; char serialInputBuffer[220] = "";
uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10 uint16_t historyIndex = sizeof(serialInputBuffer) - 1; // Empty buffer
bool debugSerialStarted = false; uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10
bool debugAnsiCodes = true; bool debugSerialStarted = false;
bool debugAnsiCodes = true;
//#define TERM_COLOR_Black "\u001b[30m" //#define TERM_COLOR_Black "\u001b[30m"
#define TERM_COLOR_GRAY "\e[37m" #define TERM_COLOR_GRAY "\e[37m"
@ -130,6 +131,9 @@ void debugStart()
void debugSetup() void debugSetup()
{ {
memset(serialInputBuffer, 0, sizeof(serialInputBuffer));
serialInputIndex = 0;
#if HASP_USE_SYSLOG > 0 #if HASP_USE_SYSLOG > 0
// syslog = new Syslog(syslogClient, debugSyslogProtocol == 0 ? SYSLOG_PROTO_IETF : SYSLOG_PROTO_BSD); // syslog = new Syslog(syslogClient, debugSyslogProtocol == 0 ? SYSLOG_PROTO_IETF : SYSLOG_PROTO_BSD);
// syslog->server(debugSyslogHost, debugSyslogPort); // syslog->server(debugSyslogHost, debugSyslogPort);
@ -220,6 +224,95 @@ inline void debugSendAnsiCode(const __FlashStringHelper * code, Print * _logOutp
if(debugAnsiCodes) _logOutput->print(code); if(debugAnsiCodes) _logOutput->print(code);
} }
size_t debugHistorycount()
{
size_t count = 0;
for(size_t i = 1; i < sizeof(serialInputBuffer); i++) {
if(serialInputBuffer[i] == 0 && serialInputBuffer[i - 1] != 0) count++;
}
return count;
}
size_t debugHistoryIndex(size_t num)
{
size_t pos = 0;
while(num > 0 && pos < sizeof(serialInputBuffer) - 2) {
if(serialInputBuffer[pos] == 0) {
num--;
// skip extra \0s
while(serialInputBuffer[pos] == 0) {
pos++;
}
} else {
pos++;
}
}
return pos;
}
void debugShowHistory()
{
size_t num = debugHistorycount();
Serial.println();
for(int i = 0; i <= num; i++) {
Serial.print("[");
Serial.print(i);
Serial.print("] ");
size_t pos = debugHistoryIndex(i);
if(pos < sizeof(serialInputBuffer)) Serial.println((char *)(serialInputBuffer + pos));
}
}
void debugGetHistoryLine(size_t num)
{
size_t pos = debugHistoryIndex(num);
size_t len = strlen(serialInputBuffer);
char * dst = serialInputBuffer;
char * src = serialInputBuffer + pos;
size_t newlen = strlen(src);
if(len < newlen) {
// make room, shift whole buffer right
dst = serialInputBuffer + newlen - len;
src = serialInputBuffer;
memmove(dst, src, sizeof(serialInputBuffer) - newlen + len);
dst = serialInputBuffer;
memset(dst, 0, newlen);
} else {
memset(dst, 0, len);
}
dst = serialInputBuffer;
src = serialInputBuffer + pos + newlen - len;
memmove(dst, src, newlen);
}
void debugPrintPrompt()
{ // Print current input - string
Serial.print(F(TERM_CLEAR_LINE)); // Move all the way left + Clear the line
Serial.print(F("hasp > "));
for(uint i = 0; i < sizeof(serialInputBuffer); i++) {
if(serialInputBuffer[i] == 0) {
Serial.print("|");
} else {
Serial.print((char)serialInputBuffer[i]);
}
}
Serial.print(historyIndex);
Serial.print("/");
Serial.print(debugHistorycount());
// 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();
}
static void debugPrintTimestamp(int level, Print * _logOutput) static void debugPrintTimestamp(int level, Print * _logOutput)
{ /* Print Current Time */ { /* Print Current Time */
time_t rawtime; time_t rawtime;
@ -417,7 +510,6 @@ static void debugPrintTag(uint8_t tag, Print * _logOutput)
void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput) void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
{ {
#if HASP_USE_SYSLOG > 0 #if HASP_USE_SYSLOG > 0
// if(!syslogClient) return;
if(_logOutput == syslogClient && syslogClient) { if(_logOutput == syslogClient && syslogClient) {
if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) { if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {
@ -442,9 +534,15 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
} else { } else {
syslogClient->print(F(": ")); syslogClient->print(F(": "));
} }
}
} debugPrintHaspMemory(level, _logOutput);
#if LV_MEM_CUSTOM == 0
debugPrintLvglMemory(level, _logOutput);
#endif #endif
}
return;
}
#endif // HASP_USE_SYSLOG
debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput); debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput);
debugPrintTimestamp(level, _logOutput); debugPrintTimestamp(level, _logOutput);
@ -452,7 +550,16 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput)
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
debugPrintLvglMemory(level, _logOutput); debugPrintLvglMemory(level, _logOutput);
#endif #endif
debugPrintPriority(level, _logOutput); switch(tag) {
case TAG_MQTT_PUB:
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
break;
case TAG_MQTT_RCV:
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
break;
default:
debugPrintPriority(level, _logOutput);
}
_logOutput->print(F(" ")); _logOutput->print(F(" "));
debugPrintTag(tag, _logOutput); debugPrintTag(tag, _logOutput);
_logOutput->print(F(": ")); _logOutput->print(F(": "));
@ -473,8 +580,8 @@ void debugPrintSuffix(uint8_t tag, int level, Print * _logOutput)
_logOutput->println(); _logOutput->println();
_logOutput->print("hasp > "); _logOutput->print("hasp > ");
_logOutput->print(serialInputBuffer);
if(_logOutput == &Serial) debugPrintPrompt();
// syslogSend(level, debugOutput); // syslogSend(level, debugOutput);
} }
@ -550,21 +657,29 @@ void debugLoop()
switch(ch) { switch(ch) {
case 1: // ^A = goto begin case 1: // ^A = goto begin
serialInputIndex = 0; serialInputIndex = 0;
historyIndex = 0;
break; break;
case 3: // ^C case 3: // ^C
serialInputIndex = 0; serialInputIndex = 0;
historyIndex = 0;
break; break;
case 5: // ^E = goto end case 5: // ^E = goto end
serialInputIndex = strlen(serialInputBuffer); serialInputIndex = strlen(serialInputBuffer);
historyIndex = 0;
break; break;
case 8: // Backspace case 8: // Backspace
{ {
if(serialInputIndex > strlen(serialInputBuffer)) {
serialInputIndex = strlen(serialInputBuffer);
}
if(serialInputIndex > 0) { if(serialInputIndex > 0) {
serialInputIndex--; serialInputIndex--;
size_t len = strlen(serialInputBuffer); size_t len = strlen(serialInputBuffer);
char * currchar = serialInputBuffer + serialInputIndex; char * currchar = serialInputBuffer + serialInputIndex;
memmove(currchar, currchar + 1, len - serialInputIndex); memmove(currchar, currchar + 1, len - serialInputIndex);
} }
historyIndex = 0;
} break; } break;
case 9: // Delete case 9: // Delete
{ {
@ -572,12 +687,21 @@ void debugLoop()
char * nextchar = serialInputBuffer + serialInputIndex; char * nextchar = serialInputBuffer + serialInputIndex;
char * remainingchars = serialInputBuffer + serialInputIndex + 1; char * remainingchars = serialInputBuffer + serialInputIndex + 1;
memmove(nextchar, remainingchars, len - serialInputIndex); memmove(nextchar, remainingchars, len - serialInputIndex);
historyIndex = 0;
} break; } break;
case 10 ... 13: // LF, VT, FF, CR case 10 ... 13: // LF, VT, FF, CR
Serial.println(); if(serialInputBuffer[0] != 0) {
if(serialInputBuffer[0] != 0) dispatchTextLine(serialInputBuffer); Serial.println();
dispatchTextLine(serialInputBuffer);
size_t numchars = 1;
memmove(serialInputBuffer + numchars, serialInputBuffer,
sizeof(serialInputBuffer) - numchars); // Shift chars right
}
serialInputIndex = 0; serialInputIndex = 0;
serialInputBuffer[0] = 0; serialInputBuffer[0] = 0;
historyIndex = 0;
debugShowHistory();
break; break;
case 27: case 27:
@ -604,6 +728,7 @@ void debugLoop()
if(nextchar == 126) { if(nextchar == 126) {
dispatchPageNext(); dispatchPageNext();
} }
historyIndex = 0;
break; break;
case 54: // Page Down case 54: // Page Down
/*if(Serial.peek() >= 0)*/ { /*if(Serial.peek() >= 0)*/ {
@ -612,20 +737,33 @@ void debugLoop()
dispatchPagePrev(); dispatchPagePrev();
} }
} }
historyIndex = 0;
break; break;
case 65: case 65: {
size_t count = debugHistorycount();
if(historyIndex < count) {
historyIndex++;
debugGetHistoryLine(historyIndex);
}
break; break;
}
case 66: case 66:
if(historyIndex > 0) {
historyIndex--;
debugGetHistoryLine(historyIndex);
}
break; break;
case 68: // Left case 68: // Left
if(serialInputIndex > 0) { if(serialInputIndex > 0) {
serialInputIndex--; serialInputIndex--;
} }
historyIndex = 0;
break; break;
case 67: // Right case 67: // Right
if(serialInputIndex < strlen(serialInputBuffer)) { if(serialInputIndex < strlen(serialInputBuffer)) {
serialInputIndex++; serialInputIndex++;
} }
historyIndex = 0;
break; break;
// default: // default:
// Serial.println((byte)nextchar); // Serial.println((byte)nextchar);
@ -637,16 +775,30 @@ void debugLoop()
} }
break; break;
case 32 ... 127: case 32 ... 126:
case 128 ... 254: {
Serial.print(ch); Serial.print(ch);
size_t len = strlen(serialInputBuffer);
if(serialInputIndex > len) serialInputIndex = len;
if(serialInputIndex == len && serialInputIndex < sizeof(serialInputBuffer) - 2) {
// expand needed
if(serialInputBuffer[serialInputIndex + 1] != 0) {
// shift right needed
char * dst = serialInputBuffer + len + 1;
char * src = serialInputBuffer + len;
memmove(dst, src, sizeof(serialInputBuffer) - len - 1);
}
}
if(serialInputIndex < sizeof(serialInputBuffer) - 2) { if(serialInputIndex < sizeof(serialInputBuffer) - 2) {
if((size_t)1 + serialInputIndex >= strlen(serialInputBuffer)) if((size_t)1 + serialInputIndex >= strlen(serialInputBuffer))
serialInputBuffer[serialInputIndex + 1] = 0; serialInputBuffer[serialInputIndex + 1] = 0;
serialInputBuffer[serialInputIndex++] = ch; serialInputBuffer[serialInputIndex++] = ch;
} }
break; } break;
case 177: // DEL case 127: // DEL
break; break;
// default: // default:
@ -656,18 +808,7 @@ void debugLoop()
// } // }
} }
// Print current input - string debugPrintPrompt();
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();
} }
} }