Advertisement
learnelectronics

Digital Picture Frame

Jun 5th, 2017
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.37 KB | None | 0 0
  1. /*
  2.  * Arduino Digital Picture Frame
  3.  *
  4.  * learnelectronics
  5.  * 4 JUN 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9.  
  10. #include <Adafruit_GFX.h>    // Core graphics library
  11. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  12. #include <SD.h>
  13. #include <SPI.h>
  14.  
  15.  
  16. #define LCD_CS A3           // Chip Select goes to Analog 3
  17. #define LCD_CD A2           // Command/Data goes to Analog 2
  18. #define LCD_WR A1           // LCD Write goes to Analog 1
  19. #define LCD_RD A0           // LCD Read goes to Analog 0
  20. #define PIN_SD_CS 10        // Adafruit SD shields and modules: pin 10
  21.  
  22. #define LCD_RESET A4        // Can alternately just connect to Arduino's reset pin
  23.  
  24.  
  25.                             // Assign human-readable names to some common 16-bit color values:
  26. #define BLACK   0x0000
  27. #define BLUE    0x001F
  28. #define RED     0xF800
  29. #define GREEN   0x07E0
  30. #define CYAN    0x07FF
  31. #define MAGENTA 0xF81F
  32. #define YELLOW  0xFFE0
  33. #define WHITE   0xFFFF
  34.  
  35. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  36.  
  37.  
  38. #define MAX_BMP         10                      // bmp file num
  39. #define FILENAME_LEN    20                      // max file name length
  40.  
  41. const int __Gnbmp_height = 320;                 // bmp hight
  42. const int __Gnbmp_width  = 240;                 // bmp width
  43.  
  44. unsigned char __Gnbmp_image_offset  = 0;        // offset
  45.  
  46. int __Gnfile_num = 4;                           // num of file
  47.  
  48. char __Gsbmp_files[4][FILENAME_LEN] =           // add file name here
  49. {
  50. "flower.bmp",
  51. "tiger.bmp",
  52. "tree.bmp",
  53. "RedRose.bmp",
  54. };
  55. File bmpFile;
  56.  
  57. /*********************************************/
  58. // This procedure reads a bitmap and draws it to the screen
  59. // its sped up by reading many pixels worth of data at a time
  60. // instead of just one pixel at a time. increading the buffer takes
  61. // more RAM but makes the drawing a little faster. 20 pixels' worth
  62. // is probably a good place
  63.  
  64. #define BUFFPIXEL       60                      // must be a divisor of 240
  65. #define BUFFPIXEL_X3    180                     // BUFFPIXELx3
  66.  
  67. void bmpdraw(File f, int x, int y)
  68. {
  69.     bmpFile.seek(__Gnbmp_image_offset);
  70.  
  71.     uint32_t time = millis();
  72.  
  73.     uint8_t sdbuffer[BUFFPIXEL_X3];                 // 3 * pixels to buffer
  74.  
  75.     for (int i=0; i< __Gnbmp_height; i++) {
  76.         for(int j=0; j<(240/BUFFPIXEL); j++) {
  77.             bmpFile.read(sdbuffer, BUFFPIXEL_X3);
  78.            
  79.             uint8_t buffidx = 0;
  80.             int offset_x = j*BUFFPIXEL;
  81.             unsigned int __color[BUFFPIXEL];
  82.            
  83.             for(int k=0; k<BUFFPIXEL; k++) {
  84.                 __color[k] = sdbuffer[buffidx+2]>>3;                        // read
  85.                 __color[k] = __color[k]<<6 | (sdbuffer[buffidx+1]>>2);      // green
  86.                 __color[k] = __color[k]<<5 | (sdbuffer[buffidx+0]>>3);      // blue
  87.                
  88.                 buffidx += 3;
  89.             }
  90.  
  91.         for (int m = 0; m < BUFFPIXEL; m ++) {
  92.               tft.drawPixel(m+offset_x, i,__color[m]);
  93.         }
  94.         }
  95.     }
  96.    
  97.     Serial.print(millis() - time, DEC);
  98.     Serial.println(" ms");
  99. }
  100.  
  101. boolean bmpReadHeader(File f)
  102. {
  103.     // read header
  104.     uint32_t tmp;
  105.     uint8_t bmpDepth;
  106.    
  107.     if (read16(f) != 0x4D42) {
  108.         // magic bytes missing
  109.         return false;
  110.     }
  111.  
  112.     // read file size
  113.     tmp = read32(f);
  114.     Serial.print("size 0x");
  115.     Serial.println(tmp, HEX);
  116.  
  117.     // read and ignore creator bytes
  118.     read32(f);
  119.  
  120.     __Gnbmp_image_offset = read32(f);
  121.     Serial.print("offset ");
  122.     Serial.println(__Gnbmp_image_offset, DEC);
  123.  
  124.     // read DIB header
  125.     tmp = read32(f);
  126.     Serial.print("header size ");
  127.     Serial.println(tmp, DEC);
  128.    
  129.     int bmp_width = read32(f);
  130.     int bmp_height = read32(f);
  131.    
  132.     if(bmp_width != __Gnbmp_width || bmp_height != __Gnbmp_height)  {    // if image is not 320x240, return false
  133.         return false;
  134.     }
  135.  
  136.     if (read16(f) != 1)
  137.     return false;
  138.  
  139.     bmpDepth = read16(f);
  140.     Serial.print("bitdepth ");
  141.     Serial.println(bmpDepth, DEC);
  142.  
  143.     if (read32(f) != 0) {
  144.         // compression not supported!
  145.         return false;
  146.     }
  147.  
  148.     Serial.print("compression ");
  149.     Serial.println(tmp, DEC);
  150.  
  151.     return true;
  152. }
  153.  
  154. /*********************************************/
  155. // These read data from the SD card file and convert them to big endian
  156. // (the data is stored in little endian format!)
  157.  
  158. // LITTLE ENDIAN!
  159. uint16_t read16(File f)
  160. {
  161.     uint16_t d;
  162.     uint8_t b;
  163.     b = f.read();
  164.     d = f.read();
  165.     d <<= 8;
  166.     d |= b;
  167.     return d;
  168. }
  169.  
  170. // LITTLE ENDIAN!
  171. uint32_t read32(File f)
  172. {
  173.     uint32_t d;
  174.     uint16_t b;
  175.  
  176.     b = read16(f);
  177.     d = read16(f);
  178.     d <<= 16;
  179.     d |= b;
  180.     return d;
  181. }
  182.  
  183. void setup(void) {
  184.   Serial.begin(9600);
  185.   Serial.println(F("TFT LCD test"));
  186.  
  187. #ifdef USE_ADAFRUIT_SHIELD_PINOUT
  188.   Serial.println(F("Using Adafruit 2.4\" TFT Arduino Shield Pinout"));
  189. #else
  190.   Serial.println(F("Using Adafruit 2.4\" TFT Breakout Board Pinout"));
  191. #endif
  192.  
  193.   Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
  194.  
  195.   tft.reset();
  196.  
  197.   uint16_t identifier = tft.readID();
  198.    
  199.   if(identifier == 0x9325) {
  200.     Serial.println(F("Found ILI9325 LCD driver"));
  201.   } else if(identifier == 0x9328) {
  202.     Serial.println(F("Found ILI9328 LCD driver"));
  203.   } else if(identifier == 0x4535) {
  204.     Serial.println(F("Found LGDP4535 LCD driver"));
  205.   }else if(identifier == 0x7575) {
  206.     Serial.println(F("Found HX8347G LCD driver"));
  207.   } else if(identifier == 0x9341) {
  208.     Serial.println(F("Found ILI9341 LCD driver"));
  209.   } else if(identifier == 0x8357) {
  210.     Serial.println(F("Found HX8357D LCD driver"));
  211.   } else if(identifier==0x0101)
  212.   {    
  213.       identifier=0x9341;
  214.        Serial.println(F("Found 0x9341 LCD driver"));
  215.   }else {
  216.     Serial.print(F("Unknown LCD driver chip: "));
  217.     Serial.println(identifier, HEX);
  218.     Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
  219.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  220.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  221.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  222.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  223.     Serial.println(F("matches the tutorial."));
  224.     identifier=0x9341;
  225.    
  226.   }
  227.  
  228.   tft.begin(identifier);
  229.   tft.fillScreen(BLUE);
  230.  
  231.  
  232.  
  233.   //Init SD_Card
  234.   pinMode(10, OUTPUT);
  235.    
  236.   if (!SD.begin(10)) {
  237.     Serial.println("initialization failed!");
  238.     tft.setCursor(0, 0);
  239.     tft.setTextColor(WHITE);    
  240.     tft.setTextSize(1);
  241.     tft.println("SD Card Init fail.");  
  242.   }else
  243.   Serial.println("initialization done.");
  244. }
  245.  
  246. void loop(void) {
  247.      for(unsigned char i=0; i<__Gnfile_num; i++) {
  248.         bmpFile = SD.open(__Gsbmp_files[i]);
  249.         if (! bmpFile) {
  250.             Serial.println("didnt find image");
  251.             tft.setTextColor(WHITE);    tft.setTextSize(1);
  252.             tft.println("didnt find BMPimage");
  253.             while (1);
  254.         }
  255.    
  256.         if(! bmpReadHeader(bmpFile)) {
  257.             Serial.println("bad bmp");
  258.             tft.setTextColor(WHITE);    tft.setTextSize(1);
  259.             tft.println("bad bmp");
  260.             return;
  261.         }
  262.  
  263.         bmpdraw(bmpFile, 0, 0);
  264.         bmpFile.close();
  265.         delay(1000);
  266.         delay(1000);
  267.     }
  268.    
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement