Spend a while trying to work out the first function called in the main while loop. It is for monitoring the battery level and charging state, but I'm a bit at a loss of why they are doing it the way they are doing it.
void monitor_battery(void)
{
int iVar1;
undefined *puVar2;
byte bVar3;
uint uVar4;
byte *pbVar5;
int iVar6;
int iVar7;
byte bVar8;
bool bVar9;
iVar1 = DAT_8000a6a8; //Base of settings data
bVar8 = 0xff;
if (0xd < *(byte *)(DAT_8000a6a8 + 10)) //Time base setting If 13 is smaller then, so 14 and up (1mS/div and down)
{
iVar7 = 0x1e; //1mS/div --- 10nS/div, factor is 30
goto LAB_8000a5bc;
}
if (*(byte *)(DAT_8000a6a8 + 10) < 0xc) //Less then 0x0C
{
if (*(byte *)(DAT_8000a6a8 + 10) < 9) //Less then 9
{
if (6 < *(byte *)(DAT_8000a6a8 + 10)) //If 6 is smaller then, so 7 and 8.
{
iVar7 = 100; //200mS/div, 100mS/div, factor is 100
goto LAB_8000a5bc;
}
if (3 < *(byte *)(DAT_8000a6a8 + 10)) //If 3 is smaller then, so 4, 5 and 6
{
iVar7 = 0x32; //2S/div, 1S/div, 500mS/div, factor is 64
goto LAB_8000a5bc;
}
if (1 < *(byte *)(DAT_8000a6a8 + 10)) //If 1 is smaller then, so 2 and 3
goto LAB_8000a5b0; //10S/div, 5S/div, factor is 10
}
iVar7 = 3; //0, 1, 9, 10, 11
//50S/div, 20S/div, 50mS/div, 20mS/div, 10mS/div, factor is 3
}
else
{
LAB_8000a5b0: //12, 13
iVar7 = 10; //5mS/div, 2mS/div factor is 10
}
LAB_8000a5bc:
uVar4 = check_port_pin(DAT_8000a6ac,0xc); //Battery charge detect
puVar2 = PTR_DAT_8000a6b0; //0x80192ed4 some global variable, some accumulator??
if (uVar4 == 0) //If pin is low the battery is being charged
{
if (*(char *)(iVar1 + 0x39) == '\0') //BatteryCharging variable
{
*PTR_DAT_8000a6b0 = 0; //If previous state was not charging this variable is set 0
}
*(undefined *)(iVar1 + 0x39) = 1; //Indicate charging
}
else
{
*(undefined *)(iVar1 + 0x39) = 0; //Indicate not charging
}
bVar3 = read_keyadc_data(); //Battery level reading. result 0 - 0x3F
//0x80361378 Some base pointer
//Some fractional addition to the adc value
// result from scan byte from 0x8036137A (screen brightness) 0x068DB8BB
uVar4 = (uint)bVar3 + (uint)((ulonglong)(uint)((int)(short)(ushort)*(byte *)(DAT_8000a6b4 + 2) * (int)(short)((ushort)bVar3 * 10)) * (ulonglong)DAT_8000a6b8 >> 0x28); //High result byte logical shifted right an extra 8
//Check if on charge
if (*(char *)(iVar1 + 0x39) != '\0')
{
uVar4 = uVar4 - 7; //Take of 7 as compensation
}
//Check if result is less then 25
if (uVar4 < 0x19)
{
//If so set it to the minimum
uVar4 = 0x19;
}
//Divide ((the result minus the minimum) multiplied by 20) by 21
pbVar5 = (byte *)divide((uVar4 - 0x19) * 0x14, 0x15);
//Get data from global variable which is reset when charging starts
//Is an array index
bVar3 = *puVar2;
//0x802F1906 some array where the result is stored
DAT_8000a6bc[bVar3] = (byte)pbVar5;
//As long as the index is lower than the factor obtained from timebase setting
if ((int)(uint)bVar3 < iVar7 + -1)
{
//Increment the index and store it back
*puVar2 = bVar3 + 1;
return; //and quit
}
//When several samples are stored it comes here
//true since none of the timebase compare paths lead to iVar being set zero
bVar9 = iVar7 != 0;
if (bVar9)
{
pbVar5 = DAT_8000a6bc; //Point to the data
}
*puVar2 = 0; //Reset the index
//As long as there is data to process
while (bVar9)
{
bVar3 = *pbVar5; //Get the first byte
pbVar5 = pbVar5 + 1; //Point to the next one
if (bVar3 < bVar8) //Determine the lowest value
{
bVar8 = bVar3; //Keep the lowest value of the samples
}
iVar7 = iVar7 + -1; //One count done
bVar9 = iVar7 != 0; //Check on done
}
//Store result in BatteryChargeLevel
*(byte *)(iVar1 + 0x38) = bVar8;
display_battery_status();
}
Based on the time base setting they determine a number of samples that needs to be monitored before the screen is updated. They store each sample in an array and once the number of needed samples is reached they look for the lowest value and use that to display. This can be easier done with one variable and keep the lowest one after taking a sample, and reset the variable at the start of a new run, but that aside.
What I don't get is the math they are doing after taking a sample. I have no experience with battery charge reading what so ever, so any input is welcomed.
The sample comes from the keyadc and is in range of 0 to 63. To this result they add some value. Looks like some fixed point fractional calculation.
They multiply the sample by 10. This is multiplied with the value coming from some variable (at 0x8036137A)
The result of this multiplication is multiplied with 0x068DB8BB. The top 24 bits are used to add to the sample.
After that they take of 7 when on charge.
Then it is checked against 25 and if less they make it 25.
From the resulting value is then 25 subtracted. The resulting value of that is then multiplied by 20 and that is divided by 21.
Anyone?
?
Edit: The variable (at 0x8036137A) used in the multiplication seems to be the screen brightness. This makes sense since it will drain the battery when it is higher.