#include "bexus_pressure_spi.h"

uint8_t m_buff[3],spi_adresse[]={0xEF,0x48,0x1E,0x00,0xA0,0x58},prom_buff[16];//<-Buffer für eingehende PROM Byte-Kette
//				   /\	    					/\
//			   	  	|							|
//Buffer für reingehende Byte-Kette				|
//												|
//Adresse vom CHIP(i2c, hier nichtmehr gebraucht),Druck-Conversion, Config, 1.PROM-Register, Temp-Conversion



struct Dataset *data_spi;


uint16_t cal_factor[6];
int32_t comp_dt,comp_temp,comp_pressure;
int64_t comp_off,comp_sens;


void Pressure_Spi_Init() {	

	//CS high
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);	
	//Reset	
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi4,&spi_adresse[2],1,40);
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
	//HAL_Delay(4);
		while(millis()<4);

	
	//read PROM
	for(int n=0; n<8;n+=2){
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
		//HAL_SPI_TransmitReceive(&hspi4,&spi_adresse[4]+n,&prom_buff[n],2,40);
		HAL_SPI_Transmit(&hspi4,&spi_adresse[4]+n,1,40);
		HAL_SPI_Receive(&hspi4,&prom_buff[n],2,100);
		//HAL_Delay(3);
			while(millis()<3);
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
		//HAL_Delay(3);
			while(millis()<13);
	}
	
	for(int i=0 ; i<6;i+=2){
		cal_factor[i]=prom_buff[i+1] + (prom_buff[i]<<8);
	}	
	
	//HAL_Delay(10);
}

int Pressure_Spi_Read() {	
	data_spi = &Dataset_Master[buff_sensor_data_index];

/*--------------------read_sensor-------------------------*/	
	//read pressure
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
	if(HAL_OK == HAL_SPI_Transmit(&hspi4,&spi_adresse[1],1,40)){
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
		HAL_Delay(3);
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
		if(HAL_OK != HAL_SPI_TransmitReceive(&hspi4,&spi_adresse[3],m_buff,3,40)){
			HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
			//return HAL_ERROR;
		}
	}
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
	
	data_spi->pressure_spi_raw=m_buff[2]+(m_buff[1]<<8)+(m_buff[0]<<16);
	
	
	//read temperature
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
	if(HAL_OK == HAL_SPI_Transmit(&hspi4,&spi_adresse[5],1,40)){
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
		HAL_Delay(3);
		HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
		if(HAL_OK != HAL_SPI_TransmitReceive(&hspi4,&spi_adresse[3],m_buff,3,40)){
			HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
			//return HAL_ERROR;
		}
	}
	HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
	
	
	data_spi->temperature_spi_raw=m_buff[2]+(m_buff[1]<<8)+(m_buff[0]<<16);
/*--------------------------------------------------------*/	
	
 
	data_spi->pressure_spi=(data_spi->pressure_spi_raw-0xF00000)*.000024; 
	data_spi->temperature_spi=(data_spi->temperature_spi_raw-0xF00000)*.000024; 
	

/*--------------------calculation-------------------------
	
	//pre-calc. compensation factors
	comp_dt = data_spi->temperature_spi_raw - cal_factor[4] * 256;
	comp_temp = 20 + comp_dt * cal_factor[5] / 8388608;
	comp_off = cal_factor[5] * 131072 + (cal_factor[3] * comp_dt) / 64;
	comp_sens = cal_factor[0] * 65536 + (cal_factor[2] * comp_dt) / 128;
	comp_pressure = (data_spi->pressure_spi_raw * comp_sens / 2097152 - comp_off) / 32768;
	
	
	//calculate more precise factors for lower temperatures
	if(data_spi->temperature_spi < 20){//°C
		data_spi->temperature_spi = comp_temp - (comp_dt * comp_dt) / 2147483648;	
		
		if(data_spi->temperature_spi < -15){//°C		
	  comp_off = comp_off - (61 * (15 * ((comp_temp - 1500) *(comp_temp - 1500))) + (61 * ((comp_temp - 2000) *(comp_temp - 2000)) / 16));//faktor?
		comp_sens = comp_sens - (8 * ((comp_temp - 1500) *(comp_temp - 1500))) + (2 * ((comp_temp - 2000) *(comp_temp - 2000)));//faktor
		}
		else{
		comp_off = comp_off - (61 * ((comp_temp - 2000) *(comp_temp - 2000)) / 16);//faktor?
		comp_sens = comp_sens - (2 * ((comp_temp - 2000) *(comp_temp - 2000)));//faktor
		}
	}
	else{
		data_spi->temperature_spi = comp_temp;
	
	}	
	
	//calculate pressure and temperature
	data_spi->pressure_spi = data_spi->pressure_spi *comp_sens - comp_off;
	data_spi->temperature_spi = 20 + comp_dt * data_spi->temperature_spi;
---------------------------------------------------------*/	

  return HAL_OK;
}
