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