Rev 14 | Rev 16 | 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 | |||
14 | mjames | 15 | 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) |
4 | mjames | 16 | { |
17 | } |
||
18 | |||
14 | mjames | 19 | 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_) |
20 | { |
||
21 | |||
4 | mjames | 22 | /* position is integer from 0 to SINE_STEPS */ |
10 | mjames | 23 | void displayDial_t::draw_needle(int16_t position) |
4 | mjames | 24 | { |
14 | mjames | 25 | // int ang = math.SINE_SCALING * ((position - (math.SINE_STEPS / 2)) * m_a1) / (math.SINE_STEPS / 2); |
26 | int ang = (math.SINE_SCALING * position * m_angleRange) / math.SINE_STEPS + m_angleLow * math.SINE_SCALING; |
||
10 | mjames | 27 | int si = math.ap_sin(ang); |
28 | int co = math.ap_cos(ang); |
||
4 | mjames | 29 | |
15 | mjames | 30 | /* calculate an orthogonal XY shift for a second side of the needle */ |
10 | mjames | 31 | int xl = math.ap_sin(ang - math.SINE_STEPS); |
32 | int yl = math.ap_cos(ang - math.SINE_STEPS); |
||
4 | mjames | 33 | |
34 | int si2 = m_siz + 2; |
||
35 | int si3 = 2 * m_siz / 3; |
||
36 | |||
10 | mjames | 37 | int xs, ys; |
4 | mjames | 38 | // three parallel lines |
39 | xs = -xl; |
||
40 | ys = -yl; |
||
41 | int step; |
||
10 | mjames | 42 | for (step = 0; step < 3; step++) |
43 | { |
||
44 | m_display.drawLine(math.AP_SCALE(si * si2 - xs) + m_xo, m_yo - math.AP_SCALE(co * si2 - ys), |
||
4 | mjames | 45 | |
10 | mjames | 46 | math.AP_SCALE(si * si3 - xs) + m_xo, m_yo - math.AP_SCALE(co * si3 - ys), INVERT); |
47 | xs += xl; |
||
48 | ys += yl; |
||
49 | } |
||
50 | } |
||
4 | mjames | 51 | |
10 | mjames | 52 | void displayDial_t::draw_scale(int16_t low, int16_t high, uint8_t width, uint8_t num_step, int16_t scale) |
4 | mjames | 53 | { |
5 | mjames | 54 | |
4 | mjames | 55 | int ang; |
7 | mjames | 56 | m_low = low; |
57 | m_high = high; |
||
10 | mjames | 58 | int sc_low = low / scale; |
59 | int sc_high = high / scale; |
||
14 | mjames | 60 | int step = 256 * m_angleRange / (4 * (sc_high - sc_low)); |
4 | mjames | 61 | int t; |
14 | mjames | 62 | ang = m_angleLow * 256; |
63 | int d = sc_low < sc_high ? 1 : -1; |
||
64 | t = sc_low * 4; |
||
65 | while(1) |
||
10 | mjames | 66 | { |
67 | int si = math.ap_sin((ang * math.SINE_SCALING) / 256); |
||
68 | int co = math.ap_cos((ang * math.SINE_SCALING) / 256); |
||
69 | |||
70 | int len; |
||
71 | switch (t % 4) |
||
4 | mjames | 72 | { |
10 | mjames | 73 | case 0: |
74 | len = width; |
||
75 | break; |
||
76 | case 1: |
||
77 | case 3: |
||
78 | case -1: |
||
79 | case -3: |
||
80 | len = width / 4; |
||
81 | break; |
||
82 | case 2: |
||
83 | case -2: |
||
84 | len = width / 2; |
||
85 | break; |
||
4 | mjames | 86 | } |
87 | |||
10 | mjames | 88 | m_display.drawLine(math.AP_SCALE((m_siz)*si) + m_xo, m_yo - math.AP_SCALE((m_siz)*co), |
89 | math.AP_SCALE((m_siz - len) * si) + m_xo, |
||
90 | m_yo - math.AP_SCALE((m_siz - len) * co), WHITE); |
||
14 | mjames | 91 | |
92 | if (t == sc_high * 4) |
||
93 | break; |
||
94 | |||
10 | mjames | 95 | ang += step; |
14 | mjames | 96 | t += d; |
10 | mjames | 97 | } |
4 | mjames | 98 | } |
7 | mjames | 99 | |
15 | mjames | 100 | // this only works for a fullsize display. |
7 | mjames | 101 | void displayDial_t::draw_limits() |
102 | { |
||
10 | mjames | 103 | m_display.fontSigDigits(small_font, 0, 56, 0, 10, m_low, WHITE); |
104 | m_display.fontSigDigits(small_font, 120, 56, 1, 10, m_high, WHITE); |
||
7 | mjames | 105 | } |