/*****************************************************************************
 * file    Inc/position.h 
 * author  Changjiale
 * version 1.0.0
 * date    18-Sep-2023
 * brief   This file provides all the headers of position function.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2023 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of WTOE nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

#ifndef __POSITION_H__
#define __POSITION_H__

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include "common.h"

/* Define --------------------------------------------------------------------*/
#define POSITION_BUF_LIST_LEN 4     //DMA FIFO 队列数
#define POSITION_CMD_BUF_LEN 1031   //最大消息接收长度

#define POSITION_HEAD_LEN 6     //消息头长度
#define POSITION_STATE_LEN 7    //命令0消息长度
#define POSITION_CFG_LEN 15     //命令1消息长度
#define POSITION_QUERY_LEN 7    //命令2消息长度
#define POSITION_WAVE_LEN 19    //命令3消息长度

#define POSITION_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define POSITION_ERR_MAX 3

enum POSITION_CMD
{
    POSITION_STATE = 0,     //命令字0，获取定位模块状态
    POSITION_CFG,           //命令字1，设置行波捕获配置
    POSITION_QUERY,         //命令字2，获取最新行波捕获结果
    POSITION_WAVE           //命令字3，获取一个行波波形数据
};

enum POSITION_PKT_STATE
{
    POSITION_PKT_NONE = 0,      //未收到
    POSITION_PKT_HALF,          //已收到但未接收完成
    POSITION_PKT_COMPLETE       //消息接收完成
};

typedef struct
{
    uint8_t cmd_send_buf[POSITION_WAVE_LEN];        //发送命令数组
    uint8_t cmd_receive_buf[POSITION_CMD_BUF_LEN];  //接收命令数组
    uint16_t cmd_buf_index;
    uint8_t pkt_id;
    uint8_t pkt_state;                              //消息接收状态指示
} _position_ctrl_t;

typedef struct
{
    struct
    {
        float v;          //幅值
        uint32_t s;       //秒时刻
        uint32_t ns;      //纳秒时刻
    } wave_info[POSITION_SUM];
    uint32_t file_s;                //录波文件秒
    uint32_t file_ns;               //录波文件纳秒
    uint32_t file_feq;              //录波文件频率
} _position_data_t;

/* Exported types ------------------------------------------------------------*/
/* DMA buf 数组. */
typedef struct
{
    uint8_t buf[POSITION_BUF_LIST_LEN][POSITION_CMD_BUF_LEN];   //DMA软件fifo
    uint16_t buf_cnt[POSITION_BUF_LIST_LEN];    //fifo中存储数据数
    uint16_t buf_addr;      //指示串口缓冲区接收数据起始地址
    uint8_t valid;
    uint8_t current;
} position_buf_list_t;

typedef struct
{
    uint8_t cfg;                                //配置录波触发阈值
    uint8_t new;                                //新行波数据
    uint8_t position_wave_up;			        //行波波形数据上传
    uint8_t old_status;                         //故障定位设备之前状态
    uint8_t offline;                            //故障定位设备是否离线
    uint8_t dma_rx_buf[POSITION_CMD_BUF_LEN];   //串口接收缓冲区
    uint32_t position_file_s;			        //行波波形文件UTC索引
    uint32_t position_file_ns;			        //行波波形文件ns索引
    uint8_t err_cnt;                            //串口错误计数
    uint8_t send_cnt;                           //串口发送计数
    uint8_t complete;                           //已轮询标志
} position_ctrl_t;

/* Exported macro ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
extern position_buf_list_t position_buf_list_rx;
extern position_ctrl_t position_ctrl;

/* Extern functions ----------------------------------------------------------*/
extern HAL_StatusTypeDef position_start(void);
extern HAL_StatusTypeDef position_wave(uint8_t *wl_data, uint16_t index, uint16_t *data_len, uint32_t file_s, uint32_t file_ns);
extern void position_init(void);

#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/