diff --git a/pwn_zero/include/pwnagotchi.h b/pwn_zero/include/pwnagotchi.h index 750c0216382..321fd59517c 100644 --- a/pwn_zero/include/pwnagotchi.h +++ b/pwn_zero/include/pwnagotchi.h @@ -115,7 +115,7 @@ enum PwnagotchiMode { typedef struct { /// Current face enum PwnagotchiFace face; - char* faceStr; + // char* faceStr; /// CH channel display at top left char channel[PWNAGOTCHI_MAX_CHANNEL_LEN]; /// AP text shown at the top diff --git a/pwn_zero/src/pwnagotchi.c b/pwn_zero/src/pwnagotchi.c index a65c1e592fc..b67fa1d1173 100644 --- a/pwn_zero/src/pwnagotchi.c +++ b/pwn_zero/src/pwnagotchi.c @@ -4,14 +4,14 @@ #include #include +#include #include Pwnagotchi* pwnagotchi_alloc() { Pwnagotchi* pwn = malloc(sizeof(Pwnagotchi)); - pwn->face = Grateful; - pwn->faceStr = GRATEFUL; + pwn->face = Cool; pwn->friendFace = NoFace; strncpy(pwn->channel, "*", 2); @@ -246,7 +246,49 @@ void pwnagotchi_draw_mode(Pwnagotchi* pwn, Canvas* canvas) { void pwnagotchi_draw_message(Pwnagotchi* pwn, Canvas* canvas) { canvas_set_font(canvas, FontSecondary); - canvas_draw_str(canvas, PWNAGOTCHI_MESSAGE_J, PWNAGOTCHI_MESSAGE_I, pwn->message); + int fontHeight = canvas_current_font_height(canvas); + + // Apparently W is the widest character (USING a for a more average approach) + size_t charLength = canvas_string_width(canvas, "a"); + + size_t horizSpace = FLIPPER_SCREEN_WIDTH - PWNAGOTCHI_MESSAGE_J; + size_t charSpaces = floor(((double) horizSpace) / charLength); + size_t messagePixLen = canvas_string_width(canvas, pwn->message); + size_t maxLines = floor((PWNAGOTCHI_MESSAGE_I - PWNAGOTCHI_LINE2_END_I) / ((double) fontHeight)); + + size_t requiredLines = ceil(((double) messagePixLen) / horizSpace); + + size_t charIndex = 0; + for (size_t i = 0; i < requiredLines && i < maxLines - 1; i++) { + // Allocate the line with room for two more characters (a space and then another char) + size_t allocSize = charSpaces + 2; + char* line = malloc(sizeof(char) * allocSize); + + // Copy the allotted characters into line + memcpy(line, (pwn->message + charIndex), allocSize); + + // Now loop backwards and cut it off at a space if we end with a letter + size_t backspaceCount = 0; + if (line[allocSize - 1] != ' ' && line[allocSize - 1] != '\0') { + for (int j = allocSize - 1; j >= 0; j--) { + if (line[j] == ' ') { + line[j] = '\0'; + break; + } + backspaceCount++; + + } + } + + // Lets make sure if backspaceCount is too large that we cut the word instead of drawing off the screen + if (backspaceCount >= charSpaces) {backspaceCount = 0;} + + canvas_draw_str(canvas, PWNAGOTCHI_MESSAGE_J, PWNAGOTCHI_MESSAGE_I + (i * fontHeight), line); + + charIndex += (charSpaces - backspaceCount + 1); + free(line); + } + } void pwnagotchi_free(Pwnagotchi* pwn) {