Dynamic delay event + infinite step model

Language refinement is a necessary component of high-quality development.

One of the principles of application logic business programming (emphasis).

When weighing the logic implementation and memory saving, you should choose the logic implementation decisively , and don’t add a lot of code lines of repetitive logic just because you save more than a dozen bits, especially in the debugging phase when multiple objects of the same type of operation are reused. You should know that these But all need maintenance. If one is changed, the other must also be changed. Once the writing is scattered, it will be a fatal and time-consuming job.

 If it can be simplified, it must be simplified, and if it can be generalized, it must be generalized. Only in this way can errors be reduced and development efficiency improved. This is also the meaning of decoupling.

under    

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

It is to couple the function with the logic. In order to save 1 byte, it turns out that it is very laborious to debug, changing one place and everywhere. It is better to use two bytes directly.

/*****************************************************************************
 * 
 * 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();
}

after optimization

/*****************************************************************************
 * 
 * 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();
}

Guess you like

Origin blog.csdn.net/loveboon1/article/details/131947310