[Dashboard\Peugeot\407\BSI 407 95128 Var5]

Size(280,310); // ширина и высота

Form
{
	Group("Пробег",15,10,120,50)
	{
        Digit(Distance," ",5,18,70);
		Label(lbkm,"КМ",90,20);
	}
	CheckBox(ClearHistory, "Очистить историю",15,70);
	Group("Идентификация",5,90,270,50)
	{
		Label(pn," ",0,15);
		Label(Login," ",0,32);
	}
	Group("Информация",5,142,270,45)
	{
		Label(msg," ",0,18);
	}    
	Group("Описание",5,190,270,65)
	{
		Label(lb1,"Калькулятор пробега BSI Peugeot 407",0,20);
		Label(lb2,"EEPROM ST95128",0,35);
	}
	Picture(Logo,"Peugeot.bmp",160,5,0,0);
}

Var Address, StartAddr, EndAddr, AddrVIN, AddrPIN;

OnShow 
{
    Address = 0x292; // Начальный адрес
	StartAddr = Address;
	EndAddr = Address + (8 * 8); 
    Var4 = 0; Var5 = 0;
	ClearHistory = 1;

    while(StartAddr < 0x2D2)
    {
		Var1 = 0;
		if(@StartAddr = 0x55) {Var1 = Var1 + 1;}
		if(@StartAddr = 0xAA) {Var1 = Var1 + 1;}
		StartAddr = StartAddr + 1;
		if(@StartAddr = 0x55) {Var1 = Var1 + 1;}
		if(@StartAddr = 0xAA) {Var1 = Var1 + 1;}
		StartAddr = StartAddr + 1;
		if(Var1 != 2) { Var5 = 1; StartAddr = EndAddr;}
		Var1 = @StartAddr ^ 0xFF;
		Var2 = @(StartAddr + 2) + @(StartAddr + 4);
		if(Var1 != Var2) { Var5 = Var5 + 1;}
		Var1 = @(StartAddr + 1) ^ 0xFF;
		Var2 = @(StartAddr + 3) + @(StartAddr + 5);
		if(Var1 != Var2) { Var5 = Var5 + 1;}

		Var3 = (@(StartAddr + 2)) | (@(StartAddr + 3) << 8) | (@(StartAddr + 4) << 16) | (@(StartAddr + 5) << 24);
		if(Var4 < Var3) {Var4 = Var3;} // Ищем наибольшее значение
        StartAddr = StartAddr + 6;
    } 

    if(Var5 > 2)
    {
        msg.Color = 0x0000FF;
        msg.Bold = 1;
        msg = "Неверный дамп!";
        Distance = 0;
    }
    else
    {
		AddrVIN = 0x240;
		AddrPIN = 0x9CA; // !!!! Надо проверять!!!!
        msg.Bold = 1;
        msg.Color = 0xFF0000;                    
        msg = "Дамп ОК. Введите новый пробег.";
        Distance = Var4 / 10;
		Var1 = @(AddrVIN); Var2 = @(AddrVIN + 1); Var3 = @(AddrVIN + 2); Var4 = @(AddrVIN + 3);
		Var5 = @(AddrVIN + 4); Var6 = @(AddrVIN + 5); Var7 = @(AddrVIN + 6);
		Var8 = @(AddrVIN + 7); Var9 = @(AddrVIN + 8); Var10 = @(AddrVIN + 9); Var11 = @(AddrVIN + 10);
		Var12 = @(AddrVIN + 11); Var13 = @(AddrVIN + 12); Var14 = @(AddrVIN + 13);
		Var15 = @(AddrVIN + 14); Var16 = @(AddrVIN + 15); Var17 = @(AddrVIN + 16);
		pn = "VIN: " + #c.Var1 + #c.Var2 + #c.Var3 + #c.Var4 + #c.Var5 + #c.Var6 + #c.Var7 + #c.Var8 + #c.Var9 + #c.Var10 +
			#c.Var11 + #c.Var12 + #c.Var13 + #c.Var14 + #c.Var15 + #c.Var16 + #c.Var17;

		Var1 = @(AddrPIN); Var2 = @(AddrPIN + 1); Var3 = @(AddrPIN + 2); Var4 = @(AddrPIN + 3);
		Login = "LOGIN: " + #c.var4 + #c.var3 + #c.var2 + #c.var1;
    }
}

OnApply 
{
	StartAddr = Address + 2;
    Var10 = Distance;
    Var5 = 0; // Индикатор ошибки
//    if(Var10 < 100)     {Var5 = 1;}
    if(Var10 > 999999)  {Var5 = 1;}

    if(Var5 = 1)
    {
		msg.Color = 0x0000FF;
        msg.Bold = 1;
        msg = "Пробег должен быть от 0 до 999999 км";        
    }
    else
    {
 		if(ClearHistory = 1)
		{
			// Чистим историю
			Var0 = 0x1000;
			while(Var0 < 0x4000)
			{
				Var1 = @Var0;
				if(Var1 != 0xFF) {@Var0 = 0xFF;}
				Var0 = Var0 + 1;
			}
		}

		Var5 = 0;

		Var10 = Var10 * 10;    
		// заполняем дамп
		while(StartAddr < EndAddr)
		{
			Var2 = Var10 & 0xFF; 
			Var3 = (Var10 >> 8) & 0xFF; 
			Var4 = (Var10 >> 16) & 0xFF; 
			Var6 = (Var10 >> 24) & 0xFF; 
			Var22 = ((Var2 + Var4) & 0xFF) ^ 0xFF;
			Var12 = ((Var3 + Var6) & 0xFF) ^ 0xFF;

			@StartAddr = Var22; StartAddr = StartAddr + 1;
			@StartAddr = Var12; StartAddr = StartAddr + 1;
			@StartAddr = Var2; StartAddr = StartAddr + 1;
			@StartAddr = Var3; StartAddr = StartAddr + 1;
			@StartAddr = Var4; StartAddr = StartAddr + 1;
			@StartAddr = Var6; StartAddr = StartAddr + 3;
        } 

        msg.Color = 0xFF0000;
        msg.Bold = 1;
        msg = "Дамп OK. Новый пробег записан.";
    }
}


