Rev 7 | Rev 13 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* |
2 | * dials.c |
||
3 | * |
||
4 | * Created on: 22 Jan 2016 |
||
5 | * Author: Mike |
||
6 | */ |
||
7 | #include "stm32f1xx_hal.h" |
||
8 | #include "ap_math.h" |
||
9 | #include "font.h" |
||
10 | #include "SSD1306.h" |
||
11 | |||
6 | mjames | 12 | static uint16_t a1 = 90; |
13 | static uint8_t xo = 64; |
||
14 | static uint8_t yo = 64; |
||
15 | static uint8_t siz = 32; |
||
2 | mjames | 16 | |
17 | /* percent is integer from 0 to 100 */ |
||
6 | mjames | 18 | void |
19 | dial_draw_needle (uint16_t percent) |
||
2 | mjames | 20 | { |
6 | mjames | 21 | int ang = ((percent - 50) * a1) / 50; |
2 | mjames | 22 | |
6 | mjames | 23 | int si = ap_sin (ang); |
24 | int co = ap_cos (ang); |
||
2 | mjames | 25 | |
26 | /* calculate a shift for a second side of the needle */ |
||
7 | mjames | 27 | int xl = ap_sin (ang - 90); |
28 | int yl = ap_cos (ang - 90); |
||
2 | mjames | 29 | |
6 | mjames | 30 | int si2 = siz + 2; |
7 | mjames | 31 | int si3 = 2 * siz / 3; |
5 | mjames | 32 | |
7 | mjames | 33 | int xs,ys; |
34 | // three parallel lines |
||
35 | xs = -xl; |
||
36 | ys = -yl; |
||
37 | int step; |
||
38 | for(step =0; step < 3; step++) |
||
39 | { |
||
40 | drawLine (AP_SCALE(si*si2-xs) + xo, yo - AP_SCALE(co * si2 - ys), |
||
5 | mjames | 41 | |
6 | mjames | 42 | AP_SCALE(si*si3-xs) + xo, |
43 | yo - AP_SCALE(co * si3 - ys), INVERT); |
||
7 | mjames | 44 | xs+=xl; |
45 | ys+=yl; |
||
46 | } |
||
47 | } |
||
5 | mjames | 48 | |
2 | mjames | 49 | /* initialise */ |
6 | mjames | 50 | void |
51 | dial_size (uint8_t size) |
||
2 | mjames | 52 | { |
6 | mjames | 53 | siz = size; |
2 | mjames | 54 | } |
55 | |||
6 | mjames | 56 | void |
57 | dial_draw_scale (uint8_t low, uint8_t high, uint8_t width, uint8_t num_step) |
||
2 | mjames | 58 | { |
6 | mjames | 59 | int sz; |
60 | int ang; |
||
61 | int step = 256 * a1 * 2 / (4 * (high - low)); |
||
62 | int t; |
||
63 | ang = -a1 * 256; |
||
64 | for (t = low * 4; t <= high * 4; t++) |
||
65 | { |
||
66 | int si = ap_sin (ang / 256); |
||
67 | int co = ap_cos (ang / 256); |
||
68 | |||
69 | int len; |
||
70 | switch (t % 4) |
||
2 | mjames | 71 | { |
6 | mjames | 72 | case 0: |
73 | len = width; |
||
74 | break; |
||
75 | case 1: |
||
76 | case 3: |
||
77 | len = width / 4; |
||
78 | break; |
||
79 | case 2: |
||
80 | len = width / 2; |
||
81 | break; |
||
82 | } |
||
2 | mjames | 83 | |
6 | mjames | 84 | drawLine (AP_SCALE((siz)*si) + xo, yo - AP_SCALE((siz) * co), |
85 | AP_SCALE((siz-len)*si) + xo, |
||
86 | yo - AP_SCALE((siz - len) * co), 1); |
||
87 | ang += step; |
||
88 | } |
||
2 | mjames | 89 | |
6 | mjames | 90 | font_gotoxy (0, 7); |
9 | mjames | 91 | font_digits(2,10,low); |
6 | mjames | 92 | font_gotoxy (19, 7); |
9 | mjames | 93 | font_digits(2,10,high); |
6 | mjames | 94 | } |
2 | mjames | 95 | |
6 | mjames | 96 | void |
97 | dial_origin (uint8_t x, uint8_t y) |
||
2 | mjames | 98 | { |
6 | mjames | 99 | xo = x; |
100 | yo = y; |
||
2 | mjames | 101 | } |