From cb92f7a6851d10a39f3653f0fa37ab372479040c Mon Sep 17 00:00:00 2001 From: Nathan Hui Date: Fri, 20 Dec 2024 14:16:36 -0800 Subject: [PATCH] feat: Switches getch --- src/chargeTask.cpp | 2 +- src/cli/cli.cpp | 2 +- src/cli/conio.cpp | 9 ++++++--- src/cli/conio.hpp | 15 ++++++++------- src/cli/menuItems/debugCommands.cpp | 6 +++--- src/cli/menuItems/gpsCommands.cpp | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/chargeTask.cpp b/src/chargeTask.cpp index 72cf7d04..fac481c8 100644 --- a/src/chargeTask.cpp +++ b/src/chargeTask.cpp @@ -28,7 +28,7 @@ STATES_e ChargeTask::run(void) { if(kbhit()) { - this->inputBuffer[CLI_BUFFER_LEN - 1] = getch(); + this->inputBuffer[CLI_BUFFER_LEN - 1] = SF_OSAL_getch(); byteshiftl(this->inputBuffer, CLI_BUFFER_LEN, 1, 0); if(strcmp(this->inputBuffer, CLI_INTERRUPT_PHRASE) == 0) { diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index a1e9a7cb..68678ad7 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -93,7 +93,7 @@ void CLI::init(void) // While there is an avaliable character typed, get it while (kbhit()) { - getch(); + SF_OSAL_getch(); } } diff --git a/src/cli/conio.cpp b/src/cli/conio.cpp index 77040785..6783e8f5 100644 --- a/src/cli/conio.cpp +++ b/src/cli/conio.cpp @@ -27,14 +27,17 @@ extern "C" return Serial.available(); } - // Get pressed key - int getch(void) + int SF_OSAL_getch(void) { +#if SF_PLATFORM == SF_PLATFORM_PARTICLE while (Serial.available() == 0) { delay(1); } return Serial.read(); +#elif SF_PLATFORM == SF_PLATFORM_GLIBC + return getchar(); +#endif } // Write character @@ -54,7 +57,7 @@ extern "C" Particle.process(); if (kbhit()) { - userInput = getch(); + userInput = SF_OSAL_getch(); switch(userInput) { case '\b': diff --git a/src/cli/conio.hpp b/src/cli/conio.hpp index 2aed145e..09853e37 100644 --- a/src/cli/conio.hpp +++ b/src/cli/conio.hpp @@ -14,15 +14,9 @@ extern "C" { #endif - /** - * @brief Gets character from serial - * - * @return int key thats pressed - */ - int getch(void); /** * @brief Checks if key is pressed - * + * * @return int whether key is pressed */ int kbhit(void); @@ -50,6 +44,13 @@ extern "C" */ int SF_OSAL_getline(char *buffer, int buflen); + /** + * @brief Reads the next character from stdin + * + * @return The obtained character on success or EOF on failure. + */ + int SF_OSAL_getch(void); + #ifdef __cplusplus } #endif diff --git a/src/cli/menuItems/debugCommands.cpp b/src/cli/menuItems/debugCommands.cpp index f04d0517..14042fad 100644 --- a/src/cli/menuItems/debugCommands.cpp +++ b/src/cli/menuItems/debugCommands.cpp @@ -149,7 +149,7 @@ void CLI_monitorTempSensor(void) { if(kbhit()) { - ch = getch(); + ch = SF_OSAL_getch(); if('q' == ch) { break; @@ -178,7 +178,7 @@ void CLI_monitorIMU(void) { if(kbhit()) { - ch = getch(); + ch = SF_OSAL_getch(); if('q' == ch) { @@ -216,7 +216,7 @@ void CLI_monitorWetDry(void) { if(kbhit()) { - ch = getch(); + ch = SF_OSAL_getch(); if('q' == ch) { diff --git a/src/cli/menuItems/gpsCommands.cpp b/src/cli/menuItems/gpsCommands.cpp index 8bc753d7..a306b2c8 100644 --- a/src/cli/menuItems/gpsCommands.cpp +++ b/src/cli/menuItems/gpsCommands.cpp @@ -16,7 +16,7 @@ void CLI_GPS() { if(kbhit()) { - ch = getch(); + ch = SF_OSAL_getch(); if('q' == ch) { break;