-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide consistent serial console interface for samples (#2739)
This PR provides a consistent mechanism for a basic serial console interface using the `LineBuffer` class. **Refactor LineBuffer** Implement LineBufferBase so we take non-trivial methods out of header. **Add `bool`, `String` operators, `printTo` method&** Simplify usage **Add `process`, `processKey` methods and update samples** Use LineBuffer for LiveDebug, Basic_Ota and Basic_FlashIP samples. These new methods are used to simplify application code. Note that the `CommandProcessing::Handler` class hasn't been updated here, mainly because it requires customisation of EOL handling (setCommandEOL, getCommandEOL). Presumably this is because of the variation in EOL sequences (CR, LF, CRLF or LFCR combinations). The LineBuffer `processKey` method doesn't care, it handles all of these.
- Loading branch information
Showing
6 changed files
with
299 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* LineBuffer.h - support for buffering/editing a line of text | ||
* | ||
* author mikee47 <mike@sillyhouse.net> Feb 2019 | ||
* | ||
****/ | ||
|
||
#include "LineBuffer.h" | ||
|
||
LineBufferBase::Action LineBufferBase::process(Stream& input, ReadWriteStream& output) | ||
{ | ||
int c; | ||
while((c = input.read()) >= 0) { | ||
auto action = processKey(c, &output); | ||
if(action == Action::clear || action == Action::submit) { | ||
return action; | ||
} | ||
} | ||
return Action::none; | ||
} | ||
|
||
LineBufferBase::Action LineBufferBase::processKey(char key, ReadWriteStream* output) | ||
{ | ||
auto prevKey = previousKey; | ||
previousKey = key; | ||
|
||
switch(key) { | ||
case '\x1b': // ESC -> delete current commandLine | ||
clear(); | ||
if(output) { | ||
output->println(); | ||
} | ||
return Action::clear; | ||
|
||
case '\b': // delete (backspace) | ||
case '\x7f': // xterm ctrl-? | ||
if(!backspace()) { | ||
return Action::none; | ||
} | ||
if(output) { | ||
output->print("\b \b"); | ||
} | ||
return Action::backspace; | ||
|
||
case '\r': | ||
case '\n': | ||
// For "\r\n" or "\n\r" sequence ignore second key | ||
if(prevKey != key && (prevKey == '\r' || prevKey == '\n')) { | ||
previousKey = '\0'; | ||
return Action::none; | ||
} | ||
if(output) { | ||
output->println(); | ||
} | ||
return Action::submit; | ||
|
||
default: | ||
if(!addChar(key)) { | ||
return Action::none; | ||
} | ||
if(output) { | ||
output->print(key); | ||
} | ||
return Action::echo; | ||
} | ||
} | ||
|
||
char LineBufferBase::addChar(char c) | ||
{ | ||
if(c == '\n' || c == '\r') { | ||
return '\n'; | ||
} | ||
|
||
if(c >= 0x20 && c < 0x7f && length < (size - 1)) { | ||
buffer[length++] = c; | ||
buffer[length] = '\0'; | ||
return c; | ||
} | ||
|
||
return '\0'; | ||
} | ||
|
||
bool LineBufferBase::backspace() | ||
{ | ||
if(length == 0) { | ||
return false; | ||
} | ||
--length; | ||
buffer[length] = '\0'; | ||
return true; | ||
} | ||
|
||
bool LineBufferBase::startsWith(const char* text) const | ||
{ | ||
auto len = strlen(text); | ||
return memcmp(buffer, text, len) == 0; | ||
} | ||
|
||
bool LineBufferBase::contains(const char* text) const | ||
{ | ||
return strstr(buffer, text) != nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule FlashIP
updated
11 files
+18 −0 | .github/workflows/ci-dispatch.yml | |
+7 −0 | .github/workflows/ci-push.yml | |
+34 −17 | samples/Basic_FlashIP/app/application.cpp | |
+4 −5 | src/Arch/Host/fip.cpp | |
+7 −1 | src/include/FlashIP.h | |
+0 −0 | test/.cs | |
+11 −0 | test/Makefile | |
+60 −0 | test/app/application.cpp | |
+8 −0 | test/component.mk | |
+796 −0 | test/files/flaship.bin | |
+33 −0 | test/fip_test.hw |
Oops, something went wrong.