Subversion Repositories libOLED

Rev

Rev 4 | 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
 
3
void
4
display_t::printString (font_t &font, char const *string, uint16_t length,
5
                        colour_t colour)
6
{
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++)
12
        {
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
              if (font.getPixel (c, j, yt) != 0)
18
                drawPixel (j + xs, ys, colour);
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;
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++)
42
        {
43
          for (jt = 0; jt < (font.spacing ()) * 256; jt += scale)
44
            {
45
              unsigned char c = (string[xt]);
46
              if (font.getPixel (c, jt / 256, yt / 256))
47
                drawPixel (curr_x, curr_y, colour);
48
              curr_x++;
49
            }
50
        }
51
      curr_y++;
52
    }
53
  if (curr_x > width ())
54
    {
55
      curr_x = 0;
56
    }
57
  m_cursor_x = curr_x;
58
}
59
 
5 mjames 60
uint8_t
61
display_t::formatNum (char *buff, uint8_t siz, uint8_t digits, uint8_t dp_pos, int val)
62
{
63
  digits++;
64
  uint8_t pos = digits;
65
  uint8_t dp_loc = pos - dp_pos;
66
  uint8_t sign = 0;
67
  if (val < 0)
68
    {
69
      sign = 1;
70
      val = -val;
71
    }
72
 
73
  buff[pos] = 0;
74
  while (pos && pos < siz)
75
    {
76
      if (pos == dp_loc)
77
        {
78
          buff[--pos] = '.';
79
        }
80
      else
81
        {
82
          buff[--pos] = val % 10 + '0';
83
          val /= 10;
84
          if (val == 0 && pos < dp_loc)
85
            break;
86
        }
87
    }
88
  if (sign)
89
    {
90
      buff[--pos] = '-';
91
    }
92
  return digits;
93
}
94
 
95
void
96
display_t::fontDigits (font_t &font, uint8_t digits, uint8_t dp_pos, int val)
97
{
98
  char buff[10] = "        ";
99
  uint8_t wid = formatNum (buff, sizeof(buff), digits, dp_pos, val);
100
  printString (font, buff, wid, WHITE );
101
}
102
 
103
void
104
display_t::fontSigDigits (font_t &font,uint8_t x, uint8_t y, bool right_justify, uint8_t dp_pos,
105
                 int val)
106
{
107
  char digits;
108
  char sign = 0;
109
  int uval;
110
  if (val < 0)
111
    {
112
      uval = -val;
113
      sign = 1; // mark as negative
114
    }
115
  else
116
    {
117
      uval = val;
118
    }
119
  if (uval < 10) // always one digit for a sign or space, one for a digit
120
    {
121
      digits = 1;
122
    }
123
  if (uval >= 10 && uval < 100)
124
    {
125
      digits = 2;
126
    }
127
  if (uval >= 100 && uval < 1000)
128
    {
129
      digits = 3;
130
    }
131
  if (uval >= 1000)
132
    {
133
      digits = 4;
134
    }
135
  // backup for the - sign if right justified
136
  if (right_justify)
137
    {
138
      if (dp_pos < 10)
139
        {
140
          digits += 2;
141
        }
142
      else
143
        {
144
          digits += 1;
145
        }
146
      x -= digits * font.spacing();
147
    }
148
 
149
  gotoxy (x, y);
150
  fontDigits (font, digits, dp_pos, val);
151
}
152
 
153