Skip to content

SPIFFS not loading file correctly #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Kirk-1 opened this issue Feb 16, 2018 · 4 comments
Closed

SPIFFS not loading file correctly #1118

Kirk-1 opened this issue Feb 16, 2018 · 4 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@Kirk-1
Copy link

Kirk-1 commented Feb 16, 2018

Hardware:

Board: ESP32 Dev Module
Core Installation/update date: 16/feb/2018
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 115200

Description:

I am trying to load BMP files onto a display using SPIFFS. It was working a few days ago but then I updated the arduino-esp32 repository and now it seems that when I load the SPIFF's file the header bytes are not where they should be. So instead of 0x4D42 being found in the first 2 bytes I have 0xFFFF.

The files were loaded into SPIFFS directory by placing the in a folder named "data" using the "ESP 32 Sketch data upload" tool.

I have been researching the forums and found that it is possibly and issue with the mkspiffs config file but I am not sure what to do exactly. I am using a MAC.

Any help greatly appreciated as I am about 10 hours in trying to fix this now.

Cheers

Sketch:

#include "FS.h"
#include <SPIFFS.h>
#include <SEPS525_OLED.h>
#include <Adafruit_GFX.h>
#include <SPI.h>

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define col 160
#define row 128

SEPS525_OLED tft;

void setup() {

Serial.begin(115200);
tft.begin();
tft.fillScreen(BLACK);
SPIFFS.begin ();

}

void loop() {
bmpDraw("/160128_0.bmp", 0, 0);
delay(2000);
bmpDraw("/160128_1.bmp", 0, 0);
delay(2000);
}

#define BUFFPIXEL 20

void bmpDraw(char *filename, uint8_t x, uint8_t y) {

File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, _row, _col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();

if((x >= col) || (y >= row)) return;

Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println(''');

// Open requested file on SD card
if ((bmpFile = SPIFFS.open(filename, "r")) == NULL) {
Serial.print("File not found");
return;
}else{if (bmpFile) {
int s = bmpFile.size();
Serial.printf("File Opened , Size=%d\r\n", s);}}
Serial.println(read16(bmpFile), HEX);
Serial.println(read32(bmpFile), HEX);
Serial.println(bmpFile, HEX);

// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: "); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("Header size: "); Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);

    // BMP rows are padded (if needed) to 4-byte boundary
    rowSize = (bmpWidth * 3 + 3) & ~3;
    rowSize = bmpWidth;
    // If bmpHeight is negative, image is in top-down order.
    // This is not canon but has been observed in the wild.
    if(bmpHeight < 0) {
      bmpHeight = -bmpHeight;
      flip      = false;
    }

    // Crop area to be loaded
    w = bmpWidth;
    h = bmpHeight;
    if((x+w-1) >= col)  w = col  - x;
    if((y+h-1) >= row) h = row - y;

    for (_row=0; _row<h; _row++) { // For each scanline...
      //tft.goTo(x, y+_row);

      // Seek to start of scan line.  It might seem labor-
      // intensive to be doing this on every line, but this
      // method covers a lot of gritty details like cropping
      // and scanline padding.  Also, the seek only takes
      // place if the file position actually needs to change
      // (avoids a lot of cluster math in SD library).
      if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
        pos = bmpImageoffset + (bmpHeight - 1 - _row) * rowSize;
      else     // Bitmap is stored top-to-bottom
        pos = bmpImageoffset + _row * rowSize;
      if(bmpFile.position() != pos) { // Need seek?
        bmpFile.seek(pos);
        buffidx = sizeof(sdbuffer); // Force buffer reload
      }

      // optimize by setting pins now
      for (_col=0; _col<w; _col++) { // For each pixel...
        // Time to read more pixel data?
        if (buffidx >= sizeof(sdbuffer)) { // Indeed
          bmpFile.read(sdbuffer, sizeof(sdbuffer));
          buffidx = 0; // Set index to beginning
        }

        // Convert pixel from BMP to TFT format, push to display
        b = sdbuffer[buffidx++];
        g = sdbuffer[buffidx++];
        r = sdbuffer[buffidx++];

        tft.drawPixel(x+_col, y+_row, tft.color565(r,g,b));
        // optimized!
        //tft.pushColor(tft.color565(r,g,b));
      } // end pixel
    } // end scanline
    Serial.print("Loaded in ");
    Serial.print(millis() - startTime);
    Serial.println(" ms");
  } // end goodBmp
}

}

bmpFile.close();
if(!goodBmp) Serial.println("BMP format not recognized.");
}

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB

return result;
}

uint32_t read32(File &f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}

Debug Messages:

No debug messages appearing.
Serial monitor print out:

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:956
load:0x40078000,len:0
load:0x40078000,len:13076
entry 0x40078a58

Loading image '/160128_0.bmp'
File Opened , Size=61496
FFFF
FFFFFFFF
1
BMP format not recognized.

Loading image '/160128_1.bmp'
File Opened , Size=61494
FFFF
FFFFFFFF
1
BMP format not recognized.

@smacyas
Copy link

smacyas commented Feb 19, 2018

It is a bug
see: #1022

@davepl
Copy link

davepl commented Aug 3, 2018

Try without the leading slash on the filename.

@stale
Copy link

stale bot commented Aug 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Aug 1, 2019
@stale
Copy link

stale bot commented Aug 15, 2019

This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

3 participants