Excellent. Thanks, Lefty.
However now I have a further problem. The code is now in an .h tab (and so are various other bits in the project) but now it is trying to tell me I am redefining things that I am defining for the first time.
errors:
In file included from version0_6.cpp:18:
storage.h:5: error: redefinition of 'Sd2Card card'
storage.h:5: error: 'Sd2Card card' previously declared here
storage.h:6: error: redefinition of 'SdVolume volume'
storage.h:6: error: 'SdVolume volume' previously declared here
storage.h:7: error: redefinition of 'SdFile root'
storage.h:7: error: 'SdFile root' previously declared here
storage.h:8: error: redefinition of 'SdFile file'
storage.h:8: error: 'SdFile file' previously declared here
storage.h:10: error: redefinition of 'template<class T> int file_writeAnything(const T&)'
storage.h:10: error: 'template<class T> int file_writeAnything(const T&)' previously declared here
storage.h:19: error: redefinition of 'template<class T> int file_readAnything(T&)'
storage.h:19: error: 'template<class T> int file_readAnything(T&)' previously declared here
storage.h: In function 'boolean setupStorage()':
storage.h:30: error: redefinition of 'boolean setupStorage()'
storage.h:30: error: 'boolean setupStorage()' previously defined here
storage.h: In function 'void getConfigData()':
storage.h:85: error: redefinition of 'void getConfigData()'
storage.h:85: error: 'void getConfigData()' previously defined here
storage.h: In function 'void updateConfigData()':
storage.h:102: error: redefinition of 'void updateConfigData()'
storage.h:102: error: 'void updateConfigData()' previously defined here
storage.h: In function 'void storeData()':
storage.h:115: error: redefinition of 'void storeData()'
storage.h:115: error: 'void storeData()' previously defined here
and the code:
#define CD_PIN 3
#define CARDSPEED SPI_HALF_SPEED //this can be set to SPI_FULL_SPEED if you want
//SD Card
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
template <class T> int file_writeAnything(const T& value)
{
const byte* p = (const byte*)(const void*)&value;
int i;
for (i = 0; i < sizeof(value); i++)
file.write(*p++);
return i;
}
template <class T> int file_readAnything(T& value)
{
byte* p = (byte*)(void*)&value;
int i;
for (i = 0; i < sizeof(value); i++)
*p++ = file.read();
return i;
}
boolean setupStorage()
{
if (!enable_storage) {
messageln ("Logger Disabled");
return false;
}
pinMode(CD_PIN, INPUT);
digitalWrite(CD_PIN, HIGH);
//look for a card
if (digitalRead(CD_PIN)) {
error ("SD Card: There is no SD card installed");
enable_storage = false;
return false;
}
//try and initalise the card
if (!card.init(CARDSPEED,9)) {
error("SD Card: Failed to initalise card");
enable_storage = false;
return false;
}
else {
messageln ("SD Card: SD Card Initalised");
}
if (!volume.init(&card)) {
error("SD Card: Failed to initalise volume");
enable_storage = false;
return false;
}
else {
messageln ("SD Card: Volume initalised");
}
if (!root.openRoot(&volume)) {
error ("SD Card: Failed to initalise Root");
enable_storage = false;
return false;
}
else {
messageln ("SD Card: Root initalised");
}
message("SD Card: Card Type - ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
messageln("SD1");
break;
case SD_CARD_TYPE_SD2:
messageln("SD2");
break;
case SD_CARD_TYPE_SDHC:
messageln("SDHC");
break;
default:
messageln("Unknown");
}
}
void getConfigData()
{
if (!enable_storage) return;
if (!file.open(&root, "config.alt", O_READ)) { //file doesn't exists
if (file.open(&root, "config.alt", O_CREAT | O_APPEND | O_WRITE)) {
file_writeAnything(configData);
file.close();
messageln("Created new config data file");
}
} else {
file_readAnything(configData);
file.close();
messageln("Loaded config data");
}
}
void updateConfigData()
{
if (!enable_storage) return;
SdFile::remove(&root, "config.alt");
if (file.open(&root, "config.alt", O_CREAT | O_APPEND | O_WRITE)) {
file_writeAnything(configData);
file.close();
messageln("Updated config data");
}
}
void storeData()
{
if (!enable_storage) return;
char name[12];
sprintf(name, "flt%05d.csv", flight_number);
if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE)) {
error("Unable to open file");
}
file.print(millis());
file.print(",");
file.print(altitude);
file.print(",");
file.print(filter.x);
file.println();
if (file.writeError) {
error("File write failed");
enable_storage = false;
}
if (!file.close()) {
error("File close failed");
enable_storage = false;
}
}