// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heather.cpp
// Function: scale_plots()
void scale_plots()
{
long i;
struct PLOT_Q q;
int k;
DATA_SIZE scale[NUM_PLOTS+DERIVED_PLOTS];
DATA_SIZE val;

   // Calculate plot scale factors and center line reference points.
   // This routine uses the min and max values collected by calc_plot_statistics()

// if((auto_scale == 0) && (auto_center == 0)) return;
   if(queue_interval <= 0) return;

   // calc_queue_stats() already scanned the queue data to find mins and maxes
   // now we calculate good looking scale factors and center points for the plots

   if(plot_q_last_col) {  // we have data to calculate good graph scale factors from
      i = (PLOT_HEIGHT / VERT_MAJOR) & 0xFFFE;  //even number of major vertical divisions
      //  if(SCREEN_HEIGHT >= MEDIUM_HEIGHT) i -= 1;  // prevents multiple consecutive rescales
      i -= 1;  //!!!!
      if(i <= 0) i = 1;

      // scale graphs to the largest side of the curve above/below
      // the graph center reference line
      for(k=0; k<NUM_PLOTS+DERIVED_PLOTS; k++) {
         if(plot[k].float_center) {
            plot[k].plot_center = (plot[k].max_disp_val+plot[k].min_disp_val) / 2.0F;
         }

         if((plot[k].max_disp_val >= plot[k].plot_center) && (plot[k].min_disp_val < plot[k].plot_center)) {
            if((plot[k].max_disp_val-plot[k].plot_center) >= (plot[k].plot_center-plot[k].min_disp_val)) {
               scale[k] = (plot[k].max_disp_val-plot[k].plot_center);
            }
            else {
               scale[k] = plot[k].plot_center - plot[k].min_disp_val;
            }
         }
         else if(plot[k].max_disp_val >= plot[k].plot_center) {
            scale[k] = (plot[k].max_disp_val - plot[k].plot_center);
         }
         else {
            scale[k] = (plot[k].plot_center - plot[k].min_disp_val);
         }

         if(scale[k] == 0.0) { // force a non-zero scale factor so plot shows correctly
            scale[k] = (DATA_SIZE) 1.0E-9;
         }

         if(i > 1) scale[k] = (scale[k] / (DATA_SIZE) (i/2));
         scale[k] *= plot[k].ref_scale;
//if(k == OSC) sprintf(debug_text, "osc scale: %g ->   %g  min:%g max:%g", scale[k], round_scale(scale[k]), plot[k].min_disp_val, plot[k].max_disp_val);

         // round scale factors to nice values
         scale[k] = round_scale(scale[k]);
      }

      // set the working scale factors to the new values
      if(peak_scale) {  // don't ever let scale factors get smaller
         for(k=0; k<NUM_PLOTS+DERIVED_PLOTS; k++) {
            if(scale[k] > plot[k].scale_factor) {
               if(plot[k].user_scale == 0) {
                  plot[k].scale_factor = scale[k];
               }
            }
         }
      }
      else {  // scale factors can change to whatever they are
         for(k=0; k<NUM_PLOTS+DERIVED_PLOTS; k++) {
            if(plot[k].user_scale == 0) plot[k].scale_factor = scale[k];
         }
      }

      if(1 || auto_center) {  // center the plots
         for(k=0; k<NUM_PLOTS+DERIVED_PLOTS; k++) {
            if(plot[k].float_center) {
               plot[k].plot_center = (plot[k].max_disp_val+plot[k].min_disp_val) / 2.0F;
            }
         }
         last_dac_voltage = plot[DAC].plot_center;
//!!!!   last_temperature = plot[TEMP].plot_center;
      }
   }
   else if(plot_q_last_col >= 1) {  // center these graphs around the last recorded value
      if(filter_count) q = filter_plot_q(plot_q_last_col-1);
      else             q = get_plot_q(plot_q_last_col-1);

      for(k=0; k<NUM_PLOTS+DERIVED_PLOTS; k++) {
         if(plot[k].float_center) {
            if(k == TEMP) plot[k].plot_center = scale_temp((double) q.data[TEMP] / (double) queue_interval);
else if(enviro_mode() && (k == PRESSURE)) plot[k].plot_center = scale_pressure((double) q.data[PRESSURE] / (double) queue_interval);
            else if(luxor && (k == TC2)) plot[k].plot_center = scale_temp((double) q.data[k] / (double) queue_interval);
            else if(luxor && (k == LUX1))  plot[k].plot_center = (q.data[k] / (DATA_SIZE) queue_interval) * (DATA_SIZE) lux_scale;
            else if(luxor && (k == LUX2))  plot[k].plot_center = (q.data[k] / (DATA_SIZE) queue_interval) * (DATA_SIZE) lum_scale;
            else if(k >= NUM_PLOTS) {  // derived plots DERIVED_PLOTS !!!!!
               if(luxor && (k == BATTW)) {        // battery watts
                  plot[k].plot_center = q.data[BATTI]*q.data[BATTV] / (DATA_SIZE) queue_interval;
               }
               else if(luxor && (k == LEDW)) {   // LED watts
                  plot[k].plot_center = q.data[LEDI]*q.data[LEDV] / (DATA_SIZE) queue_interval;
               }
               else if(luxor && (k == EFF)) { // driver efficency
                  val = q.data[BATTI]*q.data[BATTV];
                  if(val) val = q.data[LEDI]*q.data[LEDV] / val;
                  val *= 100.0F;
                  plot[k].plot_center = val;
               }
               else if(luxor && (k == CCT)) { // color temo
                  val = calc_cct(cct_type, 1, (double)q.data[REDHZ]/(double)queue_interval, (double)q.data[GREENHZ]/(double)queue_interval, (double)q.data[BLUEHZ]/(double)queue_interval);
                  plot[k].plot_center = val;
               }
               else {
//                plot[k].plot_center = (DATA_SIZE) 0.0;
                  plot[k].plot_center = q.data[k] / (DATA_SIZE) queue_interval;
               }
            }
            else plot[k].plot_center = q.data[k] / (DATA_SIZE) queue_interval;
         }
      }
   }

   // finally we round the center line reference values to multiples of
   // the scale factor (note that this tweak can allow a plot to go slightly
   // off scale)
   if(1 || auto_center) {
      for(k=0; k<NUM_PLOTS+DERIVED_PLOTS; k++) {
         if(plot[k].float_center && plot[k].scale_factor) {
            plot[k].plot_center *= plot[k].ref_scale;
            plot[k].plot_center = (DATA_SIZE) (s64) ((plot[k].plot_center / plot[k].scale_factor)) * plot[k].scale_factor;
            plot[k].plot_center /= plot[k].ref_scale;
         }
      }
   }
}

