Abstraction for the glider class

Dependents:   DropTest

Revision:
6:bb82dd1618c2
Parent:
5:460412fc4a17
Child:
7:dc93fe573846
--- a/Glider.cpp	Thu Apr 13 15:10:29 2017 +0000
+++ b/Glider.cpp	Thu Apr 13 15:38:09 2017 +0000
@@ -9,8 +9,7 @@
     pressure = 0.0;
     temp = 0.0;
     alt = 0.0;
-    //packetCount = 0;
-    comm = false;
+    cmdFlag = false;
     transmitFlag = false;
     hmc = new HMC5883L(sda, scl);
     bmp = new BMP180(sda, scl);
@@ -27,7 +26,7 @@
 }
 
 void Glider::setMissionTime() {
-    this->dev->printf("current time: %d\r\n", this->missionTime);
+    //this->dev->printf("current time: %d\r\n", this->missionTime);
     this->missionTime = time(NULL) - this->startTime;
 }
 
@@ -48,50 +47,56 @@
      if (fp == NULL) {  //file does not exist
         this->dev->printf("starting the time...\r\n");
         this->startTime = time(NULL); //initialize the start time to now
-        this->packetCount = 0;
+        this->packetCount = 0;  //start the packet count at 0
+        this->state = CRUZE;
         FILE *fp1 = fopen("/telem/data.txt", "w");  //create the data file
-        fprintf(fp1, "%d %d", this->startTime, this->packetCount);  //save the start time
+        fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state);  //save the start time
         fclose(fp1);
         return;   
      }
      
-     fscanf(fp, "%d %d", &this->startTime, &this->packetCount);
-     this->dev->printf("start time: %d\r\n", this->startTime);
-     this->dev->printf("start packetCount: %d\r\n", this->packetCount);
-     rewind(fp);
+     //else, the file exists.  Read the data
+     fscanf(fp, SAVE_DATA_FORMAT, &this->startTime, &this->packetCount, &this->state);  //set the start time, packet count, and state
+     //this->dev->printf("start time: %d\r\n", this->startTime);
+     //this->dev->printf("start packetCount: %d\r\n", this->packetCount);
+     rewind(fp);  //move cursor back to beginning of file
      fclose(fp);
 }
 
-void Glider::saveData() { //bad
+void Glider::saveData() { 
      FILE *fp = fopen("/telem/data.txt", "w");
-     this->dev->printf("saving packet count: %d\r\n", this->packetCount);
-     fprintf(fp, "%d %d", this->startTime, this->packetCount);
+     //this->dev->printf("saving packet count: %d\r\n", this->packetCount);
+     fprintf(fp, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state);
      fclose(fp);   
 }
 
 void Glider::transmitPacket() {
-    //TODO: transmit mission time, packet count, alt, pressure, speed
-            //temp, voltage, state
+    //TODO: transmit alt, pressure, speed
+            //temp, voltage
     this->packetCount++;
     
-    this->xbee->printf("3387, GLIDER, %f, %d, %d\r\n", this->heading, this->missionTime, this->packetCount);
+    this->xbee->printf("3387, GLIDER, %f, %d, %d, %d\r\n", this->heading, this->missionTime, this->packetCount, this->state);
 }
 
 void Glider::setCommandFlag() {
-    this->comm = true;   
+    this->cmdFlag = true;   
 }
 
 void Glider::processCommand() {
-    if (this->comm) {  //command recieved
-        char command = this->xbee->getc();
+    if (this->cmdFlag) {  //command recieved
+        char command = this->xbee->getc();  //get the command char
         
         switch (command) {
-            case BUZZER:
+            case CMD_BUZZER:  //'b' was recieved.  sound the buzzer
                   this->dev->printf("buzzing...\r\n");
                   while(1);
                   break; 
+            case CMD_RESET:
+                  this->dev->printf("resetting save file...\r\n");
+                  remove("/telem/data.txt");
+                  this->initSaveData();
         }
         
-        this->comm = false;
+        this->cmdFlag = false;  //reset the flag so a new command can be recieved
     }  
 }