/**
 ******************************************************************************
 * @file    main.c
 * @author  BekaBen 
 * @brief   Main program for SeedNursery Wiznet Surf5 Challenge June 2024.
 ******************************************************************************
 * @ref 		W7500x-Surf5 The project is started from MDN Surf5 Template project from : 
 * 					https://github.com/Wiznet/W7500x-Surf5/
 *
 * June 24th 2024 :o{)
 */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "wizchip_conf.h"
#include "dhcp.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DATA_BUF_SIZE 2048

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t TimingDelay;
uint8_t test_buf[DATA_BUF_SIZE];
uint16_t SoilMoisture;
wiz_NetInfo gWIZNETINFO;

/* Private function prototypes -----------------------------------------------*/
static void UART_Config(void);
static void GPIO_Config(void);
static void DUALTIMER_Config(void);
void TimingDelay_Decrement(void);
void Delay(__IO uint32_t milliseconds);
int Read_ADC(int ADC_channel);
void Pump_ctrl(uint8_t cmd);


/**
 * @brief  Main program for Seed Nursury monitoring.
 * @param  None
 * @retval None
 */
int main(void)
{
		//int i = 0;
		SystemInit();
		
		/* SysTick_Config */
    SysTick_Config((GetSystemClock() / 1000));

    /* Set WZ_100US Register */
    setTIC100US((GetSystemClock() / 10000));
		
		UART_Config();
		DUALTIMER_Config();
		GPIO_Config();

		printf("SURF5 Wiznet Contest with W750x Standard Peripheral Library version : %d.%d.%d\r\n", __W7500X_STDPERIPH_VERSION_MAIN, __W7500X_STDPERIPH_VERSION_SUB1, __W7500X_STDPERIPH_VERSION_SUB2);
	
    printf("SourceClock : %d\r\n", (int) GetSourceClock());
    printf("SystemClock : %d\r\n", (int) GetSystemClock());

    printf("Starting reading from sensors\r\n");  
		
	
  while (1) {
		
		SoilMoisture=Read_ADC(7);
		if(SoilMoisture>500){
			Pump_ctrl(0);
			printf("Shutting down irrigation\r\n");
		}else{
			Pump_ctrl(1);
			printf("Starting irrigation\r\n");
		};
	
		Delay(500);	
    }

    return 0;
}



/* Private functions ---------------------------------------------------------*/

/**
 * @brief  Configures the UART Peripheral.
 * @note
 * @param  None
 * @retval None
 */
static void UART_Config(void)
{
    UART_InitTypeDef UART_InitStructure;

    UART_StructInit(&UART_InitStructure);

    S_UART_Init(115200);
    S_UART_Cmd(ENABLE);

}

/**
 * @brief  Configures the GPIO Peripheral.
 * @note
 * @param  None
 * @retval None
 */
static void GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Direction = GPIO_Direction_IN;
    GPIO_InitStructure.GPIO_Pad = GPIO_Pad_Default;
    GPIO_InitStructure.GPIO_AF = PAD_AF0;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
		
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Direction = GPIO_Direction_OUT;
    GPIO_InitStructure.GPIO_Pad = GPIO_OpenDrainDisable | GPIO_HighDrivingStrength | GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_AF = PAD_AF1;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

}

/**
 * @brief  Configures the DUALTIMER Peripheral.
 * @note
 * @param  None
 * @retval None
 */
static void DUALTIMER_Config(void)
{
    DUALTIMER_InitTypDef DUALTIMER_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    DUALTIMER_InitStructure.Timer_Load = GetSystemClock() / 1; //1s
    DUALTIMER_InitStructure.Timer_Prescaler = DUALTIMER_Prescaler_1;
    DUALTIMER_InitStructure.Timer_Wrapping = DUALTIMER_Periodic;
    DUALTIMER_InitStructure.Timer_Repetition = DUALTIMER_Wrapping;
    DUALTIMER_InitStructure.Timer_Size = DUALTIMER_Size_32;
    DUALTIMER_Init(DUALTIMER0_0, &DUALTIMER_InitStructure);

    DUALTIMER_ITConfig(DUALTIMER0_0, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = DUALTIMER0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    DUALTIMER_Cmd(DUALTIMER0_0, ENABLE);
}

/**
 * @brief  Inserts a delay time.
 * @param  nTime: specifies the delay time length.
 * @retval None
 */
void Delay(__IO uint32_t milliseconds)
{
    TimingDelay = milliseconds;
		
		while (TimingDelay != 0) {
        ;
    };
		
}

/**
 * @brief  Decrements the TimingDelay variable.
 * @param  None
 * @retval None
 */
void TimingDelay_Decrement(void)
{
    if (TimingDelay != 0x00) {
        TimingDelay--;
    }
}

/**
 * @brief  Reading from ADC Channel Ch.
 * @param  CH : The ADC Channel
 * @retval The ADC returned value
 */
int Read_ADC(int ADC_channel)
{
	int ADC_Value;
	ADC_Cmd(ENABLE);
  ADC_ChannelConfig(ADC_channel);
  ADC_StartOfConversion();
	ADC_Value = ADC_GetConversionValue();
  printf("ADC_GetConversionValue : [%d] : %d,\r\n", ADC_channel, ADC_Value);
  ADC_Cmd(DISABLE);
	return ADC_Value;
}

/**
 * @brief  Reading from ADC Channel Ch.
 * @param  cmd : controlling irrigation 0 shutting down greater than 0 starting irrigation
 * @retval None
 */
void Pump_ctrl(uint8_t cmd)
{
	if(cmd>0){
		GPIO_SetBits(GPIOC, GPIO_Pin_15);
		GPIO_SetBits(GPIOC, GPIO_Pin_14);
	}else{
		GPIO_ResetBits(GPIOC, GPIO_Pin_15);
		GPIO_ResetBits(GPIOC, GPIO_Pin_14);
	}
}


#ifdef  USE_FULL_ASSERT

/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif
