#include "tool.h"
#include "bsp_time.h"
#include "system.h"

#if JS_RTT_DEBUG_EN

char JS_RTT_UpBuffer[4096];    // J-Scope RTT Buffer
int  JS_RTT_Channel = 1;       // J-Scope RTT Channel
acValBuffer_t acValBuffer;


void JS_RTT_Init(void)
{
	SEGGER_RTT_ConfigUpBuffer(JS_RTT_Channel, "JScope_I4I4I4I4I4I4", &JS_RTT_UpBuffer[0], sizeof(JS_RTT_UpBuffer), SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);	
}

void JS_RTT_Print(signed int Sine1,signed int Sine2,signed int Sine3)
{
	acValBuffer.Sine1 = Sine1;
	acValBuffer.Sine2 = Sine2;
	acValBuffer.Sine3 = Sine3;
	SEGGER_RTT_Write(JS_RTT_Channel, &acValBuffer, sizeof(acValBuffer));
}

void JS_RTT_Print_06(signed int Sine1,signed int Sine2,signed int Sine3,signed int Sine4,signed int Sine5,signed int Sine6)
{
	acValBuffer.Sine1 = Sine1;
	acValBuffer.Sine2 = Sine2;
	acValBuffer.Sine3 = Sine3;
	acValBuffer.Sine4 = Sine4;
	acValBuffer.Sine5 = Sine5;
	acValBuffer.Sine6 = Sine6;
	SEGGER_RTT_Write(JS_RTT_Channel, &acValBuffer, sizeof(acValBuffer));
}

static void Tool_Process(void)
{
	static uint32_t toogle = 60000;
	if(toogle == 60000)toogle = 1;
	else toogle = 60000;
	acValBuffer.Sine1 = toogle;
//	SEGGER_RTT_Write(JS_RTT_Channel, &acValBuffer, sizeof(acValBuffer));
}
#endif

/**
	@brief 求变异系数的绝对值
	@param p_array-[in] 数组地址
	@param len-[in] 数组成员个数
	@return 变异系数的绝对值
*/
float32_t CoefficientVariation(double *p_array, uint32_t len)
{
	int i;
	float32_t sum = 0;	//总和
	float32_t avg;			//平均值
	float32_t spow = 0;
	for(i=0;i<len;i++)sum += p_array[i];//求总和
	avg = sum/len;//计算平均值
	for(i=0;i<len;i++)spow += (p_array[i]-avg)*(p_array[i]-avg);//平方累加
	
	float32_t standardDeviation=0,Coefficient_variation=0;

//	static char string[200];
	if(!arm_sqrt_f32((spow/len),&standardDeviation))
	{
//		sprintf(string,"standardDeviation:%f\r\n",standardDeviation);
//		SEGGER_RTT_printf(0,"%s",string);
		standardDeviation = (standardDeviation)/avg;
		arm_abs_f32(&standardDeviation,&Coefficient_variation,1);
//		sprintf(string,"Coefficient_variation:%f\r\n",Coefficient_variation);
//		SEGGER_RTT_printf(0,"%s",string);
	}

	return Coefficient_variation;
}

void Tool_Init(void)
{
	#if JS_RTT_DEBUG_EN
	Process_Start(10,"Tool_Process",Tool_Process);
	JS_RTT_Init();
	#endif
}







