// File: jkingan[P]ladyheather/heathgps.cpp
// Function: measure_ir()
void measure_ir()
{
   // this routine measures the battery internal resistance by checking
   // how the battery voltage changes when loaded and unloaded

   if(calc_ir >= IR_TIME) {        // turn on load
      set_batt_pwm(0xFFFF);
      --calc_ir;
      sprintf(plot_title, "Calculating battery internal resistance: %2d", calc_ir);
   }
   else if(calc_ir == 3) {   // get loaded readings
      ir_v = batt_v;
      ir_i = batt_i;
      --calc_ir;
      set_batt_pwm(0);
      sprintf(plot_title, "Calculating battery internal resistance: %2d", calc_ir);
   }
   else if(calc_ir) {
      --calc_ir;
      if(calc_ir) {     // get unloaded readings
         sprintf(plot_title, "Calculating battery internal resistance: %2d", calc_ir);
      }
      else {
         ir_v = fabs(batt_v-ir_v);
         if(ir_i) {
            ir_v /= ir_i;
            sprintf(plot_title, "Battery internal resistance: %.2f milliohms at %.3f amps", ir_v*1000.0F, ir_i);
         }
         else {
            sprintf(plot_title, "No load detected on the battery");
         }
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
leaq 0x2c20b5(%rip), %rax    # 0x36c040
cmpl $0xa, (%rax)
jl 0xa9fd2
movl $0xffff, %edi           # imm = 0xFFFF
callq 0xaa160
leaq 0x2c209f(%rip), %rax    # 0x36c040
movl (%rax), %ecx
addl $-0x1, %ecx
leaq 0x2c2093(%rip), %rax    # 0x36c040
movl %ecx, (%rax)
leaq 0x2c208a(%rip), %rax    # 0x36c040
movl (%rax), %edx
leaq 0x2f3a71(%rip), %rdi    # 0x39da30
leaq 0xdb89f(%rip), %rsi     # 0x185865
movb $0x0, %al
callq 0x9060
jmp 0xaa158
leaq 0x2c2067(%rip), %rax    # 0x36c040
cmpl $0x3, (%rax)
jne 0xaa049
leaq 0x2c1fab(%rip), %rax    # 0x36bf90
movsd (%rax), %xmm0
leaq 0x2c2040(%rip), %rax    # 0x36c030
movsd %xmm0, (%rax)
leaq 0x2c1f9d(%rip), %rax    # 0x36bf98
movsd (%rax), %xmm0
leaq 0x2c2032(%rip), %rax    # 0x36c038
movsd %xmm0, (%rax)
leaq 0x2c202f(%rip), %rax    # 0x36c040
movl (%rax), %ecx
addl $-0x1, %ecx
leaq 0x2c2023(%rip), %rax    # 0x36c040
movl %ecx, (%rax)
xorl %edi, %edi
callq 0xaa160
leaq 0x2c2013(%rip), %rax    # 0x36c040
movl (%rax), %edx
leaq 0x2f39fa(%rip), %rdi    # 0x39da30
leaq 0xdb828(%rip), %rsi     # 0x185865
movb $0x0, %al
callq 0x9060
jmp 0xaa156
leaq 0x2c1ff0(%rip), %rax    # 0x36c040
cmpl $0x0, (%rax)
je 0xaa154
leaq 0x2c1fe0(%rip), %rax    # 0x36c040
movl (%rax), %ecx
addl $-0x1, %ecx
leaq 0x2c1fd4(%rip), %rax    # 0x36c040
movl %ecx, (%rax)
leaq 0x2c1fcb(%rip), %rax    # 0x36c040
cmpl $0x0, (%rax)
je 0xaa09d
leaq 0x2c1fbf(%rip), %rax    # 0x36c040
movl (%rax), %edx
leaq 0x2f39a6(%rip), %rdi    # 0x39da30
leaq 0xdb7d4(%rip), %rsi     # 0x185865
movb $0x0, %al
callq 0x9060
jmp 0xaa152
leaq 0x2c1eec(%rip), %rax    # 0x36bf90
movsd (%rax), %xmm0
leaq 0x2c1f81(%rip), %rax    # 0x36c030
subsd (%rax), %xmm0
movaps 0xc51d6(%rip), %xmm1    # 0x16f290
pand %xmm1, %xmm0
leaq 0x2c1f6b(%rip), %rax    # 0x36c030
movsd %xmm0, (%rax)
leaq 0x2c1f68(%rip), %rax    # 0x36c038
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xaa0e1
jp 0xaa0e1
jmp 0xaa13b
leaq 0x2c1f50(%rip), %rax    # 0x36c038
movsd (%rax), %xmm1
leaq 0x2c1f3d(%rip), %rax    # 0x36c030
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
leaq 0x2c1f2e(%rip), %rax    # 0x36c030
movsd %xmm0, (%rax)
movsd 0xc4f22(%rip), %xmm0    # 0x16f030
leaq 0x2c1f1b(%rip), %rax    # 0x36c030
mulsd (%rax), %xmm0
leaq 0x2c1f18(%rip), %rax    # 0x36c038
movsd (%rax), %xmm1
leaq 0x2f3905(%rip), %rdi    # 0x39da30
leaq 0xdb760(%rip), %rsi     # 0x185892
movb $0x2, %al
callq 0x9060
jmp 0xaa150
leaq 0x2f38ee(%rip), %rdi    # 0x39da30
leaq 0xdb782(%rip), %rsi     # 0x1858cb
movb $0x0, %al
callq 0x9060
jmp 0xaa152
jmp 0xaa154
jmp 0xaa156
jmp 0xaa158
popq %rbp
retq
nopw (%rax,%rax)