/*
 * board.c
 *
 *  Created on: Apr 15, 2024
 *      Author: colorbass
 */
#include "main.h"
#include "board.h"

extern ADC_HandleTypeDef hadc1;

//TODO:
//TEMP READ
//GBT_TEMP_SENSORS

void RELAY_Write(relay_t num, uint8_t state){
	if(num==RELAY_AUX)HAL_GPIO_WritePin(RELAY_AUX_GPIO_Port, RELAY_AUX_Pin, state);
	if(num==RELAY_CC)HAL_GPIO_WritePin(RELAY_CC_GPIO_Port, RELAY_CC_Pin, state);

}

uint8_t GetBoardTemp(){
	//TODO
//	HAL_ADC_Start(&hadc1); // start the adc
//
//	HAL_ADC_PollForConversion(&hadc1, 100); // poll for conversion
//
//	adc_val = HAL_ADC_GetValue(&hadc1); // get the adc value
//
//	HAL_ADC_Stop(&hadc1); // stop adc
	return 0;
}

void Init_Peripheral(){
    HAL_ADCEx_Calibration_Start(&hadc1);
    RELAY_Write(RELAY_AUX, 0);
    RELAY_Write(RELAY_CC, 1);

}

uint8_t GBT_ReadTemp(){
	//TODO
	return 0;
}

void ADC_Select_Channel(uint32_t ch) {
    ADC_ChannelConfTypeDef conf = {
        .Channel = ch,
        .Rank = 1,
        .SamplingTime = ADC_SAMPLETIME_28CYCLES_5,
    };
    if (HAL_ADC_ConfigChannel(&hadc1, &conf) != HAL_OK) {
        Error_Handler();
    }
}

uint8_t SW_GetAddr(){
	if(!HAL_GPIO_ReadPin(ADDR_0_GPIO_Port, ADDR_0_Pin)){
		if(!HAL_GPIO_ReadPin(ADDR_1_GPIO_Port, ADDR_1_Pin)){
			return 0x23;
		}else{
			return 0x21;
		}

	}else{
		if(!HAL_GPIO_ReadPin(ADDR_1_GPIO_Port, ADDR_1_Pin)){
			return 0x22;
		}else{
			return 0x20;
		}

	}
}
