Arduino style GUI

Committer:
jonebuckman
Date:
Wed Feb 27 22:34:06 2019 +0000
Revision:
4:d353b314d244
Parent:
3:b5409826d05f
Updated writeCommand and writeData.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonebuckman 3:b5409826d05f 1 /* NeatGUI Library
jonebuckman 3:b5409826d05f 2 * Copyright (c) 2013 Neil Thiessen
jonebuckman 3:b5409826d05f 3 * Copyright (c) 2017 Jon Buckman
jonebuckman 3:b5409826d05f 4 *
jonebuckman 3:b5409826d05f 5 * Licensed under the Apache License, Version 2.0 (the "License");
jonebuckman 3:b5409826d05f 6 * you may not use this file except in compliance with the License.
jonebuckman 3:b5409826d05f 7 * You may obtain a copy of the License at
jonebuckman 3:b5409826d05f 8 *
jonebuckman 3:b5409826d05f 9 * http://www.apache.org/licenses/LICENSE-2.0
jonebuckman 3:b5409826d05f 10 *
jonebuckman 3:b5409826d05f 11 * Unless required by applicable law or agreed to in writing, software
jonebuckman 3:b5409826d05f 12 * distributed under the License is distributed on an "AS IS" BASIS,
jonebuckman 3:b5409826d05f 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jonebuckman 3:b5409826d05f 14 * See the License for the specific language governing permissions and
jonebuckman 3:b5409826d05f 15 * limitations under the License.
jonebuckman 3:b5409826d05f 16 */
jonebuckman 3:b5409826d05f 17
jonebuckman 3:b5409826d05f 18 #include "ILI9163.h"
jonebuckman 3:b5409826d05f 19
jonebuckman 3:b5409826d05f 20 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),
jonebuckman 3:b5409826d05f 21 m_CS(cs), m_DC(dc), m_WR(wr), m_RST(rst)
jonebuckman 3:b5409826d05f 22 {
jonebuckman 3:b5409826d05f 23 //Set the control pins to a known state
jonebuckman 3:b5409826d05f 24 m_CS = 1;
jonebuckman 3:b5409826d05f 25 m_DC = 1;
jonebuckman 3:b5409826d05f 26 m_WR = 0;
jonebuckman 3:b5409826d05f 27 m_RST = 0;
jonebuckman 3:b5409826d05f 28 }
jonebuckman 3:b5409826d05f 29
jonebuckman 3:b5409826d05f 30 bool ILI9163::open()
jonebuckman 3:b5409826d05f 31 {
jonebuckman 3:b5409826d05f 32 // toggle RST low to reset
jonebuckman 3:b5409826d05f 33 m_RST =1;
jonebuckman 3:b5409826d05f 34 wait_ms(5);
jonebuckman 3:b5409826d05f 35 m_RST =0;
jonebuckman 3:b5409826d05f 36 wait_ms(20);
jonebuckman 3:b5409826d05f 37 m_RST =1;
jonebuckman 3:b5409826d05f 38 wait_ms(150);
jonebuckman 3:b5409826d05f 39
jonebuckman 3:b5409826d05f 40 // New Haven Displays Initialization
jonebuckman 3:b5409826d05f 41 writeCommand(0x01); // Software reset
jonebuckman 3:b5409826d05f 42 wait_ms(5);
jonebuckman 3:b5409826d05f 43 writeCommand(0x11); // Exit sleep mode
jonebuckman 3:b5409826d05f 44 wait_ms(100);
jonebuckman 3:b5409826d05f 45 writeCommand(0x26); // Set Gamma curve
jonebuckman 3:b5409826d05f 46 writeData8(0x04);
jonebuckman 3:b5409826d05f 47 writeCommand(0xF2); // Gamma adjustment enabled
jonebuckman 3:b5409826d05f 48 writeData8(0x00);
jonebuckman 3:b5409826d05f 49 writeCommand(0xB1); // Frame rate control 1
jonebuckman 3:b5409826d05f 50 writeData8(0x0A);
jonebuckman 3:b5409826d05f 51 writeData8(0x14);
jonebuckman 3:b5409826d05f 52 writeCommand(0xC0); // Power control 1
jonebuckman 3:b5409826d05f 53 writeData8(0x0A);
jonebuckman 3:b5409826d05f 54 writeData8(0x00);
jonebuckman 3:b5409826d05f 55 writeCommand(0xC1); // Power control 2
jonebuckman 3:b5409826d05f 56 writeData8(0x02);
jonebuckman 3:b5409826d05f 57 writeCommand(0xC5); // Vcom control 1
jonebuckman 3:b5409826d05f 58 writeData8(0x2F);
jonebuckman 3:b5409826d05f 59 writeData8(0x3E);
jonebuckman 3:b5409826d05f 60 writeCommand(0xC7); // Vcom offset
jonebuckman 3:b5409826d05f 61 writeData8(0x40);
jonebuckman 3:b5409826d05f 62 writeCommand(0x2A); // Set column address
jonebuckman 3:b5409826d05f 63 writeData8(0x00);
jonebuckman 3:b5409826d05f 64 writeData8(0x00);
jonebuckman 3:b5409826d05f 65 writeData8(0x00);
jonebuckman 3:b5409826d05f 66 writeData8(0x7F);
jonebuckman 3:b5409826d05f 67 writeCommand(0x2B); // Set page address
jonebuckman 3:b5409826d05f 68 writeData8(0x00);
jonebuckman 3:b5409826d05f 69 writeData8(0x00);
jonebuckman 3:b5409826d05f 70 writeData8(0x00);
jonebuckman 3:b5409826d05f 71 writeData8(0x9F);
jonebuckman 3:b5409826d05f 72 writeCommand(0x36); // Set address mode
jonebuckman 3:b5409826d05f 73 writeData8(0x48);
jonebuckman 3:b5409826d05f 74 writeCommand(0x29); // Set display on
jonebuckman 3:b5409826d05f 75 writeCommand(0x2C); // Write memory start
jonebuckman 3:b5409826d05f 76
jonebuckman 3:b5409826d05f 77 // Bodmer Initialization
jonebuckman 3:b5409826d05f 78 /*
jonebuckman 3:b5409826d05f 79 writeCommand(0x01); // Software reset
jonebuckman 3:b5409826d05f 80 wait_ms(5);
jonebuckman 3:b5409826d05f 81 writeCommand(0x11); // Exit sleep mode
jonebuckman 3:b5409826d05f 82 writeCommand(0x3A); // Set pixel format
jonebuckman 3:b5409826d05f 83 writeData8(0xC5);
jonebuckman 3:b5409826d05f 84 writeCommand(0x26); // Set Gamma curve
jonebuckman 3:b5409826d05f 85 writeData8(0x04);
jonebuckman 3:b5409826d05f 86 writeCommand(0xF2); // Gamma adjustment enabled
jonebuckman 3:b5409826d05f 87 writeData8(0x01);
jonebuckman 3:b5409826d05f 88 writeCommand(0xE0); // Positive Gamma
jonebuckman 3:b5409826d05f 89 writeData8(0x3F);
jonebuckman 3:b5409826d05f 90 writeData8(0x25);
jonebuckman 3:b5409826d05f 91 writeData8(0x1C);
jonebuckman 3:b5409826d05f 92 writeData8(0x1E);
jonebuckman 3:b5409826d05f 93 writeData8(0x20);
jonebuckman 3:b5409826d05f 94 writeData8(0x12);
jonebuckman 3:b5409826d05f 95 writeData8(0x2A);
jonebuckman 3:b5409826d05f 96 writeData8(0x90);
jonebuckman 3:b5409826d05f 97 writeData8(0x24);
jonebuckman 3:b5409826d05f 98 writeData8(0x11);
jonebuckman 3:b5409826d05f 99 writeData8(0x00);
jonebuckman 3:b5409826d05f 100 writeData8(0x00);
jonebuckman 3:b5409826d05f 101 writeData8(0x00);
jonebuckman 3:b5409826d05f 102 writeData8(0x00);
jonebuckman 3:b5409826d05f 103 writeData8(0x00);
jonebuckman 3:b5409826d05f 104 writeCommand(0xE1); // Negative Gamma
jonebuckman 3:b5409826d05f 105 writeData8(0x20);
jonebuckman 3:b5409826d05f 106 writeData8(0x20);
jonebuckman 3:b5409826d05f 107 writeData8(0x20);
jonebuckman 3:b5409826d05f 108 writeData8(0x20);
jonebuckman 3:b5409826d05f 109 writeData8(0x05);
jonebuckman 3:b5409826d05f 110 writeData8(0x00);
jonebuckman 3:b5409826d05f 111 writeData8(0x15);
jonebuckman 3:b5409826d05f 112 writeData8(0xA7);
jonebuckman 3:b5409826d05f 113 writeData8(0x3D);
jonebuckman 3:b5409826d05f 114 writeData8(0x18);
jonebuckman 3:b5409826d05f 115 writeData8(0x25);
jonebuckman 3:b5409826d05f 116 writeData8(0x2A);
jonebuckman 3:b5409826d05f 117 writeData8(0x2B);
jonebuckman 3:b5409826d05f 118 writeData8(0x2B);
jonebuckman 3:b5409826d05f 119 writeData8(0x3A);
jonebuckman 3:b5409826d05f 120 writeCommand(0xB1); // Frame rate control 1
jonebuckman 3:b5409826d05f 121 writeData8(0x08);
jonebuckman 3:b5409826d05f 122 writeData8(0x08);
jonebuckman 3:b5409826d05f 123 writeCommand(0xB4); // Display inversion
jonebuckman 3:b5409826d05f 124 writeData8(0x07);
jonebuckman 3:b5409826d05f 125 writeCommand(0xC0); // Power control 1
jonebuckman 3:b5409826d05f 126 writeData8(0x0A);
jonebuckman 3:b5409826d05f 127 writeData8(0x02);
jonebuckman 3:b5409826d05f 128 writeCommand(0xC1); // Power control 2
jonebuckman 3:b5409826d05f 129 writeData8(0x02);
jonebuckman 3:b5409826d05f 130 writeCommand(0xC5); // Vcom control 1
jonebuckman 3:b5409826d05f 131 writeData8(0x50);
jonebuckman 3:b5409826d05f 132 writeData8(0x5B);
jonebuckman 3:b5409826d05f 133 writeCommand(0xC7); // Vcom offset
jonebuckman 3:b5409826d05f 134 writeData8(0x40);
jonebuckman 3:b5409826d05f 135 writeCommand(0x2A); // Set column address
jonebuckman 3:b5409826d05f 136 writeData8(0x00);
jonebuckman 3:b5409826d05f 137 writeData8(0x00);
jonebuckman 3:b5409826d05f 138 writeData8(0x00);
jonebuckman 3:b5409826d05f 139 writeData8(0x7F);
jonebuckman 3:b5409826d05f 140 writeCommand(0x2B); // Set page address
jonebuckman 3:b5409826d05f 141 writeData8(0x00);
jonebuckman 3:b5409826d05f 142 writeData8(0x00);
jonebuckman 3:b5409826d05f 143 writeData8(0x00);
jonebuckman 3:b5409826d05f 144 writeData8(0x9F);
jonebuckman 3:b5409826d05f 145 writeCommand(0x36); // Set address mode
jonebuckman 3:b5409826d05f 146 writeData8(0x48);
jonebuckman 3:b5409826d05f 147 writeCommand(0x29); // Set display on
jonebuckman 3:b5409826d05f 148 */
jonebuckman 3:b5409826d05f 149 //Return success
jonebuckman 3:b5409826d05f 150 return true;
jonebuckman 3:b5409826d05f 151 }
jonebuckman 3:b5409826d05f 152
jonebuckman 3:b5409826d05f 153 void ILI9163::flush()
jonebuckman 3:b5409826d05f 154 {
jonebuckman 3:b5409826d05f 155 //Select low col 0, hi col 0, line 0
jonebuckman 3:b5409826d05f 156 //writeCommand(CMD_SETLOWCOLUMN | 0x0);
jonebuckman 3:b5409826d05f 157 //writeCommand(CMD_SETHIGHCOLUMN | 0x0);
jonebuckman 3:b5409826d05f 158 //writeCommand(CMD_SETSTARTLINE | 0x0);
jonebuckman 3:b5409826d05f 159
jonebuckman 3:b5409826d05f 160 //Make sure the first byte in the buffer is the control byte
jonebuckman 3:b5409826d05f 161 //m_Buffer[0] = CONTROL_DATA;
jonebuckman 3:b5409826d05f 162
jonebuckman 3:b5409826d05f 163 //Write the buffer
jonebuckman 3:b5409826d05f 164 //m_I2C.write(m_Addr, m_Buffer, 1025);
jonebuckman 3:b5409826d05f 165 }
jonebuckman 3:b5409826d05f 166
jonebuckman 3:b5409826d05f 167 Display::State ILI9163::state()
jonebuckman 3:b5409826d05f 168 {
jonebuckman 3:b5409826d05f 169 //Return the base class's state
jonebuckman 3:b5409826d05f 170 return Display::state();
jonebuckman 3:b5409826d05f 171 }
jonebuckman 3:b5409826d05f 172
jonebuckman 3:b5409826d05f 173 void ILI9163::state(State s)
jonebuckman 3:b5409826d05f 174 {
jonebuckman 3:b5409826d05f 175 //Check what the requested state is
jonebuckman 3:b5409826d05f 176 if (s == Display::DISPLAY_ON) {
jonebuckman 3:b5409826d05f 177 //Turn the display on
jonebuckman 3:b5409826d05f 178 //writeCommand(CMD_DISPLAYON);
jonebuckman 3:b5409826d05f 179 } else if (s == Display::DISPLAY_OFF) {
jonebuckman 3:b5409826d05f 180 //Turn the display off
jonebuckman 3:b5409826d05f 181 //writeCommand(CMD_DISPLAYOFF);
jonebuckman 3:b5409826d05f 182 }
jonebuckman 3:b5409826d05f 183
jonebuckman 3:b5409826d05f 184 //Update the base class
jonebuckman 3:b5409826d05f 185 Display::state(s);
jonebuckman 3:b5409826d05f 186 }
jonebuckman 3:b5409826d05f 187
jonebuckman 3:b5409826d05f 188 void ILI9163::drawPixel(int x, int y, unsigned int c)
jonebuckman 3:b5409826d05f 189 {
jonebuckman 3:b5409826d05f 190 //Range check the pixel
jonebuckman 3:b5409826d05f 191 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
jonebuckman 3:b5409826d05f 192 return;
jonebuckman 3:b5409826d05f 193
jonebuckman 3:b5409826d05f 194 //TODO: Clean up this code!!!
jonebuckman 3:b5409826d05f 195
jonebuckman 3:b5409826d05f 196 //Set the column
jonebuckman 3:b5409826d05f 197 writeCommand(0x2A);
jonebuckman 3:b5409826d05f 198 writeData16(x);
jonebuckman 3:b5409826d05f 199 writeData16(x);
jonebuckman 3:b5409826d05f 200
jonebuckman 3:b5409826d05f 201 //Set the page
jonebuckman 3:b5409826d05f 202 writeCommand(0x2B);
jonebuckman 3:b5409826d05f 203 writeData16(y);
jonebuckman 3:b5409826d05f 204 writeData16(y);
jonebuckman 3:b5409826d05f 205
jonebuckman 3:b5409826d05f 206 //Don't know what this does...
jonebuckman 3:b5409826d05f 207 writeCommand(0x2C);
jonebuckman 3:b5409826d05f 208
jonebuckman 3:b5409826d05f 209 //Woohoo, send the pixel!!!
jonebuckman 3:b5409826d05f 210 writeData16(c);
jonebuckman 3:b5409826d05f 211
jonebuckman 3:b5409826d05f 212 //TODO: Clean up this code!!!
jonebuckman 3:b5409826d05f 213 }
jonebuckman 3:b5409826d05f 214
jonebuckman 3:b5409826d05f 215 void ILI9163::writeCommand(char command)
jonebuckman 3:b5409826d05f 216 {
jonebuckman 3:b5409826d05f 217 //Pull dc low for Command
jonebuckman 3:b5409826d05f 218 m_DC = 0;
jonebuckman 3:b5409826d05f 219
jonebuckman 3:b5409826d05f 220 //Pull cs low to select the TFT
jonebuckman 3:b5409826d05f 221 m_CS = 0;
jonebuckman 3:b5409826d05f 222
jonebuckman 4:d353b314d244 223 //Pull wr low to initialize for write
jonebuckman 4:d353b314d244 224 m_WR = 0;
jonebuckman 4:d353b314d244 225
jonebuckman 4:d353b314d244 226 //Set the command byte
jonebuckman 4:d353b314d244 227 bus = command;
jonebuckman 4:d353b314d244 228
jonebuckman 3:b5409826d05f 229 //Write the command byte
jonebuckman 4:d353b314d244 230 m_WR = 1;
jonebuckman 3:b5409826d05f 231
jonebuckman 3:b5409826d05f 232 //Pull CS high to end the transfer
jonebuckman 3:b5409826d05f 233 m_CS = 1;
jonebuckman 3:b5409826d05f 234 }
jonebuckman 3:b5409826d05f 235
jonebuckman 3:b5409826d05f 236 void ILI9163::writeData8(char data)
jonebuckman 3:b5409826d05f 237 {
jonebuckman 3:b5409826d05f 238 //Pull dc low for Data
jonebuckman 3:b5409826d05f 239 m_DC = 1;
jonebuckman 3:b5409826d05f 240
jonebuckman 3:b5409826d05f 241 //Pull cs low to select the TFT
jonebuckman 3:b5409826d05f 242 m_CS = 0;
jonebuckman 3:b5409826d05f 243
jonebuckman 4:d353b314d244 244 //Pull wr low to initialize for write
jonebuckman 4:d353b314d244 245 m_WR = 0;
jonebuckman 4:d353b314d244 246
jonebuckman 4:d353b314d244 247 //Set the data byte
jonebuckman 4:d353b314d244 248 bus = data;
jonebuckman 4:d353b314d244 249
jonebuckman 3:b5409826d05f 250 //Write the command byte
jonebuckman 4:d353b314d244 251 m_WR = 1;
jonebuckman 3:b5409826d05f 252
jonebuckman 3:b5409826d05f 253 //Pull CS high to end the transfer
jonebuckman 3:b5409826d05f 254 m_CS = 1;
jonebuckman 3:b5409826d05f 255 }
jonebuckman 3:b5409826d05f 256
jonebuckman 3:b5409826d05f 257 void ILI9163::writeData16(unsigned short data)
jonebuckman 3:b5409826d05f 258 {
jonebuckman 3:b5409826d05f 259 //Pull dc low for Data
jonebuckman 3:b5409826d05f 260 m_DC = 1;
jonebuckman 3:b5409826d05f 261
jonebuckman 3:b5409826d05f 262 //Pull cs low to select the TFT
jonebuckman 3:b5409826d05f 263 m_CS = 0;
jonebuckman 3:b5409826d05f 264
jonebuckman 4:d353b314d244 265 //Pull wr low to initialize for write
jonebuckman 4:d353b314d244 266 m_WR = 0;
jonebuckman 4:d353b314d244 267
jonebuckman 3:b5409826d05f 268 //Write the 2 data bytes
jonebuckman 3:b5409826d05f 269 bus = (char)(data >> 8);
jonebuckman 4:d353b314d244 270
jonebuckman 4:d353b314d244 271 //Write the command byte
jonebuckman 4:d353b314d244 272 m_WR = 1;
jonebuckman 4:d353b314d244 273
jonebuckman 4:d353b314d244 274 //Pull wr low to initialize for write
jonebuckman 4:d353b314d244 275 m_WR = 0;
jonebuckman 4:d353b314d244 276
jonebuckman 3:b5409826d05f 277 bus = (char)data;
jonebuckman 3:b5409826d05f 278
jonebuckman 4:d353b314d244 279 //Write the command byte
jonebuckman 4:d353b314d244 280 m_WR = 1;
jonebuckman 4:d353b314d244 281
jonebuckman 3:b5409826d05f 282 //Pull CS high to end the transfer
jonebuckman 3:b5409826d05f 283 m_CS = 1;
jonebuckman 3:b5409826d05f 284 }