// File: /workspace/github_cpp_makefile_star2_O3/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, 0x2030e5(%rip)    # 0x21b398
jle 0x1857a
cmpq $0x0, 0x202faf(%rip)    # 0x21b270
je 0x18519
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl 0x2005b3(%rip), %eax    # 0x218888
cltd
idivl 0x200590(%rip)          # 0x21886c
andl $0xfffe, %eax           # imm = 0xFFFE
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
decq %rcx
shrq %rcx
cvtsi2sd %rcx, %xmm1
xorl %edx, %edx
leaq 0x145a30(%rip), %rcx    # 0x15dd30
xorpd %xmm2, %xmm2
movsd 0xf0e3c(%rip), %xmm3    # 0x109148
movsd 0xf0e14(%rip), %xmm4    # 0x109128
movsd 0xf0e14(%rip), %xmm5    # 0x109130
movsd 0xf0e14(%rip), %xmm6    # 0x109138
movsd 0xf0d7c(%rip), %xmm7    # 0x1090a8
movsd 0xf0e0b(%rip), %xmm8    # 0x109140
movsd 0xf0d2b(%rip), %xmm0    # 0x109068
imulq $0xe8, %rdx, %rdi
leaq (%rcx,%rdi), %rsi
cmpl $0x0, 0x1c(%rdi,%rcx)
movsd 0xb8(%rdi,%rcx), %xmm10
je 0x18374
movsd 0xb0(%rsi), %xmm9
addsd %xmm10, %xmm9
mulsd %xmm0, %xmm9
movsd %xmm9, 0x28(%rsi)
jmp 0x1837a
movsd 0x28(%rsi), %xmm9
ucomisd %xmm9, %xmm10
movsd 0xb0(%rsi), %xmm11
jae 0x18396
subsd %xmm11, %xmm9
movapd %xmm9, %xmm10
jmp 0x183cb
ucomisd %xmm11, %xmm9
subsd %xmm9, %xmm10
jbe 0x183cb
subsd %xmm11, %xmm9
movapd %xmm9, %xmm11
cmplesd %xmm10, %xmm11
movapd %xmm11, %xmm12
andnpd %xmm9, %xmm12
andpd %xmm10, %xmm11
orpd %xmm12, %xmm11
movapd %xmm11, %xmm10
movapd %xmm10, %xmm9
cmpeqsd %xmm2, %xmm9
movapd %xmm9, %xmm11
andpd %xmm3, %xmm11
andnpd %xmm10, %xmm9
orpd %xmm11, %xmm9
cmpl $0x3, %eax
jb 0x183f4
divsd %xmm1, %xmm9
mulsd 0x10(%rsi), %xmm9
movapd %xmm4, %xmm10
ucomisd %xmm9, %xmm10
jae 0x1844c
movapd %xmm10, %xmm11
addsd %xmm10, %xmm11
ucomisd %xmm9, %xmm11
jae 0x18451
movapd %xmm10, %xmm11
mulsd %xmm5, %xmm11
ucomisd %xmm9, %xmm11
jae 0x18451
movapd %xmm10, %xmm11
mulsd %xmm6, %xmm11
ucomisd %xmm9, %xmm11
jae 0x18451
mulsd %xmm7, %xmm10
ucomisd %xmm10, %xmm8
jae 0x183ff
movapd %xmm9, %xmm11
jmp 0x18451
movapd %xmm10, %xmm11
movsd %xmm11, -0xa0(%rbp,%rdx,8)
incq %rdx
cmpq $0x14, %rdx
jne 0x1833d
cmpb $0x0, 0x202f67(%rip)    # 0x21b3d6
je 0x184a4
leaq 0x1458f0(%rip), %rax    # 0x15dd68
xorl %edx, %edx
movsd -0xa0(%rbp,%rdx,8), %xmm1
ucomisd (%rax), %xmm1
jbe 0x18493
cmpl $0x0, -0x8(%rax)
jne 0x18493
movsd %xmm1, (%rax)
incq %rdx
addq $0xe8, %rax
cmpq $0x14, %rdx
jne 0x1847a
jmp 0x184cf
leaq 0x1458bd(%rip), %rax    # 0x15dd68
xorl %edx, %edx
cmpl $0x0, -0x8(%rax)
jne 0x184c0
movsd -0xa0(%rbp,%rdx,8), %xmm1
movsd %xmm1, (%rax)
incq %rdx
addq $0xe8, %rax
cmpq $0x14, %rdx
jne 0x184ad
movl $0xb8, %eax
addq $0x20, %rsp
popq %rbp
cmpl $0x0, -0x9c(%rax,%rcx)
je 0x184fb
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 0x184d9
movsd 0x145a17(%rip), %xmm0   # 0x15df28
movsd %xmm0, 0x2f4a57(%rip)   # 0x30cf70
movl $0x38, %eax
leaq 0x14580b(%rip), %rcx    # 0x15dd30
xorpd %xmm0, %xmm0
cmpl $0x0, -0x1c(%rax,%rcx)
je 0x1856c
movsd (%rax,%rcx), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1853d
jnp 0x1856c
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 0x18529
retq