Christian B. / ST7735_TFT

Fork of ST7735_TFT by Jonne Valola

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ST7735_TFT.cpp Source File

ST7735_TFT.cpp

00001  /* mbed library for 128*160 pixel display TFT based on ST7735 LCD Controller
00002  * ST7735 specific routines (initialization, window addressing, pixel output) 
00003  * Copyright (c) 2011 Jonne Valola
00004  *
00005  * WARNING !! WORK IN PROGRESS !!!
00006  *
00007  * Graphics routines and SPI routines derived work used with permission from:
00008  * mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
00009  * Copyright (c) 2011 Peter Drescher - DC2PD
00010  *
00011  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00012  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00013  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00014  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00015  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00017  * THE SOFTWARE.
00018  */
00019 
00020 
00021 #include "ST7735_TFT.h"
00022 #include "mbed.h"
00023 
00024 #define BPP         16                  // Bits per pixel                
00025 
00026 ST7735_TFT::ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset, const char *name)
00027         : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _rs(rs), _reset(reset) {
00028     tft_reset();
00029     orientation = 2;
00030     char_x = 0;
00031 }
00032 
00033 
00034 int ST7735_TFT::width() {
00035     if (orientation == 0 || orientation == 2) return 128;
00036     else return 160; 
00037 }
00038 
00039 
00040 int ST7735_TFT::height() {
00041     if (orientation == 0 || orientation == 2) return 160;
00042     else return 128; 
00043 }
00044 
00045 
00046 void ST7735_TFT::set_orientation(unsigned int o) {
00047     orientation = o;
00048     switch (orientation) {
00049         case 0:
00050             wr_reg(ST7735_MADCTL, 0x0008);
00051             break;
00052         case 1:
00053             wr_reg(ST7735_MADCTL, 0x0068);
00054             break;
00055         case 2:
00056             wr_reg(ST7735_MADCTL, 0x00C8);
00057             break;
00058         case 3:
00059             wr_reg(ST7735_MADCTL, 0x00A8);
00060             break;
00061     }
00062 }
00063 
00064 
00065 
00066 void ST7735_TFT::wr_cmd(int cmd) {
00067     _rs = 0; // rs low, cs low for transmitting command
00068     _cs = 0;
00069     _spi.write(cmd);
00070     _cs = 1;
00071 }
00072 
00073 
00074 
00075 void ST7735_TFT::wr_dat(int dat) {
00076     _rs = 1; // rs high, cs low for transmitting data
00077     _cs = 0;                         
00078     _spi.write(dat);                                                           
00079     _cs = 1;
00080 }
00081 
00082 
00083 
00084 void ST7735_TFT::wr_dat_start(void) {
00085     _rs = 1; //  rs high, cs low for transmitting data
00086     _cs = 0;
00087 }
00088 
00089 
00090 
00091 void ST7735_TFT::wr_dat_stop (void) {
00092     _cs = 1;
00093 }
00094 
00095 
00096 
00097 void ST7735_TFT::wr_dat_only (unsigned short dat) {  
00098     _spi.write(dat);                                            
00099 }
00100 
00101 
00102 unsigned short ST7735_TFT::rd_dat (void) {
00103     unsigned short val = 0;
00104     _cs = 0;
00105     _spi.write(0);                                /* Dummy read 1                 */
00106     val   = _spi.write(0);                        /* Read D8..D15                 */
00107     val <<= 8;
00108     val  |= _spi.write(0);                        /* Read D0..D7                  */
00109     _cs = 1;
00110     return (val);
00111 }
00112 
00113 void ST7735_TFT::wr_reg (unsigned char reg, unsigned short val) {
00114 
00115     wr_cmd(reg);
00116     wr_dat(val);
00117 }
00118 
00119 
00120 unsigned short ST7735_TFT::rd_reg (unsigned char reg) {
00121 
00122     wr_cmd(reg);
00123     return(rd_dat());
00124 }
00125 
00126 void ST7735_TFT::read_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *buffer) {
00127     // BEWARE !
00128     // DOES NOT WORK CORRECTLY YET !!!
00129     int val;
00130     window(x,y,w,h);
00131     wr_cmd(ST7735_RAMRD);  // write to RAM
00132     _cs = 0;
00133     _rs = 1;
00134     _spi.write(0);                                /* Dummy read 1                 */
00135     
00136     val   = _spi.write(0);                        /* Read D8..D15                 */
00137     val <<= 8;
00138     val  |= _spi.write(0);                        /* Read D0..D7                  */
00139     _cs = 1;
00140 }
00141 
00142 int ST7735_TFT::getpixel(unsigned int x, unsigned int y) {
00143     // BEWARE !
00144     // DOES NOT WORK CORRECTLY YET !!!
00145     int val;
00146     _spi.format(8,3);
00147     wr_cmd(ST7735_CASET);  // column addr set
00148     wr_dat(0x00);
00149     wr_dat(x+2);   // XSTART 
00150     wr_dat(0x00);
00151     wr_dat(x+2+2);   // XEND
00152 
00153     wr_cmd(ST7735_RASET);  // row addr set
00154     wr_dat(0x00);
00155     wr_dat(y+1);    // YSTART
00156     wr_dat(0x00);
00157     wr_dat(y+1+1);    // YEND
00158     
00159     _rs = 0; // rs low, cs low for transmitting command
00160     _cs = 0;
00161     _spi.write(0x2E);
00162     _rs = 1;
00163     _spi.write(0x00);                                /* Dummy read 1                 */
00164     
00165     val   = _spi.write(0x00);                        /* Read D8..D15                 */
00166     val <<= 8;
00167     val  |= _spi.write(0x00);                        /* Read D0..D7                  */
00168 
00169     _cs = 1;
00170     return val;
00171 }
00172 
00173 
00174 void ST7735_TFT::tft_reset() {
00175     // init SPI
00176     _spi.format(8,3);                 // 8 bit spi mode 3
00177     _spi.frequency(8000000);         // 16Mhz SPI clock ... 15Mhz is maximum for display, but it seems to work
00178     
00179     // reset exactly like in Arduino version
00180     _cs = 0;
00181     _reset = 1;                       // reset
00182     wait_ms(500);
00183     _reset = 0;                       // reset
00184     wait_ms(500);
00185     _reset = 1;                       // reset
00186     wait_ms(500);
00187     
00188     /* Start Initial Sequence ----------------------------------------------------*/
00189     wr_cmd(ST7735_SWRESET);                         /* SW Reset                       */
00190     wait_ms(150);
00191     wr_cmd(ST7735_SLPOUT);                         /* Out of sleepmode               */
00192     wait_ms(500);
00193     
00194     wr_cmd(ST7735_FRMCTR1);                         /* Frame rate in normal mode            */
00195     wr_dat(0x01);                              
00196     wr_dat(0x2C);
00197     wr_dat(0x2D);
00198     
00199     wr_cmd(ST7735_FRMCTR2);                         /* Frame rate in idle mode            */
00200     wr_dat(0x01);                              
00201     wr_dat(0x2C);
00202     wr_dat(0x2D);
00203 
00204     wr_cmd(ST7735_FRMCTR3);                         /* Frame rate in partial mode            */
00205     wr_dat(0x01);                              
00206     wr_dat(0x2C);
00207     wr_dat(0x2D);
00208     wr_dat(0x01);   // inversion mode settings                              
00209     wr_dat(0x2C);
00210     wr_dat(0x2D);
00211     
00212     wr_cmd(ST7735_INVCTR);   // Inverted mode off
00213     wr_dat(0x07);   
00214 
00215     wr_cmd(ST7735_PWCTR1);   // POWER CONTROL 1   
00216     wr_dat(0xA2); 
00217     wr_dat(0x02);           // -4.6V
00218     wr_dat(0x84);           // AUTO mode 
00219     
00220     wr_cmd(ST7735_PWCTR2);   // POWER CONTROL 2   
00221     wr_dat(0xC5);            // VGH25 = 2.4C VGSEL =-10 VGH = 3*AVDD
00222     
00223     wr_cmd(ST7735_PWCTR3);  // POWER CONTROL 3   
00224     wr_dat(0x0A);           // Opamp current small
00225     wr_dat(0x00);           // Boost freq
00226 
00227     wr_cmd(ST7735_PWCTR4);   // POWER CONTROL 4   
00228     wr_dat(0x8A);            // BCLK/2, Opamp current small / medium low 
00229     wr_dat(0x2A);            // 
00230   
00231     wr_cmd(ST7735_PWCTR5);   // POWER CONTROL 5   
00232     wr_dat(0x8A);            // BCLK/2, Opamp current small / medium low 
00233     wr_dat(0xEE);            //
00234     
00235     wr_cmd(ST7735_VMCTR1);   // POWER CONTROL 6   
00236     wr_dat(0x0E);            // 
00237 
00238     wr_cmd(ST7735_INVOFF);   // INVOFF
00239     
00240     wr_cmd(ST7735_MADCTL);   // ORIENTATION   
00241     wr_dat(0xC8);   // 
00242     
00243     wr_cmd(ST7735_COLMOD);   // COLOR MODE   
00244     wr_dat(0x05);            //      
00245     
00246     wr_cmd(ST7735_CASET);   // COLUMN ADDR SET   
00247     wr_dat(0x00);   //
00248     wr_dat(0x00);   // xstart = 0
00249     wr_dat(0x00);   //
00250     wr_dat(0x7F);   // xend = 127
00251     
00252     wr_cmd(ST7735_RASET);   // ROW ADDR SET   
00253     wr_dat(0x00);   //
00254     wr_dat(0x00);   // ystart = 0
00255     wr_dat(0x00);   //
00256     wr_dat(0x9F);   // yend = 159            
00257     
00258     /* Gamma settings  -----------------------------------------------------------*/
00259 
00260   wr_cmd(0xE0); // GMCTRP1
00261   wr_dat(0x02);
00262   wr_dat(0x1c);
00263   wr_dat(0x07);
00264   wr_dat(0x12);
00265   wr_dat(0x37);
00266   wr_dat(0x32);
00267   wr_dat(0x29);
00268   wr_dat(0x2d);
00269   wr_dat(0x29);
00270   wr_dat(0x25);
00271   wr_dat(0x2B);
00272   wr_dat(0x39);
00273   wr_dat(0x00);
00274   wr_dat(0x01);
00275   wr_dat(0x03);
00276   wr_dat(0x10);
00277   wr_cmd(0xE1); // GMCTRN1
00278   wr_dat(0x03); 
00279   wr_dat(0x1d); 
00280   wr_dat(0x07); 
00281   wr_dat(0x06); 
00282   wr_dat(0x2E); 
00283   wr_dat(0x2C); 
00284   wr_dat(0x29); 
00285   wr_dat(0x2D); 
00286   wr_dat(0x2E); 
00287   wr_dat(0x2E); 
00288   wr_dat(0x37); 
00289   wr_dat(0x3F); 
00290   wr_dat(0x00); 
00291   wr_dat(0x00); 
00292   wr_dat(0x02); 
00293   wr_dat(0x10); 
00294   
00295   wr_cmd(ST7735_DISPON); // display ON
00296   wait_ms(100);
00297 
00298   wr_cmd(ST7735_NORON);  // normal display on
00299   wait_ms(10);
00300   
00301   switch (orientation) {
00302         case 0:
00303             wr_reg(0xC8, 0x0008);
00304             break;
00305         case 1:
00306             wr_reg(0xC8, 0x0068);
00307             break;
00308         case 2:
00309             wr_reg(0xC8, 0x00C8);
00310             break;
00311         case 3:
00312             wr_reg(0xC8, 0x00A8);
00313             break;
00314     }
00315     WindowMax ();
00316 }
00317 
00318 
00319 void ST7735_TFT::pixel(int x, int y, int color) {
00320   if ((x >= width()) || (y >= height())) return;
00321 
00322   window(x,y,x+1,y+1);
00323   
00324   // setup for data
00325    _rs = 1; 
00326    _cs = 0;
00327   _spi.format(16,3);
00328   _spi.write(color);     
00329   _cs = 1;
00330   _spi.format(8,3);
00331 }
00332 
00333 void ST7735_TFT::window (int x, int y, int w, int h) {
00334   wr_cmd(ST7735_CASET);  // column addr set
00335   wr_dat(0x00);
00336   wr_dat(x+2);   // XSTART 
00337   wr_dat(0x00);
00338   wr_dat(x+w+1);   // XEND
00339 
00340   wr_cmd(ST7735_RASET);  // row addr set
00341   wr_dat(0x00);
00342   wr_dat(y+1);    // YSTART
00343   wr_dat(0x00);
00344   wr_dat(y+h+1);    // YEND
00345 
00346   wr_cmd(ST7735_RAMWR);  // write to RAM
00347 }
00348 
00349 
00350 void ST7735_TFT::WindowMax (void) {
00351     window (0, 0, width(),  height());
00352 }
00353 
00354 
00355 void ST7735_TFT::cls (void) {
00356     unsigned int i;
00357     WindowMax();
00358     wr_dat_start();
00359     _spi.format(16,3);
00360     for (i = 0; i < ( (width()+1) * (height()+3)); i++) {
00361         _spi.write(_background);    
00362     }
00363     _spi.format(8,3);
00364     wr_dat_stop();
00365 }
00366 
00367 
00368 void ST7735_TFT::circle(int x0, int y0, int r, int color) {
00369 
00370     int draw_x0, draw_y0;
00371     int draw_x1, draw_y1;
00372     int draw_x2, draw_y2;
00373     int draw_x3, draw_y3;
00374     int draw_x4, draw_y4;
00375     int draw_x5, draw_y5;
00376     int draw_x6, draw_y6;
00377     int draw_x7, draw_y7;
00378     int xx, yy;
00379     int di;
00380     WindowMax();
00381     if (r == 0) {       /* no radius */
00382         return;
00383     }
00384 
00385     draw_x0 = draw_x1 = x0;
00386     draw_y0 = draw_y1 = y0 + r;
00387     if (draw_y0 < height()) {
00388         pixel(draw_x0, draw_y0, color);     /* 90 degree */
00389     }
00390 
00391     draw_x2 = draw_x3 = x0;
00392     draw_y2 = draw_y3 = y0 - r;
00393     if (draw_y2 >= 0) {
00394         pixel(draw_x2, draw_y2, color);    /* 270 degree */
00395     }
00396 
00397     draw_x4 = draw_x6 = x0 + r;
00398     draw_y4 = draw_y6 = y0;
00399     if (draw_x4 < width()) {
00400         pixel(draw_x4, draw_y4, color);     /* 0 degree */
00401     }
00402 
00403     draw_x5 = draw_x7 = x0 - r;
00404     draw_y5 = draw_y7 = y0;
00405     if (draw_x5>=0) {
00406         pixel(draw_x5, draw_y5, color);     /* 180 degree */
00407     }
00408 
00409     if (r == 1) {
00410         return;
00411     }
00412 
00413     di = 3 - 2*r;
00414     xx = 0;
00415     yy = r;
00416     while (xx < yy) {
00417 
00418         if (di < 0) {
00419             di += 4*xx + 6;
00420         } else {
00421             di += 4*(xx - yy) + 10;
00422             yy--;
00423             draw_y0--;
00424             draw_y1--;
00425             draw_y2++;
00426             draw_y3++;
00427             draw_x4--;
00428             draw_x5++;
00429             draw_x6--;
00430             draw_x7++;
00431         }
00432         xx++;
00433         draw_x0++;
00434         draw_x1--;
00435         draw_x2++;
00436         draw_x3--;
00437         draw_y4++;
00438         draw_y5++;
00439         draw_y6--;
00440         draw_y7--;
00441 
00442         if ( (draw_x0 <= width()) && (draw_y0>=0) ) {
00443             pixel(draw_x0, draw_y0, color);
00444         }
00445 
00446         if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
00447             pixel(draw_x1, draw_y1, color);
00448         }
00449 
00450         if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) {
00451             pixel(draw_x2, draw_y2, color);
00452         }
00453 
00454         if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) {
00455             pixel(draw_x3, draw_y3, color);
00456         }
00457 
00458         if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) {
00459             pixel(draw_x4, draw_y4, color);
00460         }
00461 
00462         if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
00463             pixel(draw_x5, draw_y5, color);
00464         }
00465         if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) {
00466             pixel(draw_x6, draw_y6, color);
00467         }
00468         if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) {
00469             pixel(draw_x7, draw_y7, color);
00470         }
00471     }
00472     return;
00473 }
00474 
00475 void ST7735_TFT::fillcircle(int x, int y, int r, int color) {
00476     int i;
00477     for (i = 0; i <= r; i++)
00478         circle(x,y,i,color);
00479 }
00480 
00481 
00482 
00483 void ST7735_TFT::hline(int x0, int x1, int y, int color) {
00484     int w;
00485     w = x1 - x0 + 1;
00486     window(x0,y,w,1);
00487     wr_cmd(0x2C);
00488     wr_dat_start();
00489     for (int x=0; x<w; x++) {
00490         _spi.write(color);
00491         _spi.write(color >> 8);
00492     }
00493     wr_dat_stop();
00494     return;
00495 }
00496 
00497 
00498 
00499 void ST7735_TFT::vline(int x, int y0, int y1, int color) {
00500     int h;
00501     h = y1 - y0 + 1;
00502     window(x,y0,1,h);
00503     wr_cmd(0x2C);
00504     wr_dat_start();
00505     for (int y=0; y<h; y++) {
00506         _spi.write(color);
00507         _spi.write(color >> 8);
00508     }
00509     wr_dat_stop();
00510     return;
00511 }
00512 
00513 
00514 
00515 void ST7735_TFT::line(int x0, int y0, int x1, int y1, int color) {
00516     WindowMax();
00517     int   dx = 0, dy = 0;
00518     int   dx_sym = 0, dy_sym = 0;
00519     int   dx_x2 = 0, dy_x2 = 0;
00520     int   di = 0;
00521 
00522     dx = x1-x0;
00523     dy = y1-y0;
00524 
00525     if (dx == 0) {        /* vertical line */
00526         if (y1 > y0) vline(x0,y0,y1,color);
00527         else vline(x0,y1,y0,color);
00528         return;
00529     }
00530 
00531     if (dx > 0) {
00532         dx_sym = 1;
00533     } else {
00534         dx_sym = -1;
00535     }
00536     if (dy == 0) {        /* horizontal line */
00537         if (x1 > x0) hline(x0,x1,y0,color);
00538         else  hline(x1,x0,y0,color);
00539         return;
00540     }
00541 
00542     if (dy > 0) {
00543         dy_sym = 1;
00544     } else {
00545         dy_sym = -1;
00546     }
00547 
00548     dx = dx_sym*dx;
00549     dy = dy_sym*dy;
00550 
00551     dx_x2 = dx*2;
00552     dy_x2 = dy*2;
00553 
00554     if (dx >= dy) {
00555         di = dy_x2 - dx;
00556         while (x0 != x1) {
00557 
00558             pixel(x0, y0, color);
00559             x0 += dx_sym;
00560             if (di<0) {
00561                 di += dy_x2;
00562             } else {
00563                 di += dy_x2 - dx_x2;
00564                 y0 += dy_sym;
00565             }
00566         }
00567         pixel(x0, y0, color);
00568     } else {
00569         di = dx_x2 - dy;
00570         while (y0 != y1) {
00571             pixel(x0, y0, color);
00572             y0 += dy_sym;
00573             if (di < 0) {
00574                 di += dx_x2;
00575             } else {
00576                 di += dx_x2 - dy_x2;
00577                 x0 += dx_sym;
00578             }
00579         }
00580         pixel(x0, y0, color);
00581     }
00582     return;
00583 }
00584 
00585 
00586 
00587 
00588 void ST7735_TFT::rect(int x0, int y0, int x1, int y1, int color) {
00589 
00590     if (x1 > x0) hline(x0,x1,y0,color);
00591     else  hline(x1,x0,y0,color);
00592 
00593     if (y1 > y0) vline(x0,y0,y1,color);
00594     else vline(x0,y1,y0,color);
00595 
00596     if (x1 > x0) hline(x0,x1,y1,color);
00597     else  hline(x1,x0,y1,color);
00598 
00599     if (y1 > y0) vline(x1,y0,y1,color);
00600     else vline(x1,y1,y0,color);
00601 
00602     return;
00603 }
00604 
00605 
00606 
00607 void ST7735_TFT::fillrect(int x0, int y0, int x1, int y1, int color) {
00608 
00609     int h = y1 - y0 + 1;
00610     int w = x1 - x0 + 1;
00611     int pixel = h * w;
00612     window(x0,y0,w,h);
00613     wr_cmd(0x2C);
00614     wr_dat_start();
00615     for (int p=0; p<pixel; p++) {
00616         _spi.write(color);
00617         _spi.write(color >> 8);
00618     }
00619     wr_dat_stop();
00620     return;
00621 }
00622 
00623 
00624 
00625 void ST7735_TFT::locate(int x, int y) {
00626     char_x = x;
00627     char_y = y;
00628 }
00629 
00630 
00631 
00632 int ST7735_TFT::columns() {
00633     return width() / font[1];
00634 }
00635 
00636 
00637 
00638 int ST7735_TFT::rows() {
00639     return height() / font[2];
00640 }
00641 
00642 
00643 
00644 int ST7735_TFT::_putc(int value) {
00645     if (value == '\n') {    // new line
00646         char_x = 0;
00647         char_y = char_y + font[2];
00648         if (char_y >= height() - font[2]) {
00649             char_y = 0;
00650         }
00651     } else {
00652         character(char_x, char_y, value);
00653      }
00654     return value;
00655 }
00656 
00657 
00658 
00659 
00660 void ST7735_TFT::character(int x, int y, int c) {
00661     unsigned int hor,vert,offset,bpl,j,i,b;
00662     unsigned char* zeichen;
00663     unsigned char z,w;
00664 
00665     if ((c < 31) || (c > 127)) return;   // test char range
00666 
00667     // read font parameter from start of array
00668     offset = font[0];                    // bytes / char
00669     hor = font[1];                       // get hor size of font
00670     vert = font[2];                      // get vert size of font
00671     bpl = font[3];                       // bytes per line
00672 
00673     if (char_x + hor > width()) {
00674         char_x = 0;
00675         char_y = char_y + vert;
00676        if (char_y >= height() - font[2]) {
00677             char_y = 0;
00678         }
00679     }
00680 
00681     window(char_x, char_y,hor,vert); // char box
00682     wr_cmd(0x2C);
00683     wr_dat_start();
00684     zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
00685     w = zeichen[0];                          // width of actual char
00686     _spi.format(16,3);                       // pixel are 16 bit
00687 
00688     for (j=0; j<vert; j++) {  //  vert line
00689         for (i=0; i<hor; i++) {   //  horz line
00690             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
00691             b = 1 << (j & 0x07);
00692             if (( z & b ) == 0x00) {
00693                 _spi.write(_background);
00694             } else {
00695                 _spi.write(_foreground);
00696             }
00697         }
00698     }
00699     _spi.format(8,3);                      // 8 bit
00700     wr_dat_stop();
00701     if ((w + 2) < hor) {                   // x offset to next char
00702         char_x += w + 2;
00703     } else char_x += hor;
00704 }
00705 
00706 
00707 
00708 
00709 
00710 void ST7735_TFT::set_font(unsigned char* f) {
00711     font = f;
00712 }
00713 
00714 
00715 
00716 void ST7735_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) {
00717     unsigned int    i,j;
00718     unsigned short *bitmap_ptr = (unsigned short *)bitmap;
00719     window(x, y, w, h);
00720     wr_cmd(0x2C);
00721     wr_dat_start();
00722     for (j = 0; j < h; j++) {        //Lines
00723         for (i = 0; i < w; i++) {     // copy pixel data to TFT
00724             _spi.write(*bitmap_ptr);    // one line
00725             _spi.write(*bitmap_ptr >> 8);  
00726             bitmap_ptr++;
00727         }
00728     }
00729     wr_dat_stop();
00730 }
00731 
00732 /*
00733 int ST7735_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) {
00734 // BEWARE !
00735 // NOT TESTED
00736 #define OffsetPixelWidth    18
00737 #define OffsetPixelHeigh    22
00738 #define OffsetFileSize      34
00739 #define OffsetPixData       10
00740 #define OffsetBPP           28
00741 
00742     char filename[50];
00743     unsigned char BMP_Header[54];
00744     unsigned short BPP_t;
00745     unsigned int PixelWidth,PixelHeigh,start_data;
00746     unsigned int    i,off;
00747     int padd,j;
00748     unsigned short *line;
00749 
00750     // get the filename
00751     LocalFileSystem local("local");
00752     sprintf(&filename[0],"/local/");
00753     i=7;
00754     while (*Name_BMP!='\0') {
00755         filename[i++]=*Name_BMP++;
00756     }
00757     FILE *Image = fopen((const char *)&filename[0], "r");  // open the bmp file
00758     if (!Image) {
00759         return(0);      // error file not found !
00760     }
00761 
00762     fread(&BMP_Header[0],1,54,Image);      // get the BMP Header
00763 
00764     if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) {  // check magic byte
00765         fclose(Image);
00766         return(-1);     // error no BMP file
00767     }
00768 
00769     BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
00770     if (BPP_t != 0x0010) {
00771         fclose(Image);
00772         return(-2);     // error no 16 bit BMP
00773     }
00774 
00775     PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
00776     PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
00777     if (PixelHeigh > height() + y || PixelWidth > width() + x) {
00778         fclose(Image);
00779         return(-3);      // to big
00780     }
00781 
00782     start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
00783 
00784     line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line
00785     if (line == NULL) {
00786         return(-4);         // error no memory
00787     }
00788 
00789     // the lines are padded to multiple of 4 bytes
00790     padd = -1;
00791     do {
00792         padd ++;
00793     } while ((PixelWidth * 2 + padd)%4 != 0);
00794 
00795     window(x, y,PixelWidth,PixelHeigh);
00796     wr_cmd(0x2C);
00797     wr_dat_start();
00798     _spi.format(16,3);    
00799     for (j = PixelHeigh - 1; j >= 0; j--) {               //Lines bottom up
00800         off = j * (PixelWidth * 2 + padd) + start_data;   // start of line
00801         fseek(Image, off ,SEEK_SET);
00802         fread(line,1,PixelWidth * 2,Image);       // read a line - slow !
00803         for (i = 0; i < PixelWidth; i++) {        // copy pixel data to TFT
00804             _spi.write(line[i]);                  // one 16 bit pixel
00805         } 
00806     }
00807     _spi.format(8,3);
00808     wr_dat_stop();
00809     free (line);
00810     fclose(Image);
00811     return(1);
00812 }*/