From 0254dd78e5e829ec2257aa06fa9605f9c4db8188 Mon Sep 17 00:00:00 2001 From: AAC A <aac@a.com> Date: Fri, 14 Apr 2023 08:32:56 +0200 Subject: [PATCH] i2c_oled prototype fontsize --- examples/i2c_oled/i2c_oled.c | 11 +++- examples/i2c_oled/oled.h | 105 +++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/examples/i2c_oled/i2c_oled.c b/examples/i2c_oled/i2c_oled.c index 8181ea9..df6c8de 100644 --- a/examples/i2c_oled/i2c_oled.c +++ b/examples/i2c_oled/i2c_oled.c @@ -31,7 +31,7 @@ int main() printf("Looping on test modes..."); while(1) { - for(uint8_t mode=0;mode<5;mode++) + for(uint8_t mode=0;mode<6;mode++) { // clear buffer for next mode oled_setbuf(0); @@ -79,7 +79,14 @@ int main() oled_xorrect(64, 0, 64, 32); break; - + case 5: + oled_drawstr_sz(0,0, "Size 8x8", 1, fontsize_8x8); + oled_drawstr_sz(0,16, "Size 16x16", 1, fontsize_16x16); + Delay_Ms(1000); + oled_refresh(); + oled_drawstr_sz(0,0, "Size 32x32", 1, fontsize_32x32); + break; + default: break; } diff --git a/examples/i2c_oled/oled.h b/examples/i2c_oled/oled.h index 61298a4..c2edafa 100644 --- a/examples/i2c_oled/oled.h +++ b/examples/i2c_oled/oled.h @@ -483,6 +483,111 @@ void oled_drawstr(uint8_t x, uint8_t y, char *str, uint8_t color) } } +/* + * Draw character to the display buffer, scaled to size + */ + +typedef enum { + fontsize_8x8 = 1, + fontsize_16x16 = 2, + fontsize_32x32 = 4 +} font_size_t; +/* +void oled_drawchar_sz(uint8_t x, uint8_t y, uint8_t chr, uint8_t color, font_size_t font_size) +{ + uint16_t i, j, col; + uint8_t d; + + uint8_t font_scale = (uint8_t)font_size; + + for (i = 0; i < 8; i++) + { + d = fontdata[(chr << 3) + i]; + for (j = 0; j < 8; j++) + { + if (d & 0x80) + col = color; + else + col = (~color) & 1; + + oled_drawPixel(x + (j * font_scale), y + (i * font_scale), col); + + // Draw pixel at 2x larger size + if (font_scale > 1) { + oled_drawPixel(x + (j * font_scale) + 1, y + (i * font_scale), col); + oled_drawPixel(x + (j * font_scale), y + (i * font_scale) + 1, col); + oled_drawPixel(x + (j * font_scale) + 1, y + (i * font_scale) + 1, col); + } + + // Draw pixel at 4x larger size + if (font_scale > 2) { + oled_drawPixel(x + (j * font_scale) + 2, y + (i * font_scale), col); + oled_drawPixel(x + (j * font_scale), y + (i * font_scale) + 2, col); + oled_drawPixel(x + (j * font_scale) + 2, y + (i * font_scale) + 2, col); + + oled_drawPixel(x + (j * font_scale) + 1, y + (i * font_scale) + 2, col); + oled_drawPixel(x + (j * font_scale) + 2, y + (i * font_scale) + 1, col); + } + + // next bit + d <<= 1; + } + } +} +*/ + +void oled_drawchar_sz(uint8_t x, uint8_t y, uint8_t chr, uint8_t color, font_size_t font_size) +{ + uint16_t i, j, col; + uint8_t d; + + // Determine the font scale factor based on the font_size parameter + uint8_t font_scale = (uint8_t)font_size; + + // Loop through each row of the font data + for (i = 0; i < 8; i++) + { + // Retrieve the font data for the current row + d = fontdata[(chr << 3) + i]; + + // Loop through each column of the font data + for (j = 0; j < 8; j++) + { + // Determine the color to draw based on the current bit in the font data + if (d & 0x80) + col = color; + else + col = (~color) & 1; + + // Draw the pixel at the original size and scaled size using nested for-loops + for (uint8_t k = 0; k < font_scale; k++) { + for (uint8_t l = 0; l < font_scale; l++) { + oled_drawPixel(x + (j * font_scale) + k, y + (i * font_scale) + l, col); + } + } + + // Move to the next bit in the font data + d <<= 1; + } + } +} + +/* + * draw a string to the display buffer, scaled to size + */ +void oled_drawstr_sz(uint8_t x, uint8_t y, char *str, uint8_t color, font_size_t font_size) +{ + uint8_t c; + + while((c=*str++)) + { + oled_drawchar_sz(x, y, c, color, font_size); + x += 8 * font_size; + if(x>128 - 8 * font_size) + break; + } +} + /* * initialize I2C and OLED */ -- GitLab