test

Dependencies:   mbed

Committer:
chris77774
Date:
Sat Apr 30 17:57:29 2016 +0000
Revision:
0:32a06703946f
test

Who changed what in which revision?

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