kirash4

bmpRead

Feb 19th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <SdFat.h>
  2. #include "FastLED.h"
  3.  
  4. const uint8_t      SdChipSelect     = SS;
  5.  
  6. SdFile bmpFile;
  7. SdFat sd;
  8.  
  9. #define NUM_LEDS 48
  10. #define DAT_PIN 11
  11. #define CLK_PIN 12
  12. struct CRGB leds[NUM_LEDS];
  13.  
  14. void setup() {
  15.   Serial.begin(57600);
  16.  
  17.   LEDS.addLeds<LPD8806, DAT_PIN, CLK_PIN, BGR>(leds, NUM_LEDS);
  18.   fill_solid(&(leds[0]), 48, CRGB(0, 0, 0));
  19.  
  20.   if (!sd.begin(SdChipSelect, SPI_FULL_SPEED)) {
  21.     Serial.println("Can't open SD card!");
  22.     while(0);
  23.   }
  24. }
  25.  
  26. void loop()
  27. {
  28.   // really need to do error checking here
  29.   // but, I digress, this was for testing only
  30.   bmpRead("star.bmp");
  31. }
  32.  
  33. void bmpRead(char *filename) {
  34.   SdFile bmpFile;
  35.   int bmpWidth, bmpHeight;
  36.   uint8_t bmpDepth;
  37.   uint8_t bmpImgOffset;
  38.   uint32_t rowSize;
  39.   boolean goodBmp = false;
  40.   boolean flip = true;
  41.   int row, col;  // col unused
  42.   uint32_t pos = 0;
  43.  
  44.  
  45.   if ((bmpFile.open(filename, O_READ)) == NULL) {
  46.     Serial.println("Can't find image file.");
  47.     return;
  48.   }
  49.  
  50.   // Parse BMP header
  51.   if(read16(bmpFile) == 0x4D42) { // BMP signature
  52.     Serial.print("File size: "); Serial.println(read32(bmpFile));
  53.     (void)read32(bmpFile); // Read & ignore creator bytes
  54.     bmpImgOffset = read32(bmpFile); // Start of image data
  55.     Serial.print("Image Offset: "); Serial.println(bmpImgOffset, DEC);
  56.     // Read DIB header
  57.     Serial.print("Header size: "); Serial.println(read32(bmpFile));
  58.     bmpWidth = read32(bmpFile);
  59.     bmpHeight = read32(bmpFile);
  60.     if(read16(bmpFile) == 1) { // # planes -- must be '1'
  61.       bmpDepth = read16(bmpFile); // bits per pixel
  62.       Serial.print("Bit Depth: "); Serial.println(bmpDepth);
  63.       if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  64.  
  65.         goodBmp = true; // Supported BMP format -- proceed!
  66.         Serial.print("Image size: "); Serial.print(bmpWidth);
  67.         Serial.print('x'); Serial.println(bmpHeight);
  68.  
  69.         // BMP rows are padded (if needed) to 4-byte boundary
  70.         rowSize = (bmpWidth * 3 + 3) & ~3;
  71.  
  72.         // If bmpHeight is negative, image is in top-down order.
  73.         // This is not canon but has been observed in the wild.
  74.         if(bmpHeight < 0) {
  75.           bmpHeight = -bmpHeight;
  76.           flip = false;
  77.         }
  78.  
  79.         // spit image out to leds array
  80.         for (row = 0; row < bmpHeight; row++) {
  81.           if (flip) { // normal BMP, stored bottom-to-top
  82.             pos = bmpImgOffset + (bmpHeight - 1 - row) * rowSize;
  83.           } else {    // drunk BMP, stored top-to-bottom
  84.             pos = bmpImgOffset + row * rowSize;
  85.           }
  86.           bmpFile.seekSet(pos);
  87.           bmpFile.read((char*)leds, NUM_LEDS * 3);
  88.           LEDS.show();
  89.           delay(100);  // slowed way down so I can see what it's
  90.                        // displaying without needing to constantly
  91.                        // be waving the thing like crazy.
  92.         }
  93.       } // end goodBmp
  94.     }
  95.   }
  96.  
  97.   bmpFile.close();
  98.   if(!goodBmp) Serial.println("BMP format not recognized.");
  99. }
  100.  
  101. // These read 16- and 32-bit types from the SD card file.
  102. // BMP data is stored little-endian, Arduino is little-endian too.
  103. // May need to reverse subscript order if porting elsewhere.
  104.  
  105. uint16_t read16(SdFile f) {
  106.   uint16_t result;
  107.   ((uint8_t *)&result)[0] = f.read(); // LSB
  108.   ((uint8_t *)&result)[1] = f.read(); // MSB
  109.   Serial.print("read16() result: ");
  110.   Serial.println(result, HEX);
  111.   return result;
  112. }
  113.  
  114. uint32_t read32(SdFile f) {
  115.   uint32_t result;
  116.   ((uint8_t *)&result)[0] = f.read(); // LSB
  117.   ((uint8_t *)&result)[1] = f.read();
  118.   ((uint8_t *)&result)[2] = f.read();
  119.   ((uint8_t *)&result)[3] = f.read(); // MSB
  120.   Serial.print("read32() result: ");
  121.   Serial.println(result, HEX);
  122.   return result;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment