Subversion Repositories libOLED

Rev

Rev 10 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <cstring>
  3. #include "libOLED/displayclass.H"
  4.  
  5. void display_t::printString(font_t &font, char const *string, uint16_t length,
  6.                             colour_t colour)
  7. {
  8.   setPixelMode(colour);
  9.   if(length==0)
  10.     length=strlen(string);
  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++)
  16.     {
  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.       }
  23.     }
  24.   }
  25.   // move the cursor.
  26.   m_cursor_x += xSpacing * length;
  27. }
  28.  
  29. // scale is multiplied by 256
  30. void display_t::printScaledString(font_t &font, char const *string,
  31.                                   uint16_t length, uint16_t scale,
  32.                                   colour_t colour)
  33. {
  34.   uint16_t xt, yt, jt, curr_x = 0;
  35.   uint16_t curr_y = m_cursor_y;
  36.   if (scale < 1)
  37.     return;
  38.   setPixelMode(colour);
  39.  
  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++)
  46.     {
  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.       }
  53.     }
  54.     curr_y++;
  55.   }
  56.   if (curr_x > width())
  57.   {
  58.     curr_x = 0;
  59.   }
  60.   m_cursor_x = curr_x;
  61. }
  62.  
  63. uint8_t
  64. display_t::formatNum(char *buff, uint8_t siz, uint8_t digits, uint8_t dp_pos, int val)
  65. {
  66.   if (dp_pos != NO_DECIMAL)
  67.     digits++;
  68.   uint8_t pos = digits;
  69.   uint8_t dp_loc = pos - dp_pos;
  70.   uint8_t sign = 0;
  71.   if (val < 0)
  72.   {
  73.     sign = 1;
  74.     val = -val;
  75.   }
  76.  
  77.   buff[pos] = 0;
  78.   while (pos && pos < siz)
  79.   {
  80.     if (pos == dp_loc)
  81.     {
  82.       buff[--pos] = '.';
  83.     }
  84.     else
  85.     {
  86.       buff[--pos] = val % 10 + '0';
  87.       val /= 10;
  88.       if (val == 0 && pos < dp_loc)
  89.         break;
  90.     }
  91.   }
  92.   if (sign)
  93.   {
  94.     buff[--pos] = '-';
  95.   }
  96.   return digits;
  97. }
  98.  
  99. void display_t::fontDigits(font_t &font, uint8_t digits, uint8_t dp_pos, int val, colour_t colour)
  100. {
  101.   char buff[10] = "        ";
  102.   uint8_t wid = formatNum(buff, sizeof(buff), digits, dp_pos, val);
  103.   printString(font, buff, wid, colour);
  104. }
  105.  
  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)
  108. {
  109.   char digits;
  110.   uint8_t width;
  111.   char sign = 0;
  112.   int uval;
  113.   if (val < 0)
  114.   {
  115.     uval = -val;
  116.     sign = 1; // mark as negative
  117.   }
  118.   else
  119.   {
  120.     uval = val;
  121.   }
  122.   if (uval < 10) // always one digit for a sign or space, one for a digit
  123.   {
  124.     digits = 1;
  125.   }
  126.   if (uval >= 10 && uval < 100)
  127.   {
  128.     digits = 2;
  129.   }
  130.   if (uval >= 100 && uval < 1000)
  131.   {
  132.     digits = 3;
  133.   }
  134.   if (uval >= 1000)
  135.   {
  136.     digits = 4;
  137.   }
  138.   // backup for the - sign if right justified
  139.   if (right_justify)
  140.   {
  141.     if (dp_pos != NO_DECIMAL)
  142.     {
  143.       digits += 2;
  144.     }
  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.   }
  160.  
  161.   gotoxy(x, y);
  162.   fontDigits(font, digits, dp_pos, val, colour);
  163.   // right justify always leaves space for sign
  164.   return width;
  165. }
  166.