Greetings fellow tinkerers!
I'm having a doozie of an issue with a Dumilenove w/328p -- on Arduino IDE 1.0.1, using default SD library. Also included is the FAST_SPI library for outputting the data to another bus once its been read from the card. The result is the SAME with and without the FAST_SPI library/code.
EDIT the problem is mostly solved, but evolved into a completely different glitch! See below for new version/problem!
Power: I've tried with/without external power, same result.
Sketch: Loading from SD card a bitmap file, 64 pixels wide by 100 pixels tall. Trick is I only load 1 line of it at a time because I have so little ram to work with. During testing I'm spitting that array out the Serial.print.
I think I may be running out of RAM and having a pointer go haywire somewhere in the background, but I can't debug to that level to understand.
The attached 'output' from the serial is a short version. If I let it run long enough it either stops (fully crashes) or reads, but the 'for' loop in the main loop exceeds the original height (<100) and starts asking for row ++ forever.
Attached is the bitmap file, the code, and the output.
#include <FastSPI_LED.h>
#include <SD.h>
File myFile;
const int chipSelect = 4;
String fileNam = "3.bmp";
unsigned int height = 1;
unsigned long bitmapOffset = 0x36;
unsigned long filePosition = 0;
#define NUM_LEDS 64
int frameDelay = 1; //number of millis between animated frames
struct CRGB {
unsigned char r;
unsigned char g;
unsigned char b;
};
struct CRGB *leds; //I don't know what this does.
unsigned long CurTime = millis();
void setup()
{
randomSeed(analogRead(0));
Serial.begin(9600);
//Serial.println("Start");
//Serial.print(CurTime);
fastSPIsetup(); //make the fastSPI library do its magic
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
myFile = SD.open("3.bmp", FILE_READ);
Serial.print("BitmapOffsetStart:");
Serial.println(bitmapOffset,HEX);
myFile.seek(0x16);
height = myFile.read();
myFile.seek(0xA);
bitmapOffset = myFile.read();
myFile.close();
Serial.print("BitmapOffset:");
Serial.println(bitmapOffset,HEX);
Serial.print("Height:");
Serial.println(height);
for(int j=0; j<height;j++) //loop through each line in the file, then spit it out.
{
FSPIlineOut(j); //reads a line and displays
delay(frameDelay);
}
} //end void loop
void FSPIlineOut(unsigned int lineNo)
{
myFile = SD.open("3.bmp", FILE_READ);
//delay(100);
filePosition = bitmapOffset;
filePosition += (lineNo *(NUM_LEDS * 3) );
myFile.seek(filePosition);//get to data
delay(100);
//memset(leds, 0, NUM_LEDS * 3);//blank the array
Serial.print(lineNo);
Serial.print(":");
Serial.print(filePosition,HEX);
Serial.print(":");
Serial.print(myFile.position(),HEX);
Serial.print(": ");
for(int i=0; i < NUM_LEDS; i++)
{
leds[i].b=myFile.read();
leds[i].g=myFile.read();
leds[i].r=myFile.read();
Serial.print(leds[i].r,HEX);
Serial.print(",");
}
Serial.println();
if (!myFile){
Serial.println("the card broke");
//myFile.close();
}//FastSPI_LED.show(); // write all the pixels out
//delayMicroseconds(4);//to remove g-glitch (can be as low as 4)
drawArray();
//delay(frameDelay);//some actual display time
}
void drawArray() { //take the current pixel array, dump it to the leds, and shiftout
//memset(leds, 0, NUM_LEDS * 3); //clear the led array
//PORTD |= B00010000;// Set bit high
//FastSPI_LED.show(); // shift out the array.
//PORTD &= ~B00010000;// Set bit low - happens a bit fast for the NAND so above delay is required to de-glitch it
//delay(1);
}
void fastSPIsetup() //gets the fastspi library set up
{
FastSPI_LED.setLeds(NUM_LEDS);
FastSPI_LED.setChipset(CFastSPI_LED::SPI_WS2801);
FastSPI_LED.setDataRate(3); //make the library work with 5v ws2801 strips
FastSPI_LED.init();
FastSPI_LED.start();
leds = (struct CRGB*)FastSPI_LED.getRGBData();
}
**^^above, note how it loads the file, seeks to the correct byte loaded from the bitmap header (0x36 is the position to find the first byte of the data, read 3 bytes per pixel until the end). **
The serial output is below:
The line after 'Height:100' is where the data is actually spilling out.
The left columns are 'line of the bitmap file read from the bottom': "address of the start of this line byte requested", and "address actually being read"
The first 16 lines (0-15) are correct, the remainder (ad nauseum) are broken...not reading anything of value, and spitting out -1 (no data found)
Particular interest is bitmap line 0: everything is correct, just the 'red' values are displayed, also, the other good read lines are a vertical slant, also read properly.
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