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