// SD structure read/write test
// Modified Arduino SD Library example sketch: ReadWrite
// Borrows structure definitions/functions from TeensyNet.ino v0.0.29
// Yuri - 03/21/14
#include <SD.h>
File myFile;
const int chipSelect = 4; // Teensy3 w/WIZ820io+SD adapter: pin 4
const uint8_t chipAddrSize = 8; // 64bit OneWire Address
const uint8_t chipNameSize = 16;
const uint8_t maxChips = 36; // Maximum number of Chips
typedef struct
{
uint8_t chipAddr[chipAddrSize];
int16_t chipStatus;
uint32_t tempTimer;
char chipName[chipNameSize+1];
}chipStruct;
chipStruct chip[maxChips] =
{
{ {0,0,0,0,0,0,0,0}, 0, 0, "Stuff\0 " },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {1,0,1,0,1,0,1,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" },
{ {0,0,0,0,0,0,0,0}, 0, 0, "" }
};
chipStruct sdOutput[maxChips];
void displayStructure(byte *addr, int xSize, int ySize)
{
int x, y;
Serial.print(F("0x"));
Serial.print((uint32_t)addr, HEX);
Serial.print(F(": "));
for(x = 0, y = 0; x < xSize; x++)
{
if(addr[x] >=0 && addr[x] <= 15)
{
Serial.print(F("0x0"));
}else{
Serial.print(F("0x"));
}
Serial.print(addr[x], HEX);
y++;
if(y < ySize)
{
Serial.print(F(", "));
}else{
y = 0;
Serial.println();
Serial.print(F("0x"));
Serial.print((uint32_t)addr + x + 1, HEX);
Serial.print(F(": "));
}
}
Serial.println();
Serial.println();
}
void setup()
{
Serial.begin(115200);
delay(5000); // wait 5 seconds to allow for opening serial monitor
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("test.txt")) {
Serial.println("test.txt exists.");
// delete the file:
Serial.println("Removing test.txt...");
SD.remove("test.txt");
}
else {
Serial.println("test.txt doesn't exist.");
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
//myFile.println("testing 1, 2, 3.");
myFile.write((byte *) &chip, sizeof(chip)); // byte-wise write of structure
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Structure from test.txt:");
myFile.read((byte *) &sdOutput, sizeof(sdOutput)); // byte-wise read of structure
// serial output of structure from file
displayStructure((byte *)(uint32_t) &sdOutput, sizeof(sdOutput), sizeof(chipStruct));
Serial.print( (sizeof(chipStruct) / sizeof(byte) ) * maxChips);
Serial.println(F(" bytes in chip structure array"));
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}