.

Dependents:   projekat

Committer:
2018US_EminaTahirovi
Date:
Sun Jun 16 20:26:22 2019 +0000
Revision:
0:1b3481785d59
.

Who changed what in which revision?

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