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