Combines locate fix, show/hide cursor + Justin Jordans additions
Fork of Terminal by
Terminal.cpp@7:aa29b7e10cd8, 2017-03-06 (annotated)
- Committer:
- j3
- Date:
- Mon Mar 06 23:17:02 2017 +0000
- Revision:
- 7:aa29b7e10cd8
- Parent:
- 6:419eea2fe960
- Child:
- 8:9ab22fc6e88b
na
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 1:96ae39e58792 | 1 | /* mbed Terminal Library, for ANSI/VT200 Terminals and ecape codes |
simon | 2:85184c13476c | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
simon | 1:96ae39e58792 | 3 | * |
simon | 1:96ae39e58792 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
simon | 1:96ae39e58792 | 5 | * of this software and associated documentation files (the "Software"), to deal |
simon | 1:96ae39e58792 | 6 | * in the Software without restriction, including without limitation the rights |
simon | 1:96ae39e58792 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
simon | 1:96ae39e58792 | 8 | * copies of the Software, and to permit persons to whom the Software is |
simon | 1:96ae39e58792 | 9 | * furnished to do so, subject to the following conditions: |
simon | 1:96ae39e58792 | 10 | * |
simon | 1:96ae39e58792 | 11 | * The above copyright notice and this permission notice shall be included in |
simon | 1:96ae39e58792 | 12 | * all copies or substantial portions of the Software. |
simon | 1:96ae39e58792 | 13 | * |
simon | 1:96ae39e58792 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
simon | 1:96ae39e58792 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
simon | 1:96ae39e58792 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
simon | 1:96ae39e58792 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
simon | 1:96ae39e58792 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
simon | 1:96ae39e58792 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
simon | 1:96ae39e58792 | 20 | * THE SOFTWARE. |
simon | 0:2bf27af3c759 | 21 | */ |
simon | 0:2bf27af3c759 | 22 | |
simon | 0:2bf27af3c759 | 23 | #include "Terminal.h" |
simon | 0:2bf27af3c759 | 24 | |
j3 | 3:7c269f52ad77 | 25 | Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx){} |
simon | 0:2bf27af3c759 | 26 | |
simon | 0:2bf27af3c759 | 27 | void Terminal::cls() { |
simon | 0:2bf27af3c759 | 28 | this->printf("\033[2J"); |
simon | 0:2bf27af3c759 | 29 | } |
simon | 0:2bf27af3c759 | 30 | |
j3 | 4:4510b10fb5d9 | 31 | void Terminal::home(){ |
j3 | 4:4510b10fb5d9 | 32 | this->printf("\033[H"); |
j3 | 4:4510b10fb5d9 | 33 | } |
j3 | 4:4510b10fb5d9 | 34 | |
simon | 0:2bf27af3c759 | 35 | void Terminal::locate(int column, int row) { |
simon | 0:2bf27af3c759 | 36 | // Cursor Home <ESC>[{ROW};{COLUMN}H |
simon | 0:2bf27af3c759 | 37 | this->printf("\033[%d;%dH%c", row + 1, column + 1); |
simon | 0:2bf27af3c759 | 38 | } |
simon | 0:2bf27af3c759 | 39 | |
simon | 0:2bf27af3c759 | 40 | static int rgb888tobgr111(int colour) { |
simon | 0:2bf27af3c759 | 41 | int r = (colour >> 23) & 1; |
simon | 0:2bf27af3c759 | 42 | int g = (colour >> 15) & 1; |
simon | 0:2bf27af3c759 | 43 | int b = (colour >> 7) & 1; |
simon | 0:2bf27af3c759 | 44 | return (b << 2) | (g << 1) | (r << 0); |
simon | 0:2bf27af3c759 | 45 | } |
simon | 0:2bf27af3c759 | 46 | |
simon | 0:2bf27af3c759 | 47 | void Terminal::foreground(int colour) { |
simon | 0:2bf27af3c759 | 48 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 49 | // Foreground Colours : 30 + bgr |
simon | 0:2bf27af3c759 | 50 | int c = 30 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 51 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 52 | } |
simon | 0:2bf27af3c759 | 53 | |
simon | 0:2bf27af3c759 | 54 | void Terminal::background(int colour) { |
simon | 0:2bf27af3c759 | 55 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 56 | // Background Colours : 40 + bgr |
simon | 0:2bf27af3c759 | 57 | int c = 40 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 58 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 59 | } |
j3 | 3:7c269f52ad77 | 60 | |
j3 | 3:7c269f52ad77 | 61 | int32_t Terminal::get_int32(const char *msg, const int32_t min_val, const int32_t max_val) |
j3 | 3:7c269f52ad77 | 62 | { |
j3 | 3:7c269f52ad77 | 63 | int32_t user_input; |
j3 | 3:7c269f52ad77 | 64 | char str[12]; |
j3 | 3:7c269f52ad77 | 65 | uint8_t idx; |
j3 | 3:7c269f52ad77 | 66 | |
j3 | 3:7c269f52ad77 | 67 | do |
j3 | 3:7c269f52ad77 | 68 | { |
j3 | 3:7c269f52ad77 | 69 | this->printf(msg); |
j3 | 3:7c269f52ad77 | 70 | |
j3 | 3:7c269f52ad77 | 71 | //get user input |
j3 | 3:7c269f52ad77 | 72 | idx = 0; |
j3 | 3:7c269f52ad77 | 73 | do |
j3 | 3:7c269f52ad77 | 74 | { |
j3 | 3:7c269f52ad77 | 75 | if(this->readable()) |
j3 | 3:7c269f52ad77 | 76 | { |
j3 | 3:7c269f52ad77 | 77 | str[idx++] = this->getc(); |
j3 | 3:7c269f52ad77 | 78 | } |
j3 | 3:7c269f52ad77 | 79 | } |
j3 | 7:aa29b7e10cd8 | 80 | while((str[idx-1] != 0x0A) && (idx < 12)); |
j3 | 3:7c269f52ad77 | 81 | |
j3 | 3:7c269f52ad77 | 82 | //Remove trailing newline and CR, if there. |
j3 | 3:7c269f52ad77 | 83 | if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) |
j3 | 3:7c269f52ad77 | 84 | { |
j3 | 3:7c269f52ad77 | 85 | str[strlen(str) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 86 | str[strlen(str) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 87 | } |
j3 | 3:7c269f52ad77 | 88 | |
j3 | 3:7c269f52ad77 | 89 | user_input = strtol(str, NULL, 0); |
j3 | 3:7c269f52ad77 | 90 | |
j3 | 3:7c269f52ad77 | 91 | if((user_input < min_val) || (user_input > max_val)) |
j3 | 3:7c269f52ad77 | 92 | { |
j3 | 3:7c269f52ad77 | 93 | this->printf("\nYou entered = %d\n", user_input); |
j3 | 3:7c269f52ad77 | 94 | this->printf("\nOut of range\n"); |
j3 | 3:7c269f52ad77 | 95 | } |
j3 | 3:7c269f52ad77 | 96 | } |
j3 | 3:7c269f52ad77 | 97 | while((user_input < min_val) || (user_input > max_val)); |
j3 | 3:7c269f52ad77 | 98 | |
j3 | 3:7c269f52ad77 | 99 | return(user_input); |
j3 | 3:7c269f52ad77 | 100 | } |
j3 | 3:7c269f52ad77 | 101 | |
j3 | 3:7c269f52ad77 | 102 | char Terminal::get_char(const char *msg, const char min_val, const char max_val) |
j3 | 3:7c269f52ad77 | 103 | { |
j3 | 3:7c269f52ad77 | 104 | char c[3]; |
j3 | 3:7c269f52ad77 | 105 | uint8_t idx; |
j3 | 3:7c269f52ad77 | 106 | |
j3 | 3:7c269f52ad77 | 107 | do |
j3 | 3:7c269f52ad77 | 108 | { |
j3 | 3:7c269f52ad77 | 109 | this->printf(msg); |
j3 | 3:7c269f52ad77 | 110 | |
j3 | 3:7c269f52ad77 | 111 | //get user input |
j3 | 3:7c269f52ad77 | 112 | idx = 0; |
j3 | 3:7c269f52ad77 | 113 | do |
j3 | 3:7c269f52ad77 | 114 | { |
j3 | 3:7c269f52ad77 | 115 | if(this->readable()) |
j3 | 3:7c269f52ad77 | 116 | { |
j3 | 3:7c269f52ad77 | 117 | c[idx++] = this->getc(); |
j3 | 3:7c269f52ad77 | 118 | } |
j3 | 3:7c269f52ad77 | 119 | } |
j3 | 3:7c269f52ad77 | 120 | while((c[idx-1] != 0x0A) && (idx < 3)); |
j3 | 3:7c269f52ad77 | 121 | |
j3 | 3:7c269f52ad77 | 122 | //Remove trailing newline and CR, if there. |
j3 | 3:7c269f52ad77 | 123 | if((strlen(c) > 0 ) && (c[strlen(c) - 1] == 0x0A) && (c[strlen(c) - 2] == 0x0D)) |
j3 | 3:7c269f52ad77 | 124 | { |
j3 | 3:7c269f52ad77 | 125 | c[strlen(c) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 126 | c[strlen(c) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 127 | } |
j3 | 3:7c269f52ad77 | 128 | |
j3 | 3:7c269f52ad77 | 129 | if((c[0] < min_val) || (c[0] > max_val)) |
j3 | 3:7c269f52ad77 | 130 | { |
j3 | 3:7c269f52ad77 | 131 | this->printf("\nYou entered = %c\n", c[0]); |
j3 | 3:7c269f52ad77 | 132 | this->printf("\nOut of range\n"); |
j3 | 3:7c269f52ad77 | 133 | } |
j3 | 3:7c269f52ad77 | 134 | } |
j3 | 3:7c269f52ad77 | 135 | while((c[0] < min_val) || (c[0] > max_val)); |
j3 | 3:7c269f52ad77 | 136 | |
j3 | 3:7c269f52ad77 | 137 | return c[0]; |
j3 | 3:7c269f52ad77 | 138 | } |
j3 | 6:419eea2fe960 | 139 | |
j3 | 6:419eea2fe960 | 140 | |
j3 | 6:419eea2fe960 | 141 | float Terminal::get_float(const char *msg, const float min_val, const float max_val) |
j3 | 6:419eea2fe960 | 142 | { |
j3 | 6:419eea2fe960 | 143 | char str[32]; |
j3 | 6:419eea2fe960 | 144 | uint8_t idx; |
j3 | 6:419eea2fe960 | 145 | |
j3 | 6:419eea2fe960 | 146 | float user_input; |
j3 | 6:419eea2fe960 | 147 | |
j3 | 6:419eea2fe960 | 148 | do |
j3 | 6:419eea2fe960 | 149 | { |
j3 | 6:419eea2fe960 | 150 | printf(msg); |
j3 | 6:419eea2fe960 | 151 | |
j3 | 6:419eea2fe960 | 152 | //get user input |
j3 | 6:419eea2fe960 | 153 | idx = 0; |
j3 | 6:419eea2fe960 | 154 | do |
j3 | 6:419eea2fe960 | 155 | { |
j3 | 6:419eea2fe960 | 156 | if(readable()) |
j3 | 6:419eea2fe960 | 157 | { |
j3 | 6:419eea2fe960 | 158 | str[idx++] = getc(); |
j3 | 6:419eea2fe960 | 159 | } |
j3 | 6:419eea2fe960 | 160 | } |
j3 | 6:419eea2fe960 | 161 | while((str[idx-1] != 0x0A) && (idx < 32)); |
j3 | 6:419eea2fe960 | 162 | |
j3 | 6:419eea2fe960 | 163 | //Remove trailing newline and CR, if there. |
j3 | 6:419eea2fe960 | 164 | if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) |
j3 | 6:419eea2fe960 | 165 | { |
j3 | 6:419eea2fe960 | 166 | str[strlen(str) - 1] = '\0'; |
j3 | 6:419eea2fe960 | 167 | str[strlen(str) - 1] = '\0'; |
j3 | 6:419eea2fe960 | 168 | } |
j3 | 6:419eea2fe960 | 169 | |
j3 | 6:419eea2fe960 | 170 | user_input = atof(str); |
j3 | 6:419eea2fe960 | 171 | |
j3 | 6:419eea2fe960 | 172 | if((user_input <= min_val) || (user_input >= max_val)) |
j3 | 6:419eea2fe960 | 173 | { |
j3 | 6:419eea2fe960 | 174 | printf("\nYou entered = %f\n", user_input); |
j3 | 6:419eea2fe960 | 175 | printf("\nOut of range\n"); |
j3 | 6:419eea2fe960 | 176 | } |
j3 | 6:419eea2fe960 | 177 | } |
j3 | 6:419eea2fe960 | 178 | while((user_input <= min_val) || (user_input >= max_val)); |
j3 | 6:419eea2fe960 | 179 | |
j3 | 6:419eea2fe960 | 180 | return(user_input); |
j3 | 6:419eea2fe960 | 181 | } |