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