
/*
 * 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"
#include <string.h>

static ap_math math;

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_angleLow(angle_low), m_angleRange(angle_high - angle_low), m_unitStr(nullptr)
{
}

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_angleLow(-angle_), m_angleRange(2 * angle_), m_unitStr(nullptr)
{
}

/* 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 an orthogonal XY 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_value_items(int val, int dpCode, int width, int x, int y)
{

  m_display.gotoxy(x, y);
  m_display.fontDigits(large_font, width, dpCode, val, WHITE);
  m_display.gotoxy(x, y+16);
  if (m_unitStr)
  {
    int l = strlen(m_unitStr);
    if (l > 4)
      l = 4;
    m_display.printString(small_font, m_unitStr, l);
    m_display.printString(small_font, "     ", 4 - l);
  }
  else
  {
    m_display.printString(small_font, "     ", 4);
  }
}

////////////////////////////////////////////////////////////////////////////////
displayFullDial_t::displayFullDial_t(display_t &display) : displayDial_t(display, 64, 60, 60, 90)
{
}

void displayFullDial_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);
}

void displayFullDial_t ::draw_value(int val, int dp)
{
  draw_value_items(val, dp, 4, 30, 32);
}

////////////////////////////////////////////////////////////////////////////////
displayLeftQuadrantDial_t::displayLeftQuadrantDial_t(display_t &display) : displayDial_t(display, 0, 60, 60, 90, 0){};

void displayLeftQuadrantDial_t::draw_limits()
{
  m_display.fontSigDigits(small_font, 0, 10, false, display_t::NO_DECIMAL, m_high, WHITE);
  m_display.fontSigDigits(small_font, 56, 56, true, display_t::NO_DECIMAL, m_low, WHITE);
}

void displayLeftQuadrantDial_t ::draw_value(int val, int dp)
{
  draw_value_items(val, dp, 3, 0, 32);
}

////////////////////////////////////////////////////////////////////////////////
displayRightQuadrantDial_t::displayRightQuadrantDial_t(display_t &display) : displayDial_t(display, 127, 60, 60, -90, 0){};

void displayRightQuadrantDial_t::draw_limits()
{
  m_display.fontSigDigits(small_font, 128, 10, true, display_t::NO_DECIMAL, m_high, WHITE);
  m_display.fontSigDigits(small_font, 128 - 56, 56, false, display_t::NO_DECIMAL, m_low, WHITE);
}

void displayRightQuadrantDial_t::draw_value(int val, int dp)
{
  draw_value_items(val, dp, 3, 90, 32);
}
