// File: /workspace/github_cpp_makefile_star2_O2/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, 0x1edb31(%rip)    # 0x205398
jle 0x17abc
cmpq $0x0, 0x1ed9fb(%rip)    # 0x205270
je 0x17a5e
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xa8, %rsp
movl 0x1eaff7(%rip), %eax    # 0x202888
cltd
idivl 0x1eafd4(%rip)          # 0x20286c
movl %eax, %ebx
andl $0xfffe, %ebx           # imm = 0xFFFE
cmpl $0x3, %ebx
pushq $0x2
popq %rax
cmovael %ebx, %eax
decq %rax
shrq %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x20(%rbp)
leaq 0x130480(%rip), %r14    # 0x147d40
xorl %r15d, %r15d
cmpq $0x14, %r15
je 0x17996
cmpl $0x0, 0xc(%r14)
movsd 0xa8(%r14), %xmm0
je 0x178fa
movsd 0xa0(%r14), %xmm2
addsd %xmm0, %xmm2
mulsd 0xdb776(%rip), %xmm2    # 0xf3068
movsd %xmm2, 0x18(%r14)
jmp 0x17900
movsd 0x18(%r14), %xmm2
ucomisd %xmm2, %xmm0
movsd 0xa0(%r14), %xmm3
movapd %xmm2, %xmm1
subsd %xmm3, %xmm1
jb 0x17944
ucomisd %xmm3, %xmm2
subsd %xmm2, %xmm0
jbe 0x17940
movapd %xmm1, %xmm2
cmplesd %xmm0, %xmm2
movapd %xmm2, %xmm3
andnpd %xmm1, %xmm3
andpd %xmm0, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, %xmm1
jmp 0x17944
movapd %xmm0, %xmm1
movapd %xmm1, %xmm0
cmpeqsd 0xdb7c7(%rip), %xmm0    # 0xf3118
movapd %xmm0, %xmm2
movsd 0xdb7eb(%rip), %xmm3    # 0xf3148
andpd %xmm3, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
cmpl $0x3, %ebx
jb 0x17973
divsd -0x20(%rbp), %xmm0
mulsd (%r14), %xmm0
callq 0x177e7
movsd %xmm0, -0xc0(%rbp,%r15,8)
incq %r15
addq $0xe8, %r14
jmp 0x178c3
cmpb $0x0, 0x1eda39(%rip)    # 0x2053d6
je 0x179d2
leaq 0x1303c2(%rip), %rax    # 0x147d68
xorl %ecx, %ecx
cmpq $0x14, %rcx
je 0x179ff
movsd -0xc0(%rbp,%rcx,8), %xmm0
ucomisd (%rax), %xmm0
jbe 0x179c7
cmpl $0x0, -0x8(%rax)
jne 0x179c7
movsd %xmm0, (%rax)
incq %rcx
addq $0xe8, %rax
jmp 0x179a8
leaq 0x13038f(%rip), %rax    # 0x147d68
xorl %ecx, %ecx
cmpq $0x14, %rcx
je 0x179ff
cmpl $0x0, -0x8(%rax)
jne 0x179f4
movsd -0xc0(%rbp,%rcx,8), %xmm0
movsd %xmm0, (%rax)
incq %rcx
addq $0xe8, %rax
jmp 0x179db
movl $0xb8, %eax
leaq 0x130325(%rip), %rcx    # 0x147d30
cmpq $0x12d8, %rax           # imm = 0x12D8
je 0x17a41
cmpl $0x0, -0x9c(%rax,%rcx)
je 0x17a39
movsd (%rax,%rcx), %xmm0
addsd -0x8(%rax,%rcx), %xmm0
mulsd 0xdb638(%rip), %xmm0    # 0xf3068
movsd %xmm0, -0x90(%rax,%rcx)
addq $0xe8, %rax
jmp 0x17a0b
movsd 0x1304df(%rip), %xmm0   # 0x147f28
movsd %xmm0, 0x2df51f(%rip)   # 0x2f6f70
addq $0xa8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
pushq $0x38
popq %rax
leaq 0x1302c8(%rip), %rcx    # 0x147d30
xorpd %xmm0, %xmm0
cmpq $0x1258, %rax           # imm = 0x1258
je 0x17abc
cmpl $0x0, -0x1c(%rax,%rcx)
je 0x17ab4
movsd (%rax,%rcx), %xmm1
ucomisd %xmm0, %xmm1
jne 0x17a88
jnp 0x17ab4
movsd -0x28(%rax,%rcx), %xmm2
movsd -0x10(%rax,%rcx), %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
cvttsd2si %xmm3, %rdx
cvtsi2sd %rdx, %xmm3
mulsd %xmm1, %xmm3
divsd %xmm2, %xmm3
movsd %xmm3, -0x10(%rax,%rcx)
addq $0xe8, %rax
jmp 0x17a6c
retq