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