Eric Coyle / TFT_ST7735

Dependents:   ME503_VehicleAssembly

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_ST7735.cpp Source File

Adafruit_ST7735.cpp

00001 /***************************************************
00002   This is a library for the Adafruit 1.8" SPI display.
00003   This library works with the Adafruit 1.8" TFT Breakout w/SD card
00004   ----> http://www.adafruit.com/products/358
00005   as well as Adafruit raw 1.8" TFT display
00006   ----> http://www.adafruit.com/products/618
00007 
00008   Check out the links above for our tutorials and wiring diagrams
00009   These displays use SPI to communicate, 4 or 5 pins are required to
00010   interface (RST is optional)
00011   Adafruit invests time and resources providing this open source code,
00012   please support Adafruit and open-source hardware by purchasing
00013   products from Adafruit!
00014 
00015   Written by Limor Fried/Ladyada for Adafruit Industries.
00016   MIT license, all text above must be included in any redistribution
00017  ****************************************************/
00018 
00019 #include "mbed.h"
00020 #include "Adafruit_ST7735.h"
00021 
00022 // Constructor 
00023 Adafruit_ST7735::Adafruit_ST7735(PinName mosi, PinName miso, PinName clk, PinName cs, PinName rs, PinName rst): lcdPort(mosi,miso,clk), _cs(cs), _rs(rs), _rst(rst), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT)
00024 {
00025     lcdPort.format(8,3);
00026     lcdPort.frequency(10000000);
00027     initR();
00028     setRotation(0);
00029     setCursor(0,0);
00030     fillScreen(0x0000);
00031     setTextColor(0xFFFF,0x0000);
00032 }
00033 
00034 
00035 void Adafruit_ST7735::writecommand(uint8_t c)
00036 {
00037     _rs = 0;
00038     _cs = 0;
00039     lcdPort.write( c );
00040     _cs = 1;
00041 }
00042 
00043 
00044 void Adafruit_ST7735::writedata(uint8_t c)
00045 {
00046     _rs = 1;
00047     _cs = 0;
00048     lcdPort.write( c );
00049 
00050     _cs = 1;
00051 }
00052 
00053 
00054 // Rather than a bazillion writecommand() and writedata() calls, screen
00055 // initialization commands and arguments are organized in these tables
00056 // stored in PROGMEM.  The table may look bulky, but that's mostly the
00057 // formatting -- storage-wise this is hundreds of bytes more compact
00058 // than the equivalent code.  Companion function follows.
00059 #define DELAY 0x80
00060 static unsigned char
00061 Bcmd[] = {                  // Initialization commands for 7735B screens
00062     18,                       // 18 commands in list:
00063     ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
00064     50,                     //     50 ms delay
00065     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
00066     255,                    //     255 = 500 ms delay
00067     ST7735_COLMOD , 1+DELAY,  //  3: Set color mode, 1 arg + delay:
00068     0x05,                   //     16-bit color
00069     10,                     //     10 ms delay
00070     ST7735_FRMCTR1, 3+DELAY,  //  4: Frame rate control, 3 args + delay:
00071     0x00,                   //     fastest refresh
00072     0x06,                   //     6 lines front porch
00073     0x03,                   //     3 lines back porch
00074     10,                     //     10 ms delay
00075     ST7735_MADCTL , 1      ,  //  5: Memory access ctrl (directions), 1 arg:
00076     0x08,                   //     Row addr/col addr, bottom to top refresh
00077     ST7735_DISSET5, 2      ,  //  6: Display settings #5, 2 args, no delay:
00078     0x15,                   //     1 clk cycle nonoverlap, 2 cycle gate
00079     //     rise, 3 cycle osc equalize
00080     0x02,                   //     Fix on VTL
00081     ST7735_INVCTR , 1      ,  //  7: Display inversion control, 1 arg:
00082     0x0,                    //     Line inversion
00083     ST7735_PWCTR1 , 2+DELAY,  //  8: Power control, 2 args + delay:
00084     0x02,                   //     GVDD = 4.7V
00085     0x70,                   //     1.0uA
00086     10,                     //     10 ms delay
00087     ST7735_PWCTR2 , 1      ,  //  9: Power control, 1 arg, no delay:
00088     0x05,                   //     VGH = 14.7V, VGL = -7.35V
00089     ST7735_PWCTR3 , 2      ,  // 10: Power control, 2 args, no delay:
00090     0x01,                   //     Opamp current small
00091     0x02,                   //     Boost frequency
00092     ST7735_VMCTR1 , 2+DELAY,  // 11: Power control, 2 args + delay:
00093     0x3C,                   //     VCOMH = 4V
00094     0x38,                   //     VCOML = -1.1V
00095     10,                     //     10 ms delay
00096     ST7735_PWCTR6 , 2      ,  // 12: Power control, 2 args, no delay:
00097     0x11, 0x15,
00098     ST7735_GMCTRP1,16      ,  // 13: Magical unicorn dust, 16 args, no delay:
00099     0x09, 0x16, 0x09, 0x20, //     (seriously though, not sure what
00100     0x21, 0x1B, 0x13, 0x19, //      these config values represent)
00101     0x17, 0x15, 0x1E, 0x2B,
00102     0x04, 0x05, 0x02, 0x0E,
00103     ST7735_GMCTRN1,16+DELAY,  // 14: Sparkles and rainbows, 16 args + delay:
00104     0x0B, 0x14, 0x08, 0x1E, //     (ditto)
00105     0x22, 0x1D, 0x18, 0x1E,
00106     0x1B, 0x1A, 0x24, 0x2B,
00107     0x06, 0x06, 0x02, 0x0F,
00108     10,                     //     10 ms delay
00109     ST7735_CASET  , 4      ,  // 15: Column addr set, 4 args, no delay:
00110     0x00, 0x02,             //     XSTART = 2
00111     0x00, 0x81,             //     XEND = 129
00112     ST7735_RASET  , 4      ,  // 16: Row addr set, 4 args, no delay:
00113     0x00, 0x01,             //     XSTART = 1
00114     0x00, 0x80,             //     XEND = 160 or '128' /*sarvesh*/
00115     ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
00116     10,                     //     10 ms delay
00117     ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
00118     255
00119 },                  //     255 = 500 ms delay
00120 
00121 Rcmd1[] = {                 // Init for 7735R, part 1 (red or green tab)
00122     15,                       // 15 commands in list:
00123     ST7735_SWRESET,   DELAY,  //  1: Software reset, 0 args, w/delay
00124     150,                    //     150 ms delay
00125     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, 0 args, w/delay
00126     255,                    //     500 ms delay
00127     ST7735_FRMCTR1, 3      ,  //  3: Frame rate ctrl - normal mode, 3 args:
00128     0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
00129     ST7735_FRMCTR2, 3      ,  //  4: Frame rate control - idle mode, 3 args:
00130     0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
00131     ST7735_FRMCTR3, 6      ,  //  5: Frame rate ctrl - partial mode, 6 args:
00132     0x01, 0x2C, 0x2D,       //     Dot inversion mode
00133     0x01, 0x2C, 0x2D,       //     Line inversion mode
00134     ST7735_INVCTR , 1      ,  //  6: Display inversion ctrl, 1 arg, no delay:
00135     0x07,                   //     No inversion
00136     ST7735_PWCTR1 , 3      ,  //  7: Power control, 3 args, no delay:
00137     0xA2,
00138     0x02,                   //     -4.6V
00139     0x84,                   //     AUTO mode
00140     ST7735_PWCTR2 , 1      ,  //  8: Power control, 1 arg, no delay:
00141     0xC5,                   //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
00142     ST7735_PWCTR3 , 2      ,  //  9: Power control, 2 args, no delay:
00143     0x0A,                   //     Opamp current small
00144     0x00,                   //     Boost frequency
00145     ST7735_PWCTR4 , 2      ,  // 10: Power control, 2 args, no delay:
00146     0x8A,                   //     BCLK/2, Opamp current small & Medium low
00147     0x2A,
00148     ST7735_PWCTR5 , 2      ,  // 11: Power control, 2 args, no delay:
00149     0x8A, 0xEE,
00150     ST7735_VMCTR1 , 1      ,  // 12: Power control, 1 arg, no delay:
00151     0x0E,
00152     ST7735_INVOFF , 0      ,  // 13: Don't invert display, no args, no delay
00153     ST7735_MADCTL , 1      ,  // 14: Memory access control (directions), 1 arg:
00154     0xC8,                   //     row addr/col addr, bottom to top refresh
00155     ST7735_COLMOD , 1      ,  // 15: set color mode, 1 arg, no delay:
00156     0x05
00157 },                 //     16-bit color
00158 
00159 Rcmd2green[] = {            // Init for 7735R, part 2 (green tab only)
00160     2,                        //  2 commands in list:
00161     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
00162     0x00, 0x02,             //     XSTART = 0
00163     0x00, 0x7F+0x02,        //     XEND = 127
00164     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
00165     0x00, 0x01,             //     XSTART = 0
00166     0x00, 0x7F+0x01 //     XEND = not 159 but 127 /*sarvesh*/
00167 },      
00168 Rcmd2red[] = {              // Init for 7735R, part 2 (red tab only)
00169     2,                        //  2 commands in list:
00170     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
00171     0x00, 0x00,             //     XSTART = 0
00172     0x00, 0x7F,             //     XEND = 127
00173     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
00174     0x00, 0x00,             //     XSTART = 0
00175     0x00, 0x7F          //     XEND = not 159 but 127 /*sarvesh*/
00176 },           
00177 
00178 Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
00179     4,                        //  4 commands in list:
00180     ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
00181     0x02, 0x1c, 0x07, 0x12,
00182     0x37, 0x32, 0x29, 0x2d,
00183     0x29, 0x25, 0x2B, 0x39,
00184     0x00, 0x01, 0x03, 0x10,
00185     ST7735_GMCTRN1, 16      , //  2: Sparkles and rainbows, 16 args, no delay:
00186     0x03, 0x1d, 0x07, 0x06,
00187     0x2E, 0x2C, 0x29, 0x2D,
00188     0x2E, 0x2E, 0x37, 0x3F,
00189     0x00, 0x00, 0x02, 0x10,
00190     ST7735_NORON  ,    DELAY, //  3: Normal display on, no args, w/delay
00191     10,                     //     10 ms delay
00192     ST7735_DISPON ,    DELAY, //  4: Main screen turn on, no args w/delay
00193     100
00194 };                  //     100 ms delay
00195 
00196 static unsigned char Scmd[] = {                  // Initialization commands for S6D02A1 screens
00197     29,
00198     0xf0,   2,  0x5a, 0x5a,             // Excommand2
00199     0xfc,   2,  0x5a, 0x5a,             // Excommand3
00200     0x26,   1,  0x01,                   // Gamma set
00201     0xfa,   15, 0x02, 0x1f, 0x00, 0x10, 0x22, 0x30, 0x38, 0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3d, 0x02, 0x01,   // Positive gamma control
00202     0xfb,   15, 0x21, 0x00, 0x02, 0x04, 0x07, 0x0a, 0x0b, 0x0c, 0x0c, 0x16, 0x1e, 0x30, 0x3f, 0x01, 0x02,   // Negative gamma control
00203     0xfd,   11, 0x00, 0x00, 0x00, 0x17, 0x10, 0x00, 0x01, 0x01, 0x00, 0x1f, 0x1f,                           // Analog parameter control
00204     0xf4,   15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x07, 0x00, 0x3C, 0x36, 0x00, 0x3C, 0x36, 0x00,   // Power control
00205     0xf5,   13, 0x00, 0x70, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x66, 0x06,               // VCOM control
00206     0xf6,   11, 0x02, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x02, 0x00, 0x06, 0x01, 0x00,                           // Source control
00207     0xf2,   17, 0x00, 0x01, 0x03, 0x08, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x04, 0x08, 0x08,   //Display control
00208     0xf8,   1,  0x11,                   // Gate control
00209     0xf7,   4, 0xc8, 0x20, 0x00, 0x00,  // Interface control
00210     0xf3,   2, 0x00, 0x00,              // Power sequence control
00211     0x11,   DELAY, 50,                  // Wake
00212     0xf3,   2+DELAY, 0x00, 0x01, 50,    // Power sequence control
00213     0xf3,   2+DELAY, 0x00, 0x03, 50,    // Power sequence control
00214     0xf3,   2+DELAY, 0x00, 0x07, 50,    // Power sequence control
00215     0xf3,   2+DELAY, 0x00, 0x0f, 50,    // Power sequence control
00216     0xf4,   15+DELAY, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x07, 0x00, 0x3C, 0x36, 0x00, 0x3C, 0x36, 0x00, 50, // Power control
00217     0xf3,   2+DELAY, 0x00, 0x1f, 50,    // Power sequence control
00218     0xf3,   2+DELAY, 0x00, 0x7f, 50,    // Power sequence control
00219     0xf3,   2+DELAY, 0x00, 0xff, 50,    // Power sequence control
00220     0xfd,   11, 0x00, 0x00, 0x00, 0x17, 0x10, 0x00, 0x00, 0x01, 0x00, 0x16, 0x16,                           // Analog parameter control
00221     0xf4,   15, 0x00, 0x09, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x07, 0x00, 0x3C, 0x36, 0x00, 0x3C, 0x36, 0x00,   // Power control
00222     0x36,   1, 0x08,                    // Memory access data control
00223     0x35,   1, 0x00,                    // Tearing effect line on
00224     0x3a,   1+DELAY, 0x05, 150,         // Interface pixel control
00225     0x29,   0,                          // Display on
00226     0x2c,   0                           // Memory write    
00227 };
00228 
00229 // Companion code to the above tables.  Reads and issues
00230 // a series of LCD commands stored in byte array.
00231 void Adafruit_ST7735::commandList(uint8_t *addr)
00232 {
00233 
00234     uint8_t  numCommands, numArgs;
00235     uint16_t ms;
00236 
00237     numCommands = *addr++;   // Number of commands to follow
00238     while(numCommands--) {                 // For each command...
00239         writecommand(*addr++); //   Read, issue command
00240         numArgs  = *addr++;    //   Number of args to follow
00241         ms       = numArgs & DELAY;          //   If hibit set, delay follows args
00242         numArgs &= ~DELAY;                   //   Mask out delay bit
00243         while(numArgs--) {                   //   For each argument...
00244             writedata(*addr++);  //     Read, issue argument
00245         }
00246 
00247         if(ms) {
00248             ms = *addr++; // Read post-command delay time (ms)
00249             if(ms == 255) ms = 500;     // If 255, delay for 500 ms
00250             wait_ms(ms);
00251         }
00252     }
00253 }
00254 
00255 
00256 // Initialization code common to both 'B' and 'R' type displays
00257 void Adafruit_ST7735::commonInit(uint8_t *cmdList)
00258 {
00259 
00260     colstart  = rowstart = 0; // May be overridden in init func
00261 
00262     _rs = 1;
00263     _cs = 1;
00264 
00265     // use default SPI format
00266     //lcdPort.format(8,0);
00267     //lcdPort.frequency(4000000);     // Lets try 4MHz
00268 
00269     // toggle RST low to reset; CS low so it'll listen to us
00270     _cs = 0;
00271     _rst = 1;
00272     wait_ms(500);
00273     _rst = 0;
00274     wait_ms(500);
00275     _rst = 1;
00276     wait_ms(500);
00277     if(cmdList) commandList(cmdList);
00278     
00279 }
00280 
00281 
00282 // Initialization for ST7735B screens
00283 void Adafruit_ST7735::initB(void)
00284 {
00285     commonInit(Bcmd);
00286 }
00287 
00288 
00289 // Initialization for ST7735R screens (green or red tabs)
00290 void Adafruit_ST7735::initR(uint8_t options)
00291 {
00292     commonInit(Rcmd1);
00293     if(options == INITR_GREENTAB) {
00294         commandList(Rcmd2green);
00295         colstart = 2;
00296         rowstart = 3;
00297     } else {
00298         // colstart, rowstart left at default '0' values
00299         commandList(Rcmd2red);
00300     }
00301     commandList(Rcmd3);
00302 }
00303 
00304 // Initialization for ST7735B screens
00305 void Adafruit_ST7735::initS(void)
00306 {
00307     commonInit(Scmd);
00308 }
00309 
00310 void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
00311                                     uint8_t y1)
00312 {
00313 
00314     writecommand(ST7735_CASET); // Column addr set
00315     writedata(0x00);
00316     writedata(x0+colstart);     // XSTART
00317     writedata(0x00);
00318     writedata(x1+colstart);     // XEND
00319 
00320     writecommand(ST7735_RASET); // Row addr set
00321     writedata(0x00);
00322     writedata(y0+rowstart);     // YSTART
00323     writedata(0x00);
00324     writedata(y1+rowstart);     // YEND
00325 
00326     writecommand(ST7735_RAMWR); // write to RAM
00327 }
00328 
00329 
00330 void Adafruit_ST7735::fillScreen(uint16_t color)
00331 {
00332 
00333     uint8_t x, y, hi = color >> 8, lo = color;
00334 
00335     setAddrWindow(0, 0, _width-1, _height-1);
00336 
00337     _rs = 1;
00338     _cs = 0;
00339 
00340     for(y=_height; y>0; y--) 
00341     {
00342         for(x=_width; x>0; x--) 
00343         {
00344             lcdPort.write( hi );
00345             lcdPort.write( lo );
00346         }
00347     }
00348     _cs = 1;
00349     clearTextBuf(0,0,_width,_height);
00350 }
00351 
00352 
00353 void Adafruit_ST7735::pushColor(uint16_t color)
00354 {
00355     _rs = 1;
00356     _cs = 0;
00357 
00358     lcdPort.write( color >> 8 );
00359     lcdPort.write( color );
00360     _cs = 1;
00361 }
00362 
00363 
00364 void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color)
00365 {
00366 
00367     if((x < 0) ||(x >= _width+1) || (y < 0) || (y >= _height+1)) return;
00368 
00369     setAddrWindow(x,y,x+1,y+1);
00370 
00371     _rs = 1;
00372     _cs = 0;
00373 
00374     lcdPort.write( color >> 8 );
00375     lcdPort.write( color );
00376 
00377     _cs = 1;
00378 }
00379 
00380 
00381 void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h,
00382                                     uint16_t color)
00383 {
00384 
00385     // Rudimentary clipping
00386     if((x >= _width) || (y >= _height)) return;
00387     if((y+h-1) >= _height) h = _height-y;
00388     setAddrWindow(x, y, x, y+h-1);
00389 
00390     uint8_t hi = color >> 8, lo = color;
00391     _rs = 1;
00392     _cs = 0;
00393     while (h--) {
00394         lcdPort.write( hi );
00395         lcdPort.write( lo );
00396     }
00397     _cs = 1;
00398 }
00399 
00400 
00401 void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w,
00402                                     uint16_t color)
00403 {
00404 
00405     // Rudimentary clipping
00406     if((x >= _width) || (y >= _height)) return;
00407     if((x+w-1) >= _width)  w = _width-x;
00408     setAddrWindow(x, y, x+w-1, y);
00409 
00410     uint8_t hi = color >> 8, lo = color;
00411     _rs = 1;
00412     _cs = 0;
00413     while (w--) {
00414         lcdPort.write( hi );
00415         lcdPort.write( lo );
00416     }
00417     _cs = 1;
00418 }
00419 
00420 /*To draw rectangle - by Sarvesh*/
00421 void Adafruit_ST7735::drawRect(int16_t x0, int16_t y0, int16_t x1,int16_t y1,uint16_t color)
00422 {
00423     int16_t w=abs(x1-x0);
00424     int16_t h=abs(y1-y0);
00425     if (x1 > x0) drawFastHLine(x0,y0,w,color);
00426     else  drawFastHLine(x1,y1,w,color);
00427 
00428     if (y1 > y0) drawFastVLine(x0,y0,h,color);
00429     else drawFastVLine(x1,y1,h,color);
00430 
00431     if (x1 > x0) drawFastHLine(x0,y1,w,color);
00432     else  drawFastHLine(x1,y0,w,color);
00433 
00434     if (y1 > y0) drawFastVLine(x1,y0,h,color);
00435     else drawFastVLine(x0,y1,h,color);
00436 
00437     return;
00438 }
00439 
00440 // fill a rectangle
00441 void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
00442                                uint16_t color)
00443 {
00444 
00445     // rudimentary clipping (drawChar w/big text requires this)
00446     if((x >= _width) || (y >= _height)) return;
00447     if((x + w - 1) >= _width)  w = _width  - x;
00448     if((y + h - 1) >= _height) h = _height - y;
00449 
00450     setAddrWindow(x, y, x+w-1, y+h-1);
00451 
00452     uint8_t hi = color >> 8, lo = color;
00453     _rs = 1;
00454     _cs = 0;
00455     for(y=h; y>0; y--) {
00456         for(x=w; x>0; x--) {
00457             lcdPort.write( hi );
00458             lcdPort.write( lo );
00459         }
00460     }
00461 
00462     _cs = 1;
00463 }
00464 /*To draw circle - by Sarvesh*/
00465 void Adafruit_ST7735::drawCircle(int16_t x0, int16_t y0,int16_t r,uint16_t color) 
00466 {
00467     int16_t draw_x0, draw_y0;
00468     int16_t draw_x1, draw_y1;
00469     int16_t draw_x2, draw_y2;
00470     int16_t draw_x3, draw_y3;
00471     int16_t draw_x4, draw_y4;
00472     int16_t draw_x5, draw_y5;
00473     int16_t draw_x6, draw_y6;
00474     int16_t draw_x7, draw_y7;
00475     int16_t xx, yy;
00476     int16_t di;
00477     setAddrWindow(1, 1, _width, _height);
00478     if (r == 0) {       /* no radius */
00479         return;
00480     }
00481 
00482     draw_x0 = draw_x1 = x0;
00483     draw_y0 = draw_y1 = y0 + r;
00484     if (draw_y0 < height()) {
00485         drawPixel(draw_x0, draw_y0, color);     /* 90 degree */
00486     }
00487 
00488     draw_x2 = draw_x3 = x0;
00489     draw_y2 = draw_y3 = y0 - r;
00490     if (draw_y2 >= 0) {
00491         drawPixel(draw_x2, draw_y2, color);    /* 270 degree */
00492     }
00493 
00494     draw_x4 = draw_x6 = x0 + r;
00495     draw_y4 = draw_y6 = y0;
00496     if (draw_x4 < width()) {
00497         drawPixel(draw_x4, draw_y4, color);     /* 0 degree */
00498     }
00499 
00500     draw_x5 = draw_x7 = x0 - r;
00501     draw_y5 = draw_y7 = y0;
00502     if (draw_x5>=0) {
00503         drawPixel(draw_x5, draw_y5, color);     /* 180 degree */
00504     }
00505 
00506     if (r == 1) {
00507         return;
00508     }
00509 
00510     di = 3 - 2*r;
00511     xx = 0;
00512     yy = r;
00513     while (xx < yy) {
00514 
00515         if (di < 0) {
00516             di += 4*xx + 6;
00517         } else {
00518             di += 4*(xx - yy) + 10;
00519             yy--;
00520             draw_y0--;
00521             draw_y1--;
00522             draw_y2++;
00523             draw_y3++;
00524             draw_x4--;
00525             draw_x5++;
00526             draw_x6--;
00527             draw_x7++;
00528         }
00529         xx++;
00530         draw_x0++;
00531         draw_x1--;
00532         draw_x2++;
00533         draw_x3--;
00534         draw_y4++;
00535         draw_y5++;
00536         draw_y6--;
00537         draw_y7--;
00538 
00539         if ( (draw_x0 <= width()) && (draw_y0>=0) ) {
00540             drawPixel(draw_x0, draw_y0, color);
00541         }
00542 
00543         if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
00544             drawPixel(draw_x1, draw_y1, color);
00545         }
00546 
00547         if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) {
00548             drawPixel(draw_x2, draw_y2, color);
00549         }
00550 
00551         if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) {
00552             drawPixel(draw_x3, draw_y3, color);
00553         }
00554 
00555         if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) {
00556             drawPixel(draw_x4, draw_y4, color);
00557         }
00558 
00559         if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
00560             drawPixel(draw_x5, draw_y5, color);
00561         }
00562         if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) {
00563             drawPixel(draw_x6, draw_y6, color);
00564         }
00565         if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) {
00566             drawPixel(draw_x7, draw_y7, color);
00567         }
00568     }
00569     return;
00570 }
00571 
00572 
00573 // Pass 8-bit (each) R,G,B, get back 16-bit packed color
00574 uint16_t Adafruit_ST7735::Color565(uint8_t r, uint8_t g, uint8_t b)
00575 {
00576     return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
00577 }
00578 
00579 
00580 #define MADCTL_MY  0x80
00581 #define MADCTL_MX  0x40
00582 #define MADCTL_MV  0x20
00583 #define MADCTL_ML  0x10
00584 #define MADCTL_RGB 0x08
00585 #define MADCTL_MH  0x04
00586 
00587 void Adafruit_ST7735::setRotation(uint8_t m)
00588 {
00589 
00590     writecommand(ST7735_MADCTL);
00591     rotation = m % 4; // can't be higher than 3
00592     switch (rotation) {
00593         case 0:
00594             writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
00595             _width  = ST7735_TFTWIDTH;
00596             _height = ST7735_TFTHEIGHT;
00597             break;
00598         case 1:
00599             writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
00600             _width  = ST7735_TFTHEIGHT;
00601             _height = ST7735_TFTWIDTH;
00602             break;
00603         case 2:
00604             writedata(MADCTL_RGB);
00605             _width  = ST7735_TFTWIDTH;
00606             _height = ST7735_TFTHEIGHT;
00607             break;
00608         case 3:
00609             writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
00610             _width  = ST7735_TFTHEIGHT;
00611             _height = ST7735_TFTWIDTH;
00612             break;
00613     }
00614 }
00615 
00616 
00617 void Adafruit_ST7735::invertDisplay(boolean i)
00618 {
00619     writecommand(i ? ST7735_INVON : ST7735_INVOFF);
00620 }
00621 /*
00622 void Adafruit_ST7735::display(void)
00623 {
00624     writecommand(ST7735_CASET | 0x0);  // low col = 0
00625     writecommand(ST7735_RASET | 0x0);  // hi col = 0
00626     writecommand(ST7735_RAMWR | 0x0); // line #0
00627 
00628     _cs = 1;
00629     _rs = 1;
00630     _cs = 0;
00631     setAddrWindow(0,0,3,3);
00632     for (uint16_t i=0; i<(ST7735_TFTWIDTH*ST7735_TFTHEIGHT/8); i++)
00633         lcdPort.write(buffer[i]);
00634 
00635     // i wonder why we have to do this (check datasheet)
00636     if (ST7735_TFTHEIGHT == 64)
00637     {
00638         
00639         for (uint16_t i=0; i<(ST7735_TFTWIDTH*ST7735_TFTHEIGHT/8); i++)
00640             lcdPort.write(0);
00641     }
00642     
00643     _cs = 1;
00644 } */
00645 /*
00646 void Adafruit_ST7735::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size)
00647 {
00648     if(
00649         (x >= _width) || // Clip right
00650         (y >= _height) || // Clip bottom
00651         ((x + 5 * size - 1) < 0) || // Clip left
00652         ((y + 8 * size - 1) < 0) // Clip top
00653         )
00654     return;
00655     
00656     for (int8_t i=0; i<6; i++ )
00657     {
00658         uint8_t line = 0;
00659 
00660         if (i == 5) 
00661             line = 0x0;
00662         else 
00663             line = font[(c*5)+i];
00664             
00665         for (int8_t j = 0; j<8; j++)
00666         {
00667             if (line & 0x1)
00668             {
00669                 if (size == 1) // default size
00670                     drawPixel(x+i, y+j, color);
00671 #ifdef WANT_ABSTRACTS
00672                 else // big size
00673                     fillRect(x+(i*size), y+(j*size), size, size, color);
00674 #endif
00675             }
00676             else if (bg != color)
00677             {
00678                 if (size == 1) // default size
00679                     drawPixel(x+i, y+j, bg);
00680 #ifdef WANT_ABSTRACTS
00681                 else // big size
00682                     fillRect(x+i*size, y+j*size, size, size, bg);
00683 #endif
00684             }
00685             line >>= 1;
00686         }
00687     }
00688 }*/
00689 /*
00690 void Adafruit_ST7735::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
00691 {
00692     drawLine(x0, y0, x1, y1, color);
00693     drawLine(x1, y1, x2, y2, color);
00694     drawLine(x2, y2, x0, y0, color);
00695 }*/