动态延时事件+无极步进模型

语言的精炼程度是高质量开发的必要组成因素。

应用逻辑业务编程原则之一(重点)。

在逻辑实现和内存节省间权衡时,应果断选择逻辑实现,不要因为节省十几个bit却增加很多重复逻辑的代码行,尤其是在同类操作多对象复用时的调试阶段,要知道,这些可都是需要维护的。一个改了另一个也要改, 一旦写的散,那就是个致命的耗时工作了。

 能精简一定要精简,能通用化一定要通用化。这样才能减少失误,提高开发效率。这也是解耦的意义。

下面    

       uint16_t dir_ch0         : 1; /*1:increase 0:decrease*/
        uint16_t dir_ch1        : 1;

就是把功能跟逻辑耦合了,为了节省1个字节,结果调试起来非常费劲,一处改,处处改。不如直接用两个字节。

/*****************************************************************************
 * 
 * adjust the voatage
 * if there has new pwm cfg that generate adc work,then back to case 2
 * 
 *******************************************************************************/
#include "CDcAdjust.h"

#define DC_RESOLUTION	5	/*0.05*/
/************************************************************
 * adjust voltage according to the actual value read by adc..
*************************************************************/
enum
{
	DC_ADJUST_STEP_LAZY,/*LAZY mode  check every 5s ,if dma accured then goto active mode*/
	DC_ADJUST_STEP_ACTIVE,/*ACTIVE mode, check every 500ms ,if dma accured then goto active mode*/
	DC_ADJUST_STEP_START,
	DC_ADJUST_STEP_READ,/*ACTIVE mode, check every 500ms ,if dma accured then goto active mode*/
	DC_ADJUST_STEP_ADJUST,
};
typedef union _adjust_t
{
    uint16		adjustCfg;
    struct
    {
		uint16_t modeStore      : 1; /*0: slow  1: fast*/
		uint16_t step 		    : 3;
		uint16_t dir_ch0 	    : 1; /*1:increase 0:decrease*/
        uint16_t dir_ch1        : 1;
		uint16_t dmaOk      : 1; /*DMA ADC convert over*/
		uint16_t pwmNew 	: 1; /*only external will set this bit*/
	}adjustBit;
}tAdjust_t;

typedef struct __dilemma
{
	uint16_t diffInc;
	uint16_t diffDec;	
}tDilemma_t;

typedef struct _voltageAdjust_t
{
	uint8_t forceStop;
	tAdjust_t adjParam; 
	uint16_t activeCount;
    uint16_t dcDiff[MaxLpNum];
	uint16_t cntInc[MaxLpNum];
	uint16_t cntDec[MaxLpNum];
	tDilemma_t mDilemmaBuff[MaxLpNum];
	uint64_t adjustTimer;
	uint64_t checkTimeout;    /**/
}tDcAdjust_t;

static tDcAdjust_t mDCAdjust = {0};

void SetDmaOk_DcAdjust(void)
{
	JBF_SET(mDCAdjust.adjParam.adjustBit.dmaOk);
}

void ResetDmaOk_DcAdjust(void)
{
	JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
}

void SetPwmUdt_DcAdjust(void)
{
	JBF_SET(mDCAdjust.adjParam.adjustBit.pwmNew);
}

void ResetPwmUdt_DcAdjust(void)
{
	JBF_CLEAR(mDCAdjust.adjParam.adjustBit.pwmNew);
}

static void DcAdjustCountIncrese(uint8_t ch, const uint8_t dir)
{
	if (dir == 1)
	{
		mDCAdjust.cntInc[ch]++;
	}

	if(dir == 0)
	{
		mDCAdjust.cntDec[ch]++;
	}
}

/*to check if adjust is on a dilemma*/
static uint8_t DcAdjustCheckdilemma(uint8_t ch)
{
	return ((mDCAdjust.cntInc[ch] > 5) && (mDCAdjust.cntDec[ch] > 5));
}

