Jon Buckman / NeatGUIs
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ILI9163.cpp Source File

ILI9163.cpp

00001 /* NeatGUI Library
00002  * Copyright (c) 2013 Neil Thiessen
00003  * Copyright (c) 2017 Jon Buckman
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "ILI9163.h"
00019 
00020 ILI9163::ILI9163(PinName D0, PinName D1, PinName D2, PinName D3, PinName D4, PinName D5, PinName D6, PinName D7, PinName cs, PinName dc, PinName wr, PinName rst) : Display(160, 128),  bus(D7, D6, D5, D4, D3, D2, D1, D0),
00021     m_CS(cs), m_DC(dc), m_WR(wr), m_RST(rst)
00022 {
00023     //Set the control pins to a known state
00024     m_CS = 1;
00025     m_DC = 1;
00026     m_WR = 0;
00027     m_RST = 0;
00028 }
00029 
00030 bool ILI9163::open()
00031 {
00032     // toggle RST low to reset
00033     m_RST =1;
00034     wait_ms(5);
00035     m_RST =0;
00036     wait_ms(20);
00037     m_RST =1;
00038     wait_ms(150);
00039 
00040 // New Haven Displays Initialization
00041     writeCommand(0x01);     // Software reset
00042     wait_ms(5);
00043     writeCommand(0x11);     // Exit sleep mode
00044     wait_ms(100);
00045     writeCommand(0x26);     // Set Gamma curve
00046     writeData8(0x04);
00047     writeCommand(0xF2);     // Gamma adjustment enabled
00048     writeData8(0x00);
00049     writeCommand(0xB1);     // Frame rate control 1
00050     writeData8(0x0A);
00051     writeData8(0x14);
00052     writeCommand(0xC0);     // Power control 1
00053     writeData8(0x0A);
00054     writeData8(0x00);
00055     writeCommand(0xC1);     // Power control 2
00056     writeData8(0x02);
00057     writeCommand(0xC5);     // Vcom control 1
00058     writeData8(0x2F);
00059     writeData8(0x3E);
00060     writeCommand(0xC7);     // Vcom offset
00061     writeData8(0x40);
00062     writeCommand(0x2A);     // Set column address
00063     writeData8(0x00);
00064     writeData8(0x00);
00065     writeData8(0x00);
00066     writeData8(0x7F);
00067     writeCommand(0x2B);     // Set page address
00068     writeData8(0x00);
00069     writeData8(0x00);
00070     writeData8(0x00);
00071     writeData8(0x9F);
00072     writeCommand(0x36);     // Set address mode
00073     writeData8(0x48);
00074     writeCommand(0x29);     // Set display on
00075     writeCommand(0x2C);     // Write memory start
00076 
00077 // Bodmer Initialization
00078 /*
00079     writeCommand(0x01);     // Software reset
00080     wait_ms(5);
00081     writeCommand(0x11);     // Exit sleep mode
00082     writeCommand(0x3A);     // Set pixel format
00083     writeData8(0xC5);
00084     writeCommand(0x26);     // Set Gamma curve
00085     writeData8(0x04);
00086     writeCommand(0xF2);     // Gamma adjustment enabled
00087     writeData8(0x01);
00088     writeCommand(0xE0);     // Positive Gamma
00089     writeData8(0x3F);
00090     writeData8(0x25);
00091     writeData8(0x1C);
00092     writeData8(0x1E);
00093     writeData8(0x20);
00094     writeData8(0x12);
00095     writeData8(0x2A);
00096     writeData8(0x90);
00097     writeData8(0x24);
00098     writeData8(0x11);
00099     writeData8(0x00);
00100     writeData8(0x00);
00101     writeData8(0x00);
00102     writeData8(0x00);
00103     writeData8(0x00);
00104     writeCommand(0xE1);     // Negative Gamma
00105     writeData8(0x20);
00106     writeData8(0x20);
00107     writeData8(0x20);
00108     writeData8(0x20);
00109     writeData8(0x05);
00110     writeData8(0x00);
00111     writeData8(0x15);
00112     writeData8(0xA7);
00113     writeData8(0x3D);
00114     writeData8(0x18);
00115     writeData8(0x25);
00116     writeData8(0x2A);
00117     writeData8(0x2B);
00118     writeData8(0x2B);
00119     writeData8(0x3A);
00120     writeCommand(0xB1);     // Frame rate control 1
00121     writeData8(0x08);
00122     writeData8(0x08);
00123     writeCommand(0xB4);     // Display inversion
00124     writeData8(0x07);
00125     writeCommand(0xC0);     // Power control 1
00126     writeData8(0x0A);
00127     writeData8(0x02);
00128     writeCommand(0xC1);     // Power control 2
00129     writeData8(0x02);
00130     writeCommand(0xC5);     // Vcom control 1
00131     writeData8(0x50);
00132     writeData8(0x5B);
00133     writeCommand(0xC7);     // Vcom offset
00134     writeData8(0x40);
00135     writeCommand(0x2A);     // Set column address
00136     writeData8(0x00);
00137     writeData8(0x00);
00138     writeData8(0x00);
00139     writeData8(0x7F);
00140     writeCommand(0x2B);     // Set page address
00141     writeData8(0x00);
00142     writeData8(0x00);
00143     writeData8(0x00);
00144     writeData8(0x9F);
00145     writeCommand(0x36);     // Set address mode
00146     writeData8(0x48);
00147     writeCommand(0x29);     // Set display on
00148 */
00149     //Return success
00150     return true;
00151 }
00152 
00153 void ILI9163::flush()
00154 {
00155     //Select low col 0, hi col 0, line 0
00156     //writeCommand(CMD_SETLOWCOLUMN | 0x0);
00157     //writeCommand(CMD_SETHIGHCOLUMN | 0x0);
00158     //writeCommand(CMD_SETSTARTLINE | 0x0);
00159 
00160     //Make sure the first byte in the buffer is the control byte
00161     //m_Buffer[0] = CONTROL_DATA;
00162 
00163     //Write the buffer
00164     //m_I2C.write(m_Addr, m_Buffer, 1025);
00165 }
00166 
00167 Display::State ILI9163::state()
00168 {
00169     //Return the base class's state
00170     return Display::state();
00171 }
00172 
00173 void ILI9163::state(State s)
00174 {
00175     //Check what the requested state is
00176     if (s == Display::DISPLAY_ON) {
00177         //Turn the display on
00178         //writeCommand(CMD_DISPLAYON);
00179     } else if (s == Display::DISPLAY_OFF) {
00180         //Turn the display off
00181         //writeCommand(CMD_DISPLAYOFF);
00182     }
00183 
00184     //Update the base class
00185     Display::state(s);
00186 }
00187 
00188 void ILI9163::drawPixel(int x, int y, unsigned int c)
00189 {
00190     //Range check the pixel
00191     if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
00192         return;
00193     
00194     //TODO: Clean up this code!!!
00195     
00196     //Set the column
00197     writeCommand(0x2A);
00198     writeData16(x);
00199     writeData16(x);
00200     
00201     //Set the page
00202     writeCommand(0x2B);
00203     writeData16(y);
00204     writeData16(y);
00205     
00206     //Don't know what this does...
00207     writeCommand(0x2C);
00208     
00209     //Woohoo, send the pixel!!!
00210     writeData16(c);
00211     
00212     //TODO: Clean up this code!!!
00213 }
00214 
00215 void ILI9163::writeCommand(char command)
00216 {
00217     //Pull dc low for Command
00218     m_DC = 0;
00219 
00220     //Pull cs low to select the TFT
00221     m_CS = 0;
00222 
00223     //Pull wr low to initialize for write
00224     m_WR = 0;
00225 
00226     //Set the command byte
00227     bus = command;
00228 
00229     //Write the command byte
00230     m_WR = 1;
00231 
00232     //Pull CS high to end the transfer
00233     m_CS = 1;
00234 }
00235 
00236 void ILI9163::writeData8(char data)
00237 {
00238     //Pull dc low for Data
00239     m_DC = 1;
00240 
00241     //Pull cs low to select the TFT
00242     m_CS = 0;
00243 
00244     //Pull wr low to initialize for write
00245     m_WR = 0;
00246 
00247     //Set the data byte
00248     bus = data;
00249 
00250     //Write the command byte
00251     m_WR = 1;
00252 
00253     //Pull CS high to end the transfer
00254     m_CS = 1;
00255 }
00256 
00257 void ILI9163::writeData16(unsigned short data)
00258 {
00259     //Pull dc low for Data
00260     m_DC = 1;
00261 
00262     //Pull cs low to select the TFT
00263     m_CS = 0;
00264 
00265     //Pull wr low to initialize for write
00266     m_WR = 0;
00267 
00268     //Write the 2 data bytes
00269     bus = (char)(data >> 8);
00270 
00271     //Write the command byte
00272     m_WR = 1;
00273 
00274     //Pull wr low to initialize for write
00275     m_WR = 0;
00276 
00277     bus = (char)data;
00278 
00279     //Write the command byte
00280     m_WR = 1;
00281 
00282     //Pull CS high to end the transfer
00283     m_CS = 1;
00284 }