Rev 10 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
11 | mjames | 1 | |
2 | #include <cstring> |
||
4 | mjames | 3 | #include "libOLED/displayclass.H" |
4 | |||
10 | mjames | 5 | void display_t::printString(font_t &font, char const *string, uint16_t length, |
6 | colour_t colour) |
||
4 | mjames | 7 | { |
7 | mjames | 8 | setPixelMode(colour); |
11 | mjames | 9 | if(length==0) |
10 | length=strlen(string); |
||
10 | mjames | 11 | uint16_t const xSpacing = font.width() + 1; |
12 | for (uint16_t yt = 0; yt < font.height(); yt++) // iterate down scan lines |
||
13 | { |
||
14 | uint16_t ys = m_cursor_y + yt; |
||
15 | for (uint16_t xt = 0; xt < length; xt++) |
||
4 | mjames | 16 | { |
10 | mjames | 17 | unsigned char c = string[xt]; |
18 | uint16_t xs = xt * xSpacing + m_cursor_x; |
||
19 | for (uint16_t j = 0; j < font.spacing(); j++) |
||
20 | { |
||
21 | drawPixel(j + xs, ys, font.getPixel(c, j, yt)); |
||
22 | } |
||
4 | mjames | 23 | } |
10 | mjames | 24 | } |
4 | mjames | 25 | // move the cursor. |
26 | m_cursor_x += xSpacing * length; |
||
27 | } |
||
28 | |||
29 | // scale is multiplied by 256 |
||
10 | mjames | 30 | void display_t::printScaledString(font_t &font, char const *string, |
31 | uint16_t length, uint16_t scale, |
||
32 | colour_t colour) |
||
4 | mjames | 33 | { |
10 | mjames | 34 | uint16_t xt, yt, jt, curr_x = 0; |
4 | mjames | 35 | uint16_t curr_y = m_cursor_y; |
36 | if (scale < 1) |
||
37 | return; |
||
7 | mjames | 38 | setPixelMode(colour); |
39 | |||
10 | mjames | 40 | for (yt = 0; yt < font.height() * 256; yt += scale) // iterate down scan lines |
41 | { |
||
42 | // iterate along the string |
||
43 | // local x plotting coordinate |
||
44 | curr_x = m_cursor_x; |
||
45 | for (xt = 0; xt < length; xt++) |
||
4 | mjames | 46 | { |
10 | mjames | 47 | for (jt = 0; jt < (font.spacing()) * 256; jt += scale) |
48 | { |
||
49 | unsigned char c = (string[xt]); |
||
50 | drawPixel(curr_x, curr_y, font.getPixel(c, jt / 256, yt / 256)); |
||
51 | curr_x++; |
||
52 | } |
||
4 | mjames | 53 | } |
10 | mjames | 54 | curr_y++; |
55 | } |
||
56 | if (curr_x > width()) |
||
57 | { |
||
58 | curr_x = 0; |
||
59 | } |
||
4 | mjames | 60 | m_cursor_x = curr_x; |
61 | } |
||
62 | |||
5 | mjames | 63 | uint8_t |
10 | mjames | 64 | display_t::formatNum(char *buff, uint8_t siz, uint8_t digits, uint8_t dp_pos, int val) |
5 | mjames | 65 | { |
10 | mjames | 66 | if (dp_pos != NO_DECIMAL) |
67 | digits++; |
||
5 | mjames | 68 | uint8_t pos = digits; |
69 | uint8_t dp_loc = pos - dp_pos; |
||
70 | uint8_t sign = 0; |
||
71 | if (val < 0) |
||
10 | mjames | 72 | { |
73 | sign = 1; |
||
74 | val = -val; |
||
75 | } |
||
5 | mjames | 76 | |
77 | buff[pos] = 0; |
||
78 | while (pos && pos < siz) |
||
10 | mjames | 79 | { |
80 | if (pos == dp_loc) |
||
5 | mjames | 81 | { |
10 | mjames | 82 | buff[--pos] = '.'; |
5 | mjames | 83 | } |
10 | mjames | 84 | else |
5 | mjames | 85 | { |
10 | mjames | 86 | buff[--pos] = val % 10 + '0'; |
87 | val /= 10; |
||
88 | if (val == 0 && pos < dp_loc) |
||
89 | break; |
||
5 | mjames | 90 | } |
10 | mjames | 91 | } |
92 | if (sign) |
||
93 | { |
||
94 | buff[--pos] = '-'; |
||
95 | } |
||
5 | mjames | 96 | return digits; |
97 | } |
||
98 | |||
10 | mjames | 99 | void display_t::fontDigits(font_t &font, uint8_t digits, uint8_t dp_pos, int val, colour_t colour) |
5 | mjames | 100 | { |
101 | char buff[10] = " "; |
||
10 | mjames | 102 | uint8_t wid = formatNum(buff, sizeof(buff), digits, dp_pos, val); |
103 | printString(font, buff, wid, colour); |
||
5 | mjames | 104 | } |
105 | |||
10 | mjames | 106 | int8_t display_t::fontSigDigits(font_t &font, uint8_t x, uint8_t y, bool right_justify, uint8_t dp_pos, |
107 | int val, colour_t colour) |
||
5 | mjames | 108 | { |
109 | char digits; |
||
10 | mjames | 110 | uint8_t width; |
5 | mjames | 111 | char sign = 0; |
112 | int uval; |
||
113 | if (val < 0) |
||
10 | mjames | 114 | { |
115 | uval = -val; |
||
116 | sign = 1; // mark as negative |
||
117 | } |
||
5 | mjames | 118 | else |
10 | mjames | 119 | { |
120 | uval = val; |
||
121 | } |
||
5 | mjames | 122 | if (uval < 10) // always one digit for a sign or space, one for a digit |
10 | mjames | 123 | { |
124 | digits = 1; |
||
125 | } |
||
5 | mjames | 126 | if (uval >= 10 && uval < 100) |
10 | mjames | 127 | { |
128 | digits = 2; |
||
129 | } |
||
5 | mjames | 130 | if (uval >= 100 && uval < 1000) |
10 | mjames | 131 | { |
132 | digits = 3; |
||
133 | } |
||
5 | mjames | 134 | if (uval >= 1000) |
10 | mjames | 135 | { |
136 | digits = 4; |
||
137 | } |
||
5 | mjames | 138 | // backup for the - sign if right justified |
139 | if (right_justify) |
||
10 | mjames | 140 | { |
141 | if (dp_pos != NO_DECIMAL) |
||
5 | mjames | 142 | { |
10 | mjames | 143 | digits += 2; |
5 | mjames | 144 | } |
10 | mjames | 145 | else |
146 | { |
||
147 | digits += 1; |
||
148 | } |
||
149 | width = digits; |
||
150 | x -= digits * font.spacing(); |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | width = digits; |
||
155 | if (dp_pos != NO_DECIMAL) |
||
156 | ++width; |
||
157 | if (sign) |
||
158 | ++width; |
||
159 | } |
||
5 | mjames | 160 | |
10 | mjames | 161 | gotoxy(x, y); |
162 | fontDigits(font, digits, dp_pos, val, colour); |
||
163 | // right justify always leaves space for sign |
||
164 | return width; |
||
5 | mjames | 165 | } |