Anders Hörnfeldt / Display
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Display.cpp Source File

Display.cpp

00001 /*
00002  Copyright (c) 2010 Anders Hörnfeldt
00003 
00004  Permission is hereby granted, free of charge, to any person obtaining a copy
00005  of this software and associated documentation files (the "Software"), to deal
00006  in the Software without restriction, including without limitation the rights
00007  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  copies of the Software, and to permit persons to whom the Software is
00009  furnished to do so, subject to the following conditions:
00010 
00011  The above copyright notice and this permission notice shall be included in
00012  all copies or substantial portions of the Software.
00013 
00014  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  THE SOFTWARE.
00021 */
00022  
00023 #include "Display.h"
00024 #include "font.h"
00025 
00026 Display::Display (PinName _RS, PinName _RW, PinName _E, PinName _CS1, PinName _CS2, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7)
00027     : DB(DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7), RS(_RS), RW(_RW), E(_E), CS1(_CS1), CS2(_CS2) {
00028     DB.mode(PullNone);
00029     E = 0;
00030     SendCommand(0x3F, 4|8);
00031     for (int c=0;c<128;c++) {
00032         for (int r=0;r<8;r++)
00033         {
00034             write(r,c,0);
00035         }
00036     }
00037 }
00038 DisplayTTY::DisplayTTY (Display *d, int _row, int _startY, int _numOfChars, int _numOfRows, int _charOffset, int _flags)
00039 {
00040     kalle = d;
00041     row = _row;
00042     startY = _startY;
00043     numOfChars = _numOfChars;
00044     numOfRows = _numOfRows;
00045     charOffset = _charOffset;
00046     flags = _flags;
00047     CursPosY = 0;
00048     CursPosW = row;
00049     cls();
00050 }
00051 int Display::SendCommand(unsigned int Command, int f) {
00052     int value = 1;
00053     E = 0;
00054     RS = 0;
00055     RW = 0;
00056     CS1 = 0;
00057     CS2 = 0;
00058     if (f&1)
00059         RS = 1;
00060     if (f&2)
00061         RW = 1;
00062     if (f&4)
00063         CS1 = 1;
00064     if (f&8)
00065         CS2 = 1;
00066     wait(0.0000003); // 300ns
00067     E = 1;
00068     if (f&2)
00069     {
00070         DB.input();
00071         wait(0.0000003);
00072         value = DB;
00073     }
00074     else
00075     {
00076         DB.output();
00077         DB = Command;
00078     }
00079     wait(0.0000001);
00080     E = 0;
00081     return value;
00082 }
00083 
00084 void Display::write (int page, int y, unsigned int data) {
00085     int f = 0;
00086     if (y<64)
00087         f = 4;
00088     else
00089         f = 8;
00090     CurCol = y;
00091     SendCommand(0xB8+(page&0x07), f);
00092     SendCommand(0x40+(y&0x3F),f);
00093     SendCommand(data, f+1);
00094 }
00095 
00096 void Display::writec (int row, int Y, int c) {
00097     if (c>31 && c<127)
00098     {
00099         write(row,Y+0,font5x8[(c-32)*5+0]);
00100         write(row,Y+1,font5x8[(c-32)*5+1]);
00101         write(row,Y+2,font5x8[(c-32)*5+2]);
00102         write(row,Y+3,font5x8[(c-32)*5+3]);
00103         write(row,Y+4,font5x8[(c-32)*5+4]);
00104     }
00105 }
00106 int DisplayTTY::_putc (int c)
00107 {
00108     if (c == '\n')
00109         newline();
00110     else
00111     {
00112         kalle->writec(CursPosW, startY+CursPosY*charOffset, c);
00113 
00114         if (++CursPosY>=numOfChars)
00115             newline();
00116     }
00117 
00118     return 0;
00119 }
00120 int DisplayTTY::_getc() {
00121     return 0;
00122 }
00123 void DisplayTTY::newline() {
00124     CursPosY=0;
00125     if ((++CursPosW-row)>=numOfRows)
00126     {
00127         CursPosW = row;
00128     }
00129 }
00130 void DisplayTTY::cls() {
00131     CursPosY=0;
00132     CursPosW = row;
00133     for (int c=0;c<numOfChars*numOfRows;c++)
00134     {
00135         _putc(' ');
00136     }
00137 }