//
// Created by dell on 24-12-3.
//

#include "rs485.h"

extern uint32_t g_temperature_uint32;
extern uint32_t g_eddy_current_value_uint32;

uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
uint8_t package_data[4] = {0};

void rs485_config(void) {
    rcu_periph_clock_enable(RS485_GPIO_RCU);
    rcu_periph_clock_enable(RS485_RCU);

    gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);

    /* configure USART Tx&Rx as alternate function push-pull */
    gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
    gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN);

    /* configure RS485 EN Pin */
    gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN);
    gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
    gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET);

    /* USART configure */
    usart_deinit(RS485_PHY);
    usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
    usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
    usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);

    usart_enable(RS485_PHY);

    nvic_irq_enable(USART0_IRQn, 0);
    usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
    usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
}

void process_command(uint8_t *cmd, size_t length) {
    char combined_str[3];
    validation_result_t validate = VALIDATION_SUCCESS;

    validate = (validate_package_header(cmd) |
                validate_package_type(cmd) |
                validate_data_length(cmd) |
                validate_package_crc(cmd, length));

    switch (validate) {
        case VALIDATION_SUCCESS:
            // printf("%d", length);
            sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
            if (strcmp(combined_str, "M1") == 0) {
                eddy_current_value_report();
            } else if (strcmp(combined_str, "M2") == 0)
            {
                tempture_value_report();
            } else if (strcmp(combined_str, "M3") == 0)
            {
                printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
                reset_mcu();
            } else {
                printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
                return;
            }
            break;
        case VALIDATION_CRC_ERROR:
            printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
            break;
        case VALIDATION_HEADER_ERROR:
            printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
            break;
        case VALIDATION_TYPE_ERROR:
            printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
            break;
        case VALIDATION_LENGTH_ERROR:
            printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
            break;
        default:
            break;
    }
}

uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
    uint8_t crc = 0;

    for (uint8_t i = 1; i < data_length - 1; i++) {
        crc += data[i];
    }

    return (uint8_t) (crc & 0xFF);
}

validation_result_t validate_package_crc(uint8_t *data, uint8_t data_length) {
    if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_CRC_ERROR;
    }
}

validation_result_t validate_package_header(uint8_t *data) {
    if (data[0] == LDC1612_PACKAGE_HEADER) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_HEADER_ERROR;
    }
}

validation_result_t validate_package_type(uint8_t *data) {
    if (data[1] == LDC1612_BOARD_TYPE) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_TYPE_ERROR;
    }
}

validation_result_t validate_data_length(uint8_t *data) {
    if (data[2] == LDC1612_PACKAGE_LENGTH) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_LENGTH_ERROR;
    }
}

void eddy_current_value_report(void) {
    static uint32_t eddy_current_value_uint32 = 0;

    eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);

    package_data[0] = (eddy_current_value_uint32 >> 24) & 0xFF;
    package_data[1] = (eddy_current_value_uint32 >> 16) & 0xFF;
    package_data[2] = (eddy_current_value_uint32 >> 8) & 0xFF;
    package_data[3] = eddy_current_value_uint32 & 0xFF;

    uint8_t combined_data[7];
    memcpy(combined_data, package_header, 3);
    memcpy(combined_data + 3, package_data, 4);

    printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
    printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
    printf("%c", calculate_crc(combined_data, 8));
}

void tempture_value_report(void) {
    static uint32_t temperature_uint32 = 0;

    temperature_uint32 = tmp112a_get_raw_channel_result();

    package_data[0] = (temperature_uint32 >> 24) & 0xFF;
    package_data[1] = (temperature_uint32 >> 16) & 0xFF;
    package_data[2] = (temperature_uint32 >> 8) & 0xFF;
    package_data[3] = temperature_uint32 & 0xFF;

    uint8_t combined_data[7];
    memcpy(combined_data, package_header, 3);
    memcpy(combined_data + 3, package_data, 4);

    printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
    printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
    printf("%c", calculate_crc(combined_data, 8));
}
