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