static void DcAdjustCountClear(void)
{
	mDCAdjust.forceStop = 0;
	/*ch0*/
	mDCAdjust.cntInc[0]= 0;
	mDCAdjust.cntDec[0]= 0;
	mDCAdjust.mDilemmaBuff[0].diffDec = 0;
	mDCAdjust.mDilemmaBuff[0].diffInc = 0;

	/*ch1*/
	mDCAdjust.cntInc[1]= 0;
	mDCAdjust.cntDec[1]= 0;
	mDCAdjust.mDilemmaBuff[1].diffDec = 0;
	mDCAdjust.mDilemmaBuff[1].diffInc = 0;
}
/**************************************************************************
 * 
 * if there comes new Pwm set evets cause by the lcd widget events
***************************************************************************/
static void DcAdjustClearPwmNew(void)
{
    JBF_DEBUG_PRINTF_LOG("DcAdjustClearPwmNew\r\n");
	JBF_CLEAR(mDCAdjust.adjParam.adjustBit.pwmNew);
	DcAdjustCountClear();

	mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
	mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
	mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_ACTIVE;

}

static void DcAdjustDetect(void)
{
	uint8_t i = 0;
	for ( i = 0; i < MaxLpNum; i++)
	{
		JBF_DEBUG_PRINTF_LOG("cur per:%d \r\n",	tModulePara.mLpPara[i].OutputPercent);
		JBF_DEBUG_PRINTF_LOG("cur AD:%d, cur Dc\r\n", ReadAD_CAdcDMA(i), ReadVol_CAdcDMA(i));
	}
}

static void  DcGetDcDiff(void)
{
	/*channel number*/
    uint8_t i = 0;
    uint16_t _volSet = 0;
    uint16_t _volRead = 0;

	for (i = 0; i < MaxLpNum; i++)
	{
		_volSet = ((tModulePara.mLpPara[i].OutputPercent) * (tModulePara.mLpPara[i].LpOutSta)) * 10;
		_volRead = ReadVol_CAdcDMA(i);
		// _volRead = (uint16_t)((float32_t)(_volRead) / 5.1F * 25.6F);
		_volRead = (uint16_t)((uint32_t)(_volRead) / 51 * 256);
		JBF_DEBUG_PRINTF_LOG("chid%d _volRead: %d, volset: %d \r\n", i, _volRead, _volSet);
		_volRead /= 10;
		/*dir_ch0 = 1, means increase  , dir_ch1 = 0, means decerase*/
#if 1
		mDCAdjust.dcDiff[i] = ((_volSet > _volRead) ? (_volSet - _volRead) : (_volRead - _volSet));

		if (i == 0)
		{
			mDCAdjust.adjParam.adjustBit.dir_ch0 = ((_volSet > _volRead) ? 1 : 0);
			
			/*store the diffs into cores buffer*/
			if (mDCAdjust.adjParam.adjustBit.dir_ch0 == 1)
			{
				/*last is decrese*/
				mDCAdjust.mDilemmaBuff[i].diffDec = mDCAdjust.dcDiff[i];
			}
			if (mDCAdjust.adjParam.adjustBit.dir_ch0 == 0)
			{
				/*last is increse*/
				mDCAdjust.mDilemmaBuff[i].diffInc = mDCAdjust.dcDiff[i];
			}
		}
		else if (i == 1)
		{
			mDCAdjust.adjParam.adjustBit.dir_ch1 = ((_volSet > _volRead) ? 1 : 0);

			/*store the diffs into cores buffer*/
			if (mDCAdjust.adjParam.adjustBit.dir_ch1 == 1)
			{
				mDCAdjust.mDilemmaBuff[i].diffDec = mDCAdjust.dcDiff[i];
			}
			if (mDCAdjust.adjParam.adjustBit.dir_ch1 == 0)
			{
				mDCAdjust.mDilemmaBuff[i].diffInc = mDCAdjust.dcDiff[i];
			}
		}

		/*to check if it was on the dilemma*/
		if (DcAdjustCheckdilemma(i))
		{
			mDCAdjust.forceStop = 1;
		}
#endif
		JBF_DEBUG_PRINTF_LOG("_volRead: %d, volset: %d  diff: %d\r\n", _volRead, _volSet, mDCAdjust.dcDiff[i]);
	}
}


