Skip to content

Instantly share code, notes, and snippets.

@numinit
Created May 3, 2012 04:46
Show Gist options
  • Save numinit/2583219 to your computer and use it in GitHub Desktop.
Save numinit/2583219 to your computer and use it in GitHub Desktop.
void fr12_lcd::print_wrap(char *msg) {
// Copy the message string
size_t len = strlen(msg);
char *c = (char *)malloc(len);
strcpy(c, msg);
// Start tokenizing
char *p = strtok(c, " ");
while (p != NULL) {
size_t p_len = strlen(p), len = p_len + this->x;
// Zero-length token. Write an extra space and continue.
if (len == 0) {
p = strtok(NULL, " ");
this->putc_wrap(' ');
continue;
}
// This token will fit. Just write it...
else if (len <= fr12_lcd_width) {
this->puts_wrap(p, p_len);
// Length plus trailing space would fill the line
if (len + 1 >= fr12_lcd_width) {
this->x = 0;
this->hw->setCursor(this->x, ++this->y);
}
else {
this->hw->write(' ');
this->x++;
}
}
// Everything else
else {
// Simple wrap for too long stuff
if (p_len > fr12_lcd_width) {
this->puts_wrap(p, p_len);
}
// Next line
else {
this->x = 0;
this->hw->setCursor(this->x, ++this->y);
this->hw->print(p);
if (p_len + 1 < fr12_lcd_width) {
this->hw->write(' ');
this->x++;
}
}
}
p = strtok(NULL, " ");
}
// Free memory
free(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment