Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TerminalPlus by
Terminal.cpp@5:d045e3561533, 2015-09-14 (annotated)
- Committer:
- MaxScorda
- Date:
- Mon Sep 14 21:33:00 2015 +0000
- Revision:
- 5:d045e3561533
- Parent:
- 4:ee2311717b80
- Child:
- 6:f8c90e147000
Primo abbozzo di VT100 funzionante
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 | |
simon | 0:2bf27af3c759 | 27 | Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {} |
simon | 0:2bf27af3c759 | 28 | |
MaxScorda | 3:e72f2addfaf8 | 29 | void Terminal::cls() |
MaxScorda | 3:e72f2addfaf8 | 30 | { |
simon | 0:2bf27af3c759 | 31 | this->printf("\033[2J"); |
simon | 0:2bf27af3c759 | 32 | } |
simon | 0:2bf27af3c759 | 33 | |
MaxScorda | 3:e72f2addfaf8 | 34 | void Terminal::locate(int column, int row) |
MaxScorda | 3:e72f2addfaf8 | 35 | { |
simon | 0:2bf27af3c759 | 36 | // Cursor Home <ESC>[{ROW};{COLUMN}H |
MaxScorda | 3:e72f2addfaf8 | 37 | |
MaxScorda | 3:e72f2addfaf8 | 38 | // this->printf("\033[%d;%dH%c", row + 1, column + 1); //original |
MaxScorda | 5:d045e3561533 | 39 | this->printf("\033[%d;%dH", row+1 , column+1 ); |
simon | 0:2bf27af3c759 | 40 | } |
simon | 0:2bf27af3c759 | 41 | |
MaxScorda | 3:e72f2addfaf8 | 42 | static int rgb888tobgr111(int colour) |
MaxScorda | 3:e72f2addfaf8 | 43 | { |
simon | 0:2bf27af3c759 | 44 | int r = (colour >> 23) & 1; |
simon | 0:2bf27af3c759 | 45 | int g = (colour >> 15) & 1; |
simon | 0:2bf27af3c759 | 46 | int b = (colour >> 7) & 1; |
simon | 0:2bf27af3c759 | 47 | return (b << 2) | (g << 1) | (r << 0); |
simon | 0:2bf27af3c759 | 48 | } |
simon | 0:2bf27af3c759 | 49 | |
MaxScorda | 3:e72f2addfaf8 | 50 | void Terminal::foreground(int colour) |
MaxScorda | 3:e72f2addfaf8 | 51 | { |
simon | 0:2bf27af3c759 | 52 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 53 | // Foreground Colours : 30 + bgr |
simon | 0:2bf27af3c759 | 54 | int c = 30 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 55 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 56 | } |
simon | 0:2bf27af3c759 | 57 | |
MaxScorda | 3:e72f2addfaf8 | 58 | void Terminal::background(int colour) |
MaxScorda | 3:e72f2addfaf8 | 59 | { |
simon | 0:2bf27af3c759 | 60 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 61 | // Background Colours : 40 + bgr |
simon | 0:2bf27af3c759 | 62 | int c = 40 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 63 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 64 | } |
MaxScorda | 3:e72f2addfaf8 | 65 | |
MaxScorda | 5:d045e3561533 | 66 | void Terminal::reset() |
MaxScorda | 5:d045e3561533 | 67 | { |
MaxScorda | 5:d045e3561533 | 68 | // Reset Screen |
MaxScorda | 5:d045e3561533 | 69 | this->printf("\033c"); |
MaxScorda | 5:d045e3561533 | 70 | } |
MaxScorda | 5:d045e3561533 | 71 | |
MaxScorda | 5:d045e3561533 | 72 | void Terminal::eraseline() |
MaxScorda | 5:d045e3561533 | 73 | { |
MaxScorda | 5:d045e3561533 | 74 | // Reset Screen |
MaxScorda | 5:d045e3561533 | 75 | this->printf("\033[2K"); |
MaxScorda | 5:d045e3561533 | 76 | } |
MaxScorda | 5:d045e3561533 | 77 | |
MaxScorda | 5:d045e3561533 | 78 | |
MaxScorda | 5:d045e3561533 | 79 | //************************************* |
MaxScorda | 5:d045e3561533 | 80 | void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb) |
MaxScorda | 5:d045e3561533 | 81 | { |
MaxScorda | 5:d045e3561533 | 82 | char i=0; //mettere lungo come stringa |
MaxScorda | 5:d045e3561533 | 83 | int screenColumn=80; //per ora lo forziamo |
MaxScorda | 5:d045e3561533 | 84 | locate(xx, yy); |
MaxScorda | 5:d045e3561533 | 85 | |
MaxScorda | 5:d045e3561533 | 86 | //this->printf("\033[%d;%dH", xx, yy); |
MaxScorda | 5:d045e3561533 | 87 | this->printf(sstr); |
MaxScorda | 5:d045e3561533 | 88 | |
MaxScorda | 5:d045e3561533 | 89 | /* |
MaxScorda | 5:d045e3561533 | 90 | //prosegui col pad |
MaxScorda | 5:d045e3561533 | 91 | while ((padb>0) && (i<(screenColumn-xx-1))) { |
MaxScorda | 5:d045e3561533 | 92 | this->printf(" "); //migliorare con stringa unica |
MaxScorda | 5:d045e3561533 | 93 | i++; |
MaxScorda | 5:d045e3561533 | 94 | padb--; |
MaxScorda | 5:d045e3561533 | 95 | } |
MaxScorda | 5:d045e3561533 | 96 | */ |
MaxScorda | 5:d045e3561533 | 97 | } |
MaxScorda | 5:d045e3561533 | 98 | |
MaxScorda | 5:d045e3561533 | 99 | |
MaxScorda | 4:ee2311717b80 | 100 | void Terminal::frame(int x, int y, int w, int h, int boxtype) |
MaxScorda | 3:e72f2addfaf8 | 101 | { |
MaxScorda | 4:ee2311717b80 | 102 | char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ; |
MaxScorda | 4:ee2311717b80 | 103 | // draw frame |
MaxScorda | 4:ee2311717b80 | 104 | // BLOCK=219; SE SI VORRA' USARE IL FILL |
MaxScorda | 3:e72f2addfaf8 | 105 | switch (boxtype) { |
MaxScorda | 3:e72f2addfaf8 | 106 | case 0: //singolo |
MaxScorda | 4:ee2311717b80 | 107 | B_H =205; |
MaxScorda | 4:ee2311717b80 | 108 | B_V =186; |
MaxScorda | 4:ee2311717b80 | 109 | B_TL =201; |
MaxScorda | 4:ee2311717b80 | 110 | B_TR =187; |
MaxScorda | 4:ee2311717b80 | 111 | B_BL= 200; |
MaxScorda | 4:ee2311717b80 | 112 | B_BR =188; |
MaxScorda | 3:e72f2addfaf8 | 113 | break; |
MaxScorda | 3:e72f2addfaf8 | 114 | |
MaxScorda | 3:e72f2addfaf8 | 115 | case 1: //doppio |
MaxScorda | 3:e72f2addfaf8 | 116 | B_H =205; |
MaxScorda | 3:e72f2addfaf8 | 117 | B_V =186; |
MaxScorda | 3:e72f2addfaf8 | 118 | B_TL =201; |
MaxScorda | 3:e72f2addfaf8 | 119 | B_TR =187; |
MaxScorda | 3:e72f2addfaf8 | 120 | B_BL= 200; |
MaxScorda | 3:e72f2addfaf8 | 121 | B_BR =188; |
MaxScorda | 3:e72f2addfaf8 | 122 | break; |
MaxScorda | 3:e72f2addfaf8 | 123 | } |
MaxScorda | 3:e72f2addfaf8 | 124 | //riga superiore |
MaxScorda | 3:e72f2addfaf8 | 125 | this->printf("\033[%d;%dH", x, y); |
MaxScorda | 4:ee2311717b80 | 126 | this->printf((char *) B_TL); |
MaxScorda | 4:ee2311717b80 | 127 | for(int i=x+1; i<x+w; i++) this->printf((char *) B_H); |
MaxScorda | 4:ee2311717b80 | 128 | this->printf((char *) B_TR); |
MaxScorda | 3:e72f2addfaf8 | 129 | //corpo |
MaxScorda | 3:e72f2addfaf8 | 130 | for(int i=y+1; i<y+h; i++) { |
MaxScorda | 3:e72f2addfaf8 | 131 | this->printf("\033[%d;%dH", x, i); |
MaxScorda | 4:ee2311717b80 | 132 | this->printf((char *) B_V); |
MaxScorda | 3:e72f2addfaf8 | 133 | this->printf("\033[%d;%dH", x+w, i); |
MaxScorda | 4:ee2311717b80 | 134 | this->printf((char *) B_V); |
MaxScorda | 3:e72f2addfaf8 | 135 | } |
MaxScorda | 3:e72f2addfaf8 | 136 | //riga inferiore |
MaxScorda | 3:e72f2addfaf8 | 137 | this->printf("\033[%d;%dH", x, y+h); |
MaxScorda | 4:ee2311717b80 | 138 | this->printf((char *) B_BL); |
MaxScorda | 4:ee2311717b80 | 139 | for(int i=x+1; i<x+w; i++) this->printf((char *) B_H); |
MaxScorda | 4:ee2311717b80 | 140 | this->printf((char *) B_BR); |
MaxScorda | 5:d045e3561533 | 141 | } |
MaxScorda | 3:e72f2addfaf8 | 142 | |
MaxScorda | 5:d045e3561533 | 143 | void Terminal::banner() |
MaxScorda | 5:d045e3561533 | 144 | { |
MaxScorda | 5:d045e3561533 | 145 | cls(); |
MaxScorda | 5:d045e3561533 | 146 | formatPrintf("_____ Boot screen _____\n",25,1); |
MaxScorda | 5:d045e3561533 | 147 | formatPrintf("___ Nucleo Scorda IO Test ___\n",22,2); |
MaxScorda | 5:d045e3561533 | 148 | // formatPrintf(fA.string2char(fA.padstr("-\n",78,'-')),1,5); //azzo funziona... |
MaxScorda | 5:d045e3561533 | 149 | formatPrintf("Parsing \n",2,5); |
MaxScorda | 5:d045e3561533 | 150 | formatPrintf("Funzione\n",2,7); |
MaxScorda | 5:d045e3561533 | 151 | formatPrintf("Numero\n",22,7); |
MaxScorda | 5:d045e3561533 | 152 | formatPrintf("Parametro\n",42,7); |
MaxScorda | 5:d045e3561533 | 153 | // formatPrintf(fA.string2char(fA.padstr("-\n",78,'-')),1,10); //azzo funziona... |
MaxScorda | 5:d045e3561533 | 154 | formatPrintf("Status \n",2,10); |
MaxScorda | 5:d045e3561533 | 155 | formatPrintf("Led 1 \n",2,12); |
MaxScorda | 5:d045e3561533 | 156 | formatPrintf("Virtual Led \n",22,12); |
MaxScorda | 5:d045e3561533 | 157 | formatPrintf("Other Commands \n",42,12); |
MaxScorda | 5:d045e3561533 | 158 | formatPrintf("Real Out Serial\n",62,12); |
MaxScorda | 5:d045e3561533 | 159 | formatPrintf("Input string\n",2,15); |
MaxScorda | 5:d045e3561533 | 160 | formatPrintf("Result\n",42,15); |
MaxScorda | 5:d045e3561533 | 161 | // formatPrintf(fA.string2char(fA.padstr("-\n",78,'-')),1,18); //azzo funziona... |
MaxScorda | 5:d045e3561533 | 162 | formatPrintf("Serial Input \n",2,18); |
MaxScorda | 3:e72f2addfaf8 | 163 | } |
MaxScorda | 5:d045e3561533 | 164 | void Terminal::readypos(){ |
MaxScorda | 5:d045e3561533 | 165 | formatPrintf("Command: > ",2,23); |
MaxScorda | 5:d045e3561533 | 166 | } |