static void DcAdjustForce(void)
{
	uint8_t _smaller = 0;

    if (mDCAdjust.dcDiff[0] > DC_RESOLUTION)
    {
		_smaller = (mDCAdjust.mDilemmaBuff[0].diffInc < mDCAdjust.mDilemmaBuff[0].diffDec)? 1 : 0;

		if (_smaller == mDCAdjust.adjParam.adjustBit.dir_ch0)
		{
        	SetbyPercent_CtimerAPwm(0, mDCAdjust.adjParam.adjustBit.dir_ch0);
		}

    }     

    /*chn 1*/
    if (mDCAdjust.dcDiff[1] > DC_RESOLUTION)
    {
		_smaller = (mDCAdjust.mDilemmaBuff[1].diffInc > mDCAdjust.mDilemmaBuff[1].diffDec);
  		if (_smaller == mDCAdjust.adjParam.adjustBit.dir_ch1)
		{
        	SetbyPercent_CtimerAPwm(1, mDCAdjust.adjParam.adjustBit.dir_ch1);
		}
    }
}
/************************************************************************** 
 * return :   0- nochange
 *            1- change
***************************************************************************/
static uint8_t DcAdjustPercent(void)
{
	uint8_t _rst = 0;

    /*chn 0*/
    if (mDCAdjust.dcDiff[0] > DC_RESOLUTION)
    {
		_rst++;
        SetbyPercent_CtimerAPwm(0, mDCAdjust.adjParam.adjustBit.dir_ch0);
		DcAdjustCountIncrese(0, mDCAdjust.adjParam.adjustBit.dir_ch0);
    }     

    /*chn 1*/
    if (mDCAdjust.dcDiff[1] > DC_RESOLUTION)
    {
		_rst++;
        SetbyPercent_CtimerAPwm(1, mDCAdjust.adjParam.adjustBit.dir_ch1);
		DcAdjustCountIncrese(1, mDCAdjust.adjParam.adjustBit.dir_ch0);
    }
	 
	return _rst;
}

static void DcAdjust(void)
{
	/*0 means sleep*/
	uint8_t _dcAdjustStep = 0;
	_dcAdjustStep = mDCAdjust.adjParam.adjustBit.step;

	switch (_dcAdjustStep)
	{
		case DC_ADJUST_STEP_LAZY:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				DcAdjustClearPwmNew();
				break;
			}
			if (mDCAdjust.checkTimeout && gJbfSysCb.timerCb.CheckTimeout(mDCAdjust.checkTimeout, 5000))
			{
				mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_LAZY;
				mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
				mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_START;
				JBF_DEBUG_PRINTF_LOG("--\r\n");
				break;
			}
			break;
		}
		case DC_ADJUST_STEP_ACTIVE:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				JBF_CLEAR(mDCAdjust.adjParam.adjustBit.pwmNew);
				DcAdjustClearPwmNew();
				mDCAdjust.activeCount = 0;
				break;
			}
			if (mDCAdjust.checkTimeout && gJbfSysCb.timerCb.CheckTimeout(mDCAdjust.checkTimeout, 20))
			{
				mDCAdjust.activeCount++;
				mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
				mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_START;
				JBF_DEBUG_PRINTF_LOG("activeCount: %d\r\n", mDCAdjust.activeCount);
				mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
				/* no pwm set event in ten sceconds, then go back to the lazy mode*/
				if (mDCAdjust.activeCount >= 100)
				{
					if (((mDCAdjust.dcDiff[0] <= DC_RESOLUTION) && (mDCAdjust.dcDiff[1] <= DC_RESOLUTION)) ||
						mDCAdjust.forceStop)
					{
						mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_LAZY;
						mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_LAZY;
						mDCAdjust.activeCount = 0;
						DcAdjustCountClear();
						JBF_DEBUG_PRINTF_LOG("--\r\n");
						break;
					}
					else
					{
						DcAdjustDetect();
					}
				}
			}
			break;
		}
		case DC_ADJUST_STEP_START /*start*/:
		{
			Start_CAdcDMA();
			ResetDmaOk_DcAdjust();
			mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_READ;
			break;
		}
		case DC_ADJUST_STEP_READ /*wait DMA*/:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				DcAdjustClearPwmNew();
				JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
				break;
			}

			/*if dma did not convert over , then quite and wait again */
			if (mDCAdjust.adjParam.adjustBit.dmaOk == 0)
			{
				break;
			}

			DcGetDcDiff();
			JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
			mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
			mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_ADJUST;
			break;
		}
		case DC_ADJUST_STEP_ADJUST:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				DcAdjustClearPwmNew();
				JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
				break;
			}

			if (mDCAdjust.forceStop)
			{
				DcAdjustForce();
				mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
			}else{
				if (DcAdjustPercent())
				{
					mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
				}
			}
	 
			mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
			mDCAdjust.adjParam.adjustBit.step = mDCAdjust.adjParam.adjustBit.modeStore;
			break;
		}
		default:
			break;
	}
}

