writeString method

void writeString(
  1. Font font,
  2. int size,
  3. String string,
  4. bool color,
  5. bool wrap,
  6. int linespacing,
)

write text to the oled

Implementation

void writeString(Font font, int size, String string, bool color, bool wrap,
    int linespacing) {
  final wordArr = string.split(' ');

  final len = wordArr.length;

  // start x offset at cursor pos
  var offset = _cursor_x;
  var padding = 0;

  const letspace = 1;
  final leading = linespacing | 2;

  // loop through words
  for (var i = 0; i < len; i += 1) {
    // put the word space back in
    if (i < len - 1) wordArr[i] += ' ';

    final stringArr = wordArr[i].split('');
    final slen = stringArr.length;
    final compare = (font.width * size * slen) + (size * (len - 1));

    // wrap words if necessary
    if (wrap && len > 1 && (offset >= (width - compare))) {
      offset = 1;
      _cursor_y += (font.height * size) + size + leading;
      setCursor(offset, _cursor_y);
    }

    // loop through the array of each char to draw
    for (var i = 0; i < slen; i += 1) {
      // look up the position of the char, pull out the buffer slice
      final charBuf = _findCharBuf(font, stringArr[i]);
      // read the bits in the bytes that make up the char
      final charBytes = _readCharBytes(charBuf);
      // draw the entire charactei
      _drawChar(font, charBytes, size, color);

      // fills in background behind the text pixels so that it's easier to read the text
      fillRect(
          offset - padding, _cursor_y, padding, (font.height * size), !color);

      // calc new x position for the next char, add a touch of padding too if it's a non space char
      padding = (stringArr[i] == ' ') ? 0 : size + letspace;
      offset += (font.width * size) + padding;

      // wrap letters if necessary
      if (wrap && (offset >= (width - font.width - letspace))) {
        offset = 1;
        _cursor_y += (font.height * size) + size + leading;
      }
      // set the 'cursor' for the next char to be drawn, then loop again for next char
      setCursor(offset, _cursor_y);
    }
  }
}