Declare file handle before opening file

Hi,

I want to read an ascii image file and display it line by line on a WS2812 strip (it's a kind of 1D movie!). Since there won't be enough memory to load all of the file at once, I tried to write a class that handles the reading for me; it opens the file and stores 1 row) in an array every time i call the nextline() method. Here is a part of my code, it should suffice to hint at how I tried to do it:

class PPMfile 
{
public:
	PPMfile() { rowsleft = 0; };
	~PPMfile() {};
	bool open(char *filename);
	bool nextline();
	void close();
	byte size() const { return cols; }
	byte pixel(unsigned int n, byte rgb) const{ return (n<0||n>cols||rgb<0||rgb>2?0:pixels[n][rgb]); };
	byte operator()(unsigned int n, byte rgb) const { return pixel(n, rgb); };
private:
	File dataFile; // File *dataFile fails as well
	unsigned int rows, cols, rowsleft;
	byte pixels[NUMPIXELS][3]; // this is another workaround - new and malloc don't work as expected
};

My idea didn't compile - and I was able to reduce the problem to a simple sketch:

#include <FileIO.h>

void setup() 
{
  Bridge.begin();
  FileSystem.begin();
  File test;
  test = FileSystem.open("test");
}

void loop() 
{
  // don't do anything
}

The error message is
/home/walter/Dropbox/dev/arduino/arduino-1.5.5_64/libraries/Bridge/src/FileIO.h: In member function ‘File& File::operator=(const File&)’:
/home/walter/Dropbox/dev/arduino/arduino-1.5.5_64/libraries/Bridge/src/FileIO.h:28: error: non-static reference member ‘BridgeClass& File::bridge’, can't use default assignment operator
sketch_test.ino: In function ‘void setup()’:
sketch_test.ino:8: note: synthesized method ‘File& File::operator=(const File&)’ first required here

I know that File test= FileSystem.open("test"); would work, but that's the point: I need to declare File test outside the function/method that FilesSystem.opens the file.

Is there any bugfix/alternative/workaround for this? Am I the only person that wants to read files portion by portion using functions/classes?

Thanks
Walter

Rewrite FileIO for your application (read one line at time).

nano /mnt/sda1/readline.py
#!/usr/bin/python
import sys
import linecache
script,filepath,linenumber = sys.argv
retrieved_line = linecache.getline(filepath, int(linenumber))
print retrieved_line
#print retrieved_line.rstrip("\r\n")
#print retrieved_line.rstrip("\n")
chmod 755 /mnt/sda1/readline.py
nano /mnt/sda1/test.txt
1
2
3
4
5

Testing code:

/mnt/sda1/readline.py  /mnt/sda1/test.txt  4

Arduino code:

 Process p;		// Create a process and call it "p"
  p.begin("/mnt/sda1/readline.py");	// Process that launch the "readline" command
  p.addParameter("/mnt/sda1/test.txt"); // Add the  file path as parameter to readline
  p.addParameter(n);
  p.run();		// Run the process and wait for its termination

  // Print command output on the Serial.
  // A process output can be read with the stream methods
  while (p.available() > 0) {
    char c = p.read();   //one char by one from line n
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();

Loop throughabove code for whole file.

Actually my case is a little more complicated (one row of pixels is more than one line of text) but following the same approach I was able to make it work. Thank you very much!

Rewrite FileIO for your application (read one line at time).

Rather someone (not me - this is way over my head) should rewrite it so that the copy constructor works! :wink: