Arduino style GUI

Committer:
jonebuckman
Date:
Wed Feb 27 22:23:34 2019 +0000
Revision:
3:b5409826d05f
Added ILI9163 driver.

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 "SEPS525_SPI.h"
jonebuckman 3:b5409826d05f 19
jonebuckman 3:b5409826d05f 20 SEPS525_SPI::SEPS525_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc, PinName res) : Display(160, 128), m_SPI(mosi, miso, sclk), m_CS(cs), m_DC(dc), m_RES(res)
jonebuckman 3:b5409826d05f 21 {
jonebuckman 3:b5409826d05f 22 //Deselect the display
jonebuckman 3:b5409826d05f 23 m_CS = 1;
jonebuckman 3:b5409826d05f 24
jonebuckman 3:b5409826d05f 25 //Set the SPI format to 8 bit data, high steady state clock, second edge capture
jonebuckman 3:b5409826d05f 26 m_SPI.format(8, 3);
jonebuckman 3:b5409826d05f 27
jonebuckman 3:b5409826d05f 28 //Set the SPI frequency to 20MHz
jonebuckman 3:b5409826d05f 29 m_SPI.frequency(10000000);
jonebuckman 3:b5409826d05f 30 }
jonebuckman 3:b5409826d05f 31
jonebuckman 3:b5409826d05f 32 bool SEPS525_SPI::open()
jonebuckman 3:b5409826d05f 33 {
jonebuckman 3:b5409826d05f 34 //Initialize interface and reset display driver chip
jonebuckman 3:b5409826d05f 35 m_CS = 1;
jonebuckman 3:b5409826d05f 36 wait(0.01);
jonebuckman 3:b5409826d05f 37 m_RES = 0;
jonebuckman 3:b5409826d05f 38 wait(0.001);
jonebuckman 3:b5409826d05f 39 m_RES = 1;
jonebuckman 3:b5409826d05f 40 wait(0.01);
jonebuckman 3:b5409826d05f 41
jonebuckman 3:b5409826d05f 42 //Init sequence for 160x128 OLED module
jonebuckman 3:b5409826d05f 43 writeCommand(CMD_DISP_ON_OFF);
jonebuckman 3:b5409826d05f 44 writeData(0x00); //Display off
jonebuckman 3:b5409826d05f 45
jonebuckman 3:b5409826d05f 46 writeCommand(CMD_OSC_CTL);
jonebuckman 3:b5409826d05f 47 writeData(0x01); //Export1 internal clock and OSC operates with external resistor
jonebuckman 3:b5409826d05f 48
jonebuckman 3:b5409826d05f 49 writeCommand(CMD_REDUCE_CURRENT);
jonebuckman 3:b5409826d05f 50 writeData(0x00);
jonebuckman 3:b5409826d05f 51
jonebuckman 3:b5409826d05f 52 writeCommand(CMD_CLOCK_DIV);
jonebuckman 3:b5409826d05f 53 writeData(0x30); //Clock div ratio 1: freq setting 90Hz
jonebuckman 3:b5409826d05f 54
jonebuckman 3:b5409826d05f 55
jonebuckman 3:b5409826d05f 56 writeCommand(CMD_IREF);
jonebuckman 3:b5409826d05f 57 writeData(0x00); //Iref controlled by external resistor
jonebuckman 3:b5409826d05f 58
jonebuckman 3:b5409826d05f 59 writeCommand(CMD_PRECHARGE_TIME_R);
jonebuckman 3:b5409826d05f 60 writeData(0x01);
jonebuckman 3:b5409826d05f 61 writeCommand(CMD_PRECHARGE_TIME_G);
jonebuckman 3:b5409826d05f 62 writeData(0x01);
jonebuckman 3:b5409826d05f 63 writeCommand(CMD_PRECHARGE_TIME_B);
jonebuckman 3:b5409826d05f 64 writeData(0x01);
jonebuckman 3:b5409826d05f 65
jonebuckman 3:b5409826d05f 66 writeCommand(CMD_PRECHARGE_CURRENT_R);
jonebuckman 3:b5409826d05f 67 writeData(0x0A);
jonebuckman 3:b5409826d05f 68 writeCommand(CMD_PRECHARGE_CURRENT_G);
jonebuckman 3:b5409826d05f 69 writeData(0x0A);
jonebuckman 3:b5409826d05f 70 writeCommand(CMD_PRECHARGE_CURRENT_B);
jonebuckman 3:b5409826d05f 71 writeData(0x0A);
jonebuckman 3:b5409826d05f 72
jonebuckman 3:b5409826d05f 73 writeCommand(CMD_DRIVING_CURRENT_R);
jonebuckman 3:b5409826d05f 74 writeData(0x52); //R = 82uA
jonebuckman 3:b5409826d05f 75 writeCommand(CMD_DRIVING_CURRENT_G);
jonebuckman 3:b5409826d05f 76 writeData(0x38); //R = 56uA
jonebuckman 3:b5409826d05f 77 writeCommand(CMD_DRIVING_CURRENT_B);
jonebuckman 3:b5409826d05f 78 writeData(0x3A); //R = 58uA
jonebuckman 3:b5409826d05f 79
jonebuckman 3:b5409826d05f 80 writeCommand(CMD_DISPLAY_MODE_SET);
jonebuckman 3:b5409826d05f 81 writeData(0x00); //RGB, column=0-159, column data display=Normal display
jonebuckman 3:b5409826d05f 82
jonebuckman 3:b5409826d05f 83
jonebuckman 3:b5409826d05f 84 writeCommand(CMD_RGB_IF);
jonebuckman 3:b5409826d05f 85 writeData(0x01); //External interface mode=MPU
jonebuckman 3:b5409826d05f 86
jonebuckman 3:b5409826d05f 87 //6 bits triple transfer, 262K support, Horizontal address counter is increased,
jonebuckman 3:b5409826d05f 88 //vertical address counter is increased. The data is continuously written
jonebuckman 3:b5409826d05f 89 //horizontally
jonebuckman 3:b5409826d05f 90 writeCommand(CMD_MEMORY_WRITE_MODE);
jonebuckman 3:b5409826d05f 91 writeData(0x76);
jonebuckman 3:b5409826d05f 92
jonebuckman 3:b5409826d05f 93 writeCommand(CMD_MX1_ADDR); //Column start
jonebuckman 3:b5409826d05f 94 writeData(0x00);
jonebuckman 3:b5409826d05f 95 writeCommand(CMD_MX2_ADDR); //Column end
jonebuckman 3:b5409826d05f 96 writeData(width()-1);
jonebuckman 3:b5409826d05f 97 writeCommand(CMD_MY1_ADDR); //row start
jonebuckman 3:b5409826d05f 98 writeData(0x00);
jonebuckman 3:b5409826d05f 99 writeCommand(CMD_MY2_ADDR); //row end
jonebuckman 3:b5409826d05f 100 writeData(height()-1);
jonebuckman 3:b5409826d05f 101
jonebuckman 3:b5409826d05f 102 writeCommand(CMD_MEMORY_ACCESS_POINTER_X); //X
jonebuckman 3:b5409826d05f 103 writeData(0x00);
jonebuckman 3:b5409826d05f 104 writeCommand(CMD_MEMORY_ACCESS_POINTER_Y); //Y
jonebuckman 3:b5409826d05f 105 writeData(0x00);
jonebuckman 3:b5409826d05f 106
jonebuckman 3:b5409826d05f 107 writeCommand(CMD_DUTY);
jonebuckman 3:b5409826d05f 108 writeData(0x7F);
jonebuckman 3:b5409826d05f 109
jonebuckman 3:b5409826d05f 110 writeCommand(CMD_DSL);
jonebuckman 3:b5409826d05f 111 writeData(0x00);
jonebuckman 3:b5409826d05f 112
jonebuckman 3:b5409826d05f 113 writeCommand(CMD_D1_DDRAM_FAC); //X
jonebuckman 3:b5409826d05f 114 writeData(0x00);
jonebuckman 3:b5409826d05f 115 writeCommand(CMD_D1_DDRAM_FAR); //Y
jonebuckman 3:b5409826d05f 116 writeData(0x00);
jonebuckman 3:b5409826d05f 117
jonebuckman 3:b5409826d05f 118 writeCommand(CMD_SCR1_FX1); //Screen saver columns start
jonebuckman 3:b5409826d05f 119 writeData(0x00);
jonebuckman 3:b5409826d05f 120 writeCommand(CMD_SCR1_FX2); //Screen saver columns end
jonebuckman 3:b5409826d05f 121 writeData(width()-1);
jonebuckman 3:b5409826d05f 122 writeCommand(CMD_SCR1_FY1); //screen saver row start
jonebuckman 3:b5409826d05f 123 writeData(0x00);
jonebuckman 3:b5409826d05f 124 writeCommand(CMD_SCR1_FY2); //Screen saver row end
jonebuckman 3:b5409826d05f 125 writeData(height()-1);
jonebuckman 3:b5409826d05f 126
jonebuckman 3:b5409826d05f 127 writeCommand(CMD_DISP_ON_OFF);
jonebuckman 3:b5409826d05f 128 writeData(0x01); //Display on
jonebuckman 3:b5409826d05f 129
jonebuckman 3:b5409826d05f 130 //Return success
jonebuckman 3:b5409826d05f 131 return true;
jonebuckman 3:b5409826d05f 132 }
jonebuckman 3:b5409826d05f 133
jonebuckman 3:b5409826d05f 134 void SEPS525_SPI::flush()
jonebuckman 3:b5409826d05f 135 {
jonebuckman 3:b5409826d05f 136 }
jonebuckman 3:b5409826d05f 137
jonebuckman 3:b5409826d05f 138 Display::State SEPS525_SPI::state()
jonebuckman 3:b5409826d05f 139 {
jonebuckman 3:b5409826d05f 140 //Return the base class's state
jonebuckman 3:b5409826d05f 141 return Display::state();
jonebuckman 3:b5409826d05f 142 }
jonebuckman 3:b5409826d05f 143
jonebuckman 3:b5409826d05f 144 void SEPS525_SPI::state(State s)
jonebuckman 3:b5409826d05f 145 {
jonebuckman 3:b5409826d05f 146 //Check what the requested state is
jonebuckman 3:b5409826d05f 147 if (s == Display::DISPLAY_ON) {
jonebuckman 3:b5409826d05f 148 //Turn the display on
jonebuckman 3:b5409826d05f 149 writeCommand(CMD_DISP_ON_OFF);
jonebuckman 3:b5409826d05f 150 writeData(0x01); //Display on
jonebuckman 3:b5409826d05f 151 } else if (s == Display::DISPLAY_OFF) {
jonebuckman 3:b5409826d05f 152 //Turn the display off
jonebuckman 3:b5409826d05f 153 writeCommand(CMD_DISP_ON_OFF);
jonebuckman 3:b5409826d05f 154 writeData(0x00); //Display off
jonebuckman 3:b5409826d05f 155 }
jonebuckman 3:b5409826d05f 156
jonebuckman 3:b5409826d05f 157 //Update the base class
jonebuckman 3:b5409826d05f 158 Display::state(s);
jonebuckman 3:b5409826d05f 159 }
jonebuckman 3:b5409826d05f 160
jonebuckman 3:b5409826d05f 161 void SEPS525_SPI::masterCurrent(int current)
jonebuckman 3:b5409826d05f 162 {
jonebuckman 3:b5409826d05f 163 if(current < 8)
jonebuckman 3:b5409826d05f 164 {
jonebuckman 3:b5409826d05f 165 writeCommand(CMD_REDUCE_CURRENT);
jonebuckman 3:b5409826d05f 166 writeData(0x04);
jonebuckman 3:b5409826d05f 167 }
jonebuckman 3:b5409826d05f 168 else
jonebuckman 3:b5409826d05f 169 {
jonebuckman 3:b5409826d05f 170 writeCommand(CMD_REDUCE_CURRENT);
jonebuckman 3:b5409826d05f 171 writeData(0x00);
jonebuckman 3:b5409826d05f 172 }
jonebuckman 3:b5409826d05f 173 }
jonebuckman 3:b5409826d05f 174
jonebuckman 3:b5409826d05f 175 void SEPS525_SPI::drawPixel(int x, int y, unsigned int c)
jonebuckman 3:b5409826d05f 176 {
jonebuckman 3:b5409826d05f 177 //Range check the pixel
jonebuckman 3:b5409826d05f 178 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
jonebuckman 3:b5409826d05f 179 return;
jonebuckman 3:b5409826d05f 180
jonebuckman 3:b5409826d05f 181 writeCommand(CMD_MEMORY_ACCESS_POINTER_X); // memory accesspointer x
jonebuckman 3:b5409826d05f 182 writeData(x);
jonebuckman 3:b5409826d05f 183 writeCommand(CMD_MEMORY_ACCESS_POINTER_Y); // memory accesspointer y
jonebuckman 3:b5409826d05f 184 writeData(y);
jonebuckman 3:b5409826d05f 185
jonebuckman 3:b5409826d05f 186 writeCommand(CMD_DDRAM_DATA_ACCESS_PORT);
jonebuckman 3:b5409826d05f 187 writeData((c & 0xFF0000) >> 16);
jonebuckman 3:b5409826d05f 188 writeData((c & 0x00FF00) >> 8);
jonebuckman 3:b5409826d05f 189 writeData(c & 0xFF);
jonebuckman 3:b5409826d05f 190 }
jonebuckman 3:b5409826d05f 191
jonebuckman 3:b5409826d05f 192 void SEPS525_SPI::writeCommand(char command)
jonebuckman 3:b5409826d05f 193 {
jonebuckman 3:b5409826d05f 194 //Set DC to command and select the display
jonebuckman 3:b5409826d05f 195 m_DC = 0;
jonebuckman 3:b5409826d05f 196 m_CS = 0;
jonebuckman 3:b5409826d05f 197
jonebuckman 3:b5409826d05f 198 //Write the command byte
jonebuckman 3:b5409826d05f 199 m_SPI.write(command);
jonebuckman 3:b5409826d05f 200
jonebuckman 3:b5409826d05f 201 //Deselect the display
jonebuckman 3:b5409826d05f 202 m_CS = 1;
jonebuckman 3:b5409826d05f 203 }
jonebuckman 3:b5409826d05f 204
jonebuckman 3:b5409826d05f 205 void SEPS525_SPI::writeData(char data)
jonebuckman 3:b5409826d05f 206 {
jonebuckman 3:b5409826d05f 207 //Set DC to data and select the display
jonebuckman 3:b5409826d05f 208 m_DC = 1;
jonebuckman 3:b5409826d05f 209 m_CS = 0;
jonebuckman 3:b5409826d05f 210
jonebuckman 3:b5409826d05f 211 //Write the data byte
jonebuckman 3:b5409826d05f 212 m_SPI.write(data);
jonebuckman 3:b5409826d05f 213
jonebuckman 3:b5409826d05f 214 //Deselect the display
jonebuckman 3:b5409826d05f 215 m_CS = 1;
jonebuckman 3:b5409826d05f 216 }