Subversion Repositories libOLED

Rev

Rev 17 | 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"
16 mjames 12
#include <string.h>
4 mjames 13
 
14
static ap_math math;
15
 
17 mjames 16
displayDial_t::displayDial_t(display_t &display,
17
                             uint8_t x, uint8_t y,
18
                             uint8_t siz,
19
                             int16_t angle_low,
20
                             int16_t angle_high) : m_display(display),
21
                                                   m_xo(x), m_yo(y), m_siz(siz),
22
                                                   m_angleLow(angle_low),
23
                                                   m_angleRange(angle_high - angle_low),
24
                                                   m_unitStr(nullptr),
25
                                                   m_titleStr(nullptr)
4 mjames 26
{
27
}
28
 
17 mjames 29
displayDial_t::displayDial_t(display_t &display,
30
                             uint8_t x, uint8_t y,
31
                             uint8_t siz,
32
                             uint16_t angle_) : m_display(display),
33
                                                m_xo(x), m_yo(y),
34
                                                m_siz(siz),
35
                                                m_angleLow(-angle_),
36
                                                m_angleRange(2 * angle_),
37
                                                m_unitStr(nullptr),
38
                                                m_titleStr(nullptr)
14 mjames 39
{
16 mjames 40
}
14 mjames 41
 
4 mjames 42
/* position is integer from 0 to SINE_STEPS */
10 mjames 43
void displayDial_t::draw_needle(int16_t position)
4 mjames 44
{
14 mjames 45
  // int ang = math.SINE_SCALING * ((position - (math.SINE_STEPS / 2)) * m_a1) / (math.SINE_STEPS / 2);
46
  int ang = (math.SINE_SCALING * position * m_angleRange) / math.SINE_STEPS + m_angleLow * math.SINE_SCALING;
10 mjames 47
  int si = math.ap_sin(ang);
48
  int co = math.ap_cos(ang);
4 mjames 49
 
15 mjames 50
  /* calculate an orthogonal XY shift for a second side of the needle */
10 mjames 51
  int xl = math.ap_sin(ang - math.SINE_STEPS);
52
  int yl = math.ap_cos(ang - math.SINE_STEPS);
4 mjames 53
 
54
  int si2 = m_siz + 2;
55
  int si3 = 2 * m_siz / 3;
56
 
10 mjames 57
  int xs, ys;
4 mjames 58
  // three parallel lines
59
  xs = -xl;
60
  ys = -yl;
61
  int step;
10 mjames 62
  for (step = 0; step < 3; step++)
63
  {
64
    m_display.drawLine(math.AP_SCALE(si * si2 - xs) + m_xo, m_yo - math.AP_SCALE(co * si2 - ys),
65
                       math.AP_SCALE(si * si3 - xs) + m_xo, m_yo - math.AP_SCALE(co * si3 - ys), INVERT);
66
    xs += xl;
67
    ys += yl;
68
  }
69
}
4 mjames 70
 
10 mjames 71
void displayDial_t::draw_scale(int16_t low, int16_t high, uint8_t width, uint8_t num_step, int16_t scale)
4 mjames 72
{
5 mjames 73
 
4 mjames 74
  int ang;
7 mjames 75
  m_low = low;
76
  m_high = high;
10 mjames 77
  int sc_low = low / scale;
78
  int sc_high = high / scale;
14 mjames 79
  int step = 256 * m_angleRange / (4 * (sc_high - sc_low));
4 mjames 80
  int t;
14 mjames 81
  ang = m_angleLow * 256;
16 mjames 82
  int d = sc_low < sc_high ? 1 : -1;
14 mjames 83
  t = sc_low * 4;
16 mjames 84
  while (1)
10 mjames 85
  {
86
    int si = math.ap_sin((ang * math.SINE_SCALING) / 256);
87
    int co = math.ap_cos((ang * math.SINE_SCALING) / 256);
88
 
89
    int len;
90
    switch (t % 4)
4 mjames 91
    {
10 mjames 92
    case 0:
93
      len = width;
94
      break;
95
    case 1:
96
    case 3:
97
    case -1:
98
    case -3:
99
      len = width / 4;
100
      break;
101
    case 2:
102
    case -2:
103
      len = width / 2;
104
      break;
4 mjames 105
    }
106
 
10 mjames 107
    m_display.drawLine(math.AP_SCALE((m_siz)*si) + m_xo, m_yo - math.AP_SCALE((m_siz)*co),
108
                       math.AP_SCALE((m_siz - len) * si) + m_xo,
109
                       m_yo - math.AP_SCALE((m_siz - len) * co), WHITE);
16 mjames 110
 
111
    if (t == sc_high * 4)
112
      break;
113
 
10 mjames 114
    ang += step;
14 mjames 115
    t += d;
10 mjames 116
  }
4 mjames 117
}
7 mjames 118
 
