/*
Diplomova prace : Palubni pocitac

storage - api pro praci s SD kartou

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/

#ifndef STORAGE_H_INCLUDED
#define STORAGE_H_INCLUDED

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include <avr/wdt.h>
#include <string.h>
#include "define.h"

/* sdcard library */
#include "sdlib/fat.h"
#include "sdlib/fat_config.h"
#include "sdlib/partition.h"
#include "sdlib/sd_raw.h"
#include "sdlib/sd_raw_config.h"

#include "car_meas.h"
#include "i2clib/pcf8563.h"
#include "ds18lib/crc8.h"
#include "uart.h"
#include "uart_addon.h"
#include "define.h"

#define FIND_DIRS 1
#define FIND_FILES 2

#define STATE_STARTED       1
#define STATE_RUNNING       2
#define STATE_NEW_RUNNING   3
#define STATE_STOPPED       4

#define ERR     0
#define OLD_FILE    1
#define NO_DIR  2
#define NEW_FILE 3
#define ERR_CREATE 4
#define ERR_OPEN   5

typedef struct{        /* asi 123B dat */

    pcf_t storeTime;   /* cas ulozeni zaznamu */
    uint8_t index;     /* index dane cesty, unikatni pro kazdou trasu - tj urcuje data jedne trasy 0-255 - 256 tras jeden den*/
    cm_t meas;         /* dulezita namerena data, ktera jsou i v eeprom*/
    mv_t batmV;        /* diky ukladani napeti se daji najit minima a maxima v ulozenych datech */
    mv_t tankmV;       /* logovani stavu nadrze */
    int16_t tempIn;    /* vnitrni teplota */
    int16_t tempOut;   /* venkovni teplota */
    uint8_t freeBuff[32];   /* volne misto pro dalsi promenne */

    /* CRC musi byt na konci struktury !!! */
    uint8_t crc;       /* crc pocitano pouze z predeslych dat tj sizeof(data_t)-sizeof(uint8_t) */

}data_t;

unsigned char storageInit(char *dir);
void storagePwr(char enable);
void simpleShell(void);

unsigned char storeData(const uint8_t * data,uint16_t data_size,struct fat_file_struct* fd);        // ukladani raw dat do daneho souboru fd na jeho konec
unsigned char readData(uint8_t * data,  uint16_t data_size,int32_t index,unsigned char seek,struct fat_file_struct* fd);        // cteni raw dat ze souboru z pozice dane indexem a seek urcuje okdu zacina cteni
unsigned char readMeas(data_t *data,int32_t index,unsigned char seek,struct fat_file_struct *fd);       // cteni datove struktury data_t s merenimi
unsigned char storeMeas(data_t *data,struct fat_file_struct * fd);                                      // ukladani datove struktury data_t s merenimi
unsigned char createDir(char *location,char * dir_name);                        // tvorba adresare
unsigned char findDir(char * location);                     // hledani souboru daneho cestou a jmenem
uint8_t scanDir(char * location, uint8_t attrib,uint16_t max, char * buff);     // skenovani adresare a vraceni nazvu nalezlych adresaru ci souboru do textoveho bufferu buff
unsigned char scanMeasTimes(char * location,char * file,char * buff,uint16_t buffSize,uint8_t *firstIndex);     // vraceni nalezenych pocatecnich casu jizd do text. bufferu buff.. vyuziva vykresleni databaze LCD
unsigned char openCard(void);           // otevreni partisny karty
void closeCard(void);                   // uzavreni karty
struct fat_file_struct * openFile(char * location, char * file,unsigned char * state);      // otevreni souboru
unsigned char syncData(void);                               // synchronizace dat z bufferu na SD kartu
uint8_t getLastIndex(struct fat_file_struct *fd);               // vraci posledni index trasy z  daneho souboru
unsigned char findMeas(char * location,data_t *data,uint8_t lastMeas);      // hleda prvni ci posledni zaznam mereni pro danou adresarovou cestu
unsigned char getTrackMeas(char *location,char * file,data_t * data,uint8_t trackIndex,uint8_t end);        // hleda prvni ci posledni zaznam trasy v souboru..trasa je specifikovana indexem
uint8_t delFile(char *location, char *file);            // maze soubor ci adresar
#endif // STORAGE_H_INCLUDED
