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