void Init_DcAdjust(void)
{
	mDCAdjust.checkTimeout = 0;
	mDCAdjust.activeCount = 0;
	mDCAdjust.adjParam.adjustCfg = 0;
	mDCAdjust.dcDiff[0] = 0;
	mDCAdjust.dcDiff[1] = 0;
	DcAdjustCountClear();
	mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_ACTIVE;
}

void Setup_DcAdjust(void)
{
	DDL_ASSERT(gJbfSysCb.timerCb.getCurms);
	mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
}
/**************************************************************************
 * if there comes new Pwm set evets cause by the lcd widget events
***************************************************************************/
void AdjustDc_DcAdjust(void)
{
	DcAdjust();
}

优化之后

/*****************************************************************************
 * 
 * adjust the voatage
 * if there has new pwm cfg that generate adc work,then back to case 2
 * 
 *******************************************************************************/
#include "CDcAdjust.h"
#include "CSysRunFlag.h"
#include "CSysCfg.h"
#include "CSysCtrl.h"

#include "CRunCtrl.h"

#include "CParaCtrl.h"
#include "CAdcDMA.h"
#include "CTimerAPwm.h"

#define DC_RESOLUTION	5	/*0.05*/
/************************************************************
 * adjust voltage according to the actual value read by adc..
*************************************************************/
enum
{
	DC_ADJUST_STEP_LAZY,/*LAZY mode  check every 5s ,if dma accured then goto active mode*/
	DC_ADJUST_STEP_ACTIVE,/*ACTIVE mode, check every 500ms ,if dma accured then goto active mode*/
	DC_ADJUST_STEP_START,
	DC_ADJUST_STEP_READ,/*ACTIVE mode, check every 500ms ,if dma accured then goto active mode*/
	DC_ADJUST_STEP_ADJUST,
};
typedef union _adjust_t
{
    uint16		adjustCfg;
    struct
    {
		uint16_t modeStore      : 1; /*0: slow  1: fast*/
		uint16_t step 		    : 3;
		uint16_t dir_ch0 	    : 1; /*1:increase 0:decrease*/
        uint16_t dir_ch1        : 1;
		uint16_t dmaOk      : 1; /*DMA ADC convert over*/
		uint16_t pwmNew 	: 1; /*only external will set this bit*/
	}adjustBit;
}tAdjust_t;

typedef struct __dilemma
{
	uint16_t diffInc;
	uint16_t diffDec;	
}tDilemma_t;

typedef struct _voltageAdjust_t
{
	uint8_t forceStop;
	tAdjust_t adjParam; 
	uint8_t direction[2];
	uint16_t activeCount;
    uint16_t dcDiff[MaxLpNum];
	uint16_t cntInc[MaxLpNum];
	uint16_t cntDec[MaxLpNum];
	tDilemma_t mDilemmaBuff[MaxLpNum];
	uint64_t adjustTimer;
	uint64_t checkTimeout;    /**/
}tDcAdjust_t;

static tDcAdjust_t mDCAdjust = {0};

/*当前读到的电压值,在调整期间50ms更新,在通常期间5秒采集一次*/
static volatile uint16_t mVoltageRead[MaxLpNum]= {0};

uint16_t GetCurVol_DcAdjust(uint8_t ch)
{
	
	return mVoltageRead[ch];
}

void SetDmaOk_DcAdjust(void)
{
	JBF_SET(mDCAdjust.adjParam.adjustBit.dmaOk);
}

void ResetDmaOk_DcAdjust(void)
{
	JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
}

void SetPwmUdt_DcAdjust(void)
{
	JBF_SET(mDCAdjust.adjParam.adjustBit.pwmNew);
}

void ResetPwmUdt_DcAdjust(void)
{
	JBF_CLEAR(mDCAdjust.adjParam.adjustBit.pwmNew);
}

