BarGraph

Dependencies:   mbed

Committer:
Wizo
Date:
Thu Nov 15 17:23:34 2018 +0000
Revision:
0:a4839c6a1bf5
BarGraph

Who changed what in which revision?

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