Abstraction for the glider class

Dependents:   DropTest

Glider.cpp

Committer:
chasefarmer2808
Date:
2017-04-15
Revision:
8:7fc2caa10bbb
Parent:
7:dc93fe573846
Child:
9:8e34e713e6ef

File content as of revision 8:7fc2caa10bbb:

#include "Glider.h"

Glider::Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx) : xbeeRxInt(rx) {
    dev = device;
    dev->printf("constructing glider...");
    initSaveData();
    xbee = new Serial(tx, rx);
    heading = 0.0;
    pressure = 0.0;
    temp = 0.0;
    alt = 0.0;
    cmdFlag = false;
    transmitFlag = false;
    hmc = new HMC5883L(sda, scl);
    bmp = new BMP180(sda, scl);
    bmp->Initialize(1013.25, BMP180_OSS_ULTRA_LOW_POWER);
    xbeeRxInt.rise(this, &Glider::setCommandFlag);
}

void Glider::setHeading() {
    this->heading = this->hmc->getHeading();
}

void Glider::setTempPressAlt() {
    bmp->ReadData(&this->temp, &this->pressure, &this->alt);
}

void Glider::setMissionTime() {
    //this->dev->printf("current time: %d\r\n", this->missionTime);
    this->missionTime = time(NULL) - this->startTime;
}

void Glider::setTransmitFlag() {
    this->transmitFlag = true;   
}

void Glider::saveTelem() {
     FILE *fp = fopen("/telem/telem.txt", "w");
     fprintf(fp, "hello,");
     fclose(fp);
}

void Glider::initSaveData() {
     this->dev->printf("getting the time...\r\n");
     FILE *fp = fopen("/telem/data.txt", "r");   //attempt to read the saved data
     
     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;  //start the packet count at 0
        this->state = CRUZE;
        FILE *fp1 = fopen("/telem/data.txt", "w");  //create the data file
        fprintf(fp1, SAVE_DATA_FORMAT, this->startTime, this->packetCount, this->state);  //save the start time
        fclose(fp1);
        return;   
     }
     
     //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() { 
     FILE *fp = fopen("/telem/data.txt", "w");
     //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 alt, pressure, speed
            //temp, voltage
    this->packetCount++;
    
    this->xbee->printf("3387, GLIDER, %d, %d, %f, %f, %f, %f, %d\r\n", 
                        this->missionTime, 
                        this->packetCount, 
                        this->alt, 
                        this->pressure, 
                        this->temp,
                        this->heading, 
                        this->state);
}

void Glider::setCommandFlag() {
    this->cmdFlag = true;   
}

void Glider::processCommand() {
    if (this->cmdFlag) {  //command recieved
        char command = this->xbee->getc();  //get the command char
        
        switch (command) {
            case CMD_BUZZER:  //'b' was recieved.  sound the buzzer
                  this->dev->printf("buzzing...\r\n");
                  this->state = LAND;
                  while(1);
                  break; 
            case CMD_RESET:
                  this->dev->printf("resetting save file...\r\n");
                  remove("/telem/data.txt");
                  this->initSaveData();
        }
        
        this->cmdFlag = false;  //reset the flag so a new command can be recieved
    }  
}