Adapted from Peter Dresche's original for Waveshare 2.8inch TFT Touch Shield Board and Mbed 6. RGB order reversed by changing reg 16 commands, spi write code adjusted as there is no reset pin but there is data command pin. Wait commands changed for new thread_sleep style, Stream class explicitly included. Library to control a QVGA TFT connected to SPI. You can use printf to print text The lib can handle different fonts, draw lines, circles, rect and bmp

Committer:
John Durrell
Date:
Tue Mar 30 20:38:26 2021 +0100
Revision:
27:8360ab3c19d6
Parent:
26:231a28b27a76
Sorting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 8:65a4de035c3c 1 /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
dreschpe 8:65a4de035c3c 2 * Copyright (c) 2011 Peter Drescher - DC2PD
dreschpe 8:65a4de035c3c 3 *
dreschpe 8:65a4de035c3c 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dreschpe 8:65a4de035c3c 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dreschpe 8:65a4de035c3c 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dreschpe 8:65a4de035c3c 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dreschpe 8:65a4de035c3c 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dreschpe 8:65a4de035c3c 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dreschpe 8:65a4de035c3c 10 * THE SOFTWARE.
dreschpe 8:65a4de035c3c 11 */
dreschpe 8:65a4de035c3c 12
dreschpe 8:65a4de035c3c 13
dreschpe 8:65a4de035c3c 14 // fix bmp padding for Bitmap function
dreschpe 8:65a4de035c3c 15 // speed up pixel
dreschpe 8:65a4de035c3c 16 // 30.12.11 fix cls
dreschpe 8:65a4de035c3c 17 // 11.03.12 use DMA to speed up
dreschpe 8:65a4de035c3c 18 // 15.03.12 use SSEL for TFT CS to enable DMA Register writes
dreschpe 8:65a4de035c3c 19 // 06.04.12 fix SSEL CS problem
dreschpe 8:65a4de035c3c 20 // 06.04.12 use direct access to the spi register to speed up the library.
dreschpe 8:65a4de035c3c 21 // 11.09.12 switch back to using io pin as cs to avoid problems with SSEL CS.
dreschpe 13:2c91cb947161 22 // 21.09.12 fix Bug in BMP_16
dreschpe 8:65a4de035c3c 23 // 11.10.12 patch from Hans Bergles to get SPI1 working again
dreschpe 8:65a4de035c3c 24 // 03.02.13 add a switch to switch off DMA use for LPC11U24
dreschpe 13:2c91cb947161 25 // 04.03.13 add support for new Kinetis board
dreschpe 16:2efcbb2814fa 26 // 25.03.13 fix Bug in bitmap for Kinetis board
dreschpe 18:52cbeede86f0 27 // 18.10.13 Better Circle function from Michael Ammann
John Durrell 27:8360ab3c19d6 28 // 2020-2021 Hacked by JHD
dreschpe 8:65a4de035c3c 29
dreschpe 8:65a4de035c3c 30 #include "SPI_TFT.h"
dreschpe 8:65a4de035c3c 31 #include "mbed.h"
jhd25 25:f593b4adb905 32 // for fast performance on some STM boards
jhd25 25:f593b4adb905 33
jhd25 25:f593b4adb905 34
jhd25 24:b986bedb0a73 35
dreschpe 8:65a4de035c3c 36
dreschpe 8:65a4de035c3c 37 #define BPP 16 // Bits per pixel
dreschpe 13:2c91cb947161 38
dreschpe 13:2c91cb947161 39 #if defined TARGET_LPC1768
dreschpe 14:ea3206e8e3bd 40 #define USE_DMA // we use dma to speed up
dreschpe 14:ea3206e8e3bd 41 #define NO_MBED_LIB // we write direct to the SPI register to speed up
dreschpe 8:65a4de035c3c 42 #endif
dreschpe 8:65a4de035c3c 43
dreschpe 13:2c91cb947161 44 #if defined NO_DMA // if LPC1768 user want no DMA
dreschpe 8:65a4de035c3c 45 #undef USE_DMA
dreschpe 8:65a4de035c3c 46 #endif
dreschpe 13:2c91cb947161 47
dreschpe 8:65a4de035c3c 48
dreschpe 8:65a4de035c3c 49 //extern Serial pc;
dreschpe 8:65a4de035c3c 50 //extern DigitalOut xx; // debug !!
dreschpe 8:65a4de035c3c 51
dreschpe 8:65a4de035c3c 52 SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name)
dreschpe 8:65a4de035c3c 53 : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name)
dreschpe 8:65a4de035c3c 54 {
dreschpe 8:65a4de035c3c 55 orientation = 0;
dreschpe 8:65a4de035c3c 56 char_x = 0;
dreschpe 8:65a4de035c3c 57 tft_reset();
dreschpe 8:65a4de035c3c 58 }
dreschpe 8:65a4de035c3c 59
dreschpe 8:65a4de035c3c 60 int SPI_TFT::width()
dreschpe 8:65a4de035c3c 61 {
dreschpe 8:65a4de035c3c 62 if (orientation == 0 || orientation == 2) return 240;
dreschpe 8:65a4de035c3c 63 else return 320;
dreschpe 8:65a4de035c3c 64 }
dreschpe 8:65a4de035c3c 65
dreschpe 8:65a4de035c3c 66
dreschpe 8:65a4de035c3c 67 int SPI_TFT::height()
dreschpe 8:65a4de035c3c 68 {
dreschpe 8:65a4de035c3c 69 if (orientation == 0 || orientation == 2) return 320;
dreschpe 8:65a4de035c3c 70 else return 240;
dreschpe 8:65a4de035c3c 71 }
dreschpe 8:65a4de035c3c 72
dreschpe 8:65a4de035c3c 73
dreschpe 8:65a4de035c3c 74 void SPI_TFT::set_orientation(unsigned int o)
dreschpe 8:65a4de035c3c 75 {
dreschpe 8:65a4de035c3c 76 orientation = o;
dreschpe 8:65a4de035c3c 77 switch (orientation) {
dreschpe 8:65a4de035c3c 78 case 0:
jhd25 23:469bf5f3c8ac 79 wr_reg(0x16, 0x08);
dreschpe 8:65a4de035c3c 80 break;
dreschpe 8:65a4de035c3c 81 case 1:
jhd25 23:469bf5f3c8ac 82 wr_reg(0x16, 0x68);
dreschpe 8:65a4de035c3c 83 break;
dreschpe 8:65a4de035c3c 84 case 2:
jhd25 23:469bf5f3c8ac 85 wr_reg(0x16, 0xC8);
dreschpe 8:65a4de035c3c 86 break;
dreschpe 8:65a4de035c3c 87 case 3:
jhd25 23:469bf5f3c8ac 88 wr_reg(0x16, 0xA8);
dreschpe 8:65a4de035c3c 89 break;
dreschpe 8:65a4de035c3c 90 }
dreschpe 8:65a4de035c3c 91 WindowMax();
dreschpe 8:65a4de035c3c 92 }
dreschpe 8:65a4de035c3c 93
dreschpe 8:65a4de035c3c 94
dreschpe 8:65a4de035c3c 95 // write command to tft register
dreschpe 8:65a4de035c3c 96
dreschpe 8:65a4de035c3c 97 void SPI_TFT::wr_cmd(unsigned char cmd)
dreschpe 8:65a4de035c3c 98 {
jhd25 24:b986bedb0a73 99 unsigned short spi_d;
jhd25 23:469bf5f3c8ac 100 spi_d = 0x7000 | cmd ;
jhd25 23:469bf5f3c8ac 101 _spi.write(spi_d); // mbed lib
jhd25 23:469bf5f3c8ac 102
dreschpe 8:65a4de035c3c 103 }
dreschpe 8:65a4de035c3c 104
dreschpe 8:65a4de035c3c 105
dreschpe 13:2c91cb947161 106 // write data to tft register
dreschpe 8:65a4de035c3c 107 void SPI_TFT::wr_dat(unsigned char dat)
dreschpe 8:65a4de035c3c 108 {
jhd25 23:469bf5f3c8ac 109 unsigned short spi_d;
jhd25 23:469bf5f3c8ac 110 spi_d = 0x7200 | dat;
jhd25 23:469bf5f3c8ac 111 _spi.write(spi_d);
dreschpe 8:65a4de035c3c 112 }
dreschpe 8:65a4de035c3c 113
dreschpe 8:65a4de035c3c 114
dreschpe 8:65a4de035c3c 115
dreschpe 8:65a4de035c3c 116 // the HX8347-D controller do not use the MISO (SDO) Signal.
dreschpe 8:65a4de035c3c 117 // This is a bug - ?
dreschpe 14:ea3206e8e3bd 118 // A read will return 0 at the moment
dreschpe 8:65a4de035c3c 119
dreschpe 8:65a4de035c3c 120 unsigned short SPI_TFT::rd_dat (void)
dreschpe 8:65a4de035c3c 121 {
dreschpe 8:65a4de035c3c 122 unsigned short val = 0;
dreschpe 8:65a4de035c3c 123
dreschpe 8:65a4de035c3c 124 //val = _spi.write(0x73ff); /* Dummy read 1 */
dreschpe 8:65a4de035c3c 125 //val = _spi.write(0x0000); /* Read D8..D15 */
dreschpe 8:65a4de035c3c 126 return (val);
dreschpe 8:65a4de035c3c 127 }
dreschpe 8:65a4de035c3c 128
dreschpe 13:2c91cb947161 129 // write to a TFT register
dreschpe 8:65a4de035c3c 130 void SPI_TFT::wr_reg (unsigned char reg, unsigned char val)
dreschpe 8:65a4de035c3c 131 {
jhd25 26:231a28b27a76 132 _spi.lock();
jhd25 26:231a28b27a76 133 _spi.format(16,3);
jhd25 23:469bf5f3c8ac 134 _cs=0;
jhd25 23:469bf5f3c8ac 135 _reset=0;
dreschpe 8:65a4de035c3c 136 wr_cmd(reg);
jhd25 23:469bf5f3c8ac 137 _reset=1;
dreschpe 8:65a4de035c3c 138 wr_dat(val);
jhd25 23:469bf5f3c8ac 139 _cs=1;
jhd25 26:231a28b27a76 140 _spi.unlock();
dreschpe 8:65a4de035c3c 141 }
dreschpe 8:65a4de035c3c 142
dreschpe 13:2c91cb947161 143 // read from a TFT register
dreschpe 8:65a4de035c3c 144 unsigned short SPI_TFT::rd_reg (unsigned char reg)
dreschpe 8:65a4de035c3c 145 {
dreschpe 8:65a4de035c3c 146 wr_cmd(reg);
dreschpe 8:65a4de035c3c 147 return(rd_dat());
dreschpe 8:65a4de035c3c 148 }
dreschpe 8:65a4de035c3c 149
dreschpe 13:2c91cb947161 150 // setup TFT controller - this is called by constructor
dreschpe 8:65a4de035c3c 151 void SPI_TFT::tft_reset()
dreschpe 8:65a4de035c3c 152 {
jhd25 23:469bf5f3c8ac 153 // 16 Bit SPI
dreschpe 8:65a4de035c3c 154 _spi.format(16,3); // 16 bit spi mode 3
jhd25 23:469bf5f3c8ac 155
dreschpe 8:65a4de035c3c 156 _spi.frequency(48000000); // 48 Mhz SPI clock
dreschpe 8:65a4de035c3c 157 _cs = 1; // cs high
jhd25 20:275bf616ceb7 158 // end reset
jhd25 20:275bf616ceb7 159 thread_sleep_for(5);
dreschpe 8:65a4de035c3c 160
dreschpe 8:65a4de035c3c 161 /* Start Initial Sequence ----------------------------------------------------*/
dreschpe 8:65a4de035c3c 162 wr_reg(0xEA, 0x00); /* Reset Power Control 1 */
dreschpe 8:65a4de035c3c 163 wr_reg(0xEB, 0x20); /* Power Control 2 */
dreschpe 8:65a4de035c3c 164 wr_reg(0xEC, 0x0C); /* Power Control 3 */
dreschpe 8:65a4de035c3c 165 wr_reg(0xED, 0xC4); /* Power Control 4 */
dreschpe 8:65a4de035c3c 166 wr_reg(0xE8, 0x40); /* Source OPON_N */
dreschpe 8:65a4de035c3c 167 wr_reg(0xE9, 0x38); /* Source OPON_I */
dreschpe 8:65a4de035c3c 168 wr_reg(0xF1, 0x01); /* */
dreschpe 8:65a4de035c3c 169 wr_reg(0xF2, 0x10); /* */
dreschpe 8:65a4de035c3c 170 wr_reg(0x27, 0xA3); /* Display Control 2 */
dreschpe 8:65a4de035c3c 171
dreschpe 8:65a4de035c3c 172 /* Power On sequence ---------------------------------------------------------*/
dreschpe 8:65a4de035c3c 173 wr_reg(0x1B, 0x1B); /* Power Control 2 */
dreschpe 8:65a4de035c3c 174 wr_reg(0x1A, 0x01); /* Power Control 1 */
dreschpe 8:65a4de035c3c 175 wr_reg(0x24, 0x2F); /* Vcom Control 2 */
dreschpe 8:65a4de035c3c 176 wr_reg(0x25, 0x57); /* Vcom Control 3 */
dreschpe 8:65a4de035c3c 177 wr_reg(0x23, 0x8D); /* Vcom Control 1 */
dreschpe 8:65a4de035c3c 178
dreschpe 8:65a4de035c3c 179 /* Gamma settings -----------------------------------------------------------*/
dreschpe 13:2c91cb947161 180 wr_reg(0x40,0x00); // default setup
dreschpe 8:65a4de035c3c 181 wr_reg(0x41,0x00); //
dreschpe 8:65a4de035c3c 182 wr_reg(0x42,0x01); //
dreschpe 8:65a4de035c3c 183 wr_reg(0x43,0x13); //
dreschpe 8:65a4de035c3c 184 wr_reg(0x44,0x10); //
dreschpe 8:65a4de035c3c 185 wr_reg(0x45,0x26); //
dreschpe 8:65a4de035c3c 186 wr_reg(0x46,0x08); //
dreschpe 8:65a4de035c3c 187 wr_reg(0x47,0x51); //
dreschpe 8:65a4de035c3c 188 wr_reg(0x48,0x02); //
dreschpe 8:65a4de035c3c 189 wr_reg(0x49,0x12); //
dreschpe 8:65a4de035c3c 190 wr_reg(0x4A,0x18); //
dreschpe 8:65a4de035c3c 191 wr_reg(0x4B,0x19); //
dreschpe 8:65a4de035c3c 192 wr_reg(0x4C,0x14); //
dreschpe 8:65a4de035c3c 193 wr_reg(0x50,0x19); //
dreschpe 8:65a4de035c3c 194 wr_reg(0x51,0x2F); //
dreschpe 8:65a4de035c3c 195 wr_reg(0x52,0x2C); //
dreschpe 8:65a4de035c3c 196 wr_reg(0x53,0x3E); //
dreschpe 8:65a4de035c3c 197 wr_reg(0x54,0x3F); //
dreschpe 8:65a4de035c3c 198 wr_reg(0x55,0x3F); //
dreschpe 8:65a4de035c3c 199 wr_reg(0x56,0x2E); //
dreschpe 8:65a4de035c3c 200 wr_reg(0x57,0x77); //
dreschpe 8:65a4de035c3c 201 wr_reg(0x58,0x0B); //
dreschpe 8:65a4de035c3c 202 wr_reg(0x59,0x06); //
dreschpe 8:65a4de035c3c 203 wr_reg(0x5A,0x07); //
dreschpe 8:65a4de035c3c 204 wr_reg(0x5B,0x0D); //
dreschpe 8:65a4de035c3c 205 wr_reg(0x5C,0x1D); //
dreschpe 8:65a4de035c3c 206 wr_reg(0x5D,0xCC); //
dreschpe 8:65a4de035c3c 207
dreschpe 8:65a4de035c3c 208 /* Power + Osc ---------------------------------------------------------------*/
dreschpe 14:ea3206e8e3bd 209 wr_reg(0x18, 0x36); /* OSC Control 1 */
dreschpe 14:ea3206e8e3bd 210 wr_reg(0x19, 0x01); /* OSC Control 2 */
dreschpe 14:ea3206e8e3bd 211 wr_reg(0x01, 0x00); /* Display Mode Control */
dreschpe 14:ea3206e8e3bd 212 wr_reg(0x1F, 0x88); /* Power Control 6 */
jhd25 20:275bf616ceb7 213 thread_sleep_for(5); /* Delay 5 ms */
dreschpe 14:ea3206e8e3bd 214 wr_reg(0x1F, 0x80); /* Power Control 6 */
jhd25 20:275bf616ceb7 215 thread_sleep_for(5); /* Delay 5 ms */
dreschpe 14:ea3206e8e3bd 216 wr_reg(0x1F, 0x90); /* Power Control 6 */
jhd25 20:275bf616ceb7 217 thread_sleep_for(5); /* Delay 5 ms */
dreschpe 14:ea3206e8e3bd 218 wr_reg(0x1F, 0xD0); /* Power Control 6 */
jhd25 20:275bf616ceb7 219 thread_sleep_for(5); /* Delay 5 ms */
dreschpe 8:65a4de035c3c 220
dreschpe 14:ea3206e8e3bd 221 wr_reg(0x17, 0x05); /* Colmod 16Bit/Pixel */
dreschpe 8:65a4de035c3c 222
dreschpe 14:ea3206e8e3bd 223 wr_reg(0x36, 0x00); /* Panel Characteristic */
dreschpe 14:ea3206e8e3bd 224 wr_reg(0x28, 0x38); /* Display Control 3 */
jhd25 20:275bf616ceb7 225 thread_sleep_for(40);
dreschpe 14:ea3206e8e3bd 226 wr_reg(0x28, 0x3C); /* Display Control 3 */
dreschpe 8:65a4de035c3c 227 switch (orientation) {
dreschpe 8:65a4de035c3c 228 case 0:
jhd25 23:469bf5f3c8ac 229 wr_reg(0x16, 0x08);
dreschpe 8:65a4de035c3c 230 break;
dreschpe 8:65a4de035c3c 231 case 2:
jhd25 23:469bf5f3c8ac 232 wr_reg(0x16, 0xC8);
dreschpe 8:65a4de035c3c 233 break;
dreschpe 8:65a4de035c3c 234 case 3:
jhd25 23:469bf5f3c8ac 235 wr_reg(0x16, 0xA8);
dreschpe 14:ea3206e8e3bd 236 break;
dreschpe 14:ea3206e8e3bd 237 case 1:
dreschpe 14:ea3206e8e3bd 238 default:
jhd25 23:469bf5f3c8ac 239 wr_reg(0x16, 0x68);
dreschpe 14:ea3206e8e3bd 240 break;
dreschpe 14:ea3206e8e3bd 241
dreschpe 8:65a4de035c3c 242 }
jhd25 23:469bf5f3c8ac 243
dreschpe 8:65a4de035c3c 244 WindowMax ();
dreschpe 8:65a4de035c3c 245 }
dreschpe 8:65a4de035c3c 246
dreschpe 13:2c91cb947161 247 // Set one pixel
dreschpe 8:65a4de035c3c 248 void SPI_TFT::pixel(int x, int y, int color)
dreschpe 8:65a4de035c3c 249 {
dreschpe 8:65a4de035c3c 250 wr_reg(0x03, (x >> 0));
dreschpe 8:65a4de035c3c 251 wr_reg(0x02, (x >> 8));
dreschpe 8:65a4de035c3c 252 wr_reg(0x07, (y >> 0));
dreschpe 8:65a4de035c3c 253 wr_reg(0x06, (y >> 8));
jhd25 22:4a0f306be8ef 254 _spi.lock();
dreschpe 8:65a4de035c3c 255 _cs = 0;
jhd25 23:469bf5f3c8ac 256 _reset=0;
jhd25 23:469bf5f3c8ac 257 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 258 _reset=1;
jhd25 23:469bf5f3c8ac 259 // _spi.format(8,3); // 8 bit Mode 3
jhd25 23:469bf5f3c8ac 260 // _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0
jhd25 23:469bf5f3c8ac 261 // _spi.format(16,3); // switch to 16 bit Mode 3
jhd25 25:f593b4adb905 262 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 263 _spi.directWrite(color);
jhd25 25:f593b4adb905 264 #else
jhd25 25:f593b4adb905 265 _spi.write(color);
jhd25 25:f593b4adb905 266 #endif // Write D0..D15
dreschpe 8:65a4de035c3c 267 _cs = 1;
jhd25 22:4a0f306be8ef 268 _spi.unlock();
dreschpe 8:65a4de035c3c 269 }
dreschpe 8:65a4de035c3c 270
dreschpe 13:2c91cb947161 271 // define draw area
dreschpe 8:65a4de035c3c 272 void SPI_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h)
dreschpe 8:65a4de035c3c 273 {
dreschpe 8:65a4de035c3c 274 wr_reg(0x03, x );
dreschpe 8:65a4de035c3c 275 wr_reg(0x02, (x >> 8));
dreschpe 8:65a4de035c3c 276 wr_reg(0x05, x+w-1 );
jhd25 20:275bf616ceb7 277 wr_reg(0x04, ((x+w-1) >> 8));
dreschpe 8:65a4de035c3c 278 wr_reg(0x07, y );
dreschpe 8:65a4de035c3c 279 wr_reg(0x06, ( y >> 8));
dreschpe 8:65a4de035c3c 280 wr_reg(0x09, ( y+h-1 ));
jhd25 20:275bf616ceb7 281 wr_reg(0x08, ( (y+h-1) >> 8));
dreschpe 8:65a4de035c3c 282 }
dreschpe 8:65a4de035c3c 283
dreschpe 13:2c91cb947161 284 // set draw area to max
dreschpe 8:65a4de035c3c 285 void SPI_TFT::WindowMax (void)
dreschpe 8:65a4de035c3c 286 {
dreschpe 8:65a4de035c3c 287 window (0, 0, width(), height());
dreschpe 8:65a4de035c3c 288 }
dreschpe 8:65a4de035c3c 289
dreschpe 8:65a4de035c3c 290
dreschpe 13:2c91cb947161 291 // clear screen
dreschpe 8:65a4de035c3c 292 void SPI_TFT::cls (void)
dreschpe 8:65a4de035c3c 293 {
dreschpe 14:ea3206e8e3bd 294 fprintf(stderr, "CLS \n\r");
dreschpe 8:65a4de035c3c 295 int pixel = ( width() * height());
dreschpe 8:65a4de035c3c 296 WindowMax();
jhd25 23:469bf5f3c8ac 297 _spi.lock();
jhd25 23:469bf5f3c8ac 298 _cs = 0;
jhd25 23:469bf5f3c8ac 299 _reset=0;
dreschpe 8:65a4de035c3c 300 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 301 _reset=1; // 16 bit SPI
jhd25 23:469bf5f3c8ac 302 // _spi.format(8,3); // 8 bit Mode 3
jhd25 23:469bf5f3c8ac 303 // _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0
jhd25 23:469bf5f3c8ac 304 // _spi.format(16,3); // switch back to 16 bit Mode 3
jhd25 23:469bf5f3c8ac 305 unsigned int i;
jhd25 23:469bf5f3c8ac 306 for (i = 0; i < ( width() * height()); i++)
jhd25 25:f593b4adb905 307 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 308 _spi.directWrite(_background);
jhd25 25:f593b4adb905 309 #else
jhd25 25:f593b4adb905 310 _spi.write(_background);
jhd25 25:f593b4adb905 311 #endif
dreschpe 8:65a4de035c3c 312 _cs = 1;
jhd25 22:4a0f306be8ef 313 _spi.unlock();
dreschpe 8:65a4de035c3c 314 }
dreschpe 8:65a4de035c3c 315
dreschpe 8:65a4de035c3c 316 void SPI_TFT::circle(int x0, int y0, int r, int color)
dreschpe 18:52cbeede86f0 317 {
dreschpe 18:52cbeede86f0 318 int x = -r, y = 0, err = 2-2*r, e2;
dreschpe 18:52cbeede86f0 319 do {
dreschpe 18:52cbeede86f0 320 pixel(x0-x, y0+y,color);
dreschpe 18:52cbeede86f0 321 pixel(x0+x, y0+y,color);
dreschpe 18:52cbeede86f0 322 pixel(x0+x, y0-y,color);
dreschpe 18:52cbeede86f0 323 pixel(x0-x, y0-y,color);
dreschpe 18:52cbeede86f0 324 e2 = err;
dreschpe 18:52cbeede86f0 325 if (e2 <= y) {
dreschpe 18:52cbeede86f0 326 err += ++y*2+1;
dreschpe 18:52cbeede86f0 327 if (-x == y && e2 <= x) e2 = 0;
dreschpe 18:52cbeede86f0 328 }
dreschpe 18:52cbeede86f0 329 if (e2 > x) err += ++x*2+1;
dreschpe 18:52cbeede86f0 330 } while (x <= 0);
dreschpe 18:52cbeede86f0 331
dreschpe 18:52cbeede86f0 332 }
dreschpe 18:52cbeede86f0 333
dreschpe 18:52cbeede86f0 334 void SPI_TFT::fillcircle(int x0, int y0, int r, int color)
dreschpe 8:65a4de035c3c 335 {
dreschpe 18:52cbeede86f0 336 int x = -r, y = 0, err = 2-2*r, e2;
dreschpe 18:52cbeede86f0 337 do {
dreschpe 18:52cbeede86f0 338 vline(x0-x, y0-y, y0+y, color);
dreschpe 18:52cbeede86f0 339 vline(x0+x, y0-y, y0+y, color);
dreschpe 18:52cbeede86f0 340 e2 = err;
dreschpe 18:52cbeede86f0 341 if (e2 <= y) {
dreschpe 18:52cbeede86f0 342 err += ++y*2+1;
dreschpe 18:52cbeede86f0 343 if (-x == y && e2 <= x) e2 = 0;
dreschpe 8:65a4de035c3c 344 }
dreschpe 18:52cbeede86f0 345 if (e2 > x) err += ++x*2+1;
dreschpe 18:52cbeede86f0 346 } while (x <= 0);
dreschpe 8:65a4de035c3c 347 }
dreschpe 8:65a4de035c3c 348
dreschpe 8:65a4de035c3c 349
dreschpe 13:2c91cb947161 350 // draw horizontal line
dreschpe 8:65a4de035c3c 351 void SPI_TFT::hline(int x0, int x1, int y, int color)
dreschpe 8:65a4de035c3c 352 {
dreschpe 11:9bb71766cafc 353 int w;
dreschpe 8:65a4de035c3c 354 w = x1 - x0 + 1;
dreschpe 8:65a4de035c3c 355 window(x0,y,w,1);
jhd25 23:469bf5f3c8ac 356 _spi.lock();
jhd25 23:469bf5f3c8ac 357 _cs = 0;
jhd25 23:469bf5f3c8ac 358 _reset=0;
dreschpe 8:65a4de035c3c 359 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 360 _reset=1;
jhd25 25:f593b4adb905 361 // switch back to 16 bit Mode 3
dreschpe 14:ea3206e8e3bd 362 for (int j=0; j<w; j++) {
jhd25 25:f593b4adb905 363 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 364 _spi.directWrite(color);
jhd25 25:f593b4adb905 365 #else
jhd25 25:f593b4adb905 366 _spi.write(color);
jhd25 25:f593b4adb905 367 #endif // one line
dreschpe 14:ea3206e8e3bd 368 }
jhd25 23:469bf5f3c8ac 369
dreschpe 8:65a4de035c3c 370 _cs = 1;
jhd25 22:4a0f306be8ef 371 _spi.unlock();
dreschpe 8:65a4de035c3c 372 WindowMax();
dreschpe 8:65a4de035c3c 373 return;
dreschpe 8:65a4de035c3c 374 }
dreschpe 8:65a4de035c3c 375
dreschpe 13:2c91cb947161 376 // draw vertical line
dreschpe 8:65a4de035c3c 377 void SPI_TFT::vline(int x, int y0, int y1, int color)
dreschpe 8:65a4de035c3c 378 {
dreschpe 8:65a4de035c3c 379 int h;
dreschpe 8:65a4de035c3c 380 h = y1 - y0 + 1;
dreschpe 8:65a4de035c3c 381 window(x,y0,1,h);
jhd25 23:469bf5f3c8ac 382 _spi.lock();
jhd25 23:469bf5f3c8ac 383 _cs = 0;
jhd25 23:469bf5f3c8ac 384 _reset=0;
dreschpe 8:65a4de035c3c 385 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 386 _reset=1;
jhd25 24:b986bedb0a73 387 // switch to 16 bit Mode 3
jhd25 24:b986bedb0a73 388 for (int y=0; y<h; y++) {
jhd25 25:f593b4adb905 389 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 390 _spi.directWrite(color);
jhd25 25:f593b4adb905 391 #else
jhd25 25:f593b4adb905 392 _spi.write(color);
jhd25 25:f593b4adb905 393 #endif // one line
jhd25 24:b986bedb0a73 394 }
jhd25 23:469bf5f3c8ac 395
dreschpe 8:65a4de035c3c 396 _cs = 1;
jhd25 22:4a0f306be8ef 397 _spi.unlock();
dreschpe 8:65a4de035c3c 398 WindowMax();
dreschpe 8:65a4de035c3c 399 return;
dreschpe 8:65a4de035c3c 400 }
dreschpe 8:65a4de035c3c 401
dreschpe 8:65a4de035c3c 402
dreschpe 13:2c91cb947161 403 // draw line
dreschpe 8:65a4de035c3c 404 void SPI_TFT::line(int x0, int y0, int x1, int y1, int color)
dreschpe 8:65a4de035c3c 405 {
dreschpe 8:65a4de035c3c 406 //WindowMax();
dreschpe 8:65a4de035c3c 407 int dx = 0, dy = 0;
dreschpe 8:65a4de035c3c 408 int dx_sym = 0, dy_sym = 0;
dreschpe 8:65a4de035c3c 409 int dx_x2 = 0, dy_x2 = 0;
dreschpe 8:65a4de035c3c 410 int di = 0;
dreschpe 8:65a4de035c3c 411
dreschpe 8:65a4de035c3c 412 dx = x1-x0;
dreschpe 8:65a4de035c3c 413 dy = y1-y0;
dreschpe 8:65a4de035c3c 414
dreschpe 8:65a4de035c3c 415 if (dx == 0) { /* vertical line */
dreschpe 8:65a4de035c3c 416 if (y1 > y0) vline(x0,y0,y1,color);
dreschpe 8:65a4de035c3c 417 else vline(x0,y1,y0,color);
dreschpe 8:65a4de035c3c 418 return;
dreschpe 8:65a4de035c3c 419 }
dreschpe 8:65a4de035c3c 420
dreschpe 8:65a4de035c3c 421 if (dx > 0) {
dreschpe 8:65a4de035c3c 422 dx_sym = 1;
dreschpe 8:65a4de035c3c 423 } else {
dreschpe 8:65a4de035c3c 424 dx_sym = -1;
dreschpe 8:65a4de035c3c 425 }
dreschpe 8:65a4de035c3c 426 if (dy == 0) { /* horizontal line */
dreschpe 8:65a4de035c3c 427 if (x1 > x0) hline(x0,x1,y0,color);
dreschpe 8:65a4de035c3c 428 else hline(x1,x0,y0,color);
dreschpe 8:65a4de035c3c 429 return;
dreschpe 8:65a4de035c3c 430 }
dreschpe 8:65a4de035c3c 431
dreschpe 8:65a4de035c3c 432 if (dy > 0) {
dreschpe 8:65a4de035c3c 433 dy_sym = 1;
dreschpe 8:65a4de035c3c 434 } else {
dreschpe 8:65a4de035c3c 435 dy_sym = -1;
dreschpe 8:65a4de035c3c 436 }
dreschpe 8:65a4de035c3c 437
dreschpe 8:65a4de035c3c 438 dx = dx_sym*dx;
dreschpe 8:65a4de035c3c 439 dy = dy_sym*dy;
dreschpe 8:65a4de035c3c 440
dreschpe 8:65a4de035c3c 441 dx_x2 = dx*2;
dreschpe 8:65a4de035c3c 442 dy_x2 = dy*2;
dreschpe 8:65a4de035c3c 443
dreschpe 8:65a4de035c3c 444 if (dx >= dy) {
dreschpe 8:65a4de035c3c 445 di = dy_x2 - dx;
dreschpe 8:65a4de035c3c 446 while (x0 != x1) {
dreschpe 8:65a4de035c3c 447
dreschpe 8:65a4de035c3c 448 pixel(x0, y0, color);
dreschpe 8:65a4de035c3c 449 x0 += dx_sym;
dreschpe 8:65a4de035c3c 450 if (di<0) {
dreschpe 8:65a4de035c3c 451 di += dy_x2;
dreschpe 8:65a4de035c3c 452 } else {
dreschpe 8:65a4de035c3c 453 di += dy_x2 - dx_x2;
dreschpe 8:65a4de035c3c 454 y0 += dy_sym;
dreschpe 8:65a4de035c3c 455 }
dreschpe 8:65a4de035c3c 456 }
dreschpe 8:65a4de035c3c 457 pixel(x0, y0, color);
dreschpe 8:65a4de035c3c 458 } else {
dreschpe 8:65a4de035c3c 459 di = dx_x2 - dy;
dreschpe 8:65a4de035c3c 460 while (y0 != y1) {
dreschpe 8:65a4de035c3c 461 pixel(x0, y0, color);
dreschpe 8:65a4de035c3c 462 y0 += dy_sym;
dreschpe 8:65a4de035c3c 463 if (di < 0) {
dreschpe 8:65a4de035c3c 464 di += dx_x2;
dreschpe 8:65a4de035c3c 465 } else {
dreschpe 8:65a4de035c3c 466 di += dx_x2 - dy_x2;
dreschpe 8:65a4de035c3c 467 x0 += dx_sym;
dreschpe 8:65a4de035c3c 468 }
dreschpe 8:65a4de035c3c 469 }
dreschpe 8:65a4de035c3c 470 pixel(x0, y0, color);
dreschpe 8:65a4de035c3c 471 }
dreschpe 8:65a4de035c3c 472 return;
dreschpe 8:65a4de035c3c 473 }
dreschpe 8:65a4de035c3c 474
dreschpe 13:2c91cb947161 475 // draw rect
dreschpe 8:65a4de035c3c 476 void SPI_TFT::rect(int x0, int y0, int x1, int y1, int color)
dreschpe 8:65a4de035c3c 477 {
dreschpe 8:65a4de035c3c 478
dreschpe 8:65a4de035c3c 479 if (x1 > x0) hline(x0,x1,y0,color);
dreschpe 8:65a4de035c3c 480 else hline(x1,x0,y0,color);
dreschpe 8:65a4de035c3c 481
dreschpe 8:65a4de035c3c 482 if (y1 > y0) vline(x0,y0,y1,color);
dreschpe 8:65a4de035c3c 483 else vline(x0,y1,y0,color);
dreschpe 8:65a4de035c3c 484
dreschpe 8:65a4de035c3c 485 if (x1 > x0) hline(x0,x1,y1,color);
dreschpe 8:65a4de035c3c 486 else hline(x1,x0,y1,color);
dreschpe 8:65a4de035c3c 487
dreschpe 8:65a4de035c3c 488 if (y1 > y0) vline(x1,y0,y1,color);
dreschpe 8:65a4de035c3c 489 else vline(x1,y1,y0,color);
dreschpe 8:65a4de035c3c 490
dreschpe 8:65a4de035c3c 491 return;
dreschpe 8:65a4de035c3c 492 }
dreschpe 8:65a4de035c3c 493
dreschpe 8:65a4de035c3c 494
dreschpe 13:2c91cb947161 495 // fill rect
dreschpe 8:65a4de035c3c 496 void SPI_TFT::fillrect(int x0, int y0, int x1, int y1, int color)
dreschpe 8:65a4de035c3c 497 {
dreschpe 8:65a4de035c3c 498
dreschpe 8:65a4de035c3c 499 int h = y1 - y0 + 1;
dreschpe 8:65a4de035c3c 500 int w = x1 - x0 + 1;
dreschpe 8:65a4de035c3c 501 int pixel = h * w;
dreschpe 8:65a4de035c3c 502 window(x0,y0,w,h);
jhd25 23:469bf5f3c8ac 503 _spi.lock();
jhd25 23:469bf5f3c8ac 504 _cs = 0;
jhd25 23:469bf5f3c8ac 505 _reset=0;
dreschpe 8:65a4de035c3c 506 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 507 _reset=1;
jhd25 24:b986bedb0a73 508
jhd25 24:b986bedb0a73 509 for (int p=0; p<pixel; p++) {
jhd25 25:f593b4adb905 510 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 511 _spi.directWrite(color);
jhd25 25:f593b4adb905 512 #else
jhd25 25:f593b4adb905 513 _spi.write(color);
jhd25 25:f593b4adb905 514 #endif // one line
jhd25 24:b986bedb0a73 515 }
jhd25 23:469bf5f3c8ac 516
dreschpe 8:65a4de035c3c 517 _cs = 1;
jhd25 22:4a0f306be8ef 518 _spi.unlock();
dreschpe 8:65a4de035c3c 519 WindowMax();
dreschpe 8:65a4de035c3c 520 return;
dreschpe 8:65a4de035c3c 521 }
dreschpe 8:65a4de035c3c 522
dreschpe 13:2c91cb947161 523 // set cursor position
dreschpe 8:65a4de035c3c 524 void SPI_TFT::locate(int x, int y)
dreschpe 8:65a4de035c3c 525 {
dreschpe 8:65a4de035c3c 526 char_x = x;
dreschpe 8:65a4de035c3c 527 char_y = y;
dreschpe 8:65a4de035c3c 528 }
dreschpe 8:65a4de035c3c 529
dreschpe 8:65a4de035c3c 530
dreschpe 13:2c91cb947161 531 // calculate num of chars in a row
dreschpe 8:65a4de035c3c 532 int SPI_TFT::columns()
dreschpe 8:65a4de035c3c 533 {
dreschpe 8:65a4de035c3c 534 return width() / font[1];
dreschpe 8:65a4de035c3c 535 }
dreschpe 8:65a4de035c3c 536
dreschpe 13:2c91cb947161 537 // calculate num of rows on the screen
dreschpe 8:65a4de035c3c 538 int SPI_TFT::rows()
dreschpe 8:65a4de035c3c 539 {
dreschpe 8:65a4de035c3c 540 return height() / font[2];
dreschpe 8:65a4de035c3c 541 }
dreschpe 8:65a4de035c3c 542
dreschpe 13:2c91cb947161 543 // print a char on the screen
dreschpe 8:65a4de035c3c 544 int SPI_TFT::_putc(int value)
dreschpe 8:65a4de035c3c 545 {
dreschpe 8:65a4de035c3c 546 if (value == '\n') { // new line
dreschpe 8:65a4de035c3c 547 char_x = 0;
dreschpe 8:65a4de035c3c 548 char_y = char_y + font[2];
dreschpe 8:65a4de035c3c 549 if (char_y >= height() - font[2]) {
dreschpe 8:65a4de035c3c 550 char_y = 0;
dreschpe 8:65a4de035c3c 551 }
dreschpe 8:65a4de035c3c 552 } else {
dreschpe 8:65a4de035c3c 553 character(char_x, char_y, value);
dreschpe 8:65a4de035c3c 554 }
dreschpe 8:65a4de035c3c 555 return value;
dreschpe 8:65a4de035c3c 556 }
dreschpe 8:65a4de035c3c 557
dreschpe 13:2c91cb947161 558 // consrtuct the char out of the font
dreschpe 8:65a4de035c3c 559 void SPI_TFT::character(int x, int y, int c)
dreschpe 8:65a4de035c3c 560 {
dreschpe 9:a63fd1ad41b0 561 unsigned int hor,vert,offset,bpl,j,i,b;
dreschpe 8:65a4de035c3c 562 unsigned char* zeichen;
dreschpe 8:65a4de035c3c 563 unsigned char z,w;
dreschpe 8:65a4de035c3c 564
dreschpe 8:65a4de035c3c 565 if ((c < 31) || (c > 127)) return; // test char range
dreschpe 8:65a4de035c3c 566
dreschpe 8:65a4de035c3c 567 // read font parameter from start of array
dreschpe 8:65a4de035c3c 568 offset = font[0]; // bytes / char
dreschpe 8:65a4de035c3c 569 hor = font[1]; // get hor size of font
dreschpe 8:65a4de035c3c 570 vert = font[2]; // get vert size of font
dreschpe 8:65a4de035c3c 571 bpl = font[3]; // bytes per line
dreschpe 8:65a4de035c3c 572
dreschpe 8:65a4de035c3c 573 if (char_x + hor > width()) {
dreschpe 8:65a4de035c3c 574 char_x = 0;
dreschpe 8:65a4de035c3c 575 char_y = char_y + vert;
dreschpe 8:65a4de035c3c 576 if (char_y >= height() - font[2]) {
dreschpe 8:65a4de035c3c 577 char_y = 0;
dreschpe 8:65a4de035c3c 578 }
dreschpe 8:65a4de035c3c 579 }
dreschpe 8:65a4de035c3c 580 window(char_x, char_y,hor,vert); // char box
jhd25 24:b986bedb0a73 581 _spi.lock();
jhd25 23:469bf5f3c8ac 582 _cs = 0;
jhd25 23:469bf5f3c8ac 583 _reset=0;
dreschpe 8:65a4de035c3c 584 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 585 _reset=1;
jhd25 24:b986bedb0a73 586
dreschpe 14:ea3206e8e3bd 587 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
dreschpe 14:ea3206e8e3bd 588 w = zeichen[0]; // width of actual char
dreschpe 14:ea3206e8e3bd 589 for (j=0; j<vert; j++) { // vert line
dreschpe 14:ea3206e8e3bd 590 for (i=0; i<hor; i++) { // horz line
dreschpe 14:ea3206e8e3bd 591 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
dreschpe 14:ea3206e8e3bd 592 b = 1 << (j & 0x07);
dreschpe 14:ea3206e8e3bd 593 if (( z & b ) == 0x00) {
jhd25 23:469bf5f3c8ac 594
jhd25 25:f593b4adb905 595 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 596 _spi.directWrite(_background);
jhd25 25:f593b4adb905 597 #else
jhd25 25:f593b4adb905 598 _spi.write(_background);
jhd25 25:f593b4adb905 599 #endif // one line
jhd25 23:469bf5f3c8ac 600
dreschpe 14:ea3206e8e3bd 601 } else {
jhd25 25:f593b4adb905 602 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 603 _spi.directWrite(_foreground);
jhd25 25:f593b4adb905 604 #else
jhd25 25:f593b4adb905 605 _spi.write(_foreground);
jhd25 25:f593b4adb905 606 #endif // one line
dreschpe 14:ea3206e8e3bd 607 }
dreschpe 14:ea3206e8e3bd 608 }
dreschpe 14:ea3206e8e3bd 609 }
jhd25 23:469bf5f3c8ac 610
dreschpe 8:65a4de035c3c 611 _cs = 1;
jhd25 22:4a0f306be8ef 612 _spi.unlock();
dreschpe 8:65a4de035c3c 613 WindowMax();
dreschpe 8:65a4de035c3c 614 if ((w + 2) < hor) { // x offset to next char
dreschpe 8:65a4de035c3c 615 char_x += w + 2;
dreschpe 8:65a4de035c3c 616 } else char_x += hor;
dreschpe 8:65a4de035c3c 617 }
dreschpe 8:65a4de035c3c 618
dreschpe 8:65a4de035c3c 619
dreschpe 8:65a4de035c3c 620 void SPI_TFT::set_font(unsigned char* f)
dreschpe 8:65a4de035c3c 621 {
dreschpe 8:65a4de035c3c 622 font = f;
dreschpe 8:65a4de035c3c 623 }
dreschpe 8:65a4de035c3c 624
dreschpe 8:65a4de035c3c 625
dreschpe 8:65a4de035c3c 626 void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
dreschpe 8:65a4de035c3c 627 {
dreschpe 8:65a4de035c3c 628 unsigned int j;
dreschpe 8:65a4de035c3c 629 int padd;
dreschpe 16:2efcbb2814fa 630
dreschpe 16:2efcbb2814fa 631 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
jhd25 24:b986bedb0a73 632
dreschpe 16:2efcbb2814fa 633
dreschpe 8:65a4de035c3c 634 // the lines are padded to multiple of 4 bytes in a bitmap
dreschpe 8:65a4de035c3c 635 padd = -1;
dreschpe 8:65a4de035c3c 636 do {
dreschpe 8:65a4de035c3c 637 padd ++;
dreschpe 8:65a4de035c3c 638 } while (2*(w + padd)%4 != 0);
dreschpe 8:65a4de035c3c 639 window(x, y, w, h);
jhd25 23:469bf5f3c8ac 640 _spi.lock();
jhd25 23:469bf5f3c8ac 641 _cs = 0;
jhd25 23:469bf5f3c8ac 642 _reset=0;
dreschpe 8:65a4de035c3c 643 wr_cmd(0x22);
jhd25 23:469bf5f3c8ac 644 _reset=1;
dreschpe 8:65a4de035c3c 645
dreschpe 16:2efcbb2814fa 646 bitmap_ptr += ((h - 1)* (w + padd));
dreschpe 9:a63fd1ad41b0 647 unsigned int i;
dreschpe 9:a63fd1ad41b0 648 for (j = 0; j < h; j++) { //Lines
dreschpe 9:a63fd1ad41b0 649 for (i = 0; i < w; i++) { // copy pixel data to TFT
jhd25 23:469bf5f3c8ac 650
jhd25 25:f593b4adb905 651 #ifdef __DIRECTSPI_H_
jhd25 25:f593b4adb905 652 _spi.directWrite(*bitmap_ptr);
jhd25 25:f593b4adb905 653 #else
jhd25 25:f593b4adb905 654 _spi.write(*bitmap_ptr);
jhd25 25:f593b4adb905 655 #endif // one line
dreschpe 16:2efcbb2814fa 656 bitmap_ptr++;
jhd25 23:469bf5f3c8ac 657
dreschpe 9:a63fd1ad41b0 658 }
dreschpe 9:a63fd1ad41b0 659 bitmap_ptr -= 2*w;
dreschpe 9:a63fd1ad41b0 660 bitmap_ptr -= padd;
dreschpe 16:2efcbb2814fa 661 }
jhd25 23:469bf5f3c8ac 662
dreschpe 8:65a4de035c3c 663 _cs = 1;
jhd25 22:4a0f306be8ef 664 _spi.unlock();
dreschpe 8:65a4de035c3c 665 WindowMax();
dreschpe 8:65a4de035c3c 666 }
dreschpe 8:65a4de035c3c 667
jhd25 26:231a28b27a76 668 int SPI_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
jhd25 26:231a28b27a76 669 {
jhd25 26:231a28b27a76 670
jhd25 26:231a28b27a76 671 #define OffsetPixelWidth 18
jhd25 26:231a28b27a76 672 #define OffsetPixelHeigh 22
jhd25 26:231a28b27a76 673 #define OffsetFileSize 34
jhd25 26:231a28b27a76 674 #define OffsetPixData 10
jhd25 26:231a28b27a76 675 #define OffsetBPP 28
jhd25 26:231a28b27a76 676
jhd25 26:231a28b27a76 677 char filename[50];
jhd25 26:231a28b27a76 678 unsigned char BMP_Header[54];
jhd25 26:231a28b27a76 679 unsigned short BPP_t;
jhd25 26:231a28b27a76 680 int PixelWidth,PixelHeigh;
jhd25 26:231a28b27a76 681 unsigned int start_data;
jhd25 26:231a28b27a76 682 unsigned int i,off;
jhd25 26:231a28b27a76 683 int padd,j;
jhd25 26:231a28b27a76 684 unsigned short *line;
jhd25 26:231a28b27a76 685
jhd25 26:231a28b27a76 686 sprintf(&filename[0],"%s",Name_BMP);
jhd25 26:231a28b27a76 687
jhd25 26:231a28b27a76 688 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file
jhd25 26:231a28b27a76 689 if (!Image) {
jhd25 26:231a28b27a76 690 return(0); // error file not found !
jhd25 26:231a28b27a76 691 }
dreschpe 8:65a4de035c3c 692
jhd25 26:231a28b27a76 693 fread(&BMP_Header[0],1,54,Image); // get the BMP Header
jhd25 26:231a28b27a76 694
jhd25 26:231a28b27a76 695 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
jhd25 26:231a28b27a76 696 fclose(Image);
jhd25 26:231a28b27a76 697 return(-1); // error no BMP file
jhd25 26:231a28b27a76 698 }
jhd25 26:231a28b27a76 699
jhd25 26:231a28b27a76 700 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
jhd25 26:231a28b27a76 701 if (BPP_t != 0x0010) {
jhd25 26:231a28b27a76 702 fclose(Image);
jhd25 26:231a28b27a76 703 return(-2); // error no 16 bit BMP
jhd25 26:231a28b27a76 704 }
jhd25 26:231a28b27a76 705
jhd25 26:231a28b27a76 706 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
jhd25 26:231a28b27a76 707 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
jhd25 26:231a28b27a76 708 if (PixelHeigh > height() + y || PixelWidth > width() + x) {
jhd25 26:231a28b27a76 709 fclose(Image);
jhd25 26:231a28b27a76 710 return(-3); // to big
jhd25 26:231a28b27a76 711 }
jhd25 26:231a28b27a76 712
jhd25 26:231a28b27a76 713 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
jhd25 26:231a28b27a76 714
jhd25 26:231a28b27a76 715 line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line
jhd25 26:231a28b27a76 716 if (line == NULL) {
jhd25 26:231a28b27a76 717 return(-4); // error no memory
jhd25 26:231a28b27a76 718 }
jhd25 26:231a28b27a76 719
jhd25 26:231a28b27a76 720 // the bmp lines are padded to multiple of 4 bytes
jhd25 26:231a28b27a76 721 padd = -1;
jhd25 26:231a28b27a76 722 do {
jhd25 26:231a28b27a76 723 padd ++;
jhd25 26:231a28b27a76 724 } while ((PixelWidth * 2 + padd)%4 != 0);
jhd25 26:231a28b27a76 725 int k,l;
jhd25 26:231a28b27a76 726
jhd25 26:231a28b27a76 727 for ( j = PixelHeigh-1 ; j >=0 ; j-- ) { //Lines bottom up
jhd25 26:231a28b27a76 728
jhd25 26:231a28b27a76 729 off = (j * (PixelWidth * 2 + padd)) + (start_data); // start of line
jhd25 26:231a28b27a76 730 int erra=fseek(Image, off,SEEK_SET);
jhd25 26:231a28b27a76 731 int errb=fread(line,1,PixelWidth * 2,Image); // read a line - slow !
jhd25 26:231a28b27a76 732 window(x, (y+PixelHeigh)-j , PixelWidth ,1);
jhd25 26:231a28b27a76 733 _spi.lock();
jhd25 26:231a28b27a76 734 _cs=0;
jhd25 26:231a28b27a76 735 _reset=0;
jhd25 26:231a28b27a76 736 wr_cmd(0x22);
jhd25 26:231a28b27a76 737 _reset=1;
jhd25 26:231a28b27a76 738 _spi.format(16,3);
jhd25 26:231a28b27a76 739 // switch to 16 bit Mode 3
jhd25 26:231a28b27a76 740 for (l = PixelWidth - 1; l >= 0 ; l--) { // copy pixel data to TFT
jhd25 26:231a28b27a76 741 int out = line[(PixelWidth-1)-l];
jhd25 26:231a28b27a76 742 #ifdef __DIRECTSPI_H_
jhd25 26:231a28b27a76 743 _spi.directWrite(line[(PixelWidth-1)-l]);
jhd25 26:231a28b27a76 744 #else
jhd25 26:231a28b27a76 745 _spi.write(ine[(PixelWidth-1)-l]);
jhd25 26:231a28b27a76 746 #endif // one line
jhd25 26:231a28b27a76 747 }
jhd25 26:231a28b27a76 748
jhd25 26:231a28b27a76 749 _cs=1;
jhd25 26:231a28b27a76 750 _spi.unlock();
jhd25 26:231a28b27a76 751
jhd25 26:231a28b27a76 752 }
jhd25 26:231a28b27a76 753
jhd25 26:231a28b27a76 754 free (line);
jhd25 26:231a28b27a76 755 fclose(Image);
jhd25 26:231a28b27a76 756 WindowMax();
jhd25 26:231a28b27a76 757 return(1);
jhd25 26:231a28b27a76 758 }
jhd25 26:231a28b27a76 759