📄 lcd_auto.c
字号:
}
ulTemp0 = ulTemp1;
ulTemp1 = ulTemp2;
}
while (ucPhase != ucResult);
stMUD.PHASE = ucBest << 2;
Set_Phase(stMUD.PHASE);
}
#else
{
unsigned char ucPhase, ucBest;
unsigned long ulTemp0, ulTemp1, ulTemp2;
ucPhase = (ucResult - 2) & 0x1f;
Set_Phase(ucPhase << 2);
if (ERROR_SUCCEED != Read_Phase_Info(Color)) return ERROR_INPUT;
ulTemp0 = ((unsigned long*)Data)[1];
ucPhase = (ucPhase + 1) & 0x1f;
Set_Phase(ucPhase << 2);
if (ERROR_SUCCEED != Read_Phase_Info(Color)) return ERROR_INPUT;
ulTemp1 = ((unsigned long*)Data)[1];
ucResult = (ucResult + 2) & 0x1f;
ulMaxSum = 0;
do
{
ucPhase = (ucPhase + 1) & 0x1f;
Set_Phase(ucPhase << 2);
if (ERROR_SUCCEED != Read_Phase_Info(Color)) return ERROR_INPUT;
ulTemp2 = ((unsigned long*)Data)[1];
((unsigned long*)Data)[0] = ulTemp2 + ulTemp1 + ulTemp0
- ((ulTemp1 > ulTemp0 ? ulTemp1 - ulTemp0 : ulTemp0 - ulTemp1) / 2)
- ((ulTemp1 > ulTemp2 ? ulTemp1 - ulTemp2 : ulTemp2 - ulTemp1) / 2);
if (((unsigned long*)Data)[0] > ulMaxSum)
{
ulMaxSum = ((unsigned long*)Data)[0];
ucBest = (ucPhase - 1) & 0x1f;
}
ulTemp0 = ulTemp1;
ulTemp1 = ulTemp2;
}
while (ucPhase != ucResult);
stMUD.PHASE = ucBest << 2;
Set_Phase(stMUD.PHASE);
}
#endif
return ERROR_SUCCEED;
}
unsigned char Auto_Position_Do(unsigned char NM)
{
unsigned char Result;
Result = Measure_PositionV(NM);
if (ERROR_SUCCEED != Result) return Result;
Result = Measure_PositionH(NM);
if (ERROR_SUCCEED != Result) return Result;
Result = ERROR_SUCCEED;
/////////////////////////////////
// Calculate Vertical Position //
/////////////////////////////////
NM = 1;
while (1)
{
if (((usIPV_ACT_STA + ucV_Max_Margin - 128) >= usVer_Start)
&& ((usIPV_ACT_STA + ucV_Min_Margin - 128) <= usVer_Start))
{
stMUD.V_POSITION = (usVer_Start + 128) - usIPV_ACT_STA;
Set_V_Position();
break;
}
// If we can't align upper bound, we try to align lower bound.
if (0 != NM && usVer_End > usIPV_ACT_LEN)
{
usVer_Start = usVer_End - usIPV_ACT_LEN + 1;
NM = 0;
}
else
{
stMUD.V_POSITION = 128;
break;
}
}
///////////////////////////////////
// Calculate Horizontal Position //
///////////////////////////////////
NM = 1;
while (1)
{
if (((usIPH_ACT_STA + ucH_Max_Margin - stMUD.H_POSITION) >= usH_Start)
&& ((usIPH_ACT_STA + ucH_Min_Margin - stMUD.H_POSITION) <= usH_Start))
{
stMUD.H_POSITION = (unsigned int)stMUD.H_POSITION + usH_Start - usIPH_ACT_STA;
Set_H_Position();
break; // Success
}
// If we can't align upper bound, we try to align lower bound.
if (0 != NM && usH_End > usIPH_ACT_WID)
{
usH_Start = usH_End - usIPH_ACT_WID + 1;
NM = 0;
}
else
{
stMUD.H_POSITION = 128;
break;
}
}
return Result;
}
void Save_Auto_Result(void)
{
ucAUTO_HPOS = stMUD.H_POSITION;
ucAUTO_VPOS = stMUD.V_POSITION;
ucAUTO_SCLK = stMUD.CLOCK;
if ((ucV_Min_Margin + 1) > ucAUTO_VPOS)
ucAUTO_VPOS = 128;
else if (ucV_Max_Margin < ucAUTO_VPOS)
ucAUTO_VPOS = 128;
Data[0] = 6;
Data[1] = ADDR_EROM2;
Data[2] = (ucMode_Curr - 1) << 2;
Data[3] = ucAUTO_HPOS;
Data[4] = ucAUTO_VPOS;
Data[5] = ucAUTO_SCLK;
I2CWrite(Data);
Delay_Xms(SET_2404_DELAY);
Save_MUD(ucMode_Curr);
}
unsigned char Tune_Balance(void)
{
unsigned char Color, Count, Result, Margin, FineTune;
if (ERROR_SUCCEED != Measure_PositionN(0x40)) return ERROR_ABORT;
if (ERROR_SUCCEED != Measure_Color(SELECT_RED, COLOR_MAX)) return ERROR_ABORT;
if (0x60 > Data[0]) return ERROR_ABORT;
Margin = Data[0];
if (ERROR_SUCCEED != Measure_Color(SELECT_GREEN, COLOR_MAX)) return ERROR_ABORT;
if (0x60 > Data[0]) return ERROR_ABORT;
Margin = Margin > Data[0] ? Data[0] : Margin;
if (ERROR_SUCCEED != Measure_Color(SELECT_BLUE, COLOR_MAX)) return ERROR_ABORT;
if (0x60 > Data[0]) return ERROR_ABORT;
Margin = Margin > Data[0] ? Data[0] : Margin;
Margin = (Margin - 0x20) & 0xfc;
// Get usVer_Start, usVer_End, usH_Start, usH_Start for boundary information
if (ERROR_SUCCEED != Measure_PositionN(Margin)) return ERROR_ABORT;
FineTune = 0;
Color = SELECT_BLUE;
while (1)
{
Count = 0x30;
do
{
///////////////////////////////
// Maximum Adjustment (0xf2) //
///////////////////////////////
if (ERROR_SUCCEED != Measure_Color(Color, COLOR_MAX)) return ERROR_ABORT;
Margin = Data[0];
if (0xf2 < Margin)
{
Result = Margin - 0xf2;
// Non-zero return value of Change_ADC_Gain() means ADC gain reaches maximum.
if (Change_ADC_Gain(Color, Result, 0)) // Increase Gain; Decrease Contrast
{
if (Change_ADC_Offset(Color, 4, 0)) break; // Increase Offset; Decrease Brightness
}
}
else if (0xf2 > Margin)
{
Result = 0xf2 - Margin;
// Non-zero return value of Change_ADC_Gain() means ADC gain reaches minimum.
if (Change_ADC_Gain(Color, Result, 1)) // Decrease Gain; Increase Contrast
{
if (Change_ADC_Offset(Color, 4, 1)) break; // Decrease Offset; Increase Brightness
}
}
Result = Margin;
///////////////////////////////
// Minimum Adjustment (0x02) //
///////////////////////////////
if (ERROR_SUCCEED != Measure_Color(Color, COLOR_MIN)) return ERROR_ABORT;
Margin = Data[0];
if (0x0a < Margin)
{
Change_ADC_Offset(Color, 0x08, 0); // Increase Offset; Decrease Brightness
}
else if (0x02 < Margin)
{
Change_ADC_Offset(Color, Margin - 0x02, 0); // Increase Offset; Decrease Brightness
}
else if (0x02 > Margin)
{
if (0x00 == Margin && 0 == FineTune)
Change_ADC_Offset(Color, 0x06, 1); // Decrease Offset; Increase Brightness
else
Change_ADC_Offset(Color, 0x02 - Margin, 1); // Decrease Offset; Increase Brightness
}
else if (0xf2 == Result)
{
break;
}
// Prevent redundant fine tune
if (8 < Count && 0xf3 >= Result && 0xf1 <= Result && 0x03 >= Margin && 0x01 <= Margin)
{
FineTune = 1;
Count = 8;
}
}
while (--Count);
if (SELECT_BLUE == Color)
Color = SELECT_GREEN;
else if (SELECT_GREEN == Color)
Color = SELECT_RED;
else
break;
}
return ERROR_SUCCEED;
}
unsigned char Measure_Color(unsigned char color, unsigned char margin)
{
RTDSetByte(MARGIN_B_7D, (SELECT_RED == color) ? COLORS_RED : (SELECT_GREEN == color) ? COLORS_GREEN : COLORS_BLUE);
Data[0] = 9;
Data[1] = Y_INC;
Data[2] = H_BND_STA_L_75;
if (COLOR_MIN == margin)
{
Data[3] = 0x80;
Data[4] = 0x00;
Data[5] = 0x12;
Data[6] = 9 < usVer_Start ? 9 : usVer_Start - 1;
Data[7] = Data[6] + 1;
Data[8] = 0;
}
else
{
Data[3] = (unsigned char)usH_Start;
Data[4] = (unsigned char)usH_End;
Data[5] = ((unsigned char)(usH_Start >> 4) & 0x70) | ((unsigned char)(usH_End >> 8) & 0x0f);
Data[6] = (unsigned char)usVer_Start;
Data[7] = (unsigned char)usVer_End;
Data[8] = ((unsigned char)(usVer_Start >> 4) & 0x70) | ((unsigned char)(usVer_End >> 8) & 0x0f);
}
Data[9] = 0;
RTDWrite(Data);
if (COLOR_MIN == margin)
RTDSetByte(AUTO_ADJ_CTRL_7F, COLOR_MIN | 0x01); // Start the auto-adjust
else
RTDSetByte(AUTO_ADJ_CTRL_7F, COLOR_MAX | 0x01); // Start the auto-adjust
Wait_Finish();
if (ERROR_SUCCEED != Data[0]) return ERROR_ABORT;
RTDRead(AUTO_BAL_RESULT_88, 1, N_INC); // Store result in Data[0]
if (COLOR_MIN == margin) Data[0] ^= 0xff;
return ERROR_SUCCEED;
}
bit Change_ADC_Gain(unsigned char color, unsigned char delta, unsigned char inc)
{
bit OverRange = 0;
if (inc)
{
if (color & SELECT_RED)
{
if (stGUD2.AD_R_GAIN >= delta)
stGUD2.AD_R_GAIN -= delta;
else
{
stGUD2.AD_R_GAIN = 0;
OverRange = 1;
}
}
if (color & SELECT_GREEN)
{
if (stGUD2.AD_G_GAIN >= delta)
stGUD2.AD_G_GAIN -= delta;
else
{
stGUD2.AD_G_GAIN = 0;
OverRange = 1;
}
}
if (color & SELECT_BLUE)
{
if (stGUD2.AD_B_GAIN >= delta)
stGUD2.AD_B_GAIN -= delta;
else
{
stGUD2.AD_B_GAIN = 0;
OverRange = 1;
}
}
}
else
{
if (color & SELECT_RED)
{
if ((0xff - delta) >= stGUD2.AD_R_GAIN)
stGUD2.AD_R_GAIN += delta;
else
{
stGUD2.AD_R_GAIN = 0xff;
OverRange = 1;
}
}
if (color & SELECT_GREEN)
{
if ((0xff - delta) >= stGUD2.AD_G_GAIN)
stGUD2.AD_G_GAIN += delta;
else
{
stGUD2.AD_G_GAIN = 0xff;
OverRange = 1;
}
}
if (color & SELECT_BLUE)
{
if ((0xff - delta) >= stGUD2.AD_B_GAIN)
stGUD2.AD_B_GAIN += delta;
else
{
stGUD2.AD_B_GAIN = 0xff;
OverRange = 1;
}
}
}
SetADC_Gain();
return OverRange;
}
bit Change_ADC_Offset(unsigned char color, unsigned char delta, unsigned char inc)
{
bit OverRange = 0;
if (inc)
{
if (color & SELECT_RED)
{
if (stGUD2.AD_R_OFFSET >= delta)
stGUD2.AD_R_OFFSET -= delta;
else
{
stGUD2.AD_R_OFFSET = 0;
OverRange = 1;
}
}
if (color & SELECT_GREEN)
{
if (stGUD2.AD_G_OFFSET >= delta)
stGUD2.AD_G_OFFSET -= delta;
else
{
stGUD2.AD_G_OFFSET = 0;
OverRange = 1;
}
}
if (color & SELECT_BLUE)
{
if (stGUD2.AD_B_OFFSET >= delta)
stGUD2.AD_B_OFFSET -= delta;
else
{
stGUD2.AD_B_OFFSET = 0;
OverRange = 1;
}
}
}
else
{
if (color & SELECT_RED)
{
if ((0xff - delta) >= stGUD2.AD_R_OFFSET)
stGUD2.AD_R_OFFSET += delta;
else
{
stGUD2.AD_R_OFFSET = 0xff;
OverRange = 1;
}
}
if (color & SELECT_GREEN)
{
if ((0xff - delta) >= stGUD2.AD_G_OFFSET)
stGUD2.AD_G_OFFSET += delta;
else
{
stGUD2.AD_G_OFFSET = 0xff;
OverRange = 1;
}
}
if (color & SELECT_BLUE)
{
if ((0xff - delta) >= stGUD2.AD_B_OFFSET)
stGUD2.AD_B_OFFSET += delta;
else
{
stGUD2.AD_B_OFFSET = 0xff;
OverRange = 1;
}
}
}
SetADC_Offset();
return OverRange;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -