示例#1
0
void _drawMessages() {
  //Move the cursor to the bottom of the screen
  display.setCursor(0, display.height());
  display.setTextWrap(true);
  int8_t row = _bottom;

  display.clearDisplay();

  //If first message is blank quit
  ChatMessage first = getFromChatBuffer(_bottom);
  if (strlen(first.name) == 0) {
    display.setCursor(1, 1);
    display.print("Nothing's here");
    safeDisplay();
    return;
  }

  //Draw each message starting from the bottom of the screen
  while (display.getCursorY() >= 0 && row >= 0) {
    ChatMessage message = getFromChatBuffer(row);

    //Stop drawing messages if we hit an empty one
    if (strlen(message.message) == 0 && strlen(message.name) == 0) {
      break;
    }

    //Determine if the the mesage is two line or not
    bool twoLine = (strlen(message.name) + strlen(message.message) > _maxCharsPerRow);

    //Move cursor up to print the line(s)
    uint8_t y = display.getCursorY() - ANX_FONT_HEIGHT;
    if (twoLine) {
      y -= ANX_FONT_HEIGHT;
    }
    display.setCursor(0, y);

    //Ensure username is null terminated
    char name[USERNAME_MAX_LENGTH + 1];
    memcpy(name, message.name, USERNAME_MAX_LENGTH);
    name[USERNAME_MAX_LENGTH] = '\0';

    //Ensure message is null terminated
    char msg[ANX_CHAT_MAX_LENGTH + 1];
    memcpy(msg, message.message, ANX_CHAT_MAX_LENGTH);
    msg[ANX_CHAT_MAX_LENGTH] = '\0';

    display.print("@");
    display.print(name);
    display.print(":");
    display.println(msg);

    //Move cursor back up above the message we just printed
    display.setCursor(0, y);

    row--;
  }

  //Show
  safeDisplay();
}