//
// Created by IRIS on 25-2-7.
//

#include "communication.h"
#define maxValue 65535
//
void Send_Shutter_Time(uint32_t shutter_time) {
    uint32_t  tt = shutter_time*1000;
    // printf("t %d \n",t);
    // printf("tt %d \n",tt);
    uint8_t t_buff[4];
    t_buff[0] = *((uint8_t *)&tt+3);
    t_buff[1] = *((uint8_t *)&tt+2);
    t_buff[2] = *((uint8_t *)&tt+1);
    t_buff[3] = *((uint8_t *)&tt);

    // HAL_UART_Transmit(&huart1,t_buff,4,100);

    __HAL_UART_ENABLE(&huart2);
    HAL_UART_Transmit(&huart2,t_buff,4,100);
}


uint32_t  Opt_Snenser(int persent, uint16_t *data,uint32_t Size,uint32_t Shutter_Now) {
    int maxvalue=maxValue*1.0*persent / 100;
    int maxvaluenow = 0;
    uint32_t shutter_time;
    uint32_t max_shutter_time = 100;
    uint32_t min_shutter_time = 10;

    for (int i = 0; i < Size; i++) {
        if (data[i] > maxvaluenow) {
            maxvaluenow = data[i];
        }
    }

    if (Shutter_Now<=min_shutter_time || Shutter_Now>=max_shutter_time)  return 0;

    if (maxvaluenow<maxvalue*0.95 || maxvaluenow>maxvalue) {
        shutter_time = Shutter_Now * maxvaluenow / maxvalue;
        Send_Shutter_Time(shutter_time);

        HAL_Delay(1000);
        return shutter_time;
    }

    return 0;
}




