k og / Mbed 2 deprecated Aitendo_HX8347D_HelloWorld

Dependencies:   SDFileSystem mbed

Fork of Aitendo_HX8347D_HelloWorld by k og

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 ** http://www.aitendo.com/product/10942
00003 ** HX8347D
00004 
00005 https://github.com/adafruit/TFTLCD-Library/tree/master/bitmaps
00006 
00007 **/
00008 
00009 #include "mbed.h"
00010 #include "HX8347D.h"
00011 #include "SDFileSystem.h"
00012 //#define ROW  320
00013 //#define COL 240
00014 
00015 #define BLACK           0x0000
00016 #define BLUE            0x001F
00017 #define RED             0xF800
00018 #define GREEN           0x07E0
00019 #define CYAN            0x07FF
00020 #define MAGENTA         0xF81F
00021 #define YELLOW          0xFFE0 
00022 #define WHITE           0xFFFF
00023 
00024 #if defined(TARGET_ARCH_PRO)
00025 HX8347D tft(A0, A1, A2, A3, A4, D2, D3, D4, D5, D6, D7, D8, D9);
00026 SDFileSystem sd(D11, D12, D13, D10, "sd"); // mosi miso sclk cs
00027 Serial pc(USBTX, USBRX);
00028 #elif defined(TARGET_LPC1768)
00029 HX8347D tft(p15, p16, p17, p18, p19, p8, p26, p25, p24, p23, p10, p9, p22);
00030 Serial pc(USBTX, USBRX);
00031 SDFileSystem sd(p5, p6, p7, p21, "sd"); // mosi miso sclk cs
00032 #elif defined TARGET_NUCLEO_F446RE
00033 /* D0, D1 short SB62,SB63*/
00034 HX8347D tft(A0, A1, A2, A3, A4, D2, D3, D4, D5, D6, D7, D8, D9);
00035 SDFileSystem sd(D11, D12, D13, D10, "sd"); // mosi miso sclk cs
00036 Serial pc(USBTX, USBRX);
00037 
00038 #elif defined TARGET_NUCLEO_F103RB
00039 /* D0, D1 short SB62,SB63*/
00040 // SPI_MOSI D11
00041 // SPI_MISO D12
00042 // SPI_SCK  D13
00043 // SPI_CS   D10
00044 HX8347D tft(A0, A1, A2, A3, A4, D2, D3, D4, D5, D6, D7, D8, D9);
00045 SDFileSystem sd(D11, D12, D13, D10, "sd"); // mosi miso sclk cs
00046 Serial pc(USBTX, USBRX);
00047 
00048 
00049 #endif
00050 
00051 char str[128];
00052 
00053 void rotateTest() {
00054     tft.setRotation(1);
00055     for (uint16_t y=0; y < tft.height(); y+=5) {
00056         tft.drawHorizontalLine(0, y, tft.width(), RED);
00057     }
00058     wait_ms(500);
00059     for (uint16_t x=0; x < tft.width(); x+=5) {
00060         tft.drawVerticalLine(x, 0, 239, GREEN);
00061         pc.printf("x=%d  %d\n", x, tft.height());
00062     }
00063     wait_ms(500);
00064     
00065 }
00066 void rotateText() {
00067     for (uint8_t i=0; i<4; i++) {
00068         tft.fillScreen(BLACK);
00069         pc.printf("%d" , tft.getRotation());
00070         
00071         tft.setCursor(0, 30);
00072         tft.setTextColor(RED);
00073         tft.setTextSize(1);
00074         tft.println("Hello World!\n");
00075         wait_ms(500);
00076         tft.setTextColor(YELLOW);
00077         tft.setTextSize(2);
00078         tft.println("Hello World!\n");
00079         wait_ms(500);
00080         tft.setTextColor(GREEN);
00081         tft.setTextSize(3);
00082         tft.println("Hello World!\n");
00083         wait_ms(500);
00084         tft.setTextColor(CYAN);
00085         tft.setTextSize(3);
00086         tft.println("Hello World!\n");
00087         wait_ms(500);
00088         tft.setTextColor(MAGENTA);
00089         tft.setTextSize(2);
00090         tft.println("Hello World!\n");
00091         wait_ms(500);
00092         tft.setTextColor(WHITE);
00093         tft.setTextSize(1);
00094         tft.println("Hello World!\n");
00095         wait_ms(500);
00096         tft.setTextColor(BLUE);
00097         tft.setTextSize(1);
00098         tft.println("1234.567\n");
00099         wait_ms(500);
00100 
00101         tft.setRotation(tft.getRotation()+1);
00102     }
00103 }
00104 
00105 
00106 void testtext(uint16_t color) {
00107     tft.fillScreen(BLACK);
00108     tft.setCursor(0, 0);
00109     tft.setTextColor(color);
00110     tft.setTextSize(1);
00111     tft.drawString(0, 20, "Hello aitendo!", color);
00112     wait_ms(500);
00113     tft.drawChar(0, 50, '1', YELLOW, 1);
00114     tft.write('A');
00115     //wait_ms(500);
00116     tft.setCursor(0, 100);
00117     tft.println("Hello aitendo!\n");
00118     //wait_ms(500);
00119     tft.setCursor(0, 150);
00120     tft.setTextSize(3);
00121     tft.println("1234.56");
00122     //wait_ms(500);
00123     tft.setCursor(0, 200);
00124     tft.setTextSize(3);
00125     sprintf(str, "%X", 0xDEADBEEF);
00126     tft.println(str);
00127     wait_ms(500);
00128 }
00129 
00130 void testFillRoundRect() {
00131   tft.fillScreen(RED);
00132   
00133   for (uint16_t x=tft.width(); x > 20 ; x-=6) {
00134     tft.fillRoundRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, x/8,  tft.Color565(0, x, 0));
00135  }
00136 }
00137 
00138 void testtriangles() {
00139   tft.fillScreen(BLACK);
00140   for (uint16_t i=0; i<tft.width()/2; i+=5) {
00141     tft.drawTriangle(tft.width()/2, tft.height()/2-i,
00142                      tft.width()/2-i, tft.height()/2+i,
00143                      tft.width()/2+i, tft.height()/2+i, tft.Color565(0, 0, i));
00144   }
00145   tft.fillScreen(BLACK);
00146   for (uint16_t i=0; i<tft.width()/2; i+=5) {
00147     tft.drawTriangle(tft.width()/2, tft.height()/2-i,
00148                      tft.width()/2-i, tft.height()/2+i,
00149                      tft.width()/2+i, tft.height()/2+i, tft.Color565(0, i, 0));
00150   }
00151   tft.fillScreen(BLACK);
00152   for (uint16_t i=0; i<tft.width()/2; i+=5) {
00153     tft.drawTriangle(tft.width()/2, tft.height()/2-i,
00154                      tft.width()/2-i, tft.height()/2+i,
00155                      tft.width()/2+i, tft.height()/2+i, tft.Color565(i, 0, 0));
00156   }
00157 }
00158 
00159 void testfilltriangles() {
00160   tft.fillScreen(BLACK);
00161   
00162   for (uint16_t i=tft.width()/2; i>10; i-=5) {
00163     tft.fillTriangle(tft.width()/2, tft.height()/2-i,
00164                      tft.width()/2-i, tft.height()/2+i,
00165                      tft.width()/2+i, tft.height()/2+i, 
00166                      tft.Color565(0, i, i));
00167     tft.drawTriangle(tft.width()/2, tft.height()/2-i,
00168                      tft.width()/2-i, tft.height()/2+i,
00169                      tft.width()/2+i, tft.height()/2+i, tft.Color565(i, i, 0));    
00170   }
00171 }
00172 void testfillcircles(uint8_t radius, uint16_t color) {
00173   for (uint16_t x=radius; x < tft.width(); x+=radius*2) {
00174     for (uint16_t y=radius; y < tft.height(); y+=radius*2) {
00175       tft.fillCircle(x, y, radius, color);
00176     }
00177   }  
00178 }
00179 
00180 void testdrawcircles(uint8_t radius, uint16_t color) {
00181   for (uint16_t x=0; x < tft.width()+radius; x+=radius*2) {
00182     for (uint16_t y=0; y < tft.height()+radius; y+=radius*2) {
00183       tft.drawCircle(x, y, radius, color);
00184     }
00185   }  
00186 }
00187 
00188 void testfillrects(uint16_t color1, uint16_t color2) {
00189  tft.fillScreen(BLACK);
00190  for (uint16_t x=tft.width()-1; x > 6; x-=6) {
00191    //Serial.println(x, DEC);
00192    tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
00193    tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
00194  }
00195 }
00196 
00197 void testdrawrects(uint16_t color) {
00198  tft.fillScreen(BLACK);
00199  for (uint16_t x=0; x < tft.width(); x+=6) {
00200    //pc.printf("%d\r\n", x);
00201    tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
00202  }
00203 }
00204 
00205 void testfastlines(uint16_t color1, uint16_t color2) {
00206    tft.fillScreen(BLACK);
00207    for (uint16_t y=0; y < tft.height(); y+=5) {
00208    //  pc.printf("%d\r\n", y);
00209      tft.drawHorizontalLine(0, y, tft.width(), color1);
00210    }
00211    for (uint16_t x=0; x < tft.width(); x+=5) {
00212    //  pc.printf("%d\r\n", x);
00213      tft.drawVerticalLine(x, 0, tft.height(), color2);
00214    }
00215   
00216 }
00217 
00218 
00219 void testlinerotate() {
00220     uint16_t color1;
00221     uint16_t color2;
00222     uint8_t n;
00223     for (uint8_t i=0; i<4; i++) {
00224         tft.fillScreen(BLACK);
00225         n = 3+i*4;
00226         tft.setCursor(0, 10);
00227         tft.setTextSize(1);
00228         color1 = tft.Color565(n, 0, 0);
00229         color2 = tft.Color565(0, 0, n);
00230         color1 = BLUE;
00231         color2 = RED;
00232         sprintf(str, "%X\n", color1);
00233         tft.println(str);
00234         sprintf(str, "%X\n", color2);
00235         tft.println(str);
00236         for (uint16_t y=0; y < tft.height(); y+=5) {
00237             tft.drawHorizontalLine(0, y, tft.width(), color1);
00238             //pc.printf("y=%d  %d\n", y, tft.height());
00239         }
00240         //wait_ms(500);
00241         for (uint16_t x=0; x < tft.width(); x+=5) {
00242             tft.drawVerticalLine(x, 0, tft.height(), color2);
00243             //pc.printf("x=%d  %d\n", x, tft.height());
00244         }
00245         tft.setRotation(tft.getRotation()+1);
00246         //wait_ms(500);
00247     }
00248 }
00249 
00250 void testlines(uint16_t color) {
00251    tft.fillScreen(BLACK);
00252    for (uint16_t x=0; x < tft.width(); x+=6) {
00253      tft.drawLine(0, 0, x, tft.height()-1, color);
00254    }
00255    for (uint16_t y=0; y < tft.height(); y+=6) {
00256      tft.drawLine(0, 0, tft.width()-1, y, color);
00257    }
00258    
00259    tft.fillScreen(BLACK);
00260    for (uint16_t x=0; x < tft.width(); x+=6) {
00261      tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
00262    }
00263    for (uint16_t y=0; y < tft.height(); y+=6) {
00264      tft.drawLine(tft.width()-1, 0, 0, y, color);
00265    }
00266    
00267    tft.fillScreen(BLACK);
00268    for (uint16_t x=0; x < tft.width(); x+=6) {
00269      tft.drawLine(0, tft.height()-1, x, 0, color);
00270    }
00271    for (uint16_t y=0; y < tft.height(); y+=6) {
00272      tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
00273    }
00274 
00275    tft.fillScreen(BLACK);
00276    for (uint16_t x=0; x < tft.width(); x+=6) {
00277      tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
00278    }
00279    for (uint16_t y=0; y < tft.height(); y+=6) {
00280      tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
00281    }
00282 }
00283 
00284 void testBars() {
00285   tft.goHome();
00286 
00287   uint16_t i,j;
00288   for(i=0; i < tft.height(); i++)
00289   {
00290     for(j=tft.width()/2; j <tft.width(); j++)
00291     {
00292       if(i>279) tft.writeData(WHITE);
00293       else if(i>239) tft.writeData(BLUE);
00294       else if(i>199) tft.writeData(GREEN);
00295       else if(i>159) tft.writeData(CYAN);
00296       else if(i>119) tft.writeData(RED);
00297       else if(i>79) tft.writeData(MAGENTA);
00298       else if(i>39) tft.writeData(YELLOW);
00299       else tft.writeData(BLACK);
00300       //wait_ms(5);
00301     }
00302     for(j=0; j < tft.width()/2; j++)
00303     {
00304       if(i>159) tft.writeData(BLACK);
00305       //else if(i>239) tft.writeData(YELLOW);
00306       //else if(i>199) tft.writeData(MAGENTA);
00307       //else if(i>159) tft.writeData(RED);
00308       //else if(i>119) tft.writeData(CYAN);
00309       //else if(i>79) tft.writeData(GREEN);
00310       //else if(i>39) tft.writeData(BLUE);
00311       else tft.writeData(WHITE);
00312       //wait_ms(1);
00313     }
00314   }
00315 }
00316 
00317 /**
00318 
00319 
00320 **/
00321 
00322 // information we extract about the bitmap file
00323 int bmpWidth, bmpHeight;
00324 uint8_t bmpDepth, bmpImageoffset;
00325 
00326 #define ROTATION 3
00327 #define BUFFPIXEL 60
00328 #define BYTES_PER_PIXEL 3
00329 
00330 uint8_t picBuffer[BYTES_PER_PIXEL * BUFFPIXEL];  // 3 * pixels to buffer
00331 int bufferIndex = BYTES_PER_PIXEL * BUFFPIXEL;
00332 
00333 char path[80];
00334 
00335 // These read data from the SD card file and convert them to big endian 
00336 // (the data is stored in little endian format!)
00337 
00338 
00339 // LITTLE ENDIAN!
00340 
00341 uint16_t read16(FILE *f) {
00342   uint16_t d;
00343   uint8_t b;
00344   fread(&b, sizeof(uint8_t), 1, f);
00345   fread(&d, sizeof(uint8_t), 1, f);
00346   d <<= 8;
00347   d |= b;
00348   return d;
00349 }
00350 
00351 
00352 // LITTLE ENDIAN!
00353 uint32_t read32(FILE *f) {
00354   uint32_t d;
00355   uint16_t b;
00356  
00357   b = read16(f);
00358   d = read16(f);
00359   d <<= 16;
00360   d |= b;
00361   return d;
00362 }
00363 
00364 /*
00365   From example code.  Reads bitmap header.  Uses global variables.
00366 
00367 */
00368 bool bmpReadHeader(FILE *f) {
00369    // read header
00370   uint32_t tmp;
00371   
00372   if (read16(f) != 0x4D42) {
00373     // magic bytes missing
00374     return false;
00375   }
00376  
00377   // read file size
00378   tmp = read32(f);  
00379   pc.printf("size 0x %X\n", tmp);
00380   
00381   // read and ignore creator bytes
00382   read32(f);
00383   
00384   bmpImageoffset = read32(f);  
00385   pc.printf("offset %d\n", bmpImageoffset);
00386   
00387   // read DIB header
00388   tmp = read32(f);
00389   pc.printf("header size %d\n", tmp);
00390   bmpWidth = read32(f);
00391   bmpHeight = read32(f);
00392 
00393   
00394   if (read16(f) != 1)
00395     return false;
00396     
00397   bmpDepth = read16(f);
00398   pc.printf("bitdepth %d\n", bmpDepth);
00399 
00400 
00401   pc.printf("compression %d\n", tmp);
00402 
00403   return true;
00404 }
00405 
00406 /*
00407  Slightly modified routine for drawing bitmaps (From original samples).
00408  
00409  Watch out, as this one uses some global variables
00410 */
00411 void bmpdraw(FILE *f, int x, int y) {
00412   fseek(f, bmpImageoffset, SEEK_SET);
00413     
00414   uint16_t p;
00415   int i, j;
00416  
00417   pc.printf("rotation = %d\n", tft.getRotation());
00418   
00419   tft.goTo(0,0);
00420   for (i=0; i< bmpHeight; i++) {
00421     // bitmaps are stored with the BOTTOM line first so we have to move 'up'
00422     tft.goTo(0, bmpHeight - i - 1);
00423 
00424     for (j=0; j<bmpWidth; j++) {
00425       // read more pixels
00426 
00427       if (bufferIndex >= BYTES_PER_PIXEL * BUFFPIXEL) {
00428         fread(&picBuffer, sizeof(uint8_t), BYTES_PER_PIXEL * BUFFPIXEL, f);
00429         bufferIndex= 0;
00430       }
00431       
00432       p = tft.Color565( picBuffer[bufferIndex+2], picBuffer[bufferIndex+1], picBuffer[bufferIndex]);
00433       bufferIndex += 3;
00434 
00435       tft.writeData(p);
00436     }
00437   }
00438 }
00439 
00440 
00441 FILE *fp;
00442 
00443 main()
00444 {
00445     wait(1);
00446 
00447     #if defined TARGET_NUCLEO_F103RB
00448     //SystemClock_Config_INT();
00449     #endif
00450     //char bmpFile[80];
00451     DIR *dir;
00452 
00453     tft.reset();
00454     
00455     // find the TFT display
00456     uint16_t identifier = tft.readRegister(0x0);
00457     if (identifier == 0x4747) {
00458         pc.printf("Found HX8347D\n");
00459     } else {
00460         pc.printf("Unknown driver chip %X\n",identifier);
00461         while (1);
00462     }  
00463     
00464     tft.initDisplay();
00465     
00466     pc.printf("Initializing SD card...\n");
00467     
00468     dir = opendir("/sd");
00469     if ( dir == NULL ) {
00470         pc.printf("failed!\n");
00471         while(1);
00472     }
00473     closedir(dir);
00474     pc.printf("SD OK!\n");
00475     
00476     fp = fopen("/sd/woof.bmp", "r");
00477     if (fp==NULL) {
00478         pc.printf("didnt find image\n");
00479         while (1);
00480     }
00481 
00482     if (! bmpReadHeader(fp)) { 
00483         pc.printf("bad bmp\n");
00484         while(1);
00485     }
00486 
00487     pc.printf("image size %d, %d\n", bmpWidth, bmpHeight);
00488     
00489     bmpdraw(fp, 0, 0);
00490     wait(1);
00491     fclose(fp);
00492     
00493     fp = fopen("/sd/miniwoof.bmp", "r");
00494     if (fp==NULL) {
00495         pc.printf("didnt find image\n");
00496         while (1);
00497     }
00498 
00499     if (! bmpReadHeader(fp)) { 
00500         pc.printf("bad bmp\n");
00501         while(1);
00502     }
00503     pc.printf("image size %d, %d\n", bmpWidth, bmpHeight);
00504 
00505     //LCD_Init();
00506     tft.initDisplay();
00507     tft.fillScreen(BLACK);
00508     wait_ms(1);
00509     while(1)
00510     {
00511         tft.setRotation(0);
00512         tft.fillScreen(0);
00513         bmpdraw(fp, 50, 50);
00514         wait(1);
00515         
00516         tft.setRotation(1);
00517         tft.fillScreen(0);
00518         bmpdraw(fp, 50, 50);
00519         wait(1);
00520         
00521         tft.setRotation(2);
00522         tft.fillScreen(0);
00523         bmpdraw(fp, 50, 50);
00524         wait(1);
00525         
00526         tft.setRotation(3);
00527         tft.fillScreen(0);
00528         bmpdraw(fp, 50, 50);
00529         wait(1);
00530         tft.setRotation(0);
00531         rotateText();
00532         //rotateTest();
00533 
00534         testlinerotate();
00535 
00536         testtext(WHITE);
00537 
00538 
00539         testBars();
00540         testfastlines(CYAN, MAGENTA);
00541         testtriangles();
00542         testfilltriangles();
00543         testfillcircles(50, RED);
00544         tft.fillScreen(BLACK);
00545         testdrawcircles(10, YELLOW);
00546         testdrawrects(GREEN);
00547         testlines(BLUE);
00548         testfillrects(RED, WHITE);
00549         testFillRoundRect();
00550     
00551         testdrawrects(GREEN);
00552 
00553         tft.DispColor(RED);     //RED
00554         wait_us(10);
00555     
00556         tft.DispColor(GREEN);   //GREEN
00557         wait_us(10);
00558     
00559         tft.DispColor(BLUE);//BLUE
00560         wait_us(10);
00561     
00562         tft.DispColor(WHITE);   //WHITE        
00563 
00564     }
00565 }