posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

Revision:
21:d1569911500a
Parent:
20:66ecb2f0e307
Child:
22:69621e10dd26
diff -r 66ecb2f0e307 -r d1569911500a main.cpp
--- a/main.cpp	Tue Mar 07 13:30:34 2017 +0000
+++ b/main.cpp	Fri Mar 10 15:05:40 2017 +0000
@@ -61,6 +61,10 @@
  * - Added define switch for new revision of hardware
  * - HW v1r2 only supports single MPU
  *
+* CHANGELOG 10/03/2017
+ * - Added swimmer index to the status message.
+ * - Added record file for records, function appendRecord, sendRecords
+ * - Added clear root command
  */
 
 // Includes ====================================================================
@@ -80,7 +84,7 @@
 // Revision ====================================================================
 
 // Revision define, it's important to select proper version! (pinmuxes differ)
-#define REVISION 2
+#define REVISION 1
 
 // Defines =====================================================================
 
@@ -103,16 +107,19 @@
 /* Mass storage realted stuff */
 #define FSNAME "usb"
 #define LOG_FILE "/usb/log.txt"
+#define RECORDS_FILE "/usb/records.txt"                                         
 #define SPACE_LOW_THRESHOLD 100                                                 // USB Flash disk free threshold 
 #define SPACE_PERIOD 300U                                                       // How often to measure free space in seconds
 
 /* Commands swimfly understands */
 #define SAVING_START        'S'
 #define SAVING_STOP         'E'
+#define CLEAR_ROOT          'C'
 #define TRANSFER_REQUEST    'T'
 #define SCOPE_BEGIN         'B'
 #define CHECK_READY         'R'
 #define SEND_LOG            'L'
+#define SEND_RECORDS        'D'
 #define HALT                'H'
 
 /* Comm stuff */
@@ -137,6 +144,7 @@
 /* File handling */
 void clearRoot(void);
 void appendLog(char textToAppend[]);
+void appendRecord(char recordName[], char timestamp[]);
 bool doesFileExist(const char *filename);
 void spaceMeas(void);
 
@@ -239,6 +247,7 @@
 FILE *gfp;                                                                      // Global file pointer
 MSCFileSystem logger(FSNAME);                                                   // Initialize USB flash disk
 char fname[40];                                                                 // Variable for name of Text file saved on USB flash disk
+char timestamp[22];                                                             // Variable for name of Text file saved on USB flash disk
 static volatile uint64_t absolute   = 0;                                        // Variable for numbering saved data
 volatile int relative               = 0;                                        // Relative packtets saved number
 volatile short swimmer_id           = 0;                                        // Global swimmer ID                      
@@ -296,6 +305,19 @@
     fclose(log);                                                                // Close file
 }
 
+/* Append record to records file */
+void appendRecord(char recordName[], char timestamp[])
+{   
+    printf("Writing to record file...\r\n");
+
+    /* Append line to log file */
+    FILE *records;
+    records = fopen(RECORDS_FILE, "a");                                         // Open file with records name
+    fprintf(records, "%s: ", recordName);                                       // Append file name
+    fprintf(records, "%s\r\n", timestamp);                                      // Append record time stamp
+    fclose(records);                                                            // Close file
+}
+
 /* Sees whether giver file exists on flash disk */
 bool doesFileExist(const char *filename)
 {
@@ -561,6 +583,12 @@
 /* Starts to acquire data from sensors */
 void startSaving(void)
 {
+    for(int i = 0; i < 22; i++) {
+        timestamp[i] = wifi.readByte();                                         // Get bytes containing timestamp for the current record
+    }
+    
+    wifi.sendAck();
+    
     savingFlag = 1;                                                             // Set saving flag
     /* Open global file with current swimmer ID */
     gfp = fopen(fname, "wb");                                                   // Try to open that file
@@ -620,7 +648,10 @@
     fprintf(gfp, "%c%c", -1,-1);                                                // Write EOF characters
     fflush(gfp);
     fclose(gfp);                                                                // Close file
-
+    
+    /* Append this file to the records file */
+    appendRecord(fname, timestamp);
+    
     /* Prepare for next swimmer */
     swimmer_id++;                                                               // Increment swimmer ID
     sprintf(fname, "/usb/swimmer%d.txt", swimmer_id);                           // Update current file name
@@ -700,6 +731,41 @@
     }
 }
 
+/* Sends records file*/
+void sendRecords(void)
+{
+     /* Vars */
+    char name[30];                                                              // File name buffer  
+    
+    /* File transfer prerequisites */                                           
+    sprintf(name, RECORDS_FILE);                                                // Create file name from log name    
+    /* Handle transfer */
+    if(doesFileExist(name))                                                     // At first check whether file exists (fopen used to freeze mbed)
+    {
+        /* Send ACK (should make this more abstract) */
+        wifi.sendAck();
+        wait_ms(50);                                                            // Timeout is used to make sure C# gets ready (sockets are messy, flushing is pain)
+        /* Actually try to send file */                                         
+        printf("Sending %s\r\n", name);                                         // Notify user which user is being sent
+        if (wifi.sendFile(name))                                                // Send !
+        {
+            leds_error();                                                       // Handle error
+            printf("Unable to send records file\r\n");                          // Notify user via text also
+        }
+        else
+        {
+            printf("Records file sent!\r\n");                                   // Otherwise all is AOK
+        }  
+    }
+    else
+    {
+        /* In case file doest not exist send NACK */
+        wifi.sendByte('#');                                                     
+        wifi.sendByte('F');
+        printf("Requested non-existing file...\r\n");
+    }
+}
+
 /* Halts board */
 void halt(void)
 {
@@ -730,10 +796,11 @@
 /* Sends status */
 void sendStatus(void)
 {
-    uint8_t checksum = SOH + savingFlag + battLevel;                            // Calculate checksum
+    uint8_t checksum = SOH + savingFlag + battLevel + swimmer_id;               // Calculate checksum
     wifi.sendByte(SOH);                                                         // Send start of heading
     wifi.sendByte(savingFlag);                                                  // Send state of saving
     wifi.sendByte(battLevel);                                                   // Sends current battery level
+    wifi.sendByte(swimmer_id);                                                  // Sends current swimmer id index
     wifi.sendByte(checksum);                                                    // Send checksum
 }
 
@@ -826,6 +893,17 @@
                 break;                                                          // Break
             }
             
+            /* Send records file */
+            case SEND_RECORDS:
+            {
+                leds_off();                                                     // Turn off all leds
+                led_measuring = led_saving = 1;                                 // Turn on two leds to depict special behaviour
+                printf("Sending logrecords...\r\n");                            // Notify user that new scope is beginning
+                sendRecords();                                                  // Send records file
+                leds_off();                                                     // Turn off all leds
+                break;                                                          // Break
+            }
+            
             /* Halt swimfly */
             case HALT:
             {