/*
 * @Author: mbw
 * @Date: 2024-10-09 08:42:14
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2024-12-12 15:28:32
 * @FilePath: \JT-DT-YD4N02A_RTT_MRS-4G\bsp\src\bsp_emv.c
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
#include "bsp_emv.h"
#include "rtthread.h"
#include "pin.h"
#include "bsp_flash.h"


#define LOG_TAG "bsp.emv"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>

#define EMV_LINKED       0x02
#define EMV_FUNCTION_ON  0x04
#define EMV_STATUS_OPEN  0x07
#define EMV_STATUS_CLOSE 0x06
#define EMV_NOT_CONNECTED 0x04
#define EMV_FUNCTION_OFF  0x00

volatile rt_uint16_t emv_state_flag = 0; // 0:关闭，1：打开
struct rt_timer emv_timer;

// 获取电磁阀连接状态
static int Get_Emv_Link_Status(void)
{
    // TODO: 需要有检测功能,默认连接，其实只要有这个功能开关就行，连接状态只对上报服务器相关，本地动作不受影响
    return EMV_LINKED;
    // return EMV_NOT_CONNECTED;
}

// 获取电磁阀状态
int Get_Emv_Status(void)
{
    rt_uint8_t emv_state = (rt_uint8_t)Flash_Get_SysCfg(kEmagneticSwitchId) & 0x07; // 获取开关阀功能开关状态
    if (emv_state & EMV_FUNCTION_ON) // 阀门功能开启了
    {
        LOG_D("具备阀门功能");
        if (Get_Emv_Link_Status() & EMV_LINKED)
        {
            LOG_D("阀门已经连接");
            if (rt_pin_read(EMV_PIN)) // 高电平
            {
                return EMV_STATUS_OPEN;
            }
            else // 低电平
            {
                return EMV_STATUS_CLOSE;
            }
        }
        else
        {
            LOG_D("阀门未连接");
            return EMV_NOT_CONNECTED; // 0x04为未连接
        }
    }
    return EMV_FUNCTION_OFF; // 0阀门功能未开启
}

/*
 * @brief  设置电磁阀状态
 * 0x07 为打开，0x06为关闭
*/
int BSP_Set_Emv_Status(rt_bool_t status)
{
    rt_uint8_t ret = Get_Emv_Status();
    if (ret == EMV_FUNCTION_OFF || ret == EMV_NOT_CONNECTED) // 功能未开启或未连接
    {
        return (ret | 0xB0);
    }
    else
    {
        if (status == 0)//关阀指令
        {
            if (ret == EMV_STATUS_CLOSE) // 阀门已经关闭
            {
                return EMV_STATUS_CLOSE;
            }
            else
            {
                emv_state_flag = 0;
                return EMV_STATUS_CLOSE;
            }
        }
        else //开阀指令
        {
            if (ret == EMV_STATUS_OPEN) // 阀门已经打开
            {
                return EMV_STATUS_OPEN;
            }
            else
            {
                emv_state_flag = 1;
                return EMV_STATUS_OPEN;
            }
        }
    }
}

// 定时器回调函数
void BSP_EMV_Timer_Callback(void *parameter)
{
    static rt_uint8_t last_pos = 0;
    static rt_uint8_t pos             = 0;

    pos = emv_state_flag;

    // 执行电磁阀动作逻辑
    if (last_pos != pos)
    {
        if (pos == 1)
        {
            LOG_D("EMV_ON");
            rt_pin_write(EMV_PIN, 1); //关闭电磁阀
        }
        else if (pos == 0)
        {
            LOG_D("EMV_OFF");
            rt_pin_write(EMV_PIN, 0);//  断开连接
        }
        last_pos = pos;
    }
}

// 初始化电磁阀
int BSP_EMV_Init(void)
{
    rt_pin_mode(EMV_PIN, PIN_MODE_OUTPUT);
    EMV_ON;

    // LOG_D("BSP_EMV_Init");
    // rt_timer_init(&emv_timer, "emv_timer", BSP_EMV_Timer_Callback, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC);
    // rt_timer_start(&emv_timer); // 启动定时器
    return RT_EOK;
}
// INIT_DEVICE_EXPORT(BSP_EMV_Init);
