STM32 HAL library example
This commit is contained in:
107
STM32_HAL_Lib/mctext.c
Normal file
107
STM32_HAL_Lib/mctext.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* mctext.c
|
||||
*
|
||||
* Created on: May 16, 2025
|
||||
* Author: User
|
||||
*/
|
||||
|
||||
|
||||
#include "mctext.h"
|
||||
#include "ST7565.h"
|
||||
|
||||
//=========================== SET A FUNCTION NAME HERE! ===========================//
|
||||
// A function from display driver to set a pixel (x, y, color)
|
||||
void (*mct_SetPixel)(uint8_t, uint8_t, uint8_t) = ST7565_SetPixel;
|
||||
//=================================================================================//
|
||||
|
||||
|
||||
// Draw a single character. Returns width of drawn character
|
||||
uint8_t mct_Char(uint8_t x, uint8_t y, unsigned char c, uint8_t color, const uint8_t *font) {
|
||||
uint8_t pk = font[0]; // Is it a packed font?
|
||||
uint8_t w = font[1]; // Font char width
|
||||
uint8_t h = font[2]; // Font char height
|
||||
uint8_t fc = font[4]; // First char code in the font
|
||||
uint8_t lc = font[5]; // Last char code in the font
|
||||
uint8_t i, j, p, s, b, seg; // i-cur.column, j-cur.row of 8, p-rows of 8, s-height in cur.row of 8, b-cur.bit in cur.row, seg-byte.segment
|
||||
uint8_t bps; // Bytes per symbol for packed fonts
|
||||
uint16_t o; // Current offset
|
||||
|
||||
if (c < fc || c > lc) return 0;
|
||||
if (x > LCDWIDTH) return 0;
|
||||
if (y+h > LCDHEIGHT) return 0;
|
||||
|
||||
// Calc the offset for desired symbol
|
||||
if (pk) { // The font is packed
|
||||
if (w) { // The font is monospaced
|
||||
bps = w*h/8 + 1; // Bytes per symbol + width byte
|
||||
if ((w*h)%8 > 0) bps++; // Correction for the last byte
|
||||
o = FONT_HEADER+(c-fc)*bps; // Offset for desired symbol
|
||||
} else { // The font is not monospaced
|
||||
o = FONT_HEADER; // Starting offset
|
||||
for (i=0; i<c-fc; i++) { // Going through every symbol
|
||||
bps = font[o]*h/8; // Bytes per current symbol
|
||||
if ((font[o]*h)%8 > 0) bps++; // Correction for the last byte
|
||||
o += bps + 1; // Adding symbol's width to the offset (+ width byte)
|
||||
}
|
||||
w = font[o]; // Desired symbol's width
|
||||
o++; // Offset for desired symbol's data
|
||||
}
|
||||
|
||||
// Draw the packed symbol!
|
||||
bps = w*h/8; // Bytes per current symbol
|
||||
if ((w*h)%8 > 0) bps++; // Correction for the last byte
|
||||
b = 0; // bit indexer in "current" byte
|
||||
for (i=0; i<w; i++) { // Going through columns
|
||||
if (x+i > LCDWIDTH) return i-1; // Check if we're out of display size
|
||||
for (j=0; j<h; j++) { // Going through rows in column [i]
|
||||
if (b == 0) seg = font[o];
|
||||
if ((seg>>b) & 1) mct_SetPixel(x+i, y+j, color); // Paint the pixel
|
||||
if (b < 7) b++; else {b = 0; o++;} // Track bits and bytes
|
||||
}
|
||||
}
|
||||
|
||||
} else { // The font is not packed
|
||||
p = (h%8 > 0) ? h/8 + 1 : h/8; // Bytes in one column
|
||||
if (w) { // The font is monospaced
|
||||
o = FONT_HEADER+(c-fc)*w*p; // Offset for desired symbol
|
||||
} else { // The font is not monospaced
|
||||
o = FONT_HEADER; // Starting offset
|
||||
for (i=0; i<c-fc; i++) { // Going through every symbol
|
||||
o += font[o]*p + 1; // Adding symbol's width to the offset
|
||||
}
|
||||
w = font[o]; // Desired symbol's width
|
||||
o++; // Offset for desired symbol's data
|
||||
}
|
||||
|
||||
// Draw the symbol
|
||||
for (i=0; i<w; i++) { // Going through columns
|
||||
if (x+i > LCDWIDTH) return i-1; // Check if we're out of display size
|
||||
for (j=0; j<p; j++) { // Going through bytes in single column
|
||||
s = (h - j*8 >= 8) ? 8 : (h - j*8) % 8; // Clac the amount of pixels in current byte
|
||||
|
||||
seg = font[o];
|
||||
for (b=0; b<s; b++) { // Going through the byte and paint the pixel if the bit is 1
|
||||
if ((seg>>b) & 1) mct_SetPixel(x+i, y+j*8+b, color);
|
||||
}
|
||||
o++;
|
||||
}
|
||||
//mct_SetPixel(x+i, y, color); // For testing purposes
|
||||
}
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
// Draw a string of characters
|
||||
void mct_String(uint8_t x, uint8_t y, const char *c, uint8_t color, const uint8_t *font) {
|
||||
uint8_t w = font[0]; // Font char width
|
||||
uint8_t h = font[1]; // Font char height
|
||||
uint8_t s = font[2]; // Font space between characters
|
||||
|
||||
if (y+h > LCDHEIGHT) return;
|
||||
while (c[0] != 0) {
|
||||
if (x+w > LCDWIDTH) return;
|
||||
w = mct_Char(x, y, (unsigned char)*c, color, font);
|
||||
c++;
|
||||
x += w + s;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user