Rev 5 | Rev 7 | 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 */ |
||
| 6 | mjames | 27 | int xs = ap_sin (ang - 90); |
| 28 | int ys = ap_cos (ang - 90); |
||
| 2 | mjames | 29 | |
| 6 | mjames | 30 | int si2 = siz + 2; |
| 31 | int si3 = siz / 2; |
||
| 5 | mjames | 32 | |
| 6 | mjames | 33 | drawLine (AP_SCALE(si*si2-xs) + xo, yo - AP_SCALE(co * si2 - ys), |
| 5 | mjames | 34 | |
| 6 | mjames | 35 | AP_SCALE(si*si3-xs) + xo, |
| 36 | yo - AP_SCALE(co * si3 - ys), INVERT); |
||
| 37 | drawLine (AP_SCALE(si*si2+xs) + xo, yo - AP_SCALE(co * si2 + ys), |
||
| 5 | mjames | 38 | |
| 6 | mjames | 39 | AP_SCALE(si*si3+xs) + xo, |
| 40 | yo - AP_SCALE(co * si3 + ys), INVERT); |
||
| 2 | mjames | 41 | |
| 42 | } |
||
| 43 | |||
| 44 | /* initialise */ |
||
| 6 | mjames | 45 | void |
| 46 | dial_size (uint8_t size) |
||
| 2 | mjames | 47 | { |
| 6 | mjames | 48 | siz = size; |
| 2 | mjames | 49 | } |
| 50 | |||
| 6 | mjames | 51 | void |
| 52 | dial_draw_scale (uint8_t low, uint8_t high, uint8_t width, uint8_t num_step) |
||
| 2 | mjames | 53 | { |
| 6 | mjames | 54 | int sz; |
| 55 | int ang; |
||
| 56 | int step = 256 * a1 * 2 / (4 * (high - low)); |
||
| 57 | int t; |
||
| 58 | ang = -a1 * 256; |
||
| 59 | for (t = low * 4; t <= high * 4; t++) |
||
| 60 | { |
||
| 61 | int si = ap_sin (ang / 256); |
||
| 62 | int co = ap_cos (ang / 256); |
||
| 63 | |||
| 64 | int len; |
||
| 65 | switch (t % 4) |
||
| 2 | mjames | 66 | { |
| 6 | mjames | 67 | case 0: |
| 68 | len = width; |
||
| 69 | break; |
||
| 70 | case 1: |
||
| 71 | case 3: |
||
| 72 | len = width / 4; |
||
| 73 | break; |
||
| 74 | case 2: |
||
| 75 | len = width / 2; |
||
| 76 | break; |
||
| 77 | } |
||
| 2 | mjames | 78 | |
| 6 | mjames | 79 | drawLine (AP_SCALE((siz)*si) + xo, yo - AP_SCALE((siz) * co), |
| 80 | AP_SCALE((siz-len)*si) + xo, |
||
| 81 | yo - AP_SCALE((siz - len) * co), 1); |
||
| 82 | ang += step; |
||
| 83 | } |
||
| 2 | mjames | 84 | |
| 6 | mjames | 85 | font_gotoxy (0, 7); |
| 86 | int d = low / 10; |
||
| 87 | if (d) |
||
| 88 | { |
||
| 89 | font_putchar (d + '0'); |
||
| 90 | } |
||
| 91 | else |
||
| 92 | { |
||
| 93 | font_putchar (' '); |
||
| 94 | } |
||
| 95 | d = low % 10; |
||
| 96 | font_putchar (d + '0'); |
||
| 2 | mjames | 97 | |
| 6 | mjames | 98 | font_gotoxy (19, 7); |
| 99 | d = high / 10; |
||
| 100 | if (d) |
||
| 101 | { |
||
| 102 | font_putchar (d + '0'); |
||
| 103 | } |
||
| 104 | else |
||
| 105 | { |
||
| 106 | font_putchar (' '); |
||
| 107 | } |
||
| 108 | d = high % 10; |
||
| 109 | font_putchar (d + '0'); |
||
| 2 | mjames | 110 | |
| 6 | mjames | 111 | } |
| 2 | mjames | 112 | |
| 6 | mjames | 113 | void |
| 114 | dial_origin (uint8_t x, uint8_t y) |
||
| 2 | mjames | 115 | { |
| 6 | mjames | 116 | xo = x; |
| 117 | yo = y; |
||
| 2 | mjames | 118 | } |