Michał Biolik / Mbed 2 deprecated SD_TUT

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

file_manager.cpp

Committer:
micbio
Date:
2016-12-05
Revision:
1:58d2021e301d
Child:
2:e699312248f3

File content as of revision 1:58d2021e301d:

#include "file_manager.hpp"

extern Serial pc;

void file_manager::print_files(void)
{
    pc.printf("1. %s\n", path_sisk);
    pc.printf("2. %s\n", path_agh);
    pc.printf("3. %s\n", path_krk);
}

void file_manager::get_string(void)
{
//    char * p_my_//cmd = p_cmd;
//    uint8_t char_counter = 0;
//    static char byte_received;
//    
//    do
//    {
//        byte_received = pc.getc();
//        *p_my_cmd = byte_received;
//        p_my_cmd++;
//        char_counter++;
//        if (char_counter >= CMD_LENGTH)
//        {
//            break;      // CMD length reached max length.
//        }        
//        pc.putc(byte_received);
//        for (int i = 0; i < len; i++)
//        {
//            pc.putc(*(p_cmd + i));
//        }
//    }while(byte_received != '\r');
//    
//    len = char_counter;
//    
//    pc.printf("\r\n");
//    pc.putc('Y');    
}

void file_manager::get_path(void)
{
    pc.printf("Current file to write to is: %s.\n", current_path);    
}

void file_manager::set_path(void)
{
    char number;
    bool correct_number = false;

    if (fp != NULL)
    {
        pc.printf("First close the file!\n");
        return;
    }

    this->get_path();
    pc.printf("Write the number of new file to write to. \n");    
    this->print_files();

    do
    {
        correct_number = true;
        number = pc.getc();
        switch(number)
        {
            case '1':
                current_path = path_sisk;
                break;
            case '2':
                current_path = path_agh;
                break;
            case '3':
                current_path = path_krk;
                break;
            default:
                pc.printf("Incorrect number, try again.\n");
                correct_number = false;
        }
    }while(!correct_number);

    pc.printf("Setting path to: %s.\n", current_path);
}

bool file_manager::open_file(char option)
{
    if (fp != NULL)
    {
        pc.printf("File already opened!\n");
        return false;
    }

    fp = fopen("/a.txt", "w");

    if (fp != NULL)
    {
        pc.printf("Opened file: %s.\n", this->current_path);
    }

    return true;
}

bool file_manager::close_file(void)
{
    if (fp == NULL)
    {
        pc.printf("File already closed!\n");
        return false;
    }

    fclose(fp);

    fp = NULL;

    return true;
}

void file_manager::write_to_file(void)
{
    this->open_file('w');



    this->close_file();
}


void file_manager::append_to_file(void)
{
    this->open_file('a');

    

    this->close_file();
}