From 08ef3981d5db1768296b30277fc1033a2c0a1b63 Mon Sep 17 00:00:00 2001 From: sumotoy Date: Sun, 25 May 2014 00:01:05 +0200 Subject: [PATCH] Bug Fix and support for color space --- TFT_ILI9163C.cpp | 277 ++++++++++++++++++++++++----------------------- TFT_ILI9163C.h | 11 +- 2 files changed, 150 insertions(+), 138 deletions(-) diff --git a/TFT_ILI9163C.cpp b/TFT_ILI9163C.cpp index df63726..3b95a93 100644 --- a/TFT_ILI9163C.cpp +++ b/TFT_ILI9163C.cpp @@ -250,6 +250,29 @@ void TFT_ILI9163C::begin(void) { digitalWrite(_rst, HIGH); delay(500); } + +/* +7) MY: 1(bottom to top), 0(top to bottom) Row Address Order +6) MX: 1(R to L), 0(L to R) Column Address Order +5) MV: 1(Exchanged), 0(normal) Row/Column exchange +4) ML: 1(bottom to top), 0(top to bottom) Vertical Refresh Order +3) RGB: 1(BGR), 0(RGB) Color Space +2) MH: 1(R to L), 0(L to R) Horizontal Refresh Order +1) +0) + + MY, MX, MV, ML,RGB, MH, D1, D0 + 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 //normal + 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 //Y-Mirror + 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 //X-Mirror + 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 //X-Y-Mirror + 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 //X-Y Exchange + 1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 //X-Y Exchange, Y-Mirror + 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 //XY exchange + 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 +*/ + _Mactrl_Data = 0b00000000; + _colorspaceData = __COLORSPC;//start with default data; chipInit(); } @@ -259,115 +282,113 @@ void TFT_ILI9163C::chipInit() { writecommand(CMD_SWRESET);//software reset delay(500); writecommand(CMD_SLPOUT);//exit sleep - delay(50); + delay(5); writecommand(CMD_PIXFMT);//Set Color Format 16bit writedata(0x05); - delay(50); + delay(5); writecommand(CMD_GAMMASET);//default gamma curve 3 writedata(0x04);//0x04 - delay(10); + delay(1); writecommand(CMD_GAMRSEL);//Enable Gamma adj writedata(0x01); - delay(10); + delay(1); writecommand(CMD_NORML); - writecommand(0xB6); - writedata(0b11111111);//0x04 - writedata(0b00000110);//0x04 + writecommand(CMD_DFUNCTR); + writedata(0b11111111);// + writedata(0b00000110);// writecommand(CMD_PGAMMAC);//Positive Gamma Correction Setting #if defined(__GAMMASET1) - writedata(0x36);//p1 - writedata(0x29);//p2 - writedata(0x12);//p3 - writedata(0x22);//p4 - writedata(0x1C);//p5 - writedata(0x15);//p6 - writedata(0x42);//p7 - writedata(0xB7);//p8 - writedata(0x2F);//p9 - writedata(0x13);//p10 - writedata(0x12);//p11 - writedata(0x0A);//p12 - writedata(0x11);//p13 - writedata(0x0B);//p14 - writedata(0x06);//p15 + writedata(0x36);//p1 + writedata(0x29);//p2 + writedata(0x12);//p3 + writedata(0x22);//p4 + writedata(0x1C);//p5 + writedata(0x15);//p6 + writedata(0x42);//p7 + writedata(0xB7);//p8 + writedata(0x2F);//p9 + writedata(0x13);//p10 + writedata(0x12);//p11 + writedata(0x0A);//p12 + writedata(0x11);//p13 + writedata(0x0B);//p14 + writedata(0x06);//p15 #else - writedata(0x3F);//p1 - writedata(0x25);//p2 - writedata(0x1C);//p3 - writedata(0x1E);//p4 - writedata(0x20);//p5 - writedata(0x12);//p6 - writedata(0x2A);//p7 - writedata(0x90);//p8 - writedata(0x24);//p9 - writedata(0x11);//p10 - writedata(0x00);//p11 - writedata(0x00);//p12 - writedata(0x00);//p13 - writedata(0x00);//p14 - writedata(0x00);//p15 + writedata(0x3F);//p1 + writedata(0x25);//p2 + writedata(0x1C);//p3 + writedata(0x1E);//p4 + writedata(0x20);//p5 + writedata(0x12);//p6 + writedata(0x2A);//p7 + writedata(0x90);//p8 + writedata(0x24);//p9 + writedata(0x11);//p10 + writedata(0x00);//p11 + writedata(0x00);//p12 + writedata(0x00);//p13 + writedata(0x00);//p14 + writedata(0x00);//p15 #endif - writecommand(CMD_NGAMMAC);//Negative Gamma Correction Setting #if defined(__GAMMASET1) - writedata(0x09);//p1 - writedata(0x16);//p2 - writedata(0x2D);//p3 - writedata(0x0D);//p4 - writedata(0x13);//p5 - writedata(0x15);//p6 - writedata(0x40);//p7 - writedata(0x48);//p8 - writedata(0x53);//p9 - writedata(0x0C);//p10 - writedata(0x1D);//p11 - writedata(0x25);//p12 - writedata(0x2E);//p13 - writedata(0x34);//p14 - writedata(0x39);//p15 + writedata(0x09);//p1 + writedata(0x16);//p2 + writedata(0x2D);//p3 + writedata(0x0D);//p4 + writedata(0x13);//p5 + writedata(0x15);//p6 + writedata(0x40);//p7 + writedata(0x48);//p8 + writedata(0x53);//p9 + writedata(0x0C);//p10 + writedata(0x1D);//p11 + writedata(0x25);//p12 + writedata(0x2E);//p13 + writedata(0x34);//p14 + writedata(0x39);//p15 #else - writedata(0x20);//p1 - writedata(0x20);//p2 - writedata(0x20);//p3 - writedata(0x20);//p4 - writedata(0x05);//p5 - writedata(0x15);//p6 - writedata(0x00);//p7 - writedata(0xA7);//p8 - writedata(0x3D);//p9 - writedata(0x18);//p10 - writedata(0x25);//p11 - writedata(0x2A);//p12 - writedata(0x2B);//p13 - writedata(0x2B);//p14 - writedata(0x3A);//p15 + writedata(0x20);//p1 + writedata(0x20);//p2 + writedata(0x20);//p3 + writedata(0x20);//p4 + writedata(0x05);//p5 + writedata(0x15);//p6 + writedata(0x00);//p7 + writedata(0xA7);//p8 + writedata(0x3D);//p9 + writedata(0x18);//p10 + writedata(0x25);//p11 + writedata(0x2A);//p12 + writedata(0x2B);//p13 + writedata(0x2B);//p14 + writedata(0x3A);//p15 #endif - writecommand(CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors) writedata(0x08);//0x0C//0x08 writedata(0x02);//0x14//0x08 - delay(10); + delay(1); writecommand(CMD_DINVCTR);//display inversion writedata(0x07); - delay(10); + delay(1); writecommand(CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD writedata(0x0A);//4.30 - 0x0A writedata(0x02);//0x05 - delay(10); + delay(1); writecommand(CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL writedata(0x02); - delay(10); + delay(1); writecommand(CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML writedata(0x50);//0x50 writedata(99);//0x5b - delay(10); + delay(1); writecommand(CMD_VCOMOFFS); writedata(0);//0x40 - delay(10); + delay(1); writecommand(CMD_CLMADRS);//Set Column Address writedata(0x00); @@ -380,24 +401,33 @@ void TFT_ILI9163C::chipInit() { writedata(0X00); writedata(0X00); writedata(_GRAMHEIGH); - - //writecommand(CMD_MADCTL);//Set Scanning Direction && Colorspace - //writedata(DTA_MADCTL_MX | __COLORSPC); //08 - writecommand(CMD_MADCTL); - writedata(0b00001000); - delay(10); - - writecommand(CMD_DISPON);//display ON - delay(10); - writecommand(CMD_RAMWR);//Memory Write - writedata16(0X0000); - writedata16(0X0000); - writedata16(0X0000); - writedata16(0X0000); - delay(10); - fillScreen(BLACK); + //colorSpace(__COLORSPC);//set default colorspace and other access data + colorSpace(_colorspaceData); setRotation(0); + writecommand(CMD_DISPON);//display ON + delay(1); + writecommand(CMD_RAMWR);//Memory Write + + delay(1); + fillScreen(BLACK); + +} + +/* +Colorspace selection: +0: RGB +1: GBR +*/ +void TFT_ILI9163C::colorSpace(uint8_t cspace) { + if (cspace < 1){ + bitClear(_Mactrl_Data,3); + } else { + bitSet(_Mactrl_Data,3); + } + //writecommand(CMD_MADCTL); + //writedata(_Mactrl_Data); + //delay(1); } @@ -409,7 +439,7 @@ void TFT_ILI9163C::clearScreen(uint16_t color) { } void TFT_ILI9163C::homeAddress() { - setAddrWindow(0x00,0x00,_GRAMWIDTH,_GRAMHEIGH); + setAddrWindow(0x00,0x00,_GRAMWIDTH-1,_GRAMHEIGH-1); } void TFT_ILI9163C::setCursor(int16_t x, int16_t y) { @@ -435,8 +465,8 @@ void TFT_ILI9163C::drawPixel(int16_t x, int16_t y, uint16_t color) { void TFT_ILI9163C::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) { // Rudimentary clipping if (boundaryCheck(x,y)) return; - if ((y+h-1) >= _height) h = _height-y; - setAddrWindow(x, y, x, y+h-1); + if (((y + h) - 1) >= _height) h = _height-y; + setAddrWindow(x,y,x,(y+h)-1); while (h--) { writedata16(color); } @@ -450,8 +480,8 @@ bool TFT_ILI9163C::boundaryCheck(int16_t x,int16_t y){ void TFT_ILI9163C::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) { // Rudimentary clipping if (boundaryCheck(x,y)) return; - if ((x+w-1) >= _width) w = _width-x; - setAddrWindow(x, y, x+w-1, y); + if (((x+w) - 1) >= _width) w = _width-x; + setAddrWindow(x,y,(x+w)-1,y); while (w--) { writedata16(color); } @@ -459,18 +489,17 @@ void TFT_ILI9163C::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color void TFT_ILI9163C::fillScreen(uint16_t color) { clearScreen(color); - //homeAddress(); } // fill a rectangle void TFT_ILI9163C::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) { if (boundaryCheck(x,y)) return; - if ((x + w - 1) >= _width) w = _width - x; - if ((y + h - 1) >= _height) h = _height - y; - setAddrWindow(x, y, x+w-1, y+h-1); + if (((x + w) - 1) >= _width) w = _width - x; + if (((y + h) - 1) >= _height) h = _height - y; + setAddrWindow(x,y,(x+w)-1,(y+h)-1); - for (y=h; y>0; y--) { - for (x=w; x>0; x--) { + for (y = h;y > 0;y--) { + for (x = w;x > 0;x--) { writedata16(color); } } @@ -490,8 +519,8 @@ void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t writedata16(x0); writedata16(x1); } else if (rotation == 1){ - writedata16(x0+32); - writedata16(x1+32); + writedata16(x0 + __OFFSET); + writedata16(x1 + __OFFSET); } else if (rotation == 2){ writedata16(x0); writedata16(x1); @@ -502,8 +531,8 @@ void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t writecommand(CMD_PGEADRS); // Page if (rotation == 0){ - writedata16(y0+__OFFSET); - writedata16(y1+__OFFSET); + writedata16(y0 + __OFFSET); + writedata16(y1 + __OFFSET); } else if (rotation == 1){ writedata16(y0); writedata16(y1); @@ -514,57 +543,37 @@ void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t writedata16(y0); writedata16(y1); } - writecommand(CMD_RAMWR); //Into RAM } -//MY: 1(bottom to top), 0(top to bottom) Row Address Order -//MX: 1(R to L), 0(L to R) Column Address Order -//MV: 1(Exchanged), 0(normal) Row/Column exchange -//ML: 1(bottom to top), 0(top to bottom) Vertical Refresh Order -//RGB: 1(BGR), 0(RGB) Color Space -//MH: 1(R to L), 0(L to R) Horizontal Refresh Order -// MY, MX, MV, ML,RGB, MH, D1, D0 -/* - ( 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0),//normal - ( 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0),//Y-Mirror - ( 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0),//X-Mirror - ( 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0),//X-Y-Mirror - ( 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0),//X-Y Exchange - ( 1 | 0 | 1 | 0 | 1 | 0 | 0 | 0),//X-Y Exchange, Y-Mirror - ( 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0),//XY exchange - ( 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0) - }; -*/ + void TFT_ILI9163C::setRotation(uint8_t m) { - writecommand(CMD_MADCTL); rotation = m % 4; // can't be higher than 3 switch (rotation) { case 0: - writecommand(CMD_MADCTL); - writedata(0b00001000); + _Mactrl_Data = 0b00001000; _width = _TFTWIDTH; _height = _TFTHEIGHT;//-__OFFSET; break; case 1: - writecommand(CMD_MADCTL); - writedata(0b01101000); + _Mactrl_Data = 0b01101000; _width = _TFTHEIGHT;//-__OFFSET; _height = _TFTWIDTH; break; case 2: - writecommand(CMD_MADCTL); - writedata(0b11001000); + _Mactrl_Data = 0b11001000; _width = _TFTWIDTH; _height = _TFTHEIGHT;//-__OFFSET; break; case 3: - writecommand(CMD_MADCTL); - writedata(0b10101000); + _Mactrl_Data = 0b10101000; _width = _TFTWIDTH; _height = _TFTHEIGHT;//-__OFFSET; break; } + colorSpace(_colorspaceData); + writecommand(CMD_MADCTL); + writedata(_Mactrl_Data); } diff --git a/TFT_ILI9163C.h b/TFT_ILI9163C.h index b322782..c7bb6aa 100644 --- a/TFT_ILI9163C.h +++ b/TFT_ILI9163C.h @@ -66,6 +66,7 @@ 0.1b1: Beta! Addressing solved, now rotation works and boundaries ok. 0.2b1: Cleaned up. 0.2b3: Added 2.2" Red PCB parameters + 0.2b4: Bug fixes, added colorSpace (for future send image) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ BugList of the current version: @@ -128,7 +129,7 @@ you can copy those parameters and create setup for different displays. #define _GRAMWIDTH 128 #define _GRAMHEIGH 160 #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH//*see note 1 - #define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB + #define __COLORSPC 1// 1:GBR - 0:RGB #define __GAMMASET1 //uncomment for another gamma #define __OFFSET 32//*see note 2 //Tested! @@ -143,7 +144,7 @@ Not tested! #define _GRAMWIDTH 240 #define _GRAMHEIGH 320 #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH - #define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB + #define __COLORSPC 1// 1:GBR - 0:RGB #define __GAMMASET1 //uncomment for another gamma #define __OFFSET 0 #else @@ -152,7 +153,7 @@ Not tested! #define _GRAMWIDTH 128 #define _GRAMHEIGH 160 #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH - #define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB + #define __COLORSPC 1// 1:GBR - 0:RGB #define __GAMMASET1 #define __OFFSET 0 #endif @@ -267,8 +268,10 @@ class TFT_ILI9163C : public Adafruit_GFX { void setBitrate(uint32_t n); private: - + uint8_t _Mactrl_Data;//container for the memory access control data + uint8_t _colorspaceData; uint8_t tabcolor; + void colorSpace(uint8_t cspace); void writecommand(uint8_t c); void writedata(uint8_t d); void writedata16(uint16_t d);