Advertisement
trunet

Arduino Sketch to create 256Kb log files

Sep 10th, 2011
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. #include <SdFat.h>
  2. #include <Time.h>
  3.  
  4. SdFat sd;
  5.  
  6. #define DEBUG
  7.  
  8. #define SD_CHIPSELECT 9
  9. #define MAX_FILESIZE 262144
  10.  
  11. SdFile logFile;
  12. char name[] = "LOG00000.BIN";
  13.  
  14. unsigned long lastCommunication, lastCapture = 0;
  15.  
  16. void setup() {
  17.   Serial.begin(57600);
  18.  
  19.   setSyncProvider(requestSync);
  20.   setSyncInterval(300);
  21.  
  22.   if (!sd.init(SPI_FULL_SPEED, SD_CHIPSELECT)) sd.initErrorHalt();
  23.  
  24.   delay(5000);
  25.  
  26.   createLogFile();
  27. }
  28.  
  29. void loop() {
  30.   processPacket();
  31.  
  32.   if (timeStatus() != timeNotSet) {
  33.     if ((lastCapture == 0) || (millis() - lastCapture >= 60000UL)) {
  34.       logEvent();
  35.     } else if (millis() - lastCapture <= 30000UL) {
  36.       sendFile();
  37.     } // ((lastCapture == 0) || (millis() - lastCapture >= 60000UL))
  38.   } else { // (timeStatus() != timeNotSet)
  39.     Serial.write(0x02); // 0x02 = Sync Clock
  40.     Serial.write('\r');
  41.     Serial.write('\n');
  42.     delay(1000);
  43.   } // (timeStatus() != timeNotSet)
  44. }
  45.  
  46. time_t requestSync() {
  47.   Serial.write(0x01); // 0x01 = Sync clock
  48.   Serial.write('\r');
  49.   Serial.write('\n');
  50.   return 0; // the time will be sent later in response to serial mesg
  51. }
  52.  
  53. void processPacket() {
  54.   // if time sync available from serial port, update time and return true
  55.   while(Serial.available() >= 11 ){  // time message consists of a header and ten ascii digits
  56.     lastCommunication = millis();
  57.     byte c = Serial.read();
  58.     if (c == 'T') {    
  59.       time_t pctime = 0;
  60.       for (byte i=0; i < 11 -1; i++){
  61.         c = Serial.read();
  62.         if( c >= '0' && c <= '9'){
  63.           pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
  64.         }
  65.       }
  66.       setTime(pctime);   // Sync Arduino clock to the time received on the serial port
  67.     }
  68.   }
  69. }
  70.  
  71. void createLogFile(void) {
  72.   #ifdef DEBUG
  73.   Serial.println("Creating LOG File");
  74.   #endif
  75.   if (logFile.isOpen()) {
  76.     logFile.close();
  77.   }
  78.   for (uint16_t i = 0; i < 100000; i++) {
  79.     name[3] = i/10000 + '0';
  80.     name[4] = i/1000 + '0';
  81.     name[5] = i/100 + '0';
  82.     name[6] = i/10 + '0';
  83.     name[7] = i%10 + '0';
  84.     // O_CREAT - create the file if it does not exist
  85.     // O_EXCL - fail if the file exists
  86.     // O_WRITE - open for write
  87.     if (logFile.open(name, O_CREAT | O_EXCL | O_WRITE)) {
  88.       #ifdef DEBUG
  89.       Serial.print("Criado:");
  90.       Serial.println(name);
  91.       #endif
  92.       break;
  93.     } else {
  94.       if (logFile.open(name, O_WRITE | O_APPEND | O_CREAT)) {
  95.         if (name[0] == 'L' && name[1] == 'O' && name[2] == 'G') {
  96.           #ifdef DEBUG
  97.           Serial.print("Aberto:");
  98.           Serial.print(name);
  99.           Serial.print(" com ");
  100.           Serial.print(logFile.fileSize());
  101.           Serial.println(" bytes.");
  102.           #endif
  103.           if (!(logFile.fileSize() > MAX_FILESIZE)) {
  104.             #ifdef DEBUG
  105.             Serial.println("Usando arquivo < 256Kb que foi encontrado.");
  106.             #endif
  107.             break;
  108.           } else {
  109.             logFile.close();
  110.           }
  111.         }
  112.       }
  113.     }
  114.   }
  115.   if (!logFile.isOpen()) sd.errorHalt_P(PSTR("opening log for write failed"));
  116. }
  117.  
  118. void logEvent() {
  119.   if (!logFile.isOpen()) sd.errorHalt_P(PSTR("log is not opened"));
  120.   lastCapture = millis();
  121.   #ifdef DEBUG
  122.   Serial.println("Logging 64 bytes...");
  123.   #endif
  124.   logFile.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 64);
  125.   logFile.sync();
  126.   #ifdef DEBUG
  127.   Serial.println("Finished logging and syncing 64 bytes...");
  128.   #endif
  129.   checkLogSize();
  130. }
  131.  
  132. void checkLogSize() {
  133.   if (logFile.fileSize() > MAX_FILESIZE) {
  134.     #ifdef DEBUG
  135.     Serial.println("Atingido limite de 256Kb, criando outro arquivo...");
  136.     #endif
  137.     createLogFile();
  138.   }
  139. }
  140.  
  141. void sendFile() {
  142.   SdFile sentFile;
  143.   char currentFile[13];
  144.   char filename[13];
  145.   byte buffer[100];
  146.   byte c;
  147.   byte i;
  148.   int checksum;
  149.  
  150.   #ifdef DEBUG
  151.   Serial.println("Trying to send file");
  152.   #endif
  153.  
  154.   Serial.write(0x03); // 0x03 = Request to send a file
  155.   Serial.write('\r');
  156.   Serial.write('\n');
  157.   delay(1000);
  158.   if (Serial.available() == 1) {
  159.     lastCommunication = millis();
  160.     byte ch = Serial.read();
  161.     #ifdef DEBUG
  162.     Serial.print("Received: ");
  163.     Serial.println(ch, HEX);
  164.     #endif
  165.     if (ch == 0xff) { // 0xff = ACK
  166.       logFile.getFilename(currentFile);
  167.       while (sentFile.openNext(sd.vwd(), O_READ)) {
  168.         sentFile.getFilename(filename);
  169.         if (filename[0] == 'L' && filename[1] == 'O' && filename[2] == 'G') {
  170.           if (strcmp(filename, currentFile) != 0) {
  171.             #ifdef DEBUG
  172.             Serial.print("Sending file ");
  173.             Serial.println(filename);
  174.             #endif
  175.             //checksum = 0;
  176.             //i = 0;
  177.             while ((c = sentFile.read()) >= 0) {
  178.               checksum ^= c;
  179.               Serial.write(c);
  180.               //buffer[i++] = c;
  181.               //if (i == 100) {
  182.               //  i = 0;
  183.               //  Serial.write(buffer, sizeof(buffer));
  184.               //}
  185.             }
  186.             //Serial.write(buffer, i);
  187.             Serial.write('\r');
  188.             Serial.write('\n');
  189.             delay(1000);
  190.             #ifdef DEBUG
  191.             Serial.print("Generated checksum: ");
  192.             Serial.println(checksum, HEX);
  193.             #endif
  194.             if (Serial.available() == 1) {
  195.               lastCommunication = millis();
  196.               ch = Serial.read();
  197.               #ifdef DEBUG
  198.               Serial.print("Received checksum: ");
  199.               Serial.println(ch, HEX);
  200.               #endif
  201.               if (ch == checksum) {
  202.                 sd.remove(name);
  203.                 return;
  204.               } // (ch == checksum)
  205.             } // (Serial.available() == 1)
  206.           } // (strcmp(name, currentFile) != 0)
  207.         } // (name[0] == 'L' && name[1] == 'O' && name[2] == 'G')
  208.         sentFile.close();
  209.       } // while (sentFile.openNext(sd.vwd(), O_READ))
  210.     } // (ch == 0xff)
  211.   } // (Serial.available() == 1)
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement