// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: constant_load()
void constant_load()
{
DATA_SIZE delta;
u16 pwm;

   if(cc_mode == 0) return;
   if(cc_state < 5) ++cc_state;
   if(sweep_rate <= 0) {
      sweep_rate = sweep_tick = 1;
   }

   if(cc_state == 1) {  // starting up
      if(cc_mode == PWM_SWEEP) {
         cc_pwm = sweep_val = sweep_start;
         goto charged;
      }
      else if((cc_mode == CC_LIPO) && (led_v < unsafe_v)) {  // potentially unsafe cell - don't charge
         goto stop_charge;
      }
      term_timer = TERM_TIME;
      if(sweep_stop) cc_pwm = 0.0;
      else           cc_pwm = PWM_STEP;
   }
   else if(cc_mode == PWM_SWEEP) {   // sweeping PWM value between limits
      if(sweep_start > sweep_end) {  // down sweep
         if(--sweep_tick <= 0) {
            sweep_tick = sweep_rate;
            sweep_val -= PWM_STEP;
            if(sweep_val < 0.0) end_sweep();
         }
         if(sweep_val < sweep_end) {
            end_sweep();
         }
      }
      else {                         // up sweep;
         if(--sweep_tick <= 0) {
            sweep_tick = sweep_rate;
            sweep_val += PWM_STEP;
            if(sweep_val > 1.0) end_sweep();
         }
         if(sweep_val > sweep_end) {
            end_sweep();
         }
      }
      cc_pwm = sweep_val;
      goto charged;
   }
   else {
      if((cc_mode == CC_LIPO) && (led_v > (lipo_volts-0.05))) {  // constant V mode
//       if(led_v > (lipo_volts+0.05)) goto stop_charge;
         if(led_v > (lipo_volts+0.10)) goto stop_charge;
         if(led_i < (cc_val/20.0)) {  // constant I mode - charge done at charge_rate/20
            if(--term_timer < 0) {
               stop_charge:
               end_sweep();
               cc_pwm = 0.0;
               goto charged;
            }
         }
         else term_timer = TERM_TIME;

         if(led_i < cc_val) {
            if((led_v > lipo_volts) && cc_pwm) {
               cc_pwm -= PWM_STEP;
               if(cc_pwm < PWM_STEP) cc_pwm = PWM_STEP;
            }
            goto charged;
         }
      }

      if((cc_mode == CC_VOLTS) && (led_v > 0.050)) {  // must have at least 50mv load voltage
         if(sweep_stop) {
            if(led_v > sweep_stop) {
               end_sweep();
               cc_pwm = 0.0;
            }
            else if(--sweep_tick <= 0) {
               sweep_tick = sweep_rate;
               cc_pwm += PWM_STEP;
               if(cc_pwm > 1.0) {
                  end_sweep();
                  cc_pwm = 0.0;
               }
            }
         }
         else {
            delta = (cc_val - led_v);
            delta /= led_v;
            if(delta < 0.0) delta *= (DATA_SIZE) 0.50;
            else            delta *= (DATA_SIZE) 0.20;
            cc_pwm += (delta * cc_pwm);
         }
      }
      else if((cc_mode == CC_WATTS) && (led_w > 0.050)) {  // must have at least 50mw load power
         if(sweep_stop) {
            if(led_w > sweep_stop) {
               end_sweep();
               cc_pwm = 0.0;
            }
            else if(--sweep_tick <= 0) {
               sweep_tick = sweep_rate;
               cc_pwm += PWM_STEP;
               if(cc_pwm > 1.0) {
                  end_sweep();
                  cc_pwm = 0.0;
               }
            }
         }
         else {
            delta = (cc_val - led_w);
            delta /= led_w;
            if(delta < 0.0) delta *= (DATA_SIZE) 0.25;  // power is a squared function, reduce gain
            else            delta *= (DATA_SIZE) 0.04;
            cc_pwm += (delta * cc_pwm);
         }
      }
      else if((cc_mode > 1) && (led_i > 0.050)) {  // must have at least 50mA load current
         if(sweep_stop) {
            if(led_i > sweep_stop) {
               end_sweep();
               cc_pwm = 0.0;
            }
            else if(--sweep_tick <= 0) {
               sweep_tick = sweep_rate;
               cc_pwm += PWM_STEP;
               if(cc_pwm > 1.0) {
                  end_sweep();
                  cc_pwm = 0.0;
               }
            }
         }
         else {
            delta = (cc_val - led_i);
            delta /= led_i;
            if(delta < 0.0) delta *= (DATA_SIZE) 0.50;
            else            delta *= (DATA_SIZE) 0.20;
            cc_pwm += (delta * cc_pwm);
         }
      }
      else {
         if(sweep_stop) {
            if(--sweep_tick <= 0) {
               sweep_tick = sweep_rate;
               cc_pwm += PWM_STEP;
               if(cc_pwm > 1.0) {
                  end_sweep();
                  cc_pwm = 0.0;
               }
            }
         }
         else {
            cc_pwm += PWM_STEP;
         }
      }
   }

   charged:
   if(cc_pwm > 1.0)        cc_pwm = 1.0;
   else if(cc_pwm <= 0.0F) cc_pwm = 0.0;

   pwm = (u16) (cc_pwm * 65535.0);
   set_batt_pwm(pwm);

   if(cc_mode == CC_LIPO) {
      sprintf(plot_title, " LiPo Charge %.3fA to %.3fV: led_i=%.3fA  pwm=%f %04X %04X", cc_val,lipo_volts, led_i, cc_pwm,pwm,pwm>>6);
   }
   else if(cc_mode == CC_AMPS) {
      sprintf(plot_title, " CC Load %.3fA: led_i=%.3fA  pwm=%f %04X %04X", cc_val, led_i, cc_pwm,pwm,pwm>>6);
   }
   else if(cc_mode == CC_VOLTS) {
      sprintf(plot_title, " CV Load %.3fV: led_v=%.3fV  pwm=%f %04X %04X", cc_val, led_v, cc_pwm,pwm,pwm>>6);
   }
   else if(cc_mode == CC_WATTS) {
      sprintf(plot_title, " CW Load %.3fW: led_v=%.3fV  led_i=%.3fA  led_w=%.3fW  pwm=%f %04X %04X", cc_val, led_v, led_i, led_w, cc_pwm,pwm,pwm>>6);
   }
   else if(cc_mode == PWM_SWEEP) {
      sprintf(plot_title, " PWM sweep from %.3f to %.3f:  pwm=%f %04X %04X", sweep_start, sweep_end, cc_pwm,pwm,pwm>>6);
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
leaq 0x285d83(%rip), %r14    # 0x2f6e88
movzbl (%r14), %ecx
testl %ecx, %ecx
je 0x71576
leaq 0x285d71(%rip), %rax    # 0x2f6e89
movb (%rax), %dl
cmpb $0x4, %dl
ja 0x7112c
leal 0x1(%rdx), %esi
movb %sil, (%rax)
testb %dl, %dl
sete %dl
jmp 0x7112e
xorl %edx, %edx
leaq 0x285d9b(%rip), %rsi    # 0x2f6ed0
movl (%rsi), %eax
testl %eax, %eax
jg 0x71149
leaq 0x285d92(%rip), %rdi    # 0x2f6ed4
pushq $0x1
popq %rax
movl %eax, (%rdi)
movl %eax, (%rsi)
testb %dl, %dl
je 0x7117a
cmpl $0x3, %ecx
je 0x71203
cmpl $0x5, %ecx
jne 0x7121f
leaq 0x285d4a(%rip), %rax    # 0x2f6eb0
movsd (%rax), %xmm0
leaq 0x285d4f(%rip), %rax    # 0x2f6ec0
movsd %xmm0, (%rax)
jmp 0x71351
cmpl $0x3, %ecx
je 0x71258
cmpl $0x5, %ecx
jne 0x712e2
leaq 0x285d1d(%rip), %rcx    # 0x2f6eb0
movsd (%rcx), %xmm1
leaq 0x285d1a(%rip), %r15    # 0x2f6eb8
movsd (%r15), %xmm0
leaq 0x285d2a(%rip), %rcx    # 0x2f6ed4
movl (%rcx), %edx
ucomisd %xmm0, %xmm1
leal -0x1(%rdx), %esi
movl %esi, (%rcx)
leaq 0x285d04(%rip), %rbx    # 0x2f6ec0
movsd (%rbx), %xmm1
jbe 0x71314
cmpl $0x1, %edx
jg 0x711f4
movl %eax, (%rcx)
leaq 0x285c44(%rip), %rax    # 0x2f6e18
subsd (%rax), %xmm1
movsd %xmm1, (%rbx)
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm2
jbe 0x711f4
callq 0x7108f
movsd (%rbx), %xmm1
movsd (%r15), %xmm0
ucomisd %xmm1, %xmm0
ja 0x71348
jmp 0x7134d
leaq 0x285bc6(%rip), %rax    # 0x2f6dd0
leaq 0x285c87(%rip), %rcx    # 0x2f6e98
movsd (%rcx), %xmm0
ucomisd (%rax), %xmm0
ja 0x714f4
movl $0x3c, 0x2e9bbb(%rip)   # 0x35ade4
leaq 0x285c98(%rip), %rax    # 0x2f6ec8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x714fd
jp 0x714fd
leaq 0x285bc9(%rip), %rax    # 0x2f6e18
movsd (%rax), %xmm0
jmp 0x71351
leaq 0x285b71(%rip), %rdx    # 0x2f6dd0
movsd (%rdx), %xmm0
leaq 0x285c26(%rip), %rdx    # 0x2f6e90
movsd (%rdx), %xmm1
movsd 0x847ca(%rip), %xmm2    # 0xf5a40
addsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x713d1
movsd 0x81e7c(%rip), %xmm2    # 0xf3108
addsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
ja 0x714f4
leaq 0x285b37(%rip), %rdx    # 0x2f6dd8
movsd (%rdx), %xmm2
leaq 0x285bf4(%rip), %rdx    # 0x2f6ea0
movsd (%rdx), %xmm3
movapd %xmm3, %xmm4
divsd 0x848fc(%rip), %xmm4    # 0xf5bb8
ucomisd %xmm2, %xmm4
jbe 0x71381
movl 0x2e9b18(%rip), %edx    # 0x35ade4
leal -0x1(%rdx), %esi
movl %esi, 0x2e9b0f(%rip)    # 0x35ade4
testl %edx, %edx
jg 0x7138b
jmp 0x714f4
cmpl $0x1, %ecx
jne 0x713d1
leaq 0x285ade(%rip), %rdx    # 0x2f6dd0
movsd (%rdx), %xmm1
ucomisd 0x97232(%rip), %xmm1    # 0x108530
jbe 0x713d1
leaq 0x285bbd(%rip), %rcx    # 0x2f6ec8
movsd (%rcx), %xmm0
jmp 0x7144c
cmpl $0x1, %edx
jg 0x71342
movl %eax, (%rcx)
leaq 0x285af6(%rip), %rax    # 0x2f6e18
addsd (%rax), %xmm1
movsd %xmm1, (%rbx)
ucomisd 0x81d3e(%rip), %xmm1    # 0xf3070
jbe 0x71342
callq 0x7108f
movsd (%rbx), %xmm1
movsd (%r15), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x7134d
callq 0x7108f
movsd (%rbx), %xmm0
leaq 0x285b50(%rip), %rax    # 0x2f6ea8
movsd %xmm0, (%rax)
movsd 0x81d0c(%rip), %xmm1    # 0xf3070
ucomisd %xmm1, %xmm0
ja 0x714fd
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x714fd
jmp 0x7150c
movl $0x3c, 0x2e9a59(%rip)   # 0x35ade4
ucomisd %xmm2, %xmm3
jbe 0x713d1
ucomisd %xmm1, %xmm0
leaq 0x285b0c(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm0
jbe 0x7135c
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x713ae
jnp 0x7135c
leaq 0x285a63(%rip), %rcx    # 0x2f6e18
movsd (%rcx), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, (%rax)
ucomisd %xmm0, %xmm1
jbe 0x7135c
movsd %xmm1, (%rax)
movapd %xmm1, %xmm0
jmp 0x7135c
cmpl $0x4, %ecx
jne 0x71427
leaq 0x285a63(%rip), %rdx    # 0x2f6e40
movsd (%rdx), %xmm1
ucomisd 0x97147(%rip), %xmm1    # 0x108530
jbe 0x71427
leaq 0x285ad6(%rip), %rcx    # 0x2f6ec8
movsd (%rcx), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0x71458
jp 0x71458
leaq 0x285a97(%rip), %rax    # 0x2f6ea0
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
divsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm2
seta %al
leaq 0x975fb(%rip), %rcx     # 0x108a20
jmp 0x71486
leaq 0x285a9a(%rip), %rdx    # 0x2f6ec8
movsd (%rdx), %xmm0
cmpl $0x1, %ecx
je 0x714a3
leaq 0x28599a(%rip), %rcx    # 0x2f6dd8
movsd (%rcx), %xmm1
ucomisd 0x970e6(%rip), %xmm1    # 0x108530
jbe 0x714a3
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0x71458
jnp 0x71463
ucomisd %xmm0, %xmm1
jbe 0x714b3
jmp 0x714f4
leaq 0x285a36(%rip), %rax    # 0x2f6ea0
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
divsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm2
seta %al
leaq 0x9758a(%rip), %rcx     # 0x108a10
mulsd (%rcx,%rax,8), %xmm0
leaq 0x285a16(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
jmp 0x71358
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x714b3
jnp 0x716c0
leaq 0x285a1a(%rip), %rcx    # 0x2f6ed4
movl (%rcx), %edx
leal -0x1(%rdx), %esi
movl %esi, (%rcx)
cmpl $0x1, %edx
jg 0x716b0
movl %eax, (%rcx)
leaq 0x285945(%rip), %rax    # 0x2f6e18
movsd (%rax), %xmm0
leaq 0x2859ca(%rip), %rax    # 0x2f6ea8
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
ucomisd 0x81b82(%rip), %xmm0    # 0xf3070
jbe 0x7135c
callq 0x7108f
xorpd %xmm1, %xmm1
leaq 0x2859a4(%rip), %rax    # 0x2f6ea8
movsd %xmm1, (%rax)
movapd %xmm1, %xmm0
mulsd 0x844c4(%rip), %xmm0    # 0xf59d8
cvttsd2si %xmm0, %eax
movzwl %ax, %ebx
movl %ebx, %edi
callq 0x71025
movzbl (%r14), %eax
decl %eax
cmpl $0x4, %eax
ja 0x71576
leaq 0x97968(%rip), %rcx     # 0x108e9c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x28595c(%rip), %rax    # 0x2f6ea0
movsd (%rax), %xmm0
leaq 0x285881(%rip), %rax    # 0x2f6dd0
movsd (%rax), %xmm1
leaq 0x28594e(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm2
movl %ebx, %ecx
shrl $0x6, %ecx
leaq 0x2b72e6(%rip), %rdi    # 0x328850
leaq 0x9b09b(%rip), %rsi     # 0x10c60c
jmp 0x7169d
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x285928(%rip), %rax    # 0x2f6eb0
movsd (%rax), %xmm0
leaq 0x285925(%rip), %rax    # 0x2f6eb8
movsd (%rax), %xmm1
leaq 0x28590a(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm2
movl %ebx, %ecx
shrl $0x6, %ecx
leaq 0x2b72a2(%rip), %rdi    # 0x328850
leaq 0x9b0cd(%rip), %rsi     # 0x10c682
jmp 0x7169d
leaq 0x2858df(%rip), %rax    # 0x2f6ea0
movsd (%rax), %xmm0
leaq 0x2858c4(%rip), %rax    # 0x2f6e90
movsd (%rax), %xmm1
leaq 0x285801(%rip), %rax    # 0x2f6dd8
movsd (%rax), %xmm2
leaq 0x2858c6(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm3
movl %ebx, %ecx
shrl $0x6, %ecx
leaq 0x2b725e(%rip), %rdi    # 0x328850
leaq 0x9afaa(%rip), %rsi     # 0x10c5a3
movl %ebx, %edx
movb $0x4, %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9060
leaq 0x28588d(%rip), %rax    # 0x2f6ea0
movsd (%rax), %xmm0
leaq 0x2857b2(%rip), %rax    # 0x2f6dd0
movsd (%rax), %xmm1
leaq 0x2857af(%rip), %rax    # 0x2f6dd8
movsd (%rax), %xmm2
leaq 0x28580c(%rip), %rax    # 0x2f6e40
movsd (%rax), %xmm3
leaq 0x285869(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm4
movl %ebx, %ecx
shrl $0x6, %ecx
leaq 0x2b7201(%rip), %rdi    # 0x328850
leaq 0x9afe4(%rip), %rsi     # 0x10c63a
movl %ebx, %edx
movb $0x5, %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9060
leaq 0x285830(%rip), %rax    # 0x2f6ea0
movsd (%rax), %xmm0
leaq 0x28575d(%rip), %rax    # 0x2f6dd8
movsd (%rax), %xmm1
leaq 0x285822(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm2
movl %ebx, %ecx
shrl $0x6, %ecx
leaq 0x2b71ba(%rip), %rdi    # 0x328850
leaq 0x9af41(%rip), %rsi     # 0x10c5de
movl %ebx, %edx
movb $0x3, %al
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9060
leaq 0x2857f1(%rip), %rax    # 0x2f6ea8
movsd (%rax), %xmm0
jmp 0x7135c
leaq 0x285751(%rip), %rax    # 0x2f6e18
movsd (%rax), %xmm0
leaq 0x2857d6(%rip), %rax    # 0x2f6ea8
addsd (%rax), %xmm0
jmp 0x71358