Abstraction for the glider class

Dependents:   DropTest

Glider.cpp

Committer:
chasefarmer2808
Date:
2017-04-13
Revision:
4:ff59603d9fb6
Parent:
3:d8932a3036a8
Child:
5:460412fc4a17

File content as of revision 4:ff59603d9fb6:

#include "Glider.h"

Glider::Glider(Serial* device, PinName sda, PinName scl, PinName tx, PinName rx) : xbeeRxInt(rx) {
    dev = device;
    dev->printf("constructing glider...");
    readTime();
    xbee = new Serial(tx, rx);
    heading = 0.0;
    pressure = 0.0;
    temp = 0.0;
    alt = 0.0;
    packetCount = 0;
    comm = 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->getHeadingXYDeg();
}

void Glider::setTempPress() {
    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::readTime() {
     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
        FILE *fp1 = fopen("/telem/data.txt", "w");  //create the data file
        fprintf(fp1, "%d", this->startTime);  //save the start time
        fclose(fp1);
        return;   
     }
     
     fscanf(fp, "%d", &this->startTime);
     this->dev->printf("start time: %d\r\n", this->startTime);
     rewind(fp);
     fclose(fp);
}

void Glider::saveData() { //bad
     FILE *fp = fopen("/telem/data.txt", "w");
     this->dev->printf("saving time: %d\r\n", this->missionTime);
     fprintf(fp, "%d", this->missionTime);
     fclose(fp);   
}

void Glider::transmitPacket() {
    //TODO: transmit mission time, packet count, alt, pressure, speed
            //temp, voltage, state
    this->packetCount++;
    
    this->xbee->printf("3387, GLIDER, %f, %d, %d\r\n", this->heading, this->missionTime, this->packetCount);
}

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

void Glider::processCommand() {
    if (this->comm) {  //command recieved
        char command = this->xbee->getc();
        
        switch (command) {
            case BUZZER:
                  this->dev->printf("buzzing...\r\n");
                  while(1);
                  break; 
        }
        
        this->comm = false;
    }  
}