Skip to content

Commit

Permalink
Add cmake files for linux example
Browse files Browse the repository at this point in the history
  • Loading branch information
funbiscuit committed Nov 17, 2023
1 parent c5a3047 commit 1968f0a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
11 changes: 11 additions & 0 deletions examples/linux-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
24 changes: 13 additions & 11 deletions examples/linux-example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, {
Expand Down Expand Up @@ -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;
Expand All @@ -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";
}

Expand Down

0 comments on commit 1968f0a

Please sign in to comment.