Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Adafruit_ST7735 by
Revision 7:20134e1718b2, committed 2015-12-16
- Comitter:
- Cognoscan
- Date:
- Wed Dec 16 18:44:10 2015 +0000
- Parent:
- 6:7d45da201480
- Commit message:
- Updated to work with ST7735S (teal-colored tab)
Changed in this revision
Adafruit_ST7735.cpp | Show annotated file Show diff for this revision Revisions of this file |
Adafruit_ST7735.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7d45da201480 -r 20134e1718b2 Adafruit_ST7735.cpp --- a/Adafruit_ST7735.cpp Thu Jul 30 22:55:47 2015 +0000 +++ b/Adafruit_ST7735.cpp Wed Dec 16 18:44:10 2015 +0000 @@ -151,6 +151,62 @@ 0x05 }, // 16-bit color +Scmd[] = { // Initialization commands for 7735S screens + 16, // 17 commands in list: + ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay + 120, // 255 = 500 ms delay + ST7735_FRMCTR1, 3, // 4: Frame rate control 1, 3 args + 0x05, // fastest refresh + 0x3C, // 6 lines front porch + 0x3C, // 3 lines back porch + ST7735_FRMCTR2, 3, + 0x05, + 0x3C, + 0x3C, + ST7735_FRMCTR3, 6, + 0x05, + 0x3C, + 0x3C, + 0x05, + 0x3C, + 0x3C, + ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg: + 0x03, // Line inversion + ST7735_PWCTR1 , 3, // 8: Power control, 2 args + delay: + 0x28, // GVDD = 4.7V + 0x08, // 1.0uA + 0x04, + ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay: + 0xC0, // VGH = 14.7V, VGL = -7.35V + ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay: + 0x0D, // Opamp current small + 0x00, // Boost frequency + ST7735_PWCTR4, 2, + 0x8D, + 0x2A, + ST7735_PWCTR5, 2, + 0x8D, + 0xEE, + ST7735_VMCTR1 , 1, // 11: Power control, 2 args + delay: + 0x1A, // VCOMH = 4V + ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg: + 0xC0, // Row addr/col addr, bottom to top refresh + ST7735_GMCTRP1, 16, // 13: Magical unicorn dust, 16 args, no delay: + 0x04, 0x22, 0x07, 0x0A, // (seriously though, not sure what + 0x2E, 0x30, 0x25, 0x2A, // these config values represent) + 0x28, 0x26, 0x2E, 0x3A, + 0x00, 0x01, 0x03, 0x13, + ST7735_GMCTRN1, 16, // 14: Sparkles and rainbows, 16 args + delay: + 0x04, 0x16, 0x06, 0x0D, // (ditto) + 0x2D, 0x26, 0x23, 0x27, + 0x27, 0x25, 0x2D, 0x3B, + 0x00, 0x01, 0x04, 0x13, + ST7735_COLMOD , 1, // 3: Set color mode, 1 arg + delay: + 0x05, // 16-bit color + ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay + 255 +}, // 255 = 500 ms delay + Rcmd2green[] = { // Init for 7735R, part 2 (green tab only) 2, // 2 commands in list: ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay: @@ -229,14 +285,17 @@ lcdPort.format(8,0); lcdPort.frequency(4000000); // Lets try 4MHz - // toggle RST low to reset; CS low so it'll listen to us - _cs = 0; + // toggle RST low to reset + _cs = 1; _rst = 1; - wait_ms(500); + wait_ms(5); _rst = 0; - wait_ms(500); + wait_ms(5); _rst = 1; - wait_ms(500); + wait_ms(5); + _cs = 1; + wait_ms(5); + _cs = 0; if(cmdList) commandList(cmdList); } @@ -248,6 +307,10 @@ commonInit(Bcmd); } +void Adafruit_ST7735::initS(void) +{ + commonInit(Scmd); +} // Initialization for ST7735R screens (green or red tabs) void Adafruit_ST7735::initR(uint8_t options) @@ -284,7 +347,6 @@ writecommand(ST7735_RAMWR); // write to RAM } - void Adafruit_ST7735::fillScreen(uint16_t color) { @@ -316,7 +378,6 @@ _cs = 1; } - void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) { @@ -333,7 +394,6 @@ _cs = 1; } - void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) { @@ -443,140 +503,46 @@ } } - -void Adafruit_ST7735::invertDisplay(boolean i) +void Adafruit_ST7735::invertDisplay(bool i) { writecommand(i ? ST7735_INVON : ST7735_INVOFF); } - -int Adafruit_ST7735::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) -{ - - - +// BMP File Constants #define OffsetPixelWidth 18 #define OffsetPixelHeigh 22 #define OffsetFileSize 34 #define OffsetPixData 10 #define OffsetBPP 28 - pc.printf("TEST START\r\n"); - char filename[50]; - unsigned char BMP_Header[54]; - unsigned short BPP_t; - unsigned int PixelWidth,PixelHeigh,start_data; - unsigned int i,off; - int padd,j; - unsigned short *line; - - pc.printf("TEST START1\r\n"); - // get the filename - i=0; - while (*Name_BMP!='\0') { - filename[i++]=*Name_BMP++; - } - pc.printf("TEST START2\r\n"); - filename[i] = 0; - - pc.printf("TEST START3\r\n"); - FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file - pc.printf("TEST FILEOPEN\r\n"); - if (!Image) { - pc.printf("TEST error file not found"); - return(0); // error file not found ! - } - - fread(&BMP_Header[0],1,54,Image); // get the BMP Header - - if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte - pc.printf("TEST error no BMP file"); - fclose(Image); - return(-1); // error no BMP file - } - - BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); - if (BPP_t != 0x0010) { - pc.printf("TEST error no 16 bit BMP\r\n"); - fclose(Image); - return(-2); // error no 16 bit BMP - } - - PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); - PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); - if (PixelHeigh > _height + y || PixelWidth > _width + x) { - pc.printf("TEST to big\r\n"); - fclose(Image); - return(-3); // to big - } - - start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); - - line = (unsigned short *) malloc (2 * PixelHeigh); // we need a buffer for a line - if (line == NULL) { - pc.printf("TEST error no memory\r\n"); - return(-4); // error no memory - } - pc.printf("TEST 506"); - // the bmp lines are padded to multiple of 4 bytes - padd = -1; - do {pc.printf("TEST padd : %d\r\n",padd); - padd ++; - } while ((PixelHeigh * 2 + padd)%4 != 0); - - setAddrWindow(x, y,PixelWidth ,PixelHeigh); - writecommand(0x2C); // send pixel - - pc.printf("TEST 518"); - for (j = PixelWidth - 1; j >= 0; j--) { //Lines bottom up - off = j * (PixelHeigh * 2 + padd) + start_data; // start of line - fseek(Image, off ,SEEK_SET); - fread(line,1,PixelHeigh * 2,Image); // read a line - slow - for (i = 0; i < PixelHeigh; i++) { // copy pixel data to TFT - _rs = 1; - _cs = 0; - lcdPort.write(line[i]); - } - } - _cs = 1; - lcdPort.format(8,3); - free (line); - fclose(Image); - setAddrWindow(0,0,_width,_height); - pc.printf("TEST END"); - return(1); -} - #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue #define TFT_DEBUG int Adafruit_ST7735::DrawBitmapFile(const char *Name_BMP) { + return DrawBitmapFile(0,0,Name_BMP); +} + +int Adafruit_ST7735::DrawBitmapFile(unsigned int x, unsigned int y, const char *Name_BMP) +{ char img[3*240]; uint32_t imgsize = 0; uint32_t offset = 0; uint32_t imgw = 0; uint32_t imgh = 0; - char colbits = 0; - char compress = 0; + char colbits; uint16_t col; int i, j; char filename[50]; - - pc.printf("TEST START1\r\n"); - // get the filename i=0; while (*Name_BMP!='\0') { filename[i++]=*Name_BMP++; } - pc.printf("TEST START2\r\n"); filename[i] = 0; - pc.printf("TEST START3\r\n"); FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file - pc.printf("TEST FILEOPEN\r\n"); if(Image == NULL) return -1; if(fgetc(Image) != 0x42) return -2; @@ -586,94 +552,50 @@ { imgsize += (((uint32_t)fgetc(Image)) << i*8); } -#ifdef TFT_DEBUG - pc.printf("BMP SIZE:%d\r\n",imgsize); -#endif fseek(Image,4,SEEK_CUR); for(i = 0; i < 4; i++) { offset += (((uint32_t)fgetc(Image)) << i*8); } -#ifdef TFT_DEBUG - pc.printf("BMP OFFSET:%d\r\n",offset); -#endif fseek(Image,4,SEEK_CUR); for(i = 0; i < 4; i++) { imgw += (((uint32_t)fgetc(Image)) << i*8); } - if(imgw > 240) return -3; + if(imgw > ST7735_TFTHEIGHT) return -3; for(i = 0; i < 4; i++) { imgh += (((uint32_t)fgetc(Image)) << i*8); } - if(imgh > 320) return -3; + if(imgh > ST7735_TFTWIDTH) return -3; fseek(Image,2,SEEK_CUR); colbits = fgetc(Image); - //if(colbits != 16 || colbits != 24) return -4; fgetc(Image); - if((compress=fgetc(Image)) != 0) + if(fgetc(Image) != 0) // Check for compression { - #ifdef TFT_DEBUG - pc.printf("Not supported compress : %d\r\n",compress); - #endif return -4; } - - -#ifdef TFT_DEBUG - pc.printf("RESOL : %d col, %d X %d",colbits,imgw,imgh); -#endif - + fseek(Image, offset, SEEK_SET); for (j = imgh; j >= 0; j--) //Lines { fread(img,sizeof(char),imgw*3,Image); _cs = 1; - setAddrWindow(0, j, imgw ,1); + setAddrWindow(x, y+j, x+imgw, y+j+1); writecommand(0x2C); // send pixel - #ifdef TARGET_WIZWIKI_W7500 - lcdPort.format(16,3); - #endif - + _rs = 1; + _cs = 0; for(i = 0; i < imgw; i++) { -/* if(colbits == 16) - { - col = (uint16_t)img[2*i+1]; - col <<= 8; - col += (uint16_t)img[2*i]; - } - else if(colbits == 24) */ - //{ - col = RGB((uint16_t)img[3*i+2],(uint16_t)img[3*i+1], (uint16_t)img[3*i]); - //} - #ifdef TFT_DEBUG - /* - - pc.printf("RGB(%d): ",i); - pc.printf("(%d,",img[3*i+2]); - pc.printf("%d,", img[3*i+1]); - pc.printf("%d),->", img[3*i]); - pc.printf("%04x\r\n",col); - */ - //pc.printf("RGB(%d): (%d,%d,%d) -> %04X\r\n ",i,img[3*i+2],img[3*i+1],img[3*i],col); - #endif - _rs = 1; - _cs = 0; - - lcdPort.write(col); + col = RGB((uint16_t)img[3*i+2],(uint16_t)img[3*i+1], (uint16_t)img[3*i]); + lcdPort.write( col >> 8 ); + lcdPort.write( col ); } _cs = 1; - #ifdef TARGET_WIZWIKI_W7500 - lcdPort.format(8,3); - #endif } setAddrWindow(0,0,_width,_height); return 0; } - -
diff -r 7d45da201480 -r 20134e1718b2 Adafruit_ST7735.h --- a/Adafruit_ST7735.h Thu Jul 30 22:55:47 2015 +0000 +++ b/Adafruit_ST7735.h Wed Dec 16 18:44:10 2015 +0000 @@ -96,6 +96,7 @@ Adafruit_ST7735(PinName mosi, PinName miso, PinName sck, PinName CS, PinName RS, PinName RST); void initB(void); // for ST7735B displays + void initS(void); // for ST7735S displays void initR(uint8_t options = INITR_GREENTAB); // for ST7735R void setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1); void pushColor(uint16_t color); @@ -105,10 +106,10 @@ virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); void setRotation(uint8_t r); - void invertDisplay(boolean i); + void invertDisplay(bool i); uint16_t Color565(uint8_t r, uint8_t g, uint8_t b); - int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP); int DrawBitmapFile(const char *Name_BMP); + int DrawBitmapFile(unsigned int x, unsigned int y, const char *Name_BMP); private: