Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SdFat.h>
- #include <Time.h>
- SdFat sd;
- #define DEBUG
- #define SD_CHIPSELECT 9
- #define MAX_FILESIZE 262144
- SdFile logFile;
- char name[] = "LOG00000.BIN";
- unsigned long lastCommunication, lastCapture = 0;
- void setup() {
- Serial.begin(57600);
- setSyncProvider(requestSync);
- setSyncInterval(300);
- if (!sd.init(SPI_FULL_SPEED, SD_CHIPSELECT)) sd.initErrorHalt();
- delay(5000);
- createLogFile();
- }
- void loop() {
- processPacket();
- if (timeStatus() != timeNotSet) {
- if ((lastCapture == 0) || (millis() - lastCapture >= 60000UL)) {
- logEvent();
- } else if (millis() - lastCapture <= 30000UL) {
- sendFile();
- } // ((lastCapture == 0) || (millis() - lastCapture >= 60000UL))
- } else { // (timeStatus() != timeNotSet)
- Serial.write(0x02); // 0x02 = Sync Clock
- Serial.write('\r');
- Serial.write('\n');
- delay(1000);
- } // (timeStatus() != timeNotSet)
- }
- time_t requestSync() {
- Serial.write(0x01); // 0x01 = Sync clock
- Serial.write('\r');
- Serial.write('\n');
- return 0; // the time will be sent later in response to serial mesg
- }
- void processPacket() {
- // if time sync available from serial port, update time and return true
- while(Serial.available() >= 11 ){ // time message consists of a header and ten ascii digits
- lastCommunication = millis();
- byte c = Serial.read();
- if (c == 'T') {
- time_t pctime = 0;
- for (byte i=0; i < 11 -1; i++){
- c = Serial.read();
- if( c >= '0' && c <= '9'){
- pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
- }
- }
- setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- }
- }
- void createLogFile(void) {
- #ifdef DEBUG
- Serial.println("Creating LOG File");
- #endif
- if (logFile.isOpen()) {
- logFile.close();
- }
- for (uint16_t i = 0; i < 100000; i++) {
- name[3] = i/10000 + '0';
- name[4] = i/1000 + '0';
- name[5] = i/100 + '0';
- name[6] = i/10 + '0';
- name[7] = i%10 + '0';
- // O_CREAT - create the file if it does not exist
- // O_EXCL - fail if the file exists
- // O_WRITE - open for write
- if (logFile.open(name, O_CREAT | O_EXCL | O_WRITE)) {
- #ifdef DEBUG
- Serial.print("Criado:");
- Serial.println(name);
- #endif
- break;
- } else {
- if (logFile.open(name, O_WRITE | O_APPEND | O_CREAT)) {
- if (name[0] == 'L' && name[1] == 'O' && name[2] == 'G') {
- #ifdef DEBUG
- Serial.print("Aberto:");
- Serial.print(name);
- Serial.print(" com ");
- Serial.print(logFile.fileSize());
- Serial.println(" bytes.");
- #endif
- if (!(logFile.fileSize() > MAX_FILESIZE)) {
- #ifdef DEBUG
- Serial.println("Usando arquivo < 256Kb que foi encontrado.");
- #endif
- break;
- } else {
- logFile.close();
- }
- }
- }
- }
- }
- if (!logFile.isOpen()) sd.errorHalt_P(PSTR("opening log for write failed"));
- }
- void logEvent() {
- if (!logFile.isOpen()) sd.errorHalt_P(PSTR("log is not opened"));
- lastCapture = millis();
- #ifdef DEBUG
- Serial.println("Logging 64 bytes...");
- #endif
- logFile.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 64);
- logFile.sync();
- #ifdef DEBUG
- Serial.println("Finished logging and syncing 64 bytes...");
- #endif
- checkLogSize();
- }
- void checkLogSize() {
- if (logFile.fileSize() > MAX_FILESIZE) {
- #ifdef DEBUG
- Serial.println("Atingido limite de 256Kb, criando outro arquivo...");
- #endif
- createLogFile();
- }
- }
- void sendFile() {
- SdFile sentFile;
- char currentFile[13];
- char filename[13];
- byte buffer[100];
- byte c;
- byte i;
- int checksum;
- #ifdef DEBUG
- Serial.println("Trying to send file");
- #endif
- Serial.write(0x03); // 0x03 = Request to send a file
- Serial.write('\r');
- Serial.write('\n');
- delay(1000);
- if (Serial.available() == 1) {
- lastCommunication = millis();
- byte ch = Serial.read();
- #ifdef DEBUG
- Serial.print("Received: ");
- Serial.println(ch, HEX);
- #endif
- if (ch == 0xff) { // 0xff = ACK
- logFile.getFilename(currentFile);
- while (sentFile.openNext(sd.vwd(), O_READ)) {
- sentFile.getFilename(filename);
- if (filename[0] == 'L' && filename[1] == 'O' && filename[2] == 'G') {
- if (strcmp(filename, currentFile) != 0) {
- #ifdef DEBUG
- Serial.print("Sending file ");
- Serial.println(filename);
- #endif
- //checksum = 0;
- //i = 0;
- while ((c = sentFile.read()) >= 0) {
- checksum ^= c;
- Serial.write(c);
- //buffer[i++] = c;
- //if (i == 100) {
- // i = 0;
- // Serial.write(buffer, sizeof(buffer));
- //}
- }
- //Serial.write(buffer, i);
- Serial.write('\r');
- Serial.write('\n');
- delay(1000);
- #ifdef DEBUG
- Serial.print("Generated checksum: ");
- Serial.println(checksum, HEX);
- #endif
- if (Serial.available() == 1) {
- lastCommunication = millis();
- ch = Serial.read();
- #ifdef DEBUG
- Serial.print("Received checksum: ");
- Serial.println(ch, HEX);
- #endif
- if (ch == checksum) {
- sd.remove(name);
- return;
- } // (ch == checksum)
- } // (Serial.available() == 1)
- } // (strcmp(name, currentFile) != 0)
- } // (name[0] == 'L' && name[1] == 'O' && name[2] == 'G')
- sentFile.close();
- } // while (sentFile.openNext(sd.vwd(), O_READ))
- } // (ch == 0xff)
- } // (Serial.available() == 1)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement