
/*
 * dials.c
 *
 *  Created on: 22 Jan 2016
 *      Author: Mike
 */

#include "libOLED/ap_math.H"
#include "libOLED/displayclass.H"
#include "libOLED/fontclass.H"
#include "libOLED/displayDial.H"

static ap_math math;
// this is the number of degrees between top centre of scale and
// left or right hand end of scale : 90 degrees produces a semicircle.

displayDial_t::displayDial_t(display_t &display, uint8_t x, uint8_t y, uint8_t siz, int16_t angle_low, int16_t angle_high) : m_display(display), m_xo(x), m_yo(y), m_siz(siz), m_angleRange(angle_high - angle_low), m_angleLow(angle_low)
{
}

displayDial_t::displayDial_t(display_t &display, uint8_t x, uint8_t y, uint8_t siz, uint16_t angle_) : m_display(display), m_xo(x), m_yo(y), m_siz(siz), m_angleRange(2 * angle_), m_angleLow(-angle_)
{
}

/* position is integer from 0 to SINE_STEPS */
void displayDial_t::draw_needle(int16_t position)
{
  // int ang = math.SINE_SCALING * ((position - (math.SINE_STEPS / 2)) * m_a1) / (math.SINE_STEPS / 2);
  int ang = (math.SINE_SCALING * position * m_angleRange) / math.SINE_STEPS + m_angleLow * math.SINE_SCALING;
  int si = math.ap_sin(ang);
  int co = math.ap_cos(ang);

  /* calculate a shift for a second side of the needle */
  int xl = math.ap_sin(ang - math.SINE_STEPS);
  int yl = math.ap_cos(ang - math.SINE_STEPS);

  int si2 = m_siz + 2;
  int si3 = 2 * m_siz / 3;

  int xs, ys;
  // three parallel lines
  xs = -xl;
  ys = -yl;
  int step;
  for (step = 0; step < 3; step++)
  {
    m_display.drawLine(math.AP_SCALE(si * si2 - xs) + m_xo, m_yo - math.AP_SCALE(co * si2 - ys),

                       math.AP_SCALE(si * si3 - xs) + m_xo, m_yo - math.AP_SCALE(co * si3 - ys), INVERT);
    xs += xl;
    ys += yl;
  }
}

void displayDial_t::draw_scale(int16_t low, int16_t high, uint8_t width, uint8_t num_step, int16_t scale)
{

  int ang;
  m_low = low;
  m_high = high;
  int sc_low = low / scale;
  int sc_high = high / scale;
  int step = 256 * m_angleRange / (4 * (sc_high - sc_low));
  int t;
  ang = m_angleLow * 256;
  int d =  sc_low < sc_high ? 1 : -1;
  t = sc_low * 4;
  while(1)
  {
    int si = math.ap_sin((ang * math.SINE_SCALING) / 256);
    int co = math.ap_cos((ang * math.SINE_SCALING) / 256);

    int len;
    switch (t % 4)
    {
    case 0:
      len = width;
      break;
    case 1:
    case 3:
    case -1:
    case -3:
      len = width / 4;
      break;
    case 2:
    case -2:
      len = width / 2;
      break;
    }

    m_display.drawLine(math.AP_SCALE((m_siz)*si) + m_xo, m_yo - math.AP_SCALE((m_siz)*co),
                       math.AP_SCALE((m_siz - len) * si) + m_xo,
                       m_yo - math.AP_SCALE((m_siz - len) * co), WHITE);
   
   if (t == sc_high * 4)
    break; 
   
    ang += step;
    t += d;
  }
}

void displayDial_t::draw_limits()
{
  m_display.fontSigDigits(small_font, 0, 56, 0, 10, m_low, WHITE);
  m_display.fontSigDigits(small_font, 120, 56, 1, 10, m_high, WHITE);
}
