diff --git a/CMakeLists.txt b/CMakeLists.txt index 352ce23..98ab01e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,9 @@ if (BUILD_EXAMPLES) if (WIN32) add_subdirectory(examples/win32-example) endif (WIN32) + IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + add_subdirectory(examples/linux-example) + ENDIF() endif () if (${BUILD_TESTS}) diff --git a/examples/linux-example/CMakeLists.txt b/examples/linux-example/CMakeLists.txt new file mode 100644 index 0000000..c4db8c1 --- /dev/null +++ b/examples/linux-example/CMakeLists.txt @@ -0,0 +1,11 @@ +add_executable(embedded_cli_linux main.cpp) + +target_include_directories(embedded_cli_linux PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ) + +if (${BUILD_SINGLE_HEADER}) + target_link_libraries(embedded_cli_linux PRIVATE EmbeddedCLI::SingleHeader) +else () + target_link_libraries(embedded_cli_linux PRIVATE EmbeddedCLI::EmbeddedCLI) +endif () diff --git a/examples/linux-example/main.cpp b/examples/linux-example/main.cpp index 797b11e..542769f 100644 --- a/examples/linux-example/main.cpp +++ b/examples/linux-example/main.cpp @@ -27,14 +27,14 @@ void onLed(EmbeddedCli *cli, char *args, void *context); void onAdc(EmbeddedCli *cli, char *args, void *context); int main() { - /* single character buffer for reading keystrokes */ + // Single character buffer for reading keystrokes unsigned char c; - /* Structures to save the terminal settings for original settings & raw mode */ + // Structures to save the terminal settings for original settings & raw mode struct termios original_stdin; struct termios raw_stdin; - /* Backup the terminal settings, and switch to raw mode */ + // Backup the terminal settings, and switch to raw mode tcgetattr(STDIN_FILENO, &original_stdin); raw_stdin = original_stdin; cfmakeraw(&raw_stdin); @@ -47,7 +47,7 @@ int main() { onCommand(command->name == nullptr ? "" : command->name, command->args); }; cli->writeChar = [](EmbeddedCli *embeddedCli, char c) { - write(STDOUT_FILENO, &c, 1); + write(STDOUT_FILENO, &c, 1); }; embeddedCliAddBinding(cli, { @@ -86,14 +86,14 @@ int main() { embeddedCliProcess(cli); while (!exitFlag) { - /* grab the next character and feed it to the CLI processor */ + // grab the next character and feed it to the CLI processor if(read(STDIN_FILENO,&c,1)>0) { - embeddedCliReceiveChar(cli, c); - embeddedCliProcess(cli); - } + embeddedCliReceiveChar(cli, c); + embeddedCliProcess(cli); } + } - /* restore terminal settings */ + // restore terminal settings tcsetattr(STDIN_FILENO, TCSANOW, &original_stdin); return 0; @@ -114,10 +114,12 @@ void onExit(EmbeddedCli *cli, char *args, void *context) { void onHello(EmbeddedCli *cli, char *args, void *context) { std::cout << "Hello, "; - if (embeddedCliGetTokenCount(args) == 0) + if (embeddedCliGetTokenCount(args) == 0) { std::cout << (const char *) context; - else + } + else { std::cout << embeddedCliGetToken(args, 1); + } std::cout << "\r\n"; }