Skip to content

Commit

Permalink
GPS Time Working, Change to Sdfat Lib
Browse files Browse the repository at this point in the history
- Changed to SdFat lib because of the long file names. File creation and
Logging is working
- GPS Time Sync is working.
- Restarting of streams still hangs
  • Loading branch information
Flydroid committed Jul 6, 2017
1 parent f728472 commit a1a7409
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/ak_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void AKLogger::addOutputStream(OutputStream *ostream) {
} else {
Serial.println("Failed to open '" + ostream->getName() + "': " + String(err));
}
writeHeader();
}

void AKLogger::addInputStream(InputStream* istream) {
Expand All @@ -52,12 +53,14 @@ void AKLogger::addInputStream(InputStream* istream) {
} else {
Serial.println("Failed to open '"+istream->getName()+"': "+String(err));
}

}

void AKLogger::logLineFormat() {
String lineformat="";
for(SimpleList<InputStream*>::iterator it = istreams.begin(); it != istreams.end(); it++) {
lineformat+=(*it)->getLineFormat()+";";
Serial.println(lineformat);
}
log(lineformat);
}
Expand Down Expand Up @@ -120,7 +123,6 @@ void AKLogger::closeAndDeleteOutputStreams() {
Serial.println("Successully closed '"+ (*it)->getName()+"'");
delete &(*it);
it = ostreams.erase(it);

}
}

Expand Down
20 changes: 9 additions & 11 deletions src/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ void setupGPS() {
void updateTimeFromGPS() {
while (timeStatus() != timeSet) {
Serial.println("waiting for GPS");
digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
while (Serial2.available()) {
if (gps.encode(Serial2.read())) {
Serial.println("GPS encoded");
Serial.println(gps.time.age());
Serial.println(gps.time.isValid());
if (gps.time.isValid() && gps.time.age() < 1000) {
setTime(gps.time.hour() + 2, gps.time.minute(), gps.time.second(),
gps.date.day(), gps.date.month(), gps.date.year());
// Serial.println(gps.time.age());
// Serial.println(gps.time.isValid());
// Serial.println(gps.date.isValid());
if (gps.time.isValid() && gps.date.isValid() && gps.time.age() < 1000) {
// gps.time.hour() for correct Germany timezone
setTime(gps.time.hour() + 2, gps.time.minute(), gps.time.second(), gps.date.day(), gps.date.month(), gps.date.year());
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with GPS Time");
} else {
Expand All @@ -86,9 +87,6 @@ void updateTimeFromGPS() {
}
}
}
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
}
digitalWrite(LED_PIN, LOW);
}
Expand All @@ -106,7 +104,7 @@ void setup() {
// setupOutputStreams();
// setupInputStreams();

logger.writeHeader();
// logger.writeHeader();
pinMode(0, INPUT_PULLDOWN);

}
Expand Down Expand Up @@ -143,9 +141,9 @@ void loop() {
}
gps_timer =0;
}

if (log_timer >= LOG_INTERVAL_LENGTH_IN_MILLIS && logger.isActive) {
logger.logData();
log_timer = 0;
}

}
24 changes: 12 additions & 12 deletions src/sdcard_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ SDCardStream::~SDCardStream() {
}

int SDCardStream::open() {
if(!SD.begin(SD_CARD_PIN)) {
if(!sd.begin(SD_CARD_PIN, SD_SCK_MHZ(50))){
return ERR_INIT_SD_CARD;
Serial.println("Error SDCard");
sd.initErrorHalt();
}

fullName = filename + "." + fileextension;
logFile = SD.open(fullName.c_str(),FILE_WRITE);
Serial.println(fullName);
if(!logFile.open(fullName.c_str(), O_CREAT | O_WRITE | O_EXCL)){
sd.errorHalt("open file failed");
}

isOpened = true;
return AKSTREAM_SUCC;
Expand All @@ -29,18 +33,14 @@ int SDCardStream::close() {
}

void SDCardStream::writeLine(String line) {
if(!isOpened){
Serial.println("Ready for Write");
return;
}
if(logFile) {
if( logFile.println(line)){
Serial.println("write Succesful");
}
logFile.flush();
}
if (!isOpened) {
return;
}
logFile.println(line);
logFile.flush();
}


void SDCardStream::flush() {
//Nothing to do
}
Expand Down
13 changes: 10 additions & 3 deletions src/sdcard_stream.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#pragma once

#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include "SdFat.h"

#include "output_stream.h"

#define SD_CARD_PIN 14
#define ERR_INIT_SD_CARD 1





class SDCardStream: public OutputStream {
public:
SDCardStream(String filename, String fileextension);
Expand All @@ -23,9 +26,13 @@ class SDCardStream: public OutputStream {
String getName();

private:
SdFat sd;
SdFile logFile;

String filename;
String fileextension;
String fullName;
bool isOpened;
File logFile;


};

0 comments on commit a1a7409

Please sign in to comment.