#include "hal_data.h"
#include "debug_uart/bsp_debug_uart.h"
#include "led/bsp_led.h"
#include "gpt/bsp_gpt_pwm_output.h"
#include "hs3003/bsp_hw_i2c.h"
#include "hs3003/bsl_hs3003.h"
#include "bsp_esp8266/bsp_wifi_esp8266.h"
#include "VL53L0X/bsl_vl53l0x.h"

/*
通信协议：
发送：
温湿度
MLJ+TH=000,000\r\n  温湿度
MLJ+FEED=0000\r\n   食物剩余
MLJ+CAT=0000\r\n    猫猫距离
 */


#define VERSION "V1.0.0"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
    char tcpSendData[100] = {0};
    s_Hs300xDataType humi_temp;
    uint16_t higValue = 0;
    uint16_t catValue = 0;


    /* TODO: add your own code here */

    LED_Init(); // LED 初始化
    Debug_UART4_Init(); // SCI4 UART 调试串口初始化
    printf("Start Run %s!!!\r\n", VERSION);
    GPT_PWM_Init(); // GPT 初始化
    i2c_init();
    bsl_vl53l0x_init();
    ESP8266_AP_Test();



    while(1)
    {
        LED1_ON;
        R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
        LED1_OFF;
        R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);

        humi_temp = Hs300x_ReadData();
        printf("MLJ+TH=%03ld,%03ld\r\n", (uint32_t)humi_temp.temp, (uint32_t)humi_temp.humi);
        sprintf(tcpSendData, "MLJ+TH=%03ld,%03ld\r\n\0", (uint32_t)humi_temp.temp, (uint32_t)humi_temp.humi);
        ESP8266_AT_Send(tcpSendData);   //发送温湿度
        R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
        higValue = bsl_vl53l0x_hig_single();
        printf("MLJ+FEED=%04d\r\n", higValue);
        sprintf(tcpSendData, "MLJ+FEED=%04d\r\n\0", higValue);
        ESP8266_AT_Send(tcpSendData);   //发送温湿度
        R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
        catValue = bsl_vl53l0x_cat_single();
        printf("MLJ+CAT=%04d\r\n", catValue);
        sprintf(tcpSendData, "MLJ+CAT=%04d\r\n\0", catValue);
        ESP8266_AT_Send(tcpSendData);   //发送温湿度
    }
#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

/*******************************************************************************************************************//**
 * This function is called at various points during the startup process.  This implementation uses the event that is
 * called right before main() to set up the pins.
 *
 * @param[in]  event    Where at in the start up process the code is currently at
 **********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
#endif
