/*
 * ee_manager.c
 *
 *  Created on: Jul 16, 2025
 *      Author: herli
 */
#include "ee_manager.h"
#include "ee.h"

uint8_t memInit;
uint8_t memWrite;

uint32_t quart_hour_counter = 0;

typedef struct
{
	uint8_t isCoded;
	float dFi;  //-128,127 -> *3/256 = dfi
	uint8_t inj_mode;
	uint32_t quart_hour_counter;

} Storage_t;

Storage_t ee;

void MEM_Init(){
	EE_Init(&ee, sizeof(Storage_t));
	EE_Read();
	memInit = 1;
	if(!ee.isCoded){ 	//if mem is empty
		MEM_WriteDefault();
	}
	if(ee.isCoded == 255){
		MEM_WriteCustom(0.7, 0);
	}
}
void MEM_RetrieveValues(){
	if(!memInit){
		MEM_Init();
	}
	dFi = ee.dFi;
	inj_mode = ee.inj_mode;
	quart_hour_counter = ee.quart_hour_counter;

}

void MEM_Cleanup(){ 	//only after changes to the struct

}
void MEM_WriteAll(){
	ee.isCoded = 1;
	ee.dFi = dFi;
	ee.inj_mode = inj_mode;
	ee.quart_hour_counter = quart_hour_counter;

	EE_Write();
}

void MEM_WriteDefault(){
	ee.isCoded = 1;
	ee.dFi = 0.0;
	ee.inj_mode = 0;
	EE_Write();
}
void MEM_WriteCustom(float c_dFi, uint8_t c_inj_mode){
	ee.isCoded = 1;
	ee.dFi = c_dFi;
	ee.inj_mode = c_inj_mode;
	EE_Write();
}
void IncrementQuartHourCounter(){
	quart_hour_counter++;
	memWrite = 1;
}
