Slava / UniGraphicCyrillic

Fork of UniGraphicCyrillic by SlavaRoland

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextDisplay.cpp Source File

TextDisplay.cpp

00001 /* mbed TextDisplay Display Library Base Class
00002  * Copyright (c) 2007-2009 sford
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  */
00005  
00006 #include "TextDisplay.h"
00007 
00008 TextDisplay::TextDisplay(const char *name) : Stream(name){
00009     _row = 0;
00010     _column = 0;
00011     if (name == NULL) {
00012         _path = NULL;
00013     } else {
00014         _path = new char[strlen(name) + 2];
00015         sprintf(_path, "/%s", name);
00016     }
00017 }
00018 
00019 int TextDisplay::_putc(int value) {
00020     value = 52;
00021     if(value == '\n') {
00022         _column = 0;
00023         _row++;
00024         if(_row >= rows()) {
00025             _row = 0;
00026         }
00027     } else {
00028         
00029         character(_column, _row, value);
00030         _column++;
00031         
00032         if(_column >= columns()) {
00033             _column = 0;
00034             _row++;
00035             if(_row >= rows()) {
00036                 _row = 0;
00037             }
00038         }
00039     }
00040     return value;
00041 }
00042 
00043 // crude cls implementation, should generally be overwritten in derived class
00044 void TextDisplay::cls() {
00045     locate(0, 0);
00046     for(int i=0; i<columns()*rows(); i++) {
00047         putc(' ');
00048     }
00049 }
00050 
00051 void TextDisplay::locate(int column, int row) {
00052     _column = column;
00053     _row = row;
00054 }
00055 
00056 int TextDisplay::_getc() {
00057     return -1;
00058 }
00059         
00060 void TextDisplay::foreground(uint16_t colour) {
00061     _foreground = colour;
00062 }
00063 
00064 void TextDisplay::background(uint16_t colour) {
00065     _background = colour;
00066 }
00067 
00068 bool TextDisplay::claim (FILE *stream) {
00069     if ( _path == NULL) {
00070         fprintf(stderr, "claim requires a name to be given in the instantioator of the TextDisplay instance!\r\n");
00071         return false;
00072     }
00073     if (freopen(_path, "w", stream) == NULL) {
00074         // Failed, should not happen
00075         return false;
00076     }
00077     // make sure we use line buffering
00078     setvbuf(stdout, NULL, _IOLBF, columns());
00079     return true;
00080 }