Fork of LCD-Window which works with Enhanced TextLCD from Wim
Fork of LcdWindow by
terminal.cpp@11:85f30e1f19fc, 2016-01-04 (annotated)
- Committer:
- charly
- Date:
- Mon Jan 04 21:20:43 2016 +0000
- Revision:
- 11:85f30e1f19fc
- Parent:
- 3:e5d5e2fe4bf6
- Child:
- 13:99b500b05716
Terminal::addText: wrap long lines to the next line.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:ae5037e3d6e0 | 1 | /* |
hlipka | 0:ae5037e3d6e0 | 2 | * mbed LCDWindow library |
hlipka | 0:ae5037e3d6e0 | 3 | * Copyright (c) 2010 Hendrik Lipka |
hlipka | 2:5ac5bab7daaf | 4 | * |
hlipka | 0:ae5037e3d6e0 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hlipka | 0:ae5037e3d6e0 | 6 | * of this software and associated documentation files (the "Software"), to deal |
hlipka | 0:ae5037e3d6e0 | 7 | * in the Software without restriction, including without limitation the rights |
hlipka | 0:ae5037e3d6e0 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hlipka | 0:ae5037e3d6e0 | 9 | * copies of the Software, and to permit persons to whom the Software is |
hlipka | 0:ae5037e3d6e0 | 10 | * furnished to do so, subject to the following conditions: |
hlipka | 2:5ac5bab7daaf | 11 | * |
hlipka | 0:ae5037e3d6e0 | 12 | * The above copyright notice and this permission notice shall be included in |
hlipka | 0:ae5037e3d6e0 | 13 | * all copies or substantial portions of the Software. |
hlipka | 2:5ac5bab7daaf | 14 | * |
hlipka | 0:ae5037e3d6e0 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hlipka | 0:ae5037e3d6e0 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hlipka | 0:ae5037e3d6e0 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hlipka | 0:ae5037e3d6e0 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hlipka | 0:ae5037e3d6e0 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hlipka | 0:ae5037e3d6e0 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hlipka | 0:ae5037e3d6e0 | 21 | * THE SOFTWARE. |
hlipka | 0:ae5037e3d6e0 | 22 | */ |
hlipka | 0:ae5037e3d6e0 | 23 | |
hlipka | 0:ae5037e3d6e0 | 24 | #include "terminal.h" |
hlipka | 0:ae5037e3d6e0 | 25 | #include "string.h" |
hlipka | 0:ae5037e3d6e0 | 26 | |
hlipka | 3:e5d5e2fe4bf6 | 27 | Terminal::Terminal(Window* window):Window() { |
hlipka | 0:ae5037e3d6e0 | 28 | _window=window; |
hlipka | 2:5ac5bab7daaf | 29 | _columns=window->getColumns(); |
hlipka | 2:5ac5bab7daaf | 30 | _rows=window->getRows(); |
hlipka | 2:5ac5bab7daaf | 31 | _lineBuffer=new char*[_rows]; |
hlipka | 2:5ac5bab7daaf | 32 | clear(); |
hlipka | 0:ae5037e3d6e0 | 33 | } |
hlipka | 0:ae5037e3d6e0 | 34 | |
hlipka | 2:5ac5bab7daaf | 35 | char* Terminal::createLine() { |
hlipka | 2:5ac5bab7daaf | 36 | char* text=new char[_columns+1]; |
hlipka | 2:5ac5bab7daaf | 37 | memset(text,32,_columns); |
hlipka | 2:5ac5bab7daaf | 38 | text[_columns]=0; |
hlipka | 0:ae5037e3d6e0 | 39 | return text; |
hlipka | 0:ae5037e3d6e0 | 40 | } |
hlipka | 0:ae5037e3d6e0 | 41 | |
hlipka | 2:5ac5bab7daaf | 42 | void Terminal::character(int column, int row, int c) { |
hlipka | 2:5ac5bab7daaf | 43 | if (column>_columns || row > _rows) { |
hlipka | 2:5ac5bab7daaf | 44 | return; |
hlipka | 2:5ac5bab7daaf | 45 | } |
hlipka | 2:5ac5bab7daaf | 46 | _lineBuffer[row][column]=c; |
hlipka | 2:5ac5bab7daaf | 47 | _window->character(column,row,c); |
hlipka | 2:5ac5bab7daaf | 48 | |
hlipka | 2:5ac5bab7daaf | 49 | } |
hlipka | 2:5ac5bab7daaf | 50 | |
hlipka | 2:5ac5bab7daaf | 51 | void Terminal::writeText(const unsigned int column, const unsigned int row, const char text[]) { |
hlipka | 2:5ac5bab7daaf | 52 | _window->writeText(column,row,text); |
hlipka | 2:5ac5bab7daaf | 53 | int min=column+strlen(text); |
hlipka | 2:5ac5bab7daaf | 54 | if (min>_columns) |
hlipka | 2:5ac5bab7daaf | 55 | min=_columns; |
hlipka | 2:5ac5bab7daaf | 56 | for (int i=column;i<min;i++) { |
hlipka | 2:5ac5bab7daaf | 57 | _lineBuffer[row][i]=text[i-column]; // copy text into proper line |
hlipka | 0:ae5037e3d6e0 | 58 | } |
hlipka | 0:ae5037e3d6e0 | 59 | } |
hlipka | 0:ae5037e3d6e0 | 60 | |
hlipka | 1:65f72ed914fa | 61 | void Terminal::addText(const char text[]) { |
hlipka | 2:5ac5bab7daaf | 62 | delete [] _lineBuffer[0]; |
hlipka | 2:5ac5bab7daaf | 63 | for (int i=0;i<_rows-1;i++) { |
hlipka | 2:5ac5bab7daaf | 64 | _lineBuffer[i]=_lineBuffer[i+1]; |
hlipka | 0:ae5037e3d6e0 | 65 | } |
hlipka | 2:5ac5bab7daaf | 66 | _lineBuffer[_rows-1]=createLine(); |
hlipka | 2:5ac5bab7daaf | 67 | memset(_lineBuffer[_rows-1],32,_columns); |
hlipka | 0:ae5037e3d6e0 | 68 | int min=strlen(text); |
hlipka | 2:5ac5bab7daaf | 69 | if (min>_columns) |
hlipka | 2:5ac5bab7daaf | 70 | min=_columns; |
hlipka | 0:ae5037e3d6e0 | 71 | for (int i=0;i<min;i++) { |
hlipka | 2:5ac5bab7daaf | 72 | _lineBuffer[_rows-1][i]=text[i]; // copy text into proper line |
hlipka | 0:ae5037e3d6e0 | 73 | } |
hlipka | 2:5ac5bab7daaf | 74 | _window->clear(); |
hlipka | 2:5ac5bab7daaf | 75 | for (int i=0;i<_rows;i++) { |
hlipka | 2:5ac5bab7daaf | 76 | _window->writeText(0,i,_lineBuffer[i]); |
hlipka | 0:ae5037e3d6e0 | 77 | } |
charly | 11:85f30e1f19fc | 78 | if (strlen(text) > _columns){ |
charly | 11:85f30e1f19fc | 79 | // add the remaining text to the next Line |
charly | 11:85f30e1f19fc | 80 | this->addText(text + _columns); |
charly | 11:85f30e1f19fc | 81 | } |
hlipka | 0:ae5037e3d6e0 | 82 | } |
hlipka | 0:ae5037e3d6e0 | 83 | |
hlipka | 0:ae5037e3d6e0 | 84 | void Terminal::clear() { |
hlipka | 2:5ac5bab7daaf | 85 | for (int i=0;i<_rows;i++) { |
hlipka | 2:5ac5bab7daaf | 86 | _lineBuffer[i]=createLine(); |
hlipka | 2:5ac5bab7daaf | 87 | } |
hlipka | 0:ae5037e3d6e0 | 88 | _window->clear(); |
hlipka | 0:ae5037e3d6e0 | 89 | } |