chris stevens / TextOLED_custom

Dependents:   Raymon_pub_ver

Fork of TextLCD by Simon Ford

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextOLED.cpp Source File

TextOLED.cpp

00001 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
00002  * Copyright (c) 2007-2010, sford, http://mbed.org
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 "TextOLED.h"
00024 #include "mbed.h"
00025 
00026 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
00027                  PinName d6, PinName d7, LCDType type) : _rs(rs),
00028         _e(e), _d(d4, d5, d6, d7),
00029         _type(type) {
00030 //edited 06082012 to run winstart oled 16x2 display
00031     _e  = 1;
00032     _rs = 0;            // command mode
00033 
00034    wait(0.100);        // Wait 100ms to ensure powered up - need this long if run of 3.3V from mbed vout
00035 
00036    wait(0.015); //wait `15ms for power up
00037    
00038 
00039     // send 4 bit mode commenad twice
00040     writeByte(0x2); //set 4 bit mode
00041     wait(0.00164);
00042     writeByte(0x2); //set 4 bit mode
00043     wait(0.00164);
00044     writeByte(0x2a); //4 bit 2 lines display font table 1
00045    
00046     wait(0.00164);
00047     writeByte(0x01); // clear screen
00048     writeByte(0x6); // cursor mode
00049     writeByte(0xc); // Lcd on 
00050 }
00051 
00052 void TextLCD::character(int column, int row, int c) {
00053     int a = address(column, row);
00054     writeCommand(a);
00055     writeData(c);
00056 }
00057 
00058 void TextLCD::cls() {
00059     writeCommand(0x01); // cls, and set cursor to 0
00060     wait(0.00164f);     // This command takes 1.64 ms
00061     locate(0, 0);
00062 }
00063 
00064 void TextLCD::locate(int column, int row) {
00065     _column = column;
00066     _row = row;
00067 }
00068 
00069 int TextLCD::_putc(int value) {
00070     if (value == '\n') {
00071         _column = 0;
00072         _row++;
00073         if (_row >= rows()) {
00074             _row = 0;
00075         }
00076     } else {
00077         character(_column, _row, value);
00078         _column++;
00079         if (_column >= columns()) {
00080             _column = 0;
00081             _row++;
00082             if (_row >= rows()) {
00083                 _row = 0;
00084             }
00085         }
00086     }
00087     return value;
00088 }
00089 
00090 int TextLCD::_getc() {
00091     return -1;
00092 }
00093 
00094 void TextLCD::writeByte(int value) {
00095     _d = value >> 4;
00096     wait(0.000040f); // most instructions take 40us
00097     _e = 0;
00098     wait(0.000040f);
00099     _e = 1;
00100     _d = value >> 0;
00101     wait(0.000040f);
00102     _e = 0;
00103     wait(0.000040f);  // most instructions take 40us
00104     _e = 1;
00105 }
00106 
00107 void TextLCD::writeCommand(int command) {
00108     _rs = 0;
00109     writeByte(command);
00110 }
00111 
00112 void TextLCD::writeData(int data) {
00113     _rs = 1;
00114     writeByte(data);
00115 }
00116 
00117 int TextLCD::address(int column, int row) {
00118     switch (_type) {
00119         case LCD20x4:
00120             switch (row) {
00121                 case 0:
00122                     return 0x80 + column;
00123                 case 1:
00124                     return 0xc0 + column;
00125                 case 2:
00126                     return 0x94 + column;
00127                 case 3:
00128                     return 0xd4 + column;
00129             }
00130         case LCD16x2B:
00131             return 0x80 + (row * 40) + column;
00132         case LCD16x2:
00133         case LCD20x2:
00134         default:
00135             return 0x80 + (row * 0x40) + column;
00136     }
00137 }
00138 
00139 int TextLCD::columns() {
00140     switch (_type) {
00141         case LCD20x4:
00142         case LCD20x2:
00143             return 20;
00144         case LCD16x2:
00145         case LCD16x2B:
00146         default:
00147             return 16;
00148     }
00149 }
00150 
00151 int TextLCD::rows() {
00152     switch (_type) {
00153         case LCD20x4:
00154             return 4;
00155         case LCD16x2:
00156         case LCD16x2B:
00157         case LCD20x2:
00158         default:
00159             return 2;
00160     }
00161 }