#include "bexus_sd.h"

/**----------------variables---------------------------*/
SD_HandleTypeDef hsd1;
HAL_SD_CardInfoTypeDef SDCardInfo1;
FRESULT status_SD;
FATFS SDFatFs;
FIL File_SD;
FILINFO fno;

char * buff_write_SD;
char * buff_read_SD;
char  buff_filename_SD[15]="log0000.log";
uint32_t  byteswritten_SD, bytesread_SD;
uint32_t File_Index=0;
uint32_t errorCount = 0;
uint32_t Dateigrosse = 0;
uint8_t sd_errortype=0;

uint32_t sdTime, tmp;

char eol[2] = "\r\n";
/**-----------------------------------------------------*/

/* Work-memory needed for compression. Allocate memory in units
 * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned.
 */
/*
#define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]

static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS); // standard ist 16384 bytes als LZO1X_1_MEM_COMPRESS

int r;
*/
/**------------------functions--------------------------*/
void SD_FileName(){

	File_Index = 0;
	sprintf(buff_filename_SD,"log%04d.log", File_Index);
	f_close(&File_SD);
	
	while(FR_OK == f_stat(buff_filename_SD, &fno)) { //Wenn eine Datei mit bestehende Namen gefunden wurde, index inkrementieren
		File_Index++;
		sprintf(buff_filename_SD,"log%04d.log", File_Index);
	}
	
	status_SD =	f_open(&File_SD,buff_filename_SD,FA_WRITE|FA_CREATE_ALWAYS);
	
	if(FR_OK != status_SD)
		Error_SD(10);
}

void Error_SD(int Error_Type_SD){
	if(Error_Type_SD != 99 )
	errorCount++;
	sd_errortype = Error_Type_SD;
	
	if(Error_Type_SD == 9 && errorCount>10){
		SD_FileName();
	}
	if(Error_Type_SD == 3 && errorCount % 100 == 1) {
		SD_FileName();
	}
}

void SD_Init(){
  status_SD =	f_mount(&SDFatFs,"",1); //SD slot 1
	
	if(FR_OK != status_SD) {
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_12,GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14,GPIO_PIN_RESET);
		HAL_Delay(1);
		
		status_SD =	f_mount(&SDFatFs,"",1); //SD slot 2
	}
	if(FR_OK != status_SD) {
		Error_SD(0);
	}
		
	SD_FileName();

}

void SD_Write(){
	
	tmp = millis();

	
	//for(int i=0; i<DATASET_LENGTH; i++) {
		
		
		//status_SD =  f_write(&File_SD, &Dataset_Master[buff_sensor_data_index].pressure_cis_raw, 20 * 4, &byteswritten_SD);
		//status_SD |= f_write(&File_SD, &Dataset_Master[buff_sensor_data_index].longitude,         8 * 4, &byteswritten_SD);
		
		//status_SD =  f_write(&File_SD, &Dataset_Slave[buff_sensor_data_index].pressure_cis_raw, 20 * 4, &byteswritten_SD);
		//status_SD |= f_write(&File_SD, &Dataset_Slave[buff_sensor_data_index].longitude,         8 * 4, &byteswritten_SD);
		
		status_SD =  f_write(&File_SD, &Dataset_Master[buff_sensor_data_index], sizeof(Dataset_Master[buff_sensor_data_index]), &byteswritten_SD);
		status_SD |= f_write(&File_SD, &Dataset_Slave[buff_sensor_data_index], sizeof(Dataset_Slave[buff_sensor_data_index]), &byteswritten_SD);
		status_SD |= f_write(&File_SD, &Dataset_Fusion[buff_sensor_data_index], sizeof(Dataset_Fusion[buff_sensor_data_index]), &byteswritten_SD);
		
		
		status_SD |= f_write(&File_SD, eol, sizeof(eol), &byteswritten_SD);


		
		if(status_SD !=FR_OK){
			Error_SD(3);
		}
	//}
	
	sdTime = millis() - tmp;
	
	f_sync(&File_SD);
}
/**-----------------------------------------------------*/