static void DcAdjustCountIncrese(uint8_t ch, const uint8_t dir)
{
	if (dir == 1)
	{
		mDCAdjust.cntInc[ch]++;
	}

	if(dir == 0)
	{
		mDCAdjust.cntDec[ch]++;
	}
}

/*to check if adjust is on a dilemma*/
static uint8_t DcAdjustCheckdilemma(uint8_t ch)
{
	return ((mDCAdjust.cntInc[ch] > 5) && (mDCAdjust.cntDec[ch] > 5));
}

static void DcAdjustCountClear(void)
{
	mDCAdjust.forceStop = 0;
	/*ch0*/
	mDCAdjust.cntInc[0]= 0;
	mDCAdjust.cntDec[0]= 0;
	mDCAdjust.mDilemmaBuff[0].diffDec = 0;
	mDCAdjust.mDilemmaBuff[0].diffInc = 0;

	/*ch1*/
	mDCAdjust.cntInc[1]= 0;
	mDCAdjust.cntDec[1]= 0;
	mDCAdjust.mDilemmaBuff[1].diffDec = 0;
	mDCAdjust.mDilemmaBuff[1].diffInc = 0;
}
/**************************************************************************
 * 
 * if there comes new Pwm set evets cause by the lcd widget events
***************************************************************************/
static void DcAdjustClearPwmNew(void)
{
    JBF_DEBUG_PRINTF_LOG("DcAdjustClearPwmNew\r\n");
	JBF_CLEAR(mDCAdjust.adjParam.adjustBit.pwmNew);
	DcAdjustCountClear();

	mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
	mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
	mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_ACTIVE;

}

static void DcAdjustDetect(void)
{
	uint8_t i = 0;
	for ( i = 0; i < MaxLpNum; i++)
	{
		JBF_DEBUG_PRINTF_LOG("cur per:%d \r\n",	tModulePara.mLpPara[i].OutputPercent);
		JBF_DEBUG_PRINTF_LOG("cur AD:%d, cur Dc\r\n", ReadAD_CAdcDMA(i), ReadVol_CAdcDMA(i));
	}
}

static void  DcGetDcDiff(void)
{
	/*channel number*/
    uint8_t i = 0;
    uint16_t _volSet = 0;
    uint16_t _volRead = 0;

	for (i = 0; i < MaxLpNum; i++)
	{
		_volSet = ((tModulePara.mLpPara[i].OutputPercent) * (tModulePara.mLpPara[i].LpOutSta)) * 10;
		_volRead = ReadVol_CAdcDMA(i);
		// _volRead = (uint16_t)((float32_t)(_volRead) / 5.1F * 25.6F);
		_volRead = (uint16_t)((uint32_t)(_volRead) / 51 * 256);
		mVoltageRead[i] = _volRead;
		JBF_DEBUG_PRINTF_LOG("chid%d _volRead: %d, volset: %d \r\n", i, _volRead, _volSet);
		_volRead /= 10;
		/*dir_ch0 = 1, means increase  , dir_ch1 = 0, means decerase*/
		mDCAdjust.dcDiff[i] = ((_volSet > _volRead) ? (_volSet - _volRead) : (_volRead - _volSet));
#if  TTTTTTT
		mDCAdjust.direction[i] = ((_volSet > _volRead) ? 1 : 0);
		if (i == 0)
		{
			mDCAdjust.adjParam.adjustBit.dir_ch0 = ((_volSet > _volRead) ? 1 : 0);

			/*store the diffs into cores buffer*/
			if (mDCAdjust.adjParam.adjustBit.dir_ch0 == 1)
			{
				/*last is decrese*/
				mDCAdjust.mDilemmaBuff[i].diffDec = mDCAdjust.dcDiff[i];
			}
			if (mDCAdjust.adjParam.adjustBit.dir_ch0 == 0)
			{
				/*last is increse*/
				mDCAdjust.mDilemmaBuff[i].diffInc = mDCAdjust.dcDiff[i];
			}
		}
		else if (i == 1)
		{
			mDCAdjust.adjParam.adjustBit.dir_ch1 = ((_volSet > _volRead) ? 1 : 0);

			/*store the diffs into cores buffer*/
			if (mDCAdjust.adjParam.adjustBit.dir_ch1 == 1)
			{
				mDCAdjust.mDilemmaBuff[i].diffDec = mDCAdjust.dcDiff[i];
			}
			if (mDCAdjust.adjParam.adjustBit.dir_ch1 == 0)
			{
				mDCAdjust.mDilemmaBuff[i].diffInc = mDCAdjust.dcDiff[i];
			}
		}
#else
		mDCAdjust.direction[i] =  ((_volSet > _volRead) ? 1 : 0);
			/*store the diffs into cores buffer*/
		if (mDCAdjust.direction[i] == 1)
		{
			/*last is decrese*/
			mDCAdjust.mDilemmaBuff[i].diffDec = mDCAdjust.dcDiff[i];
		}
		if (mDCAdjust.direction[i] == 0)
		{
			/*last is increse*/
			mDCAdjust.mDilemmaBuff[i].diffInc = mDCAdjust.dcDiff[i];
		}
#endif

		/*to check if it was on the dilemma*/
		if (DcAdjustCheckdilemma(i))
		{
			mDCAdjust.forceStop = 1;
		}
		JBF_DEBUG_PRINTF_LOG("_volRead: %d, volset: %d  diff: %d\r\n", _volRead, _volSet, mDCAdjust.dcDiff[i]);
	}
}


