Combines locate fix, show/hide cursor + Justin Jordans additions

Fork of Terminal by Justin Jordan

Committer:
j3
Date:
Tue Apr 12 22:05:59 2016 +0000
Revision:
4:4510b10fb5d9
Parent:
3:7c269f52ad77
Child:
6:419eea2fe960
added home fx

Who changed what in which revision?

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