Combines locate fix, show/hide cursor + Justin Jordans additions
Fork of Terminal by
Diff: Terminal.cpp
- Revision:
- 3:7c269f52ad77
- Parent:
- 2:85184c13476c
- Child:
- 4:4510b10fb5d9
--- a/Terminal.cpp Tue Nov 23 16:03:35 2010 +0000 +++ b/Terminal.cpp Mon Mar 28 23:48:32 2016 +0000 @@ -24,7 +24,7 @@ #include "mbed.h" -Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {} +Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx){} void Terminal::cls() { this->printf("\033[2J"); @@ -55,3 +55,82 @@ int c = 40 + rgb888tobgr111(colour); this->printf("\033[%dm", c); } + +int32_t Terminal::get_int32(const char *msg, const int32_t min_val, const int32_t max_val) +{ + int32_t user_input; + char str[12]; + uint8_t idx; + + do + { + this->printf(msg); + + //get user input + idx = 0; + do + { + if(this->readable()) + { + str[idx++] = this->getc(); + } + } + while((str[idx-1] != 0x0A) && (idx < 12)); + + //Remove trailing newline and CR, if there. + if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) + { + str[strlen(str) - 1] = '\0'; + str[strlen(str) - 1] = '\0'; + } + + user_input = strtol(str, NULL, 0); + + if((user_input < min_val) || (user_input > max_val)) + { + this->printf("\nYou entered = %d\n", user_input); + this->printf("\nOut of range\n"); + } + } + while((user_input < min_val) || (user_input > max_val)); + + return(user_input); +} + +char Terminal::get_char(const char *msg, const char min_val, const char max_val) +{ + char c[3]; + uint8_t idx; + + do + { + this->printf(msg); + + //get user input + idx = 0; + do + { + if(this->readable()) + { + c[idx++] = this->getc(); + } + } + while((c[idx-1] != 0x0A) && (idx < 3)); + + //Remove trailing newline and CR, if there. + if((strlen(c) > 0 ) && (c[strlen(c) - 1] == 0x0A) && (c[strlen(c) - 2] == 0x0D)) + { + c[strlen(c) - 1] = '\0'; + c[strlen(c) - 1] = '\0'; + } + + if((c[0] < min_val) || (c[0] > max_val)) + { + this->printf("\nYou entered = %c\n", c[0]); + this->printf("\nOut of range\n"); + } + } + while((c[0] < min_val) || (c[0] > max_val)); + + return c[0]; +}