From 45639e5c236446ca4de939b5012084adbb7d8c08 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Thu, 29 Jun 2023 18:46:19 +1000 Subject: [PATCH] Number Bounded draw --- source/Core/Drivers/OLED.cpp | 42 ++++++++++++++++++++++++++++++------ source/Core/Drivers/OLED.hpp | 11 ++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/source/Core/Drivers/OLED.cpp b/source/Core/Drivers/OLED.cpp index 83fa065e..16a35cc5 100644 --- a/source/Core/Drivers/OLED.cpp +++ b/source/Core/Drivers/OLED.cpp @@ -432,14 +432,25 @@ void OLED::setInverseDisplay(bool inverse) { OLED_Setup_Array[21].val = normalInverseCmd; I2C_CLASS::I2C_RegisterWrite(DEVICEADDR_OLED, 0x80, normalInverseCmd); } -void OLED::printBounded(const char *str, const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h) { +void OLED::printBounded(const char *str, const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h, FontStyle fontStyle) { setCursor(x, y); - const uint8_t *next = reinterpret_cast(str); - FontStyle fontStyle = FontStyle::SMALL; - if (next[0] == 0x01) { - fontStyle = FontStyle::LARGE; - next++; + + const uint8_t *next = reinterpret_cast(str); + // Determine font size by newline magic marker + if (fontStyle == FontStyle::FROM_TEXT) { + fontStyle = FontStyle::SMALL; + if (next[0] == 0x01) { + fontStyle = FontStyle::LARGE; + next++; + } + } else if (fontStyle == FontStyle::FROM_HEIGHT) { + if (h > get_fontstyle_height(FontStyle::SMALL)) { + fontStyle = FontStyle::LARGE; + } else { + fontStyle = FontStyle::SMALL; + } } + // Now we walk and print while (next[0]) { uint16_t index; @@ -534,6 +545,25 @@ void OLED::drawHex(uint32_t x, FontStyle fontStyle, uint8_t digits) { drawChar(value + 2, fontStyle); } } +void OLED::printNumberBounded(const uint16_t num, bool noLeaderZeros, const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h) { + // Print the number to the screen, bounded by the given dimension + FontStyle fontStyle = get_fontstyle_fromHeight(h); + uint8_t places = w / get_fontstyle_width(fontStyle); + if (places > 5) + places = 5; + char buffer[7] = {0, 0, 0, 0, 0, 0, 0}; + + uint16_t number = num; + + for (int i = places; i >= 0; i--) { + buffer[i] = 2 /*Skew past null and newline*/ + number % 10; + number /= 10; + } + + if (noLeaderZeros) + stripLeaderZeros(buffer, places); + printBounded(buffer, x, y, w, h, FontStyle::FROM_HEIGHT); +} // maximum places is 5 void OLED::printNumber(uint16_t number, uint8_t places, FontStyle fontStyle, bool noLeaderZeros) { char buffer[7] = {0}; diff --git a/source/Core/Drivers/OLED.hpp b/source/Core/Drivers/OLED.hpp index f757b391..ded87efe 100644 --- a/source/Core/Drivers/OLED.hpp +++ b/source/Core/Drivers/OLED.hpp @@ -101,10 +101,13 @@ public: static void setBrightness(uint8_t contrast); static void setInverseDisplay(bool inverted); static int16_t getCursorX() { return cursor_x; } - static void printBounded(const char *str, const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h); - static void print(const char *string, FontStyle fontStyle, - uint8_t length = 255); // Draw a string to the current location, with selected font; optionally - with MAX length only - static void printWholeScreen(const char *string); + + static void printBounded(const char *str, const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h, FontStyle fontStyle = FontStyle::FROM_TEXT); + void printNumberBounded(const uint16_t num, bool noLeaderZeros, const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h); + + static void print(const char *string, FontStyle fontStyle, + uint8_t length = 255); // Draw a string to the current location, with selected font; optionally - with MAX length only + static void printWholeScreen(const char *string); // Set the cursor location by pixels static void setCursor(int16_t x, int16_t y) { cursor_x = x;