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