#ifndef DATA_PACKET_H
#define DATA_PACKET_H

#include <stdint.h>

#define PACKET_START_BYTE 0xFFFFFFFF
#define PACKET_END_BYTE   0x0000

// 数据包结构 - 启用校验和和包尾
typedef struct __attribute__((packed)) {
    uint32_t start_byte;        // 包头 (4字节)
    uint32_t timestamp;         // 时间戳 (4字节)
    int32_t adc_data1;          // ADC1 数据 (4字节)
    int32_t adc_data2;          // ADC2 数据 (4字节)
    int32_t adc_data3;          // ADC3 数据 (4字节)
    uint16_t checksum;          // CRC16校验和 (2字节)
    uint16_t end_byte;          // 包尾 (2字节)
} DataPacket_t;

// 校正后数据包结构
typedef struct __attribute__((packed)) {
    uint32_t start_byte;        // 包头
    uint32_t timestamp;         // 时间戳
    float corrected_x;          // 校正后X轴数据
    float corrected_y;          // 校正后Y轴数据
    float corrected_z;          // 校正后Z轴数据
    uint16_t checksum;          // CRC16校验和
    uint16_t end_byte;          // 包尾
} CorrectedDataPacket_t;

// 函数声明
uint16_t Calculate_CRC16(const uint8_t *data, uint16_t len);
void PackData(DataPacket_t *packet, int32_t adc1, int32_t adc2, int32_t adc3);
void PackCorrectedData(CorrectedDataPacket_t *packet, float x, float y, float z);
uint8_t ValidatePacket(const DataPacket_t *packet);
uint8_t ValidateCorrectedPacket(const CorrectedDataPacket_t *packet);

#endif // DATA_PACKET_H
