Subversion Repositories libOLED

Rev

Rev 7 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "libOLED/displayclass.H"
  2.  
  3. void display_t::printString(font_t &font, char const *string, uint16_t length,
  4.                             colour_t colour)
  5. {
  6.   setPixelMode(colour);
  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.         drawPixel(j + xs, ys, font.getPixel(c, j, yt));
  18.       }
  19.     }
  20.   }
  21.   // move the cursor.
  22.   m_cursor_x += xSpacing * length;
  23. }
  24.  
  25. // scale is multiplied by 256
  26. void display_t::printScaledString(font_t &font, char const *string,
  27.                                   uint16_t length, uint16_t scale,
  28.                                   colour_t colour)
  29. {
  30.   uint16_t xt, yt, jt, curr_x = 0;
  31.   uint16_t curr_y = m_cursor_y;
  32.   if (scale < 1)
  33.     return;
  34.   setPixelMode(colour);
  35.  
  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.         drawPixel(curr_x, curr_y, font.getPixel(c, jt / 256, yt / 256));
  47.         curr_x++;
  48.       }
  49.     }
  50.     curr_y++;
  51.   }
  52.   if (curr_x > width())
  53.   {
  54.     curr_x = 0;
  55.   }
  56.   m_cursor_x = curr_x;
  57. }
  58.  
  59. uint8_t
  60. display_t::formatNum(char *buff, uint8_t siz, uint8_t digits, uint8_t dp_pos, int val)
  61. {
  62.   if (dp_pos != NO_DECIMAL)
  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 display_t::fontDigits(font_t &font, uint8_t digits, uint8_t dp_pos, int val, colour_t colour)
  96. {
  97.   char buff[10] = "        ";
  98.   uint8_t wid = formatNum(buff, sizeof(buff), digits, dp_pos, val);
  99.   printString(font, buff, wid, colour);
  100. }
  101.  
  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)
  104. {
  105.   char digits;
  106.   uint8_t width;
  107.   char sign = 0;
  108.   int uval;
  109.   if (val < 0)
  110.   {
  111.     uval = -val;
  112.     sign = 1; // mark as negative
  113.   }
  114.   else
  115.   {
  116.     uval = val;
  117.   }
  118.   if (uval < 10) // always one digit for a sign or space, one for a digit
  119.   {
  120.     digits = 1;
  121.   }
  122.   if (uval >= 10 && uval < 100)
  123.   {
  124.     digits = 2;
  125.   }
  126.   if (uval >= 100 && uval < 1000)
  127.   {
  128.     digits = 3;
  129.   }
  130.   if (uval >= 1000)
  131.   {
  132.     digits = 4;
  133.   }
  134.   // backup for the - sign if right justified
  135.   if (right_justify)
  136.   {
  137.     if (dp_pos != NO_DECIMAL)
  138.     {
  139.       digits += 2;
  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.   }
  156.  
  157.   gotoxy(x, y);
  158.   fontDigits(font, digits, dp_pos, val, colour);
  159.   // right justify always leaves space for sign
  160.   return width;
  161. }
  162.