working commands. singleton deleted

Dependents:   GonioTrainer

OfflineCommand.cpp

Committer:
dkester
Date:
2015-06-07
Revision:
2:c9e47ac47edb
Parent:
1:fd4c0e2decb8
Child:
3:32afe87d4b62

File content as of revision 2:c9e47ac47edb:

#include "OfflineCommand.h"

volatile int32_t OfflineCommand::addr = 0;

OfflineCommand::OfflineCommand(Sensors* sensors, Storage* storage)
{
    this->sensors = sensors;
    this->storage = storage;
    addr = 0x0;
    this->running = 0;
    this->state = 0;
}

void OfflineCommand::initialize()
{
    printf("OfflineCommand\n");
    this->state = 0;
    //setLed(1);
    setTicker(0.1);
}

void OfflineCommand::execute()
{
    if(running) {
        writeToMemory();
    }
}

void OfflineCommand::button()
{
    switch(state) {
        case 0:
        printf("ERASE COMMAND\n");
            detachTicker();
            setLed(1);
            addr = 0;
            wait(0.1);
            eraseMemory();
            this->state = 1;
            setTicker(0.1);
            break;
        case 1:
            detachTicker();
            setLed(1);
            printf("WRITE COMMAND\n");
            addr = 0;
            this->running = 1; //This will unlock the writeToMemory from the execution
            this->state = 2;
            this->sensors->wakeIMU();
            break;
        case 2:
            setTicker(0.1);
            printf("DONE WRITE COMMAND...");
            printf("%d\n", addr);
            this->sensors->disableIMU();
            this->running = 0;
            this->state = 0;
            break;
    }

}

void OfflineCommand::finish()
{
    this->state = 0;
     detachTicker();
    setLed(0);
}

void OfflineCommand::writeToMemory()
{
    if(!storage->isBusy()) {

        storage->pageProgram(addr);

        /*
        storage->writeData()
        storage->writeData()
        storage->writeData()
        storage->writeData()
        */
        
        storage->endAction();

        addr = addr + 0x4;

    } else {
        printf("busy: %d\n", storage->getSR1());
    }

}

void OfflineCommand::eraseMemory()
{
    __disable_irq(); 
    storage->reset();
    
    while(storage->isBusy()) {
        setLed(1);
        wait(0.1);
        setLed(0);
    }
    storage->setWREN();
    
    wait(0.01);
    
    storage->bulkErase();
    
    wait(0.01);
    
    printf("erasing...\n");

    while(storage->isBusy()) {
        wait(0.1);
    }

    printf("succes...%d\n", storage->getSR1());
    __enable_irq(); 
    
/*
    __disable_irq(); 

    Storage::getInstance()->read(addr);
    
    while(Storage::getInstance()->readData() != 0xFF){
        Storage::getInstance()->endAction(); //end previous read action
        
        Storage::getInstance()->sectorErase(addr);
        
        while( Storage::getInstance()->isBusy()){
            wait(0.01);
            }
        addr = addr + (1<<18);
        printf("sector %d erased\n", addr);
        Storage::getInstance()->read(addr);   //set next section addr for read in the while statement
    }
    printf("sector erase done\n");
    Storage::getInstance()->endAction();
    
     __enable_irq(); 
     
     */
}

int32_t OfflineCommand::getLastAddress(){
    return addr;
    }