Subversion Repositories libOLED

Rev

Rev 7 | Rev 11 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 mjames 1
/*
2
 * dials.c
3
 *
4
 *  Created on: 22 Jan 2016
5
 *      Author: Mike
6
 */
7
 
8
#include "libOLED/ap_math.H"
9
#include "libOLED/displayclass.H"
10
#include "libOLED/fontclass.H"
11
#include "libOLED/displayDial.H"
12
 
13
static ap_math math;
14
// this is the number of degrees between top centre of scale and
15
// left or right hand end of scale : 90 degrees produces a semicircle.
16
 
10 mjames 17
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_a1(angle_)
4 mjames 18
{
19
}
20
 
21
/* position is integer from 0 to SINE_STEPS */
10 mjames 22
void displayDial_t::draw_needle(int16_t position)
4 mjames 23
{
10 mjames 24
  int ang = math.SINE_SCALING * ((position - (math.SINE_STEPS / 2)) * m_a1) / (math.SINE_STEPS / 2);
4 mjames 25
 
10 mjames 26
  int si = math.ap_sin(ang);
27
  int co = math.ap_cos(ang);
4 mjames 28
 
29
  /* calculate a shift for a second side of the needle */
10 mjames 30
  int xl = math.ap_sin(ang - math.SINE_STEPS);
31
  int yl = math.ap_cos(ang - math.SINE_STEPS);
4 mjames 32
 
33
  int si2 = m_siz + 2;
34
  int si3 = 2 * m_siz / 3;
35
 
10 mjames 36
  int xs, ys;
4 mjames 37
  // three parallel lines
38
  xs = -xl;
39
  ys = -yl;
40
  int step;
10 mjames 41
  for (step = 0; step < 3; step++)
42
  {
43
    m_display.drawLine(math.AP_SCALE(si * si2 - xs) + m_xo, m_yo - math.AP_SCALE(co * si2 - ys),
4 mjames 44
 
10 mjames 45
                       math.AP_SCALE(si * si3 - xs) + m_xo, m_yo - math.AP_SCALE(co * si3 - ys), INVERT);
46
    xs += xl;
47
    ys += yl;
48
  }
49
}
4 mjames 50
 
10 mjames 51
void displayDial_t::draw_scale(int16_t low, int16_t high, uint8_t width, uint8_t num_step, int16_t scale)
4 mjames 52
{
5 mjames 53
 
4 mjames 54
  int ang;
7 mjames 55
  m_low = low;
56
  m_high = high;
10 mjames 57
  int sc_low = low / scale;
58
  int sc_high = high / scale;
4 mjames 59
  int step = 256 * m_a1 * 2 / (4 * (sc_high - sc_low));
60
  int t;
61
  ang = -m_a1 * 256;
62
  for (t = sc_low * 4; t <= sc_high * 4; t++)
10 mjames 63
  {
64
    int si = math.ap_sin((ang * math.SINE_SCALING) / 256);
65
    int co = math.ap_cos((ang * math.SINE_SCALING) / 256);
66
 
67
    int len;
68
    switch (t % 4)
4 mjames 69
    {
10 mjames 70
    case 0:
71
      len = width;
72
      break;
73
    case 1:
74
    case 3:
75
    case -1:
76
    case -3:
77
      len = width / 4;
78
      break;
79
    case 2:
80
    case -2:
81
      len = width / 2;
82
      break;
4 mjames 83
    }
84
 
10 mjames 85
    m_display.drawLine(math.AP_SCALE((m_siz)*si) + m_xo, m_yo - math.AP_SCALE((m_siz)*co),
86
                       math.AP_SCALE((m_siz - len) * si) + m_xo,
87
                       m_yo - math.AP_SCALE((m_siz - len) * co), WHITE);
88
    ang += step;
89
  }
4 mjames 90
}
7 mjames 91
 
92
void displayDial_t::draw_limits()
93
{
10 mjames 94
  m_display.fontSigDigits(small_font, 0, 56, 0, 10, m_low, WHITE);
95
  m_display.fontSigDigits(small_font, 120, 56, 1, 10, m_high, WHITE);
7 mjames 96
}