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