Martin Smith
/
TerminalFruit
Revision 0:408a2c6feb1f, committed 2010-01-20
- Comitter:
- ms523
- Date:
- Wed Jan 20 15:09:13 2010 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.cpp Wed Jan 20 15:09:13 2010 +0000 @@ -0,0 +1,90 @@ +/* mbed ANSI/VT100 Terminal Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "Terminal.h" +#include "mbed.h" + +#define ASCII_BLOCK 219 +#define ASCII_BORDER_H 205 +#define ASCII_BORDER_V 186 +#define ASCII_BORDER_TL 201 +#define ASCII_BORDER_TR 187 +#define ASCII_BORDER_BL 200 +#define ASCII_BORDER_BR 188 +#define WIDTH 30 + +Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {} + +void Terminal::cls() { + this->printf("\033[2J"); +} + +void Terminal::locate(int column, int row) { + // Cursor Home <ESC>[{ROW};{COLUMN}H + this->printf("\033[%d;%dH", row + 1, column + 1); +} + +static int rgb888tobgr111(int colour) { + int r = (colour >> 23) & 1; + int g = (colour >> 15) & 1; + int b = (colour >> 7) & 1; + return (b << 2) | (g << 1) | (r << 0); +} + +void Terminal::foreground(int colour) { + // Set Attribute Mode <ESC>[{n}m + // Foreground Colours : 30 + bgr + int c = 30 + rgb888tobgr111(colour); + this->printf("\033[%dm", c); +} + +void Terminal::background(int colour) { + // Set Attribute Mode <ESC>[{n}m + // Background Colours : 40 + bgr + int c = 40 + rgb888tobgr111(colour); + this->printf("\033[%dm", c); +} + +void Terminal::box(int x, int y, int w, int h) { + // corners + locate(x, y); + putc(ASCII_BORDER_TL); + locate(x + w - 1, y); + putc(ASCII_BORDER_TR); + locate(x, y + h - 1); + putc(ASCII_BORDER_BL); + locate(x + w - 1, y + h - 1); + putc(ASCII_BORDER_BR); + + // top + locate(x + 1, y); + for(int i=0; i<(w-2); i++){ + putc(ASCII_BORDER_H); + } + + // bottom + locate(x + 1, y + h - 1); + for(int i=0; i<(w-2); i++){ + putc(ASCII_BORDER_H); + } + + // left + locate(x, y + 1); + for(int i=1; i<(h-1); i++){ + putc(ASCII_BORDER_V); + printf("\n"); + putc(0x08); + } + + // right + locate(x + w - 1, y + 1); + for(int i=1; i<(h-1); i++){ + putc(ASCII_BORDER_V); + printf("\n"); + putc(0x08); + } +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.h Wed Jan 20 15:09:13 2010 +0000 @@ -0,0 +1,28 @@ +/* mbed Terminal TextDisplay Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * + * Implementation of ANSI/VT100 Terminal escape codes + * for use with e.g. Teraterm, Hyperterminal + */ + +#include "mbed.h" + +#ifndef MBED_TERMINAL_H +#define MBED_TERMINAL_H + +class Terminal : public Serial { +public: + + Terminal(PinName tx, PinName rx); + + // printf(), put(), baud() etc - inherited from Serial + + void cls(); + void locate(int column, int row); + void foreground(int colour); + void background(int colour); + void box(int x, int y, int w, int h); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jan 20 15:09:13 2010 +0000 @@ -0,0 +1,106 @@ +#include "mbed.h" +#include "Terminal.h" + +#define MaxNo_Menu 5 + +Terminal term(USBTX, USBRX); // tx, rx + +char *menu_list1[MaxNo_Menu] = { "Apple", "Pear", "Banana", "Kiwi", "Plum" }; +char *menu_list2[MaxNo_Menu] = { "Orange", "Peach", "Strawberry", "Blueberry", "Grapes" }; +int ypos[MaxNo_Menu] = { 3, 6, 9, 12, 15 }; + +void InitScreen(void){ //Print all the fruits on the screen + + term.background(0xFF0000); + term.foreground(0xFFFFFF); + for(int i=0; i< MaxNo_Menu; i++){ + term.locate(5, ypos[i]); + term.printf("%s",menu_list1[i]); + term.locate(15, ypos[i]); + term.printf("%s",menu_list2[i]); + } +} + +void UpdateScreen(int LastIndex, int LastColumn, int Index, int Column){ //Just update the fuits that has changed + + term.background(0xFFFFFF); //Highlight the new fruit + term.foreground(0xFF0000); + if(Column==0){ + term.locate(5, ypos[Index]); + term.printf("%s",menu_list1[Index]); + } + else{ + term.locate(15, ypos[Index]); + term.printf("%s",menu_list2[Index]); + } + + term.background(0xFF0000); //Black out the old fruit + term.foreground(0xFFFFFF); + if(LastColumn==0){ + term.locate(5, ypos[LastIndex]); + term.printf("%s",menu_list1[LastIndex]); + } + else{ + term.locate(15, ypos[LastIndex]); + term.printf("%s",menu_list2[LastIndex]); + } +} + +int main() { + + int Key; + int Index=0, Column=0, LastIndex=0, LastColumn=1; + + term.background(0xFF0000); + term.foreground(0xFFFFFF); + term.cls(); + term.locate(15,1); + term.printf("What is your favourite fruit?"); + InitScreen(); + UpdateScreen(LastIndex, LastColumn,Index, Column); + term.box(2,2,30,15); + + while(1){ + Key=term.getc(); //Get the keypad pressed + if((Key>=0x30)&&(Key<=0x39)){ //See if it's a number (0-9) + } + else if(Key==0x1B){ //If the keypress was an arrow key + Key = term.getc(); //Check again! + if (Key == 0x5B){ + Key = term.getc(); + LastIndex=Index; + LastColumn=Column; + switch(Key){ //Check to see which arrow key... + case 0x41: //It was the UP arrow key... + Index--; + if(Index<0) + Index = MaxNo_Menu - 1; + break; + case 0x42: //It was the DOWN arrow key... + Index++; + if(Index >= MaxNo_Menu) + Index = 0; + break; + case 0x43: //It was the RIGHT arrow key... + Column++; + if(Column>1) + Column=0; + break; + case 0x44: //It was the LEFT arrow key... + Column--; + if(Column<0) + Column=1; + break; + } + UpdateScreen(LastIndex, LastColumn,Index, Column); + } + } + else if(Key>=0x0D){ //See if it was ENTER + term.locate(15,18); + if(Column==0) + term.printf("Your favourite fruit is %s ",menu_list1[Index]); + else + term.printf("Your favourite fruit is %s ",menu_list2[Index]); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jan 20 15:09:13 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/32af5db564d4