#include "PLSR.h"
#include "PSCARR.h"
#include "tim.h"
#include "sram.h"
#include <string.h>
#include "modbus.h"
#include "bitset.h"

#include <stdio.h>

Pulse PulseOutput[10]; // 十段脉冲的配置
Option Options;


uint8_t NowPulse;     /* 当前处于第几段脉冲 */
uint8_t PrePulse;     /* 之前处于第几段脉冲 */
uint8_t PulseNum;     /* 一共有多少段脉冲 */
int32_t MAX_Pulse;    /* 最多的脉冲数 */
int32_t AccUpCount;     /* 加速脉冲数 */
int32_t AccDownCount;    /* 减速脉冲数 */
int32_t NowMAXPulse;  /* 当前要达到的总脉冲 */

uint32_t NowFrequency;

int32_t PulseCount = 0;

/**
* @brief 根据脉冲的频率设置对应的PSC和ARR
* @param[in] SentPost  脉冲通道
* @param[in] Ferquency 脉冲的频率
* @return 无
*/
void SetFrequency(uint8_t SentPost, uint32_t Frequency)
{
	CalculatePSCARR(Frequency, 72000000, 65535, &psc, &arr);
	printf("Fre=%d,",Frequency);
	switch (SentPost)
	{
		case 0: TIM10->ARR = arr, TIM10->PSC = psc, TIM10->CCR1 = arr / 2; break;
		case 1: TIM11->ARR = arr, TIM11->PSC = psc, TIM11->CCR1 = arr / 2; break;
		case 2: TIM13->ARR = arr, TIM13->PSC = psc, TIM13->CCR1 = arr / 2; break;
		case 3: TIM14->ARR = arr, TIM14->PSC = psc, TIM14->CCR1 = arr / 2; break;		
	}
	
}

/**
* @brief 脉冲开始
* @param[in] StartPulse 起始脉冲段
* @return 无
*/
void PulseStart(void)
{
	SetNextPulse();		
	switch(Options.SentPost)
	{
		case 0: HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1); break;
		case 1: HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1); break;
		case 2: HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1); break;
		case 3: HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); break;		
	}
	
}

/**
* @brief 获取加速度
* @param[in] Start     起始脉冲频率
* @param[in] End       目标脉冲频率
* @param[in] AccCount  需要多少个脉冲完成加速
* @return 加速度
*/
float GetAcc(int32_t Start, int32_t End, int32_t AccCount)
{
	return ((float)End - (float)Start) / (float)AccCount;
}

/**
* @brief 设置结果脉冲数
* @param[in] PulseCount    要设定的脉冲数
* @return 无
*/
void MAXPulseSet(int32_t PulseCount)
{
	if (NowPulse == 0)  
	{
		NowMAXPulse = PulseOutput[NowPulse].PulseCount;
	}
	else
	{
		NowMAXPulse += PulseOutput[NowPulse].PulseCount;
	}
}

/**
* @brief 设置下一次进入中断的脉冲数
* @return 无
*/
void SetNextPulse(void)
{	
	int32_t temp;	
	if (Options.RunMod == 0) /* 如果是相对模式 */
	{
		if (PulseOutput[NowPulse].PulseCount > 0)
		{
			TIM2->ARR = PulseOutput[NowPulse].PulseCount;
		}
		else
		{
			TIM2->ARR = - PulseOutput[NowPulse].PulseCount;
		}
		
	}
	else if (Options.RunMod == 1)/* 如果是绝对模式 */
	{
		if (PulseOutput[NowPulse].PulseCount * PulseOutput[PrePulse].PulseCount >= 0)
		{
			temp = PulseOutput[NowPulse].PulseCount - PulseOutput[PrePulse].PulseCount;
		}
		else
		{
			temp = PulseOutput[NowPulse].PulseCount + PulseOutput[PrePulse].PulseCount;
		}
		if(temp >= 0)
		{
			TIM2->ARR = temp;
		}
		else
		{
			TIM2->ARR = - temp;
		}
		if (Options.StartPulse == NowPulse)
		{
			TIM2->ARR = PulseOutput[NowPulse].PulseCount;
		}
	}
}

/**
* @brief 从寄存器读取设置参数
* @return 无
*/
void PLSROptionLoad(void)
{
	/* 数据转换 */
	uint32_t InitSpeed;
	uint16_t tempH, tempL;
	Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
	Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
	Set16_32(&InitSpeed, &tempL, &tempH);
	
	/* 数据读取 */
	Options.SentPost    = Register_L[0x1000];
	Options.DirPost     = Register_L[0x1001];
	Options.EXT         = Register_L[0x1002];
	Options.DirDelay    = Register_L[0x1003];  //可能更大。
	Options.Dir         = Register_L[0x1004];
	Options.AccMod      = Register_L[0x1005];
	Options.RunMod      = Register_L[0x1006];
	Options.AllPulse    = Register_L[0x1007];
	Options.StartPulse  = Register_L[0x1008] - 1;
	Options.InitSpeed   = InitSpeed;
	Options.AccUpTime   = (Register_H[0x100B]<< 8) | Register_L[0x100B];
	Options.AccDownTime = (Register_H[0x100C]<< 8) | Register_L[0x100C];

}

/**
* @brief 从寄存器读取脉冲参数
* @return 无
*/
void PLSRPluseLoad(void)
{
	uint32_t temp_Frequency, temp_PulseCount;
	uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;	
	for (int i = 0; i < 10; i++)
	{
		Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
				, &Register_L[0x1100 + 0x10 * i]);
		Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
				, &Register_L[0x1101 + 0x10 * i]);
		Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
		
		Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
				, &Register_L[0x1102 + 0x10 * i]);
		Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
				, &Register_L[0x1103 + 0x10 * i]);
		Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);		
		
		PulseOutput[i].Frequency  = temp_Frequency;
		PulseOutput[i].PulseCount = temp_PulseCount;
		PulseOutput[i].EXT        = Register_L[0x1104 + 0x10 * i];
		PulseOutput[i].NextPulse  = Register_L[0x1105 + 0x10 * i];
		
	}
}

/**
* @brief 保存总的脉冲数至指定的寄存器
* @return 无
*/
void CountSave(void)
{
	int32_t temp_Count = 0;
	uint16_t DatH, DatL;
	
	
	memcpy(&temp_Count, &AllPulseCNT, 4);
	DatH = (temp_Count >> 16);
	DatL = (temp_Count & 0x0000ffff);
	Register_H[0x2001] = (DatH >> 8);
	Register_L[0x2001] = (DatH & 0x00ff);
	Register_H[0x2000] = (DatL >> 8);
	Register_L[0x2000] = (DatL & 0x00ff);
}