static void DcAdjustForce(void)
{
	uint8_t _smaller = 0;
#if  TTTTTTT
    if (mDCAdjust.dcDiff[0] > DC_RESOLUTION)
    {
		_smaller = (mDCAdjust.mDilemmaBuff[0].diffInc < mDCAdjust.mDilemmaBuff[0].diffDec)? 1 : 0;

		if (_smaller == mDCAdjust.adjParam.adjustBit.dir_ch0)
		{
        	SetbyPercent_CtimerAPwm(0, mDCAdjust.adjParam.adjustBit.dir_ch0);
		}

    }     

    /*chn 1*/
    if (mDCAdjust.dcDiff[1] > DC_RESOLUTION)
    {
		_smaller = (mDCAdjust.mDilemmaBuff[0].diffInc < mDCAdjust.mDilemmaBuff[0].diffDec)? 1 : 0;
  		if (_smaller == mDCAdjust.adjParam.adjustBit.dir_ch1)
		{
        	SetbyPercent_CtimerAPwm(1, mDCAdjust.adjParam.adjustBit.dir_ch1);
		}
    }
#else
	int i = 0;
	for (i = 0; i < MaxLpNum; i++)
	{
		if (mDCAdjust.dcDiff[i] > DC_RESOLUTION)
		{
			_smaller = (mDCAdjust.mDilemmaBuff[i].diffInc < mDCAdjust.mDilemmaBuff[i].diffDec) ? 1 : 0;

			if (_smaller == mDCAdjust.direction[i])
			{
				SetbyPercent_CtimerAPwm(i, mDCAdjust.direction[i]);
			}
		}
	}
#endif
}

/************************************************************************** 
 * return :   0- nochange
 *            1- change
***************************************************************************/
static uint8_t DcAdjustPercent(void)
{
	uint8_t _rst = 0;
#if  TTTTTTT
    /*chn 0*/
    if (mDCAdjust.dcDiff[0] > DC_RESOLUTION)
    {
		_rst++;
        SetbyPercent_CtimerAPwm(0, mDCAdjust.adjParam.adjustBit.dir_ch0);
		DcAdjustCountIncrese(0, mDCAdjust.adjParam.adjustBit.dir_ch0);
    }     

    /*chn 1*/
    if (mDCAdjust.dcDiff[1] > DC_RESOLUTION)
    {
		_rst++;
        SetbyPercent_CtimerAPwm(1, mDCAdjust.adjParam.adjustBit.dir_ch1);
		DcAdjustCountIncrese(1, mDCAdjust.adjParam.adjustBit.dir_ch1);
    }
#else
	int i = 0;
	for (i = 0; i < MaxLpNum; i++)
	{
		if (mDCAdjust.dcDiff[i] > DC_RESOLUTION)
		{
			_rst++;
			SetbyPercent_CtimerAPwm(i, mDCAdjust.direction[i]);
			DcAdjustCountIncrese(i, mDCAdjust.direction[i]);
		}
	}
#endif	
	return _rst;
}

