/*
 * SmartBeeHive_GetHiveWeight.c
 *
 *  Created on: 09.01.2026
 *      Author: zafst
 */

#include "SmartBeeHive_GetHiveWeight.h"
#include "ADS124S08_LIB.h"
#include "main.h"
#include "stm32u3xx_hal.h"

#define NUM_SAMPLES_WEIGHT	100

const int32_t BaseOffsetWeightCells = 711;


const int32_t BaseDividertWeightCells = 54;




void EnableHiveExcication(HiveNumber_e Hive);
void DisableHiveExcication();

uint8_t MeasurmentEnabled = 0;
uint8_t MeasurmentDone = 0;
uint8_t MeasurmentCounter = 0;
uint8_t SampleCounter = 0;
int64_t temp = 0;

int32_t SmoothedResult = 0;



void SmartBeeHive_NewMeasurmentDone(int32_t MeasurmentData)
{
	if(MeasurmentEnabled == 1)
	{
		SampleCounter++;

		if(SampleCounter <= NUM_SAMPLES_WEIGHT)
		{
			temp = temp + MeasurmentData / BaseDividertWeightCells;
		}
		else
		{
			SmoothedResult = temp / NUM_SAMPLES_WEIGHT;
			temp = 0;
			MeasurmentEnabled = 0;
			SampleCounter = 0;
			MeasurmentDone = 1;
			ADS124S08_START_CONVERSION(ADS124S08_ID0);

		}
	}
}


uint8_t SmartBeeHive_GetWeightOfHive(HiveNumber_e Hive, int32_t* pData)
{

	EnableHiveExcication(Hive);

	ADS124S08_INIT(ADS124S08_ID0);
	ADS124S08_SYSTEM_CONTROL(ADS124S08_ID0, ADS124S08_SEND_STAT_DISABLED, ADS124S08_SEND_CRC_DISABLED, ADS124S08_SPI_TIMEOUT_DISABLED, ADS124S08_CAL_SAMPLES_16);
	ADS124S08_SET_CHOP_MODE(ADS124S08_ID0, ADS124S08_GLOBAL_CHOP_MODE_ENABLED);
	ADS124S08_SET_PGA_SETTINGS(ADS124S08_ID0, ADS124S08_PROG_CONV_DELAY_256_x_tMOD, ADS124S08_PGA_ENABLED, ADS124S08_GAIN_128);
	ADS124S08_SET_DATA_RATE(ADS124S08_ID0, ADS124S08_DATA_RATE_100SPS);
	ADS124S08_SET_REFERENCE_SETTINGS(ADS124S08_ID0, ADS124S08_INTERNAL_REFERENCE_ALWAYS_ON, ADS124S08_REFERENCE_IN_REFP0_REFN0, ADS124S08_REFN_BUFFER_BYPASSED, ADS124S08_REFP_BUFFER_NOT_BYPASSED, ADS124S08_REFERENCE_MONITOR_DISABLED);
	ADS124S08_SET_CONV_MODE(ADS124S08_ID0, ADS124S08_CONV_MODE_CONTINOUS);

	switch(Hive)
	{
	case HIVE_1:
	{
		ADS124S08_SET_NEGATIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_NEG_IN_AIN_6 );
		ADS124S08_SET_POSITIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_POS_IN_AIN_7);

		break;
	}
	case HIVE_2:
	{
		ADS124S08_SET_NEGATIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_NEG_IN_AIN_4);
		ADS124S08_SET_POSITIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_POS_IN_AIN_5);
		break;
	}
	case HIVE_3:
	{
		ADS124S08_SET_NEGATIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_NEG_IN_AIN_2 );
		ADS124S08_SET_POSITIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_POS_IN_AIN_3);
		break;
	}
	case HIVE_4:
	{
		ADS124S08_SET_NEGATIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_NEG_IN_AIN_0 );
		ADS124S08_SET_POSITIVE_ANALOG_INPUT_CHANNEL(ADS124S08_ID0, ADS124S08_POS_IN_AIN_1);
		break;
	}
	}

	HAL_GPIO_WritePin(CELL_SELECT_EN_GPIO_Port, CELL_SELECT_EN_Pin, GPIO_PIN_SET);

	HAL_GPIO_WritePin(CELL_SELECT_A0_GPIO_Port, CELL_SELECT_A0_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(CELL_SELECT_A1_GPIO_Port, CELL_SELECT_A1_Pin, GPIO_PIN_RESET);

	MeasurmentEnabled = 1;
	MeasurmentDone = 0;

	HAL_Delay(500);

	ADS124S08_START_CONVERSION(ADS124S08_ID0);

	while(MeasurmentDone != 1)
	{
		//warten bis Messung fertig, Timeout wäre vermutlich sinnvoll
	}

	*pData = SmoothedResult - BaseOffsetWeightCells;

	return 0;
}


void EnableHiveExcication(HiveNumber_e Hive)
{

	HAL_GPIO_WritePin(HIVE_SELECT_EN_GPIO_Port,HIVE_SELECT_EN_Pin,GPIO_PIN_SET);	/* Multiplexer Einschalten */

	switch(Hive)
	{
	case HIVE_1:
	{
		HAL_GPIO_WritePin(HIVE_SELECT_A0_GPIO_Port, HIVE_SELECT_A0_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(HIVE_SELECT_A1_GPIO_Port, HIVE_SELECT_A1_Pin, GPIO_PIN_RESET);
		break;
	}
	case HIVE_2:
	{
		HAL_GPIO_WritePin(HIVE_SELECT_A0_GPIO_Port, HIVE_SELECT_A0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(HIVE_SELECT_A1_GPIO_Port, HIVE_SELECT_A1_Pin, GPIO_PIN_RESET);
		break;
	}
	case HIVE_3:
	{
		HAL_GPIO_WritePin(HIVE_SELECT_A0_GPIO_Port, HIVE_SELECT_A0_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(HIVE_SELECT_A1_GPIO_Port, HIVE_SELECT_A1_Pin, GPIO_PIN_SET);
		break;
	}
	case HIVE_4:
	{
		HAL_GPIO_WritePin(HIVE_SELECT_A0_GPIO_Port, HIVE_SELECT_A0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(HIVE_SELECT_A1_GPIO_Port, HIVE_SELECT_A1_Pin, GPIO_PIN_SET);
		break;
	}
	}
}

void DisableHiveExcication()
{
	HAL_GPIO_WritePin(HIVE_SELECT_EN_GPIO_Port,HIVE_SELECT_EN_Pin,GPIO_PIN_RESET);	/* Multiplexer Aus*/
}
