TINF_Test_230418

Dependencies:   mbed

Committer:
Wizo
Date:
Thu Nov 15 18:10:38 2018 +0000
Revision:
0:fbb75c0396c7
TINF_Test_230418

Who changed what in which revision?

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