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