#include "rotate_motor.h"
#include "stm32f10x.h"
#include "common.h"

// 初始化定时器 TIM3 函数
void TIM3_CH1_Init(uint16_t psc,uint16_t arr){
    // 使能 TIM3 时钟
    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;

    // 设置预分频器为 72-1，使时钟频率为 1 MHz (72 MHz / 72)
    TIM3->PSC = psc;//72 - 1;

    // 设置自动重装载值为 1000 - 1，对应于 1 毫秒 (1 MHz * 1 ms)
    TIM3->ARR =arr;//1000 - 1;

    // 配置 TIM3 通道 1 为 PWM 模式 1
    TIM3->CCMR1 &= ~TIM_CCMR1_OC1M;                           // 清空模式位
    TIM3->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2;       // PWM 模式1

    TIM3->CCER |= TIM_CCER_CC1E;                              // 使能通道 1 输出
    TIM3->CCR1 = 500;                                         // 设置占空比为 50%

    // 使能 TIM3
    TIM3->CR1 |= TIM_CR1_CEN;
}


// 设置方向
// 设置方向函数
void Rotate_SetDirection(int direction){
    if (direction){
        GPIOC->ODR |= (1 << 5);   // 设置高电平 (顺时针方向)
    } else {
        GPIOC->ODR &= ~(1 << 5);  // 设置低电平 (逆时针方向)
    }
}



// 使能控制函数 
void Rotate_Enable_Rotate(bool enable) {
    if (enable) {
        GPIOF->ODR &= ~(1 << 11);  // 设置低电平 (使能步进电机)
    } else {
        GPIOF->ODR |= (1 << 11);   // 设置高电平 (关闭步进电机)
    }
}

// 设置PWM频率
void Set_PWM_Frequency(uint32_t frequency) {
    // 计算ARR值
    uint32_t arr_value = (SYSCLK_FREQUENCY / ((TIM3->PSC + 1) * frequency)) - 1;

    // 更新自动重装载寄存器
    TIM3->ARR = arr_value;
    TIM3->CCR1 = arr_value / 2;  // 保持50%占空比
}


// 梯形加速函数
void RotateMotor_Accelerate(uint32_t targetRPM, uint32_t acceleration) {
    uint32_t currentRPM = 0;
    uint32_t deltaRPM = (acceleration / 60000) * 10; // 10ms内的速度增量

    while (currentRPM < targetRPM) {
			if (currentRPM == 0) {
        currentRPM = 100;
      }
        // 更新PWM频率
        uint32_t pwmFrequency = (currentRPM * STEPMOTOR_PULSEPERROUND) / 60;
        Set_PWM_Frequency(pwmFrequency);

        // 每次加速的增量
        currentRPM += deltaRPM;
        if (currentRPM > targetRPM) {
            currentRPM = targetRPM;  // 防止超过目标速度
        }

        // 延时 10ms，每次迭代增加速度				
				Delay(10, 'm');
    }

    // 达到目标速度，保持目标速度的PWM频率
    uint32_t finalFrequency = (targetRPM * STEPMOTOR_PULSEPERROUND) / 60;
    Set_PWM_Frequency(finalFrequency);
}



// 梯形减速函数：将当前转速逐步减速为 0
void RotateMotor_Decelerate(uint32_t currentRPM, uint32_t deceleration) {
    uint32_t deltaRPM = (deceleration / 60000) * 10;  // 10ms 内的速度减量

    while (currentRPM > 0) {
        // 计算 PWM 频率
        uint32_t pwmFrequency = (currentRPM * STEPMOTOR_PULSEPERROUND) / 60;
        Set_PWM_Frequency(pwmFrequency);  // 更新PWM频率以调整电机速度

        // 每次减速的增量
        if (currentRPM > deltaRPM) {
            currentRPM -= deltaRPM;  // 减速
        } else {
            currentRPM = 0; 
        }

				Delay(10, 'm');
    }

    Set_PWM_Frequency(0);
}

void Update_Motor_Status(RotateMotor *motor, SpeedStatus status, uint32_t targetSpeed, uint32_t acceleration) {
    motor->base.speedStatus = status; // 更新电机的速度状态
    motor->base.desSpeed= targetSpeed; // 更新目标速度
    motor->base.acceleration = acceleration; // 更新加速度

    switch (status) {
        case SPEED_ACC:
            // 加速过程 
            RotateMotor_Accelerate(targetSpeed, acceleration);
            break;

        case SPEED_DEC:
            // 减速过程
            RotateMotor_Decelerate(motor->base.curSpeed, acceleration);
            break;

        case SPEED_NONE:
            // 匀速，保持当前速度
            Set_PWM_Frequency((motor->base.curSpeed * STEPMOTOR_PULSEPERROUND) / 60);
            break;

        case SPEED_STOP:
            // 停止电机
            motor->base.curSpeed = 0;
            Set_PWM_Frequency(0); // 关闭PWM
            break;

        default:
            break;
    }
}

