Committer:
apm_litoral
Date:
Tue Apr 10 03:33:49 2012 +0000
Revision:
0:ea1b1135bb4e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apm_litoral 0:ea1b1135bb4e 1 /* mbed TextDisplay Display Library Base Class
apm_litoral 0:ea1b1135bb4e 2 * Copyright (c) 2007-2009 sford
apm_litoral 0:ea1b1135bb4e 3 * Released under the MIT License: http://mbed.org/license/mit
apm_litoral 0:ea1b1135bb4e 4 */
apm_litoral 0:ea1b1135bb4e 5
apm_litoral 0:ea1b1135bb4e 6 #include "TextDisplay.h"
apm_litoral 0:ea1b1135bb4e 7
apm_litoral 0:ea1b1135bb4e 8 TextDisplay::TextDisplay(const char *name) : Stream(name){
apm_litoral 0:ea1b1135bb4e 9 _row = 0;
apm_litoral 0:ea1b1135bb4e 10 _column = 0;
apm_litoral 0:ea1b1135bb4e 11 if (name == NULL) {
apm_litoral 0:ea1b1135bb4e 12 _path = NULL;
apm_litoral 0:ea1b1135bb4e 13 } else {
apm_litoral 0:ea1b1135bb4e 14 _path = new char[strlen(name) + 2];
apm_litoral 0:ea1b1135bb4e 15 sprintf(_path, "/%s", name);
apm_litoral 0:ea1b1135bb4e 16 }
apm_litoral 0:ea1b1135bb4e 17 }
apm_litoral 0:ea1b1135bb4e 18
apm_litoral 0:ea1b1135bb4e 19 int TextDisplay::_putc(int value) {
apm_litoral 0:ea1b1135bb4e 20 if(value == '\n') {
apm_litoral 0:ea1b1135bb4e 21 _column = 0;
apm_litoral 0:ea1b1135bb4e 22 _row++;
apm_litoral 0:ea1b1135bb4e 23 if(_row >= rows()) {
apm_litoral 0:ea1b1135bb4e 24 _row = 0;
apm_litoral 0:ea1b1135bb4e 25 }
apm_litoral 0:ea1b1135bb4e 26 } else {
apm_litoral 0:ea1b1135bb4e 27 character(_column, _row, value);
apm_litoral 0:ea1b1135bb4e 28 _column++;
apm_litoral 0:ea1b1135bb4e 29 if(_column >= columns()) {
apm_litoral 0:ea1b1135bb4e 30 _column = 0;
apm_litoral 0:ea1b1135bb4e 31 _row++;
apm_litoral 0:ea1b1135bb4e 32 if(_row >= rows()) {
apm_litoral 0:ea1b1135bb4e 33 _row = 0;
apm_litoral 0:ea1b1135bb4e 34 }
apm_litoral 0:ea1b1135bb4e 35 }
apm_litoral 0:ea1b1135bb4e 36 }
apm_litoral 0:ea1b1135bb4e 37 return value;
apm_litoral 0:ea1b1135bb4e 38 }
apm_litoral 0:ea1b1135bb4e 39
apm_litoral 0:ea1b1135bb4e 40 // crude cls implementation, should generally be overwritten in derived class
apm_litoral 0:ea1b1135bb4e 41 void TextDisplay::cls() {
apm_litoral 0:ea1b1135bb4e 42 locate(0, 0);
apm_litoral 0:ea1b1135bb4e 43 for(int i=0; i<columns()*rows(); i++) {
apm_litoral 0:ea1b1135bb4e 44 putc(' ');
apm_litoral 0:ea1b1135bb4e 45 }
apm_litoral 0:ea1b1135bb4e 46 }
apm_litoral 0:ea1b1135bb4e 47
apm_litoral 0:ea1b1135bb4e 48 void TextDisplay::locate(int column, int row) {
apm_litoral 0:ea1b1135bb4e 49 _column = column;
apm_litoral 0:ea1b1135bb4e 50 _row = row;
apm_litoral 0:ea1b1135bb4e 51 }
apm_litoral 0:ea1b1135bb4e 52
apm_litoral 0:ea1b1135bb4e 53 int TextDisplay::_getc() {
apm_litoral 0:ea1b1135bb4e 54 return -1;
apm_litoral 0:ea1b1135bb4e 55 }
apm_litoral 0:ea1b1135bb4e 56
apm_litoral 0:ea1b1135bb4e 57 void TextDisplay::foreground(uint16_t colour) {
apm_litoral 0:ea1b1135bb4e 58 _foreground = colour;
apm_litoral 0:ea1b1135bb4e 59 }
apm_litoral 0:ea1b1135bb4e 60
apm_litoral 0:ea1b1135bb4e 61 void TextDisplay::background(uint16_t colour) {
apm_litoral 0:ea1b1135bb4e 62 _background = colour;
apm_litoral 0:ea1b1135bb4e 63 }
apm_litoral 0:ea1b1135bb4e 64
apm_litoral 0:ea1b1135bb4e 65 bool TextDisplay::claim (FILE *stream) {
apm_litoral 0:ea1b1135bb4e 66 if ( _path == NULL) {
apm_litoral 0:ea1b1135bb4e 67 fprintf(stderr, "claim requires a name to be given in the instantioator of the TextDisplay instance!\r\n");
apm_litoral 0:ea1b1135bb4e 68 return false;
apm_litoral 0:ea1b1135bb4e 69 }
apm_litoral 0:ea1b1135bb4e 70 if (freopen(_path, "w", stream) == NULL) {
apm_litoral 0:ea1b1135bb4e 71 // Failed, should not happen
apm_litoral 0:ea1b1135bb4e 72 return false;
apm_litoral 0:ea1b1135bb4e 73 }
apm_litoral 0:ea1b1135bb4e 74 // make sure we use line buffering
apm_litoral 0:ea1b1135bb4e 75 setvbuf(stdout, NULL, _IOLBF, columns());
apm_litoral 0:ea1b1135bb4e 76 return true;
apm_litoral 0:ea1b1135bb4e 77 }