Combines locate fix, show/hide cursor + Justin Jordans additions
Fork of Terminal by
Terminal.cpp@3:7c269f52ad77, 2016-03-28 (annotated)
- Committer:
- j3
- Date:
- Mon Mar 28 23:48:32 2016 +0000
- Revision:
- 3:7c269f52ad77
- Parent:
- 2:85184c13476c
- Child:
- 4:4510b10fb5d9
fork of mbed Terminal
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 | |
simon | 0:2bf27af3c759 | 25 | #include "mbed.h" |
simon | 0:2bf27af3c759 | 26 | |
j3 | 3:7c269f52ad77 | 27 | Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx){} |
simon | 0:2bf27af3c759 | 28 | |
simon | 0:2bf27af3c759 | 29 | void Terminal::cls() { |
simon | 0:2bf27af3c759 | 30 | this->printf("\033[2J"); |
simon | 0:2bf27af3c759 | 31 | } |
simon | 0:2bf27af3c759 | 32 | |
simon | 0:2bf27af3c759 | 33 | void Terminal::locate(int column, int row) { |
simon | 0:2bf27af3c759 | 34 | // Cursor Home <ESC>[{ROW};{COLUMN}H |
simon | 0:2bf27af3c759 | 35 | this->printf("\033[%d;%dH%c", row + 1, column + 1); |
simon | 0:2bf27af3c759 | 36 | } |
simon | 0:2bf27af3c759 | 37 | |
simon | 0:2bf27af3c759 | 38 | static int rgb888tobgr111(int colour) { |
simon | 0:2bf27af3c759 | 39 | int r = (colour >> 23) & 1; |
simon | 0:2bf27af3c759 | 40 | int g = (colour >> 15) & 1; |
simon | 0:2bf27af3c759 | 41 | int b = (colour >> 7) & 1; |
simon | 0:2bf27af3c759 | 42 | return (b << 2) | (g << 1) | (r << 0); |
simon | 0:2bf27af3c759 | 43 | } |
simon | 0:2bf27af3c759 | 44 | |
simon | 0:2bf27af3c759 | 45 | void Terminal::foreground(int colour) { |
simon | 0:2bf27af3c759 | 46 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 47 | // Foreground Colours : 30 + bgr |
simon | 0:2bf27af3c759 | 48 | int c = 30 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 49 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 50 | } |
simon | 0:2bf27af3c759 | 51 | |
simon | 0:2bf27af3c759 | 52 | void Terminal::background(int colour) { |
simon | 0:2bf27af3c759 | 53 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 54 | // Background Colours : 40 + bgr |
simon | 0:2bf27af3c759 | 55 | int c = 40 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 56 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 57 | } |
j3 | 3:7c269f52ad77 | 58 | |
j3 | 3:7c269f52ad77 | 59 | int32_t Terminal::get_int32(const char *msg, const int32_t min_val, const int32_t max_val) |
j3 | 3:7c269f52ad77 | 60 | { |
j3 | 3:7c269f52ad77 | 61 | int32_t user_input; |
j3 | 3:7c269f52ad77 | 62 | char str[12]; |
j3 | 3:7c269f52ad77 | 63 | uint8_t idx; |
j3 | 3:7c269f52ad77 | 64 | |
j3 | 3:7c269f52ad77 | 65 | do |
j3 | 3:7c269f52ad77 | 66 | { |
j3 | 3:7c269f52ad77 | 67 | this->printf(msg); |
j3 | 3:7c269f52ad77 | 68 | |
j3 | 3:7c269f52ad77 | 69 | //get user input |
j3 | 3:7c269f52ad77 | 70 | idx = 0; |
j3 | 3:7c269f52ad77 | 71 | do |
j3 | 3:7c269f52ad77 | 72 | { |
j3 | 3:7c269f52ad77 | 73 | if(this->readable()) |
j3 | 3:7c269f52ad77 | 74 | { |
j3 | 3:7c269f52ad77 | 75 | str[idx++] = this->getc(); |
j3 | 3:7c269f52ad77 | 76 | } |
j3 | 3:7c269f52ad77 | 77 | } |
j3 | 3:7c269f52ad77 | 78 | while((str[idx-1] != 0x0A) && (idx < 12)); |
j3 | 3:7c269f52ad77 | 79 | |
j3 | 3:7c269f52ad77 | 80 | //Remove trailing newline and CR, if there. |
j3 | 3:7c269f52ad77 | 81 | if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) |
j3 | 3:7c269f52ad77 | 82 | { |
j3 | 3:7c269f52ad77 | 83 | str[strlen(str) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 84 | str[strlen(str) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 85 | } |
j3 | 3:7c269f52ad77 | 86 | |
j3 | 3:7c269f52ad77 | 87 | user_input = strtol(str, NULL, 0); |
j3 | 3:7c269f52ad77 | 88 | |
j3 | 3:7c269f52ad77 | 89 | if((user_input < min_val) || (user_input > max_val)) |
j3 | 3:7c269f52ad77 | 90 | { |
j3 | 3:7c269f52ad77 | 91 | this->printf("\nYou entered = %d\n", user_input); |
j3 | 3:7c269f52ad77 | 92 | this->printf("\nOut of range\n"); |
j3 | 3:7c269f52ad77 | 93 | } |
j3 | 3:7c269f52ad77 | 94 | } |
j3 | 3:7c269f52ad77 | 95 | while((user_input < min_val) || (user_input > max_val)); |
j3 | 3:7c269f52ad77 | 96 | |
j3 | 3:7c269f52ad77 | 97 | return(user_input); |
j3 | 3:7c269f52ad77 | 98 | } |
j3 | 3:7c269f52ad77 | 99 | |
j3 | 3:7c269f52ad77 | 100 | char Terminal::get_char(const char *msg, const char min_val, const char max_val) |
j3 | 3:7c269f52ad77 | 101 | { |
j3 | 3:7c269f52ad77 | 102 | char c[3]; |
j3 | 3:7c269f52ad77 | 103 | uint8_t idx; |
j3 | 3:7c269f52ad77 | 104 | |
j3 | 3:7c269f52ad77 | 105 | do |
j3 | 3:7c269f52ad77 | 106 | { |
j3 | 3:7c269f52ad77 | 107 | this->printf(msg); |
j3 | 3:7c269f52ad77 | 108 | |
j3 | 3:7c269f52ad77 | 109 | //get user input |
j3 | 3:7c269f52ad77 | 110 | idx = 0; |
j3 | 3:7c269f52ad77 | 111 | do |
j3 | 3:7c269f52ad77 | 112 | { |
j3 | 3:7c269f52ad77 | 113 | if(this->readable()) |
j3 | 3:7c269f52ad77 | 114 | { |
j3 | 3:7c269f52ad77 | 115 | c[idx++] = this->getc(); |
j3 | 3:7c269f52ad77 | 116 | } |
j3 | 3:7c269f52ad77 | 117 | } |
j3 | 3:7c269f52ad77 | 118 | while((c[idx-1] != 0x0A) && (idx < 3)); |
j3 | 3:7c269f52ad77 | 119 | |
j3 | 3:7c269f52ad77 | 120 | //Remove trailing newline and CR, if there. |
j3 | 3:7c269f52ad77 | 121 | if((strlen(c) > 0 ) && (c[strlen(c) - 1] == 0x0A) && (c[strlen(c) - 2] == 0x0D)) |
j3 | 3:7c269f52ad77 | 122 | { |
j3 | 3:7c269f52ad77 | 123 | c[strlen(c) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 124 | c[strlen(c) - 1] = '\0'; |
j3 | 3:7c269f52ad77 | 125 | } |
j3 | 3:7c269f52ad77 | 126 | |
j3 | 3:7c269f52ad77 | 127 | if((c[0] < min_val) || (c[0] > max_val)) |
j3 | 3:7c269f52ad77 | 128 | { |
j3 | 3:7c269f52ad77 | 129 | this->printf("\nYou entered = %c\n", c[0]); |
j3 | 3:7c269f52ad77 | 130 | this->printf("\nOut of range\n"); |
j3 | 3:7c269f52ad77 | 131 | } |
j3 | 3:7c269f52ad77 | 132 | } |
j3 | 3:7c269f52ad77 | 133 | while((c[0] < min_val) || (c[0] > max_val)); |
j3 | 3:7c269f52ad77 | 134 | |
j3 | 3:7c269f52ad77 | 135 | return c[0]; |
j3 | 3:7c269f52ad77 | 136 | } |