Here's the code as of now. It's successfully getting the length of the track, length of each event, and the number of channels from a .vix file.
Lots of code has been commented out because I've been battling with running out of ram. :-/
Code:
//#include <EEPROM.h>
//#include <avr/wdt.h>
#include <SD.h>
//#include <Base64.h>
//#include <avr/pgmspace.h>
#define filename "32ch" //leave out the .vix
#define chipSelect 10
//File periodFile, vixen1, lengthFile, channelFile;
File vixen1;
void setup()
{
Serial.begin(115200);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); //card select pin
if (!SD.begin(chipSelect))
{
//If card failed to initialize, exit.
Serial.println(" Card failed, or not present. ");
Serial.println("Try rebooting your Arduino and ensuring your card is properly formatted.");
return;
}
Serial.println(" card initialized.");
//Look for first sequence named 1.vix
if (SD.exists(filename".vix"))
{
Serial.print(filename);
Serial.println(".vix found. ");
Serial.println();
}
//If not found, error and exit
else
{
Serial.println("ERROR - File not found. ");
Serial.println("Ensure that you've entered the correct filename and try again.");
Serial.println("Exiting...");
//vixenlog.flush();
//vixenlog.close();
return;
}
if (!SD.exists(filename".lng"))
{
uint32_t trackLength = 0, eventPeriod = 0, numChannels = 0;
boolean flag = 0;
char searchArray[9], Read;
uint16_t Cursor = 0;
Serial.println("Looks like a new sequence. Entering configuration mode.");
//Open 1.vix and find the track length in milliseconds
vixen1 = SD.open(filename".vix", FILE_READ);
// Cursor = 60;
// vixen1.seek(Cursor);
Serial.print(filename);
Serial.print(".vix opened. Searching for configuration data...");
vixen1.seek(1);
// while (vixen1.available())
// Serial.write(vixenlog.read());
//Code to find TIME
while(flag == 0 && vixen1.available())
{
searchArray[0] = searchArray[1];
searchArray[1] = searchArray[2];
searchArray[2] = searchArray[3];
searchArray[3] = vixen1.read();
if (searchArray[0] == 'i' && searchArray[1] == 'm' && searchArray[2] == 'e' && searchArray[3] == '>')
{
flag = 1;
Serial.print(".");
}
}
if (flag == 0)
{
Serial.println(" ERROR - Unable to determine TRACK LENGTH. Please ensure the correct file name was declared and it is a *.vix file.");
return;
}
flag = 0;
while (Read != '<')
{
Read = vixen1.read();
if (Read != '<')
{
trackLength += Read - 48;
trackLength *= 10;
Serial.print(".");
}
else
trackLength /= 10;
}
Cursor = vixen1.position();
Serial.print(".");
//Find the event period in milliseconds
Cursor += 37;
vixen1.seek(Cursor);
Read = 0;
while (Read != '<')
{
Read = vixen1.read();
if (Read != '<')
{
eventPeriod += Read - 48;
eventPeriod *= 10;
Serial.print(".");
}
else
eventPeriod /= 10;
}
//Code to find NUMBER OF CHANNELS
while(vixen1.available())
{
searchArray[0] = searchArray[1];
searchArray[1] = searchArray[2];
searchArray[2] = searchArray[3];
searchArray[3] = searchArray[4];
searchArray[4] = searchArray[5];
searchArray[5] = searchArray[6];
searchArray[6] = searchArray[7];
searchArray[7] = searchArray[8];
searchArray[8] = vixen1.read();
if (searchArray[0] == '/' && searchArray[1] == 'C' && searchArray[2] == 'h' && searchArray[3] == 'a' && searchArray[4] == 'n' && searchArray[5] == 'n' && searchArray[6] == 'e' && searchArray[7] == 'l' && searchArray[8] == '>')
{
numChannels += 1;
Serial.print(".");
}
}
Serial.println(" Finsihed.");
vixen1.close();
/*
//Save TRACK LENGTH, EVENT PERIOD, CHANNELS files.
Serial.print("Saving length file... ");
lengthFile = SD.open(filename".lng", FILE_WRITE);
lengthFile.write(trackLength);
lengthFile.close();
Serial.println(" finished.");
Serial.print("Saving period file... ");
periodFile = SD.open(filename".per", FILE_WRITE);
periodFile.write(eventPeriod);
periodFile.close();
Serial.println(" finished.");
Serial.println();
Serial.print("Saving Channels file... ");
channelFile = SD.open(filename".chl", FILE_WRITE);
channelFile.write(numChannels);
channelFile.close();
Serial.println(" finished.");
Serial.println();
*/
//Print out discovered TRACK LENGTH, EVENT PERIOD, CHANNELS.
Serial.println();
Serial.print("Track length is ");
Serial.print(trackLength);
Serial.println("ms. ");
Serial.println();
Serial.print("Event period is ");
Serial.print(eventPeriod);
Serial.println("ms.");
Serial.println();
Serial.print("Number of Channels is ");
Serial.print(numChannels);
Serial.println(".");
Serial.println("Sequence configuration successful.");
// cli(); // Clear interrupts
//wdt_enable(WDTO_15MS); // Set the Watchdog to 15ms
//while(1); // Enter an infinite loop
//Serial.print(".");
}
/*
Serial.println("Looks like this sequence is configured, time to decode.");
char searchArray[4], Read;
boolean flag = 0;
// lengthFile = SD.open(filename".lng", FILE_READ);
//Find the event values
Serial.println("");
Serial.println("Prepare for some gibberish while I look for the event values... ");
while(flag == 0 && vixen1.available())
{
searchArray[0] = searchArray[1];
searchArray[1] = searchArray[2];
searchArray[2] = searchArray[3];
searchArray[3] = vixen1.read();
Serial.print(searchArray[3]);
if (searchArray[0] == 'u' && searchArray[1] == 'e' && searchArray[2] == 's' && searchArray[3] == '>')
{
flag = 1;
Serial.println(" They're here!");
Serial.println("found data");
}
}
if (flag == 0)
{
Serial.println("ERROR - Unable to find data to decode. Please say 3 hail mary's and try again");
return;
}
/*
Cursor = vixen1.position();
Read = 0;
while (Read != '<' && vixen1.available())
{
Read = vixen1.read();
}
Cursor = vixen1.position() - Cursor;
Serial.println(" HEY JON LOOK HERE!!! ");
Serial.print(Cursor);
// vixdat = SD.open("1.avx", FILE_WRITE);
Serial.println();
Serial.print("Track length is ");
Serial.print(trackLength);
Serial.println("ms. You must have worked hard on that.");
Serial.println();
Serial.print("Event period is ");
Serial.print(eventPeriod);
Serial.println("ms. Should make for a good show.");
*/
//Dump vixenlog to serial
//vixenlog.seek(1);
// while (vixenlog.available())
// Serial.write(vixenlog.read());
//Save and close files for now
vixen1.close();
//vixenlog.flush();
//vixenlog.close();
}
void loop()
{
}
/*
//this is the code is to reboot the arduino
cli(); // Clear interrupts
wdt_enable(WDTO_15MS); // Set the Watchdog to 15ms
while(1); // Enter an infinite loop
*/
and here is the serial output:
Code:
Initializing SD card... card initialized.
32ch.vix found.
Looks like a new sequence. Entering configuration mode.
32ch.vix opened. Searching for configuration data............................................. Finsihed.
Track length is 253204ms.
Event period is 50ms.
Number of Channels is 32.
Sequence configuration successful.
I had initially planned on creating separate files for track length, event period, and number of channels... but now I think that might be a bad idea. In the best interests of RAM, i think it may be best to use the SD card library as little as possible.
Opinions on using EEPROM to store those values through a reboot?
Also, anyone with a SD card on their Arduino want to give this code a shot and see if it works with their .vix files?
The vixen file needs to be in the root directory of the SD card and the filename needs to be declared in the code.
Bookmarks