#include <SD.h>
#include <serialStr.h>
#include "eventLog.h"
#define SD_CS 4 // My chip select choice was 4, that's pretty typical. But yours may be different.
serialStr cmdMgr; // Create a sterial string object to bring in commands & test log strings.
eventLog ourEventLog; // The eventlog object. Remeber this was for testing eventLog?
void setup() {
Serial.begin(9600);
if (!SD.begin(SD_CS)) {
Serial.println("NO SD CARD!");
while(1);
}
if (!ourEventLog.begin("/logTest.txt")) {
Serial.println("Event log failed begin() call.");
while(1);
}
cmdMgr.setCallback(newCommand);
ourEventLog.setLogging(true);
Serial.println("Type in a string to log it.");
Serial.println("Type ? for list of commands.");
}
// Complete strings come in here. First we see if we can make a command out of them.
// If not, we log them.
void newCommand(char* inCmd) {
File logFile;
int numBytes;
if (!strcmp(inCmd,"?")) {
Serial.println("Well, first is \"?\" to print this.");
Serial.println("Type \"show\" to print the datafile to the monitor.");
Serial.println("Type \"stop\" to shut down logging.");
Serial.println("Type \"start\" to turn on logging.");
Serial.println("Type \"clear\" to clear out the logfile.");
Serial.println();
} else if (!strcmp(inCmd,"show")) {
Serial.println("Reading log file.");
logFile = SD.open(ourEventLog.getPath(),FILE_READ);
if (logFile) {
Serial.println();
numBytes = logFile.size();
for (int i=0;i<numBytes;i++) {
Serial.print((char)logFile.read());
}
Serial.println();
}
Serial.println("Complete.");
} else if (!strcmp(inCmd,"stop")) {
ourEventLog.setLogging(false);
Serial.println("logging has been turned off.");
} else if (!strcmp(inCmd,"start")) {
ourEventLog.setLogging(true);
Serial.println("logging has been resumed.");
} else if (!strcmp(inCmd,"clear")) {
ourEventLog.deleteFile();
Serial.println("Logfile has been deleted.");
} else {
if (ourEventLog.addEvent(inCmd)) { // Send the string off to be logged.
Serial.print(inCmd); // Show us what we logged.
Serial.println(", added to event log."); // And that we logged it.
} else { // Else..
Serial.println("No data added."); // Show that we couldn't log it for some reason.
}
}
}
void loop() {
idle(); // Always call idler() first.
}