static void DcAdjust(void)
{
	/*0 means sleep*/
	uint8_t _dcAdjustStep = 0;
	_dcAdjustStep = mDCAdjust.adjParam.adjustBit.step;

	switch (_dcAdjustStep)
	{
		case DC_ADJUST_STEP_LAZY:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				DcAdjustClearPwmNew();
				break;
			}
			if (mDCAdjust.checkTimeout && gJbfSysCb.timerCb.CheckTimeout(mDCAdjust.checkTimeout, 5000))
			{
				mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_LAZY;
				mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
				mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_START;
				JBF_DEBUG_PRINTF_LOG("--\r\n");
				break;
			}
			break;
		}
		case DC_ADJUST_STEP_ACTIVE:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				JBF_CLEAR(mDCAdjust.adjParam.adjustBit.pwmNew);
				DcAdjustClearPwmNew();
				mDCAdjust.activeCount = 0;
				break;
			}
			if (mDCAdjust.checkTimeout && gJbfSysCb.timerCb.CheckTimeout(mDCAdjust.checkTimeout, 20))
			{
				mDCAdjust.activeCount++;
				mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
				mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_START;
				JBF_DEBUG_PRINTF_LOG("activeCount: %d\r\n", mDCAdjust.activeCount);
				mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
				/* no pwm set event in ten sceconds, then go back to the lazy mode*/
				if (mDCAdjust.activeCount >= 100)
				{
					if (((mDCAdjust.dcDiff[0] <= DC_RESOLUTION) && (mDCAdjust.dcDiff[1] <= DC_RESOLUTION)) ||
						mDCAdjust.forceStop)
					{
						mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_LAZY;
						mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_LAZY;
						mDCAdjust.activeCount = 0;
						DcAdjustCountClear();
						JBF_DEBUG_PRINTF_LOG("--\r\n");
						break;
					}
					else
					{
						DcAdjustDetect();
					}
				}
			}
			break;
		}
		case DC_ADJUST_STEP_START /*start*/:
		{
			Start_CAdcDMA();
			ResetDmaOk_DcAdjust();
			mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_READ;
			break;
		}
		case DC_ADJUST_STEP_READ /*wait DMA*/:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				DcAdjustClearPwmNew();
				JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
				break;
			}

			/*if dma did not convert over , then quite and wait again */
			if (mDCAdjust.adjParam.adjustBit.dmaOk == 0)
			{
				break;
			}

			DcGetDcDiff();
			JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
			mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
			mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_ADJUST;
			break;
		}
		case DC_ADJUST_STEP_ADJUST:
		{
			if (mDCAdjust.adjParam.adjustBit.pwmNew)
			{
				DcAdjustClearPwmNew();
				JBF_CLEAR(mDCAdjust.adjParam.adjustBit.dmaOk);
				break;
			}

			if (mDCAdjust.forceStop)
			{
				DcAdjustForce();
				mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
			}else{
				if (DcAdjustPercent())
				{
					mDCAdjust.adjParam.adjustBit.modeStore = DC_ADJUST_STEP_ACTIVE;
				}
			}
	 
			mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
			mDCAdjust.adjParam.adjustBit.step = mDCAdjust.adjParam.adjustBit.modeStore;
			break;
		}
		default:
			break;
	}
}

void Init_DcAdjust(void)
{
	mDCAdjust.checkTimeout = 0;
	mDCAdjust.activeCount = 0;
	mDCAdjust.adjParam.adjustCfg = 0;
	mDCAdjust.dcDiff[0] = 0;
	mDCAdjust.dcDiff[1] = 0;
	DcAdjustCountClear();
	mDCAdjust.adjParam.adjustBit.step = DC_ADJUST_STEP_ACTIVE;
}

void Setup_DcAdjust(void)
{
	DDL_ASSERT(gJbfSysCb.timerCb.getCurms);
	mDCAdjust.checkTimeout = gJbfSysCb.timerCb.getCurms();
}
/**************************************************************************
 * if there comes new Pwm set evets cause by the lcd widget events
***************************************************************************/
void AdjustDc_DcAdjust(void)
{
	DcAdjust();
}

你可能感兴趣的:(算法)