arduino-libraries / Bridge

Bridge Library for Arduino
http://arduino.cc/
13 stars 28 forks source link

Problem opening bmp file with FileIO on the Arduino Yún SD card to image on TFT screen. #9

Open agdl opened 8 years ago

agdl commented 8 years ago

From @Lalieu on September 23, 2014 12:49

Hi all,

I have been trying to print a .bmp image on the arduino 1.77" TFT screen using the Arduino Yún, but I have some trouble opening the file using the FileIO library of the Yún. Although I can find the .bmp file on the SD card installed on the Yún, and can open it using: FileSystem.open(filename), it is not loaded as an image file properly when using: PImage logo = tft.loadImage(filename), causing logo.isValid() to return false.

Here is how far I got:

System:

With the system I was able to:

So, from this I conclude that the connection between the TFT screen and Yún is fine and the image on the SD card installed in the Yún is accessible using the FileIO library of the Yún.

However, when the .bmp is stored on the SD card in the Yún, the FileIO library is needed to access the file. To display the image, the TFT library is needed, which in turn needs the SD library. The SD and FileIO library interfere with each other, due to double declarations of the File class. The TFT library just uses the Adafruit_ST7735 and Adafruit_GFX libraries. Looking at these libraries it seems that the SD library is only needed to open the file on the SD card: bmpFile = SD.open(fileName) Therefore the PImage class and image handling functions needed to print the image on the screen are only declared when the SD library is loaded (#if defined(_SDH)).

In order to use the Adafruit libraries in combination with the FileIO library instead of the SD library I changed: bmpFile = SD.open(fileName) into: File bmpFile = FileSystem.open(fileName); and #if defined(_SDH) into #if defined(_FILEIOH) in the Adafruit library files and Pimage.h file.

With these modifications the different libraries are able to work with each other, and I can access the image on the SD card in the Yún, while using the Adafruit libraries, which should allow to print the image on the screen using the following code:

#include Bridge.h
#include FileIO.h
#include SPI.h
#include "Adafruit_ST7735.h"
#include "Adafruit_GFX.h"

// pin definitions
#define cs   7
#define dc   5
#define rst  6

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

void setup() {

  tft.initG();
  tft.setRotation(1);

  Bridge.begin();
  Serial.begin(9600);

  tft.fillScreen(ST7735_WHITE);
  tft.setTextColor(ST7735_BLACK);  
  tft.setTextSize(1);

  tft.println("Initializing FileSystem...");
  if (!FileSystem.begin()) {
    tft.println("failed!");
    return;
  }
  tft.println("OK!");

  PImage logo = tft.loadImage("/mnt/sda1/arduino.bmp");
  if (!logo.isValid()) {
    tft.println("error while loading Image");
    return;
  }
  tft.println("Image loaded");
_
  delay(5000);

  tft.fillScreen(ST7735_WHITE);
  tft.image(logo, 0, 0);
}

void loop() {
}

However, logo.isValid() returns false. So I guess the image is loaded differently now I’m using: File bmpFile = FileSystem.open(fileName); instead of: bmpFile = SD.open(fileName); However, until now I haven’t been able to figure out what is going wrong.

Therefore I hope there is someone who knows what is going wrong, or who encountered the problem before and discovered a solution.

Cheers,

Mark

Copied from original issue: arduino/Arduino#2320

agdl commented 8 years ago

From @ffissore on October 9, 2014 9:36

When you load the image, lots of debug info should be printed on the serial monitor. Can you paste them here?

agdl commented 8 years ago

From @Lalieu on October 10, 2014 14:20

When the image is loaded the following is printed to the serial monitor:

File size: 2770445601 Image Offset: 2770445601 Header size: 2770445601 loadImage: invalid n. of planes

any ideas?

agdl commented 8 years ago

From @ffissore on October 10, 2014 14:44

So this is where it's failing: https://github.com/arduino/Arduino/blob/ide-1.5.x/libraries/TFT/src/utility/Adafruit_GFX.h#L340 Thank you, we'll look into it.

agdl commented 8 years ago

From @ffissore on October 10, 2014 14:50

If you have suggestions or fixes, please don't hesitate to share them

agdl commented 8 years ago

From @Lalieu on October 10, 2014 15:3

I don't know if the spot you highlighted in your previous comment (where read16() is called) is the first spot where it is failing. The numbers printed for the File Size, Image Offset and Header Size seem a bit strange to me, being equal. These numbers are generated using read32() (just above your highlighted part), so maybe it already fails there.

I should be able to check the numbers by formatting the SD card, plugging it back into the TFT SD slot and reading the image from the SD card in the TFT SD slot (in which case i was able to print the image on the TFT screen). Hope to check that this evening.

agdl commented 8 years ago

From @sngl on October 13, 2014 16:35

@Lalieu , can you share your modified files? The modified library and the sketch Thanks

agdl commented 8 years ago

From @Lalieu on October 13, 2014 19:13

@sngl , Hey, I don't know if this is the best way to share the files, but I've put the changed files and the sketch here.

agdl commented 8 years ago

From @sngl on October 16, 2014 10:27

Hello @Lalieu , what causes the problem is the method you use to pass the bmpFile to the functions read16 and read32.

The FileIO class updates its internal data after every read or write to remain in sync with the linux-side Bridge library. In your code you are passing the File object by value, so for example when you use f.read in read16 function, you are communicating with the linux-side but you are updating the internal data of the local File object. When you exit from the function the local object is lost and if you call again read16 or read32 you will pass them the old bmpFile object that is not in sync with the Bridge on linux. This causes errors in the communication so the data you read on the Serial monitor is not the data coming from the file. You can see this error because when the TFT library reads the BMP header it works correctly and then tries to read the next sections of the bmp file. After the header reading is competed it exits from the read16 function and then it call again read16 or read32, causing the problem.

The solution is to pass the bmpFile object by reference, so you have to modify the functions in this way uint16_t PImage::read16(File f) ----> uint16_t PImage::read16(File f) and inside the function: ((uint8_t )&result)[0] = f.read(); ----> ((uint8_t *)&result)[0] = f->read();
and so on... and calling it by read16(&bmpFile) instead of read16(bmpFile); Passing the bmpFile by reference to read16 the f->read( ) method will update the original object so when you call different times the read16 and read32 functions it will be always in sync.


Initializing FileSystem... OK! File size: 9482 Image Offset: 122 Header size: 108 Bit Depth: 24 Image size: 52x60

Image loaded

agdl commented 8 years ago

From @Lalieu on October 17, 2014 7:19

@sngl , Thank you for the time and effort! I understand the problem now, and will try the fix this weekend.

agdl commented 8 years ago

From @ld21 on October 18, 2014 22:39

@lalieu: I just implemented what you are trying to do. Check out http://forum.arduino.cc/index.php?topic=268492.0 where I left my sketch for you. Good luck and a lot of fun with your project I wish!

agdl commented 8 years ago

@Lalieu @sngl if the problem has been solved please close the issue!

agdl commented 8 years ago

This issue was moved to arduino-libraries/Bridge#8