// Assembly:
cmpq $0x0, 0x203122(%rip)    # 0x21b398
jle 0x1856a
cmpq $0x0, 0x202fec(%rip)    # 0x21b270
je 0x18509
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl 0x2005f0(%rip), %eax    # 0x218888
cltd
idivl 0x2005cd(%rip)          # 0x21886c
andl $0xfffe, %eax           # imm = 0xFFFE
cmpq $0x3, %rax
movl $0x2, %ecx
cmovgeq %rax, %rcx
decq %rcx
shrq %rcx
cvtsi2sd %rcx, %xmm1
xorl %edx, %edx
leaq 0x145a6b(%rip), %rcx    # 0x15dd30
xorpd %xmm2, %xmm2
movabsq $0x3e112e0be826d695, %rsi # imm = 0x3E112E0BE826D695
movsd 0xf0e4d(%rip), %xmm3    # 0x109128
movsd 0xf0e4d(%rip), %xmm4    # 0x109130
movsd 0xf0e4d(%rip), %xmm5    # 0x109138
movsd 0xf0db5(%rip), %xmm6    # 0x1090a8
movsd 0xf0e45(%rip), %xmm7    # 0x109140
movsd 0xf0d65(%rip), %xmm0    # 0x109068
imulq $0xe8, %rdx, %r8
leaq (%rcx,%r8), %rdi
cmpl $0x0, 0x1c(%r8,%rcx)
je 0x18333
movsd 0xb8(%rdi), %xmm8
addsd 0xb0(%rdi), %xmm8
mulsd %xmm0, %xmm8
movsd %xmm8, 0x28(%rdi)
movsd 0x28(%rdi), %xmm8
movsd 0xb8(%rdi), %xmm9
ucomisd %xmm8, %xmm9
jae 0x1835e
subsd 0xb0(%rdi), %xmm8
movsd %xmm8, -0xa0(%rbp,%rdx,8)
jmp 0x18397
movsd 0xb0(%rdi), %xmm10
ucomisd %xmm10, %xmm8
jbe 0x18381
subsd %xmm8, %xmm9
subsd %xmm10, %xmm8
ucomisd %xmm8, %xmm9
jb 0x18352
jmp 0x1838d
ucomisd %xmm8, %xmm9
jb 0x18349
subsd %xmm8, %xmm9
movsd %xmm9, -0xa0(%rbp,%rdx,8)
movsd -0xa0(%rbp,%rdx,8), %xmm8
ucomisd %xmm2, %xmm8
jne 0x183b2
jp 0x183b2
movq %rsi, -0xa0(%rbp,%rdx,8)
cmpl $0x3, %eax
jb 0x183d0
movsd -0xa0(%rbp,%rdx,8), %xmm8
divsd %xmm1, %xmm8
movsd %xmm8, -0xa0(%rbp,%rdx,8)
movsd 0x10(%rdi), %xmm8
mulsd -0xa0(%rbp,%rdx,8), %xmm8
movsd %xmm8, -0xa0(%rbp,%rdx,8)
movapd %xmm3, %xmm9
ucomisd %xmm8, %xmm9
jae 0x1843c
movapd %xmm9, %xmm10
addsd %xmm9, %xmm10
ucomisd %xmm8, %xmm10
jae 0x18441
movapd %xmm9, %xmm10
mulsd %xmm4, %xmm10
ucomisd %xmm8, %xmm10
jae 0x18441
movapd %xmm9, %xmm10
mulsd %xmm5, %xmm10
ucomisd %xmm8, %xmm10
jae 0x18441
mulsd %xmm6, %xmm9
ucomisd %xmm9, %xmm7
jae 0x183ef
movapd %xmm8, %xmm10
jmp 0x18441
movapd %xmm9, %xmm10
movsd %xmm10, -0xa0(%rbp,%rdx,8)
incq %rdx
cmpq $0x14, %rdx
jne 0x18303
cmpb $0x0, 0x202f77(%rip)    # 0x21b3d6
je 0x18494
leaq 0x145900(%rip), %rax    # 0x15dd68
xorl %edx, %edx
movsd -0xa0(%rbp,%rdx,8), %xmm1
ucomisd (%rax), %xmm1
jbe 0x18483
cmpl $0x0, -0x8(%rax)
jne 0x18483
movsd %xmm1, (%rax)
incq %rdx
addq $0xe8, %rax
cmpq $0x14, %rdx
jne 0x1846a
jmp 0x184bf
leaq 0x1458cd(%rip), %rax    # 0x15dd68
xorl %edx, %edx
cmpl $0x0, -0x8(%rax)
jne 0x184b0
movsd -0xa0(%rbp,%rdx,8), %xmm1
movsd %xmm1, (%rax)
incq %rdx
addq $0xe8, %rax
cmpq $0x14, %rdx
jne 0x1849d
movl $0xb8, %eax
addq $0x20, %rsp
popq %rbp
cmpl $0x0, -0x9c(%rax,%rcx)
je 0x184eb
movsd (%rax,%rcx), %xmm1
addsd -0x8(%rax,%rcx), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x90(%rax,%rcx)
addq $0xe8, %rax
cmpq $0x12d8, %rax           # imm = 0x12D8
jne 0x184c9
movsd 0x145a27(%rip), %xmm0   # 0x15df28
movsd %xmm0, 0x2f4a67(%rip)   # 0x30cf70
movl $0x38, %eax
leaq 0x14581b(%rip), %rcx    # 0x15dd30
xorpd %xmm0, %xmm0
cmpl $0x0, -0x1c(%rax,%rcx)
je 0x1855c
movsd (%rax,%rcx), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1852d
jnp 0x1855c
movsd -0x28(%rax,%rcx), %xmm2
movsd -0x10(%rax,%rcx), %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
cvttsd2si %xmm3, %rdx
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
mulsd %xmm1, %xmm3
divsd %xmm2, %xmm3
movsd %xmm3, -0x10(%rax,%rcx)
addq $0xe8, %rax
cmpq $0x1258, %rax           # imm = 0x1258
jne 0x18519
retq