Sean Cassidy / PCF2119

Fork of PCF2119 by Suga koubou

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCF2119.cpp Source File

PCF2119.cpp

00001 /* PCF2119x based I2C LCD Library
00002  * Copyright (c) 2014, Hiroshi Suga
00003  */
00004 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
00005  * Copyright (c) 2007-2010, sford, http://mbed.org
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 #include "PCF2119.h"
00027 #include "mbed.h"
00028 
00029 PCF2119::PCF2119(PinName sda, PinName sck, PinName reset, LCDType type, char addr) :
00030         _i2c(sda, sck), _reset(reset), _type(type), _addr(addr) {
00031     init();
00032 }
00033 
00034 PCF2119::PCF2119(I2C &i2c, PinName reset, LCDType type, char addr) :
00035         _i2c(i2c), _reset(reset), _type(type), _addr(addr) {
00036     init();
00037 }
00038 
00039 void PCF2119::init() {
00040     unsigned char buf[12];
00041 
00042     _reset = 1;
00043     wait(0.001);
00044     _reset = 0;
00045     wait(0.015);        // Wait 15ms to ensure powered up
00046 
00047     raw = 0;
00048     buf[0]  = 0x00; // CO=0, RS=0
00049     buf[1]  = 0x34; // Function_set     DL=1, M=1, SL=0, (H=0)
00050     buf[2]  = 0x0c; // Display_ctl      D=1, C=1, B=0
00051     buf[3]  = 0x06; // Entry_mode_set   ID=1, S=0
00052     buf[4]  = 0x35; // Function_set     DL=1, M=1, SL=0, (H=1)
00053     buf[5]  = 0x04; // Disp_conf        P=0, Q=0
00054     buf[6]  = 0x10; // Temp_ctl         TC1=0, TC2=0
00055     buf[7]  = 0x42; // HV_gen           S1=1, S2=0
00056     buf[8]  = 0x9f; // VLCD_set         V=0, VA=1f
00057     buf[9]  = 0x34; // Function_set     DL=1, M=1, SL=0, (H=0)
00058     buf[10] = 0x80; // set DDRAM
00059     buf[11] = 0x02; // Screen_conf      L=0
00060     _i2c.write(_addr, (char*)buf, 12);
00061     cls();
00062 }
00063 
00064 void PCF2119::character(int column, int row, int c) {
00065     unsigned char buf[2];
00066     buf[0] = 0x00; // CO=0, RS=0
00067     buf[1] = address(column, row);
00068     _i2c.write(_addr, (char*)buf, 2);
00069     writeData(c);
00070 }
00071 
00072 void PCF2119::cls() {
00073     char buf[2];
00074     buf[0] = 0x00; // CO=0, RS=0
00075     buf[1] = 0x01;
00076     _i2c.write(_addr, buf, 2);
00077     locate(0, 0);
00078 }
00079 
00080 void PCF2119::locate(int column, int row) {
00081     _column = column;
00082     _row = row;
00083 }
00084 
00085 int PCF2119::_putc(int value) {
00086     if (value == '\n') {
00087         _column = 0;
00088         _row++;
00089         if (_row >= rows()) {
00090             _row = 0;
00091         }
00092     } else {
00093         character(_column, _row, ascii2lcd(value));
00094         _column++;
00095         if (_column >= columns()) {
00096             _column = 0;
00097             _row++;
00098             if (_row >= rows()) {
00099                 _row = 0;
00100             }
00101         }
00102     }
00103     return value;
00104 }
00105 
00106 int PCF2119::_getc() {
00107     return -1;
00108 }
00109 
00110 void PCF2119::writeCommand(int command) {
00111     unsigned char buf[2];
00112     buf[0] = 0x00; // CO=0, RS=0
00113     buf[1] = command;
00114     _i2c.write(_addr, (char*)buf, 2);
00115 }
00116 
00117 void PCF2119::writeData(int data) {
00118     unsigned char buf[2];
00119     buf[0] = 0x40; // CO=0, RS=1
00120     buf[1] = data;
00121     _i2c.write(_addr, (char*)buf, 2);
00122 }
00123 
00124 int PCF2119::address(int column, int row) {
00125     switch (_type) {
00126         case LCD20x4:
00127             switch (row) {
00128                 case 0:
00129                     return 0x80 + column;
00130                 case 1:
00131                     return 0xc0 + column;
00132                 case 2:
00133                     return 0x94 + column;
00134                 case 3:
00135                     return 0xd4 + column;
00136             }
00137         case LCD16x2B:
00138             return 0x80 + (row * 40) + column;
00139         case LCD16x2:
00140         case LCD20x2:
00141         default:
00142             return 0x80 + (row * 0x40) + column;
00143     }
00144 }
00145 
00146 int PCF2119::columns() {
00147     switch (_type) {
00148         case LCD20x4:
00149         case LCD20x2:
00150             return 20;
00151         case LCD16x2:
00152         case LCD16x2B:
00153         default:
00154             return 16;
00155     }
00156 }
00157 
00158 int PCF2119::rows() {
00159     switch (_type) {
00160         case LCD20x4:
00161             return 4;
00162         case LCD16x2:
00163         case LCD16x2B:
00164         case LCD20x2:
00165         default:
00166             return 2;
00167     }
00168 }
00169 
00170 int PCF2119::ascii2lcd (int c) {
00171     if (raw) return c;
00172     if (((c >= ' ') && (c <= '?')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
00173         c |= 0x80;
00174     } else
00175     if (c >= 0xf0 && c <= 0xff) {
00176         c &= 0x0f;
00177     }
00178     return c;
00179 }
00180 
00181 void PCF2119::cgram(int num, const char *data, int len) {
00182     int i;
00183     unsigned char buf[1 + len];
00184     buf[0] = 0x00; // CO=0, RS=0
00185     buf[1] = 0x80 | (num >= 8 ? 0x40 : 0);
00186     _i2c.write(_addr, (char*)buf, 2);
00187     buf[0] = 0x00; // CO=0, RS=0
00188     buf[1] = 0x40 | (8 * (num % 8));
00189     _i2c.write(_addr, (char*)buf, 2);
00190     buf[0] = 0x40; // CO=0, RS=1
00191     for (i = 0; i < len; i ++) {
00192         buf[1 + i] = data[i];
00193     }
00194     _i2c.write(_addr, (char*)buf, 1 + len);
00195 }