16 mjames 119
void displayDial_t::draw_value_items(int val, int dpCode, int width, int x, int y)
7 mjames 120
{
16 mjames 121
 
122
  m_display.gotoxy(x, y);
17 mjames 123
  m_display.fontDigits(medium_font, width, dpCode, val, WHITE);
18 mjames 124
  m_display.gotoxy(x, y + medium_font.height());
16 mjames 125
  if (m_unitStr)
126
  {
127
    int l = strlen(m_unitStr);
128
    if (l > 4)
129
      l = 4;
130
    m_display.printString(small_font, m_unitStr, l);
131
    m_display.printString(small_font, "     ", 4 - l);
132
  }
133
  else
134
  {
135
    m_display.printString(small_font, "     ", 4);
136
  }
137
}
138
 
139
////////////////////////////////////////////////////////////////////////////////
140
displayFullDial_t::displayFullDial_t(display_t &display) : displayDial_t(display, 64, 60, 60, 90)
141
{
142
}
143
 
144
void displayFullDial_t::draw_limits()
145
{
10 mjames 146
  m_display.fontSigDigits(small_font, 0, 56, 0, 10, m_low, WHITE);
147
  m_display.fontSigDigits(small_font, 120, 56, 1, 10, m_high, WHITE);
17 mjames 148
 
149
  if (m_titleStr)
150
  {
151
    int i = strlen(m_titleStr);
152
    m_display.gotoxy(64 - i * 5, 48);
153
    m_display.printString(large_font, m_titleStr, i, WHITE);
154
  }
7 mjames 155
}
16 mjames 156
 
17 mjames 157
void displayFullDial_t ::draw_value(int val, int dp, int digits)
16 mjames 158
{
17 mjames 159
  draw_value_items(val, dp, digits, 30, 32);
16 mjames 160
}
161
 
162
////////////////////////////////////////////////////////////////////////////////
163
displayLeftQuadrantDial_t::displayLeftQuadrantDial_t(display_t &display) : displayDial_t(display, 0, 60, 60, 90, 0){};
164
 
165
void displayLeftQuadrantDial_t::draw_limits()
166
{
167
  m_display.fontSigDigits(small_font, 0, 10, false, display_t::NO_DECIMAL, m_high, WHITE);
168
  m_display.fontSigDigits(small_font, 56, 56, true, display_t::NO_DECIMAL, m_low, WHITE);
17 mjames 169
 
170
  if (m_titleStr)
171
  {
172
    int i = strlen(m_titleStr);
173
    m_display.gotoxy(64 - i * 5, 0);
174
    m_display.printString(large_font, m_titleStr, i, WHITE);
175
  }
16 mjames 176
}
177
 
17 mjames 178
void displayLeftQuadrantDial_t ::draw_value(int val, int dp, int digits)
16 mjames 179
{
17 mjames 180
  draw_value_items(val, dp, digits, 0, 32);
16 mjames 181
}
182
 
183
////////////////////////////////////////////////////////////////////////////////
184
displayRightQuadrantDial_t::displayRightQuadrantDial_t(display_t &display) : displayDial_t(display, 127, 60, 60, -90, 0){};
185
 
186
void displayRightQuadrantDial_t::draw_limits()
187
{
188
  m_display.fontSigDigits(small_font, 128, 10, true, display_t::NO_DECIMAL, m_high, WHITE);
189
  m_display.fontSigDigits(small_font, 128 - 56, 56, false, display_t::NO_DECIMAL, m_low, WHITE);
190
}
191
 
17 mjames 192
void displayRightQuadrantDial_t::draw_value(int val, int dp, int digits)
16 mjames 193
{
17 mjames 194
  draw_value_items(val, dp, digits, 90, 32);
16 mjames 195
}