Bug Fix and support for color space

This commit is contained in:
sumotoy 2014-05-25 00:01:05 +02:00
parent 31c662cf59
commit 08ef3981d5
2 changed files with 150 additions and 138 deletions

View file

@ -250,6 +250,29 @@ void TFT_ILI9163C::begin(void) {
digitalWrite(_rst, HIGH); digitalWrite(_rst, HIGH);
delay(500); 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(); chipInit();
} }
@ -259,115 +282,113 @@ void TFT_ILI9163C::chipInit() {
writecommand(CMD_SWRESET);//software reset writecommand(CMD_SWRESET);//software reset
delay(500); delay(500);
writecommand(CMD_SLPOUT);//exit sleep writecommand(CMD_SLPOUT);//exit sleep
delay(50); delay(5);
writecommand(CMD_PIXFMT);//Set Color Format 16bit writecommand(CMD_PIXFMT);//Set Color Format 16bit
writedata(0x05); writedata(0x05);
delay(50); delay(5);
writecommand(CMD_GAMMASET);//default gamma curve 3 writecommand(CMD_GAMMASET);//default gamma curve 3
writedata(0x04);//0x04 writedata(0x04);//0x04
delay(10); delay(1);
writecommand(CMD_GAMRSEL);//Enable Gamma adj writecommand(CMD_GAMRSEL);//Enable Gamma adj
writedata(0x01); writedata(0x01);
delay(10); delay(1);
writecommand(CMD_NORML); writecommand(CMD_NORML);
writecommand(0xB6); writecommand(CMD_DFUNCTR);
writedata(0b11111111);//0x04 writedata(0b11111111);//
writedata(0b00000110);//0x04 writedata(0b00000110);//
writecommand(CMD_PGAMMAC);//Positive Gamma Correction Setting writecommand(CMD_PGAMMAC);//Positive Gamma Correction Setting
#if defined(__GAMMASET1) #if defined(__GAMMASET1)
writedata(0x36);//p1 writedata(0x36);//p1
writedata(0x29);//p2 writedata(0x29);//p2
writedata(0x12);//p3 writedata(0x12);//p3
writedata(0x22);//p4 writedata(0x22);//p4
writedata(0x1C);//p5 writedata(0x1C);//p5
writedata(0x15);//p6 writedata(0x15);//p6
writedata(0x42);//p7 writedata(0x42);//p7
writedata(0xB7);//p8 writedata(0xB7);//p8
writedata(0x2F);//p9 writedata(0x2F);//p9
writedata(0x13);//p10 writedata(0x13);//p10
writedata(0x12);//p11 writedata(0x12);//p11
writedata(0x0A);//p12 writedata(0x0A);//p12
writedata(0x11);//p13 writedata(0x11);//p13
writedata(0x0B);//p14 writedata(0x0B);//p14
writedata(0x06);//p15 writedata(0x06);//p15
#else #else
writedata(0x3F);//p1 writedata(0x3F);//p1
writedata(0x25);//p2 writedata(0x25);//p2
writedata(0x1C);//p3 writedata(0x1C);//p3
writedata(0x1E);//p4 writedata(0x1E);//p4
writedata(0x20);//p5 writedata(0x20);//p5
writedata(0x12);//p6 writedata(0x12);//p6
writedata(0x2A);//p7 writedata(0x2A);//p7
writedata(0x90);//p8 writedata(0x90);//p8
writedata(0x24);//p9 writedata(0x24);//p9
writedata(0x11);//p10 writedata(0x11);//p10
writedata(0x00);//p11 writedata(0x00);//p11
writedata(0x00);//p12 writedata(0x00);//p12
writedata(0x00);//p13 writedata(0x00);//p13
writedata(0x00);//p14 writedata(0x00);//p14
writedata(0x00);//p15 writedata(0x00);//p15
#endif #endif
writecommand(CMD_NGAMMAC);//Negative Gamma Correction Setting writecommand(CMD_NGAMMAC);//Negative Gamma Correction Setting
#if defined(__GAMMASET1) #if defined(__GAMMASET1)
writedata(0x09);//p1 writedata(0x09);//p1
writedata(0x16);//p2 writedata(0x16);//p2
writedata(0x2D);//p3 writedata(0x2D);//p3
writedata(0x0D);//p4 writedata(0x0D);//p4
writedata(0x13);//p5 writedata(0x13);//p5
writedata(0x15);//p6 writedata(0x15);//p6
writedata(0x40);//p7 writedata(0x40);//p7
writedata(0x48);//p8 writedata(0x48);//p8
writedata(0x53);//p9 writedata(0x53);//p9
writedata(0x0C);//p10 writedata(0x0C);//p10
writedata(0x1D);//p11 writedata(0x1D);//p11
writedata(0x25);//p12 writedata(0x25);//p12
writedata(0x2E);//p13 writedata(0x2E);//p13
writedata(0x34);//p14 writedata(0x34);//p14
writedata(0x39);//p15 writedata(0x39);//p15
#else #else
writedata(0x20);//p1 writedata(0x20);//p1
writedata(0x20);//p2 writedata(0x20);//p2
writedata(0x20);//p3 writedata(0x20);//p3
writedata(0x20);//p4 writedata(0x20);//p4
writedata(0x05);//p5 writedata(0x05);//p5
writedata(0x15);//p6 writedata(0x15);//p6
writedata(0x00);//p7 writedata(0x00);//p7
writedata(0xA7);//p8 writedata(0xA7);//p8
writedata(0x3D);//p9 writedata(0x3D);//p9
writedata(0x18);//p10 writedata(0x18);//p10
writedata(0x25);//p11 writedata(0x25);//p11
writedata(0x2A);//p12 writedata(0x2A);//p12
writedata(0x2B);//p13 writedata(0x2B);//p13
writedata(0x2B);//p14 writedata(0x2B);//p14
writedata(0x3A);//p15 writedata(0x3A);//p15
#endif #endif
writecommand(CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors) writecommand(CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors)
writedata(0x08);//0x0C//0x08 writedata(0x08);//0x0C//0x08
writedata(0x02);//0x14//0x08 writedata(0x02);//0x14//0x08
delay(10); delay(1);
writecommand(CMD_DINVCTR);//display inversion writecommand(CMD_DINVCTR);//display inversion
writedata(0x07); writedata(0x07);
delay(10); delay(1);
writecommand(CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD writecommand(CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD
writedata(0x0A);//4.30 - 0x0A writedata(0x0A);//4.30 - 0x0A
writedata(0x02);//0x05 writedata(0x02);//0x05
delay(10); delay(1);
writecommand(CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL writecommand(CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL
writedata(0x02); writedata(0x02);
delay(10); delay(1);
writecommand(CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML writecommand(CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML
writedata(0x50);//0x50 writedata(0x50);//0x50
writedata(99);//0x5b writedata(99);//0x5b
delay(10); delay(1);
writecommand(CMD_VCOMOFFS); writecommand(CMD_VCOMOFFS);
writedata(0);//0x40 writedata(0);//0x40
delay(10); delay(1);
writecommand(CMD_CLMADRS);//Set Column Address writecommand(CMD_CLMADRS);//Set Column Address
writedata(0x00); writedata(0x00);
@ -380,24 +401,33 @@ void TFT_ILI9163C::chipInit() {
writedata(0X00); writedata(0X00);
writedata(0X00); writedata(0X00);
writedata(_GRAMHEIGH); writedata(_GRAMHEIGH);
//writecommand(CMD_MADCTL);//Set Scanning Direction && Colorspace
//writedata(DTA_MADCTL_MX | __COLORSPC); //08
writecommand(CMD_MADCTL);
writedata(0b00001000);
delay(10);
//colorSpace(__COLORSPC);//set default colorspace and other access data
writecommand(CMD_DISPON);//display ON colorSpace(_colorspaceData);
delay(10);
writecommand(CMD_RAMWR);//Memory Write
writedata16(0X0000);
writedata16(0X0000);
writedata16(0X0000);
writedata16(0X0000);
delay(10);
fillScreen(BLACK);
setRotation(0); 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() { 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) { 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) { void TFT_ILI9163C::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
// Rudimentary clipping // Rudimentary clipping
if (boundaryCheck(x,y)) return; if (boundaryCheck(x,y)) return;
if ((y+h-1) >= _height) h = _height-y; if (((y + h) - 1) >= _height) h = _height-y;
setAddrWindow(x, y, x, y+h-1); setAddrWindow(x,y,x,(y+h)-1);
while (h--) { while (h--) {
writedata16(color); 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) { void TFT_ILI9163C::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
// Rudimentary clipping // Rudimentary clipping
if (boundaryCheck(x,y)) return; if (boundaryCheck(x,y)) return;
if ((x+w-1) >= _width) w = _width-x; if (((x+w) - 1) >= _width) w = _width-x;
setAddrWindow(x, y, x+w-1, y); setAddrWindow(x,y,(x+w)-1,y);
while (w--) { while (w--) {
writedata16(color); 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) { void TFT_ILI9163C::fillScreen(uint16_t color) {
clearScreen(color); clearScreen(color);
//homeAddress();
} }
// fill a rectangle // fill a rectangle
void TFT_ILI9163C::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) { 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 (boundaryCheck(x,y)) return;
if ((x + w - 1) >= _width) w = _width - x; if (((x + w) - 1) >= _width) w = _width - x;
if ((y + h - 1) >= _height) h = _height - y; if (((y + h) - 1) >= _height) h = _height - y;
setAddrWindow(x, y, x+w-1, y+h-1); setAddrWindow(x,y,(x+w)-1,(y+h)-1);
for (y=h; y>0; y--) { for (y = h;y > 0;y--) {
for (x=w; x>0; x--) { for (x = w;x > 0;x--) {
writedata16(color); writedata16(color);
} }
} }
@ -490,8 +519,8 @@ void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t
writedata16(x0); writedata16(x0);
writedata16(x1); writedata16(x1);
} else if (rotation == 1){ } else if (rotation == 1){
writedata16(x0+32); writedata16(x0 + __OFFSET);
writedata16(x1+32); writedata16(x1 + __OFFSET);
} else if (rotation == 2){ } else if (rotation == 2){
writedata16(x0); writedata16(x0);
writedata16(x1); 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 writecommand(CMD_PGEADRS); // Page
if (rotation == 0){ if (rotation == 0){
writedata16(y0+__OFFSET); writedata16(y0 + __OFFSET);
writedata16(y1+__OFFSET); writedata16(y1 + __OFFSET);
} else if (rotation == 1){ } else if (rotation == 1){
writedata16(y0); writedata16(y0);
writedata16(y1); writedata16(y1);
@ -514,57 +543,37 @@ void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t
writedata16(y0); writedata16(y0);
writedata16(y1); writedata16(y1);
} }
writecommand(CMD_RAMWR); //Into RAM 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) { void TFT_ILI9163C::setRotation(uint8_t m) {
writecommand(CMD_MADCTL);
rotation = m % 4; // can't be higher than 3 rotation = m % 4; // can't be higher than 3
switch (rotation) { switch (rotation) {
case 0: case 0:
writecommand(CMD_MADCTL); _Mactrl_Data = 0b00001000;
writedata(0b00001000);
_width = _TFTWIDTH; _width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET; _height = _TFTHEIGHT;//-__OFFSET;
break; break;
case 1: case 1:
writecommand(CMD_MADCTL); _Mactrl_Data = 0b01101000;
writedata(0b01101000);
_width = _TFTHEIGHT;//-__OFFSET; _width = _TFTHEIGHT;//-__OFFSET;
_height = _TFTWIDTH; _height = _TFTWIDTH;
break; break;
case 2: case 2:
writecommand(CMD_MADCTL); _Mactrl_Data = 0b11001000;
writedata(0b11001000);
_width = _TFTWIDTH; _width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET; _height = _TFTHEIGHT;//-__OFFSET;
break; break;
case 3: case 3:
writecommand(CMD_MADCTL); _Mactrl_Data = 0b10101000;
writedata(0b10101000);
_width = _TFTWIDTH; _width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET; _height = _TFTHEIGHT;//-__OFFSET;
break; break;
} }
colorSpace(_colorspaceData);
writecommand(CMD_MADCTL);
writedata(_Mactrl_Data);
} }

View file

@ -66,6 +66,7 @@
0.1b1: Beta! Addressing solved, now rotation works and boundaries ok. 0.1b1: Beta! Addressing solved, now rotation works and boundaries ok.
0.2b1: Cleaned up. 0.2b1: Cleaned up.
0.2b3: Added 2.2" Red PCB parameters 0.2b3: Added 2.2" Red PCB parameters
0.2b4: Bug fixes, added colorSpace (for future send image)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
BugList of the current version: BugList of the current version:
@ -128,7 +129,7 @@ you can copy those parameters and create setup for different displays.
#define _GRAMWIDTH 128 #define _GRAMWIDTH 128
#define _GRAMHEIGH 160 #define _GRAMHEIGH 160
#define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH//*see note 1 #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 __GAMMASET1 //uncomment for another gamma
#define __OFFSET 32//*see note 2 #define __OFFSET 32//*see note 2
//Tested! //Tested!
@ -143,7 +144,7 @@ Not tested!
#define _GRAMWIDTH 240 #define _GRAMWIDTH 240
#define _GRAMHEIGH 320 #define _GRAMHEIGH 320
#define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH #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 __GAMMASET1 //uncomment for another gamma
#define __OFFSET 0 #define __OFFSET 0
#else #else
@ -152,7 +153,7 @@ Not tested!
#define _GRAMWIDTH 128 #define _GRAMWIDTH 128
#define _GRAMHEIGH 160 #define _GRAMHEIGH 160
#define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH
#define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB #define __COLORSPC 1// 1:GBR - 0:RGB
#define __GAMMASET1 #define __GAMMASET1
#define __OFFSET 0 #define __OFFSET 0
#endif #endif
@ -267,8 +268,10 @@ class TFT_ILI9163C : public Adafruit_GFX {
void setBitrate(uint32_t n); void setBitrate(uint32_t n);
private: private:
uint8_t _Mactrl_Data;//container for the memory access control data
uint8_t _colorspaceData;
uint8_t tabcolor; uint8_t tabcolor;
void colorSpace(uint8_t cspace);
void writecommand(uint8_t c); void writecommand(uint8_t c);
void writedata(uint8_t d); void writedata(uint8_t d);
void writedata16(uint16_t d); void writedata16(uint16_t d);