From e0d8b6249278a4aeabb212b41c78444924ae4ced Mon Sep 17 00:00:00 2001 From: Kayce Basques Date: Thu, 22 Aug 2024 19:44:07 -0700 Subject: [PATCH] Fix SSD1306 example to display all text on small (32px) screens --- i2c/ssd1306_i2c/ssd1306_i2c.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/i2c/ssd1306_i2c/ssd1306_i2c.c b/i2c/ssd1306_i2c/ssd1306_i2c.c index 6be5ea42c..0ab7af9ca 100644 --- a/i2c/ssd1306_i2c/ssd1306_i2c.c +++ b/i2c/ssd1306_i2c/ssd1306_i2c.c @@ -387,13 +387,31 @@ int main() { "by the name of", " PICO" }; - + int max_lines = SSD1306_HEIGHT / 8; // Each line is 8px tall. int y = 0; - for (uint i = 0 ;i < count_of(text); i++) { + for (uint i = 0; i < count_of(text); i++) { + int line_number = i + 1; WriteString(buf, 5, y, text[i]); - y+=8; + y += 8; + // On the 32px-tall versions of SSD1306, we can't display + // all the text at once. We need to display 4 lines at a time. + bool is_end_of_screen = (line_number % max_lines) == 0; + if (is_end_of_screen) { + render(buf, &frame_area); + sleep_ms(3000); + memset(buf, 0, SSD1306_BUF_LEN); + y = 0; + } + // If it's the last line... + if (line_number == count_of(text)) { + // And the last line is not a multiple of max_lines... + if (line_number % max_lines != 0) { + // Then we will have a little more text to display. + render(buf, &frame_area); + sleep_ms(3000); + } + } } - render(buf, &frame_area); // Test the display invert function sleep_ms(3000);