STM32 HAL library example
This commit is contained in:
@@ -29,6 +29,8 @@ Some basic hints on the interface:
|
||||
|
||||
Download in the [Releases](https://gitea.mcflyer.ru/McFLY/McBitFont/releases) section!
|
||||
|
||||
Library example instructions to implement to STM32 code: [mctext](STM32_HAL_Lib/README.md)
|
||||
|
||||
#### Important:
|
||||
**Since v2.0 new save file format is implemented. You can use [McBitFont v1.7](https://gitea.mcflyer.ru/McFLY/McBitFont/releases/tag/v1.7) to convert old saved files to the new format.**
|
||||
|
||||
|
13
STM32_HAL_Lib/README.md
Normal file
13
STM32_HAL_Lib/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# McText Library
|
||||
|
||||
- The library uses "Left to Right, Top to bottom" scan and "LSB Top" for pixels alignment
|
||||
|
||||
#### Instructions
|
||||
|
||||
To use the library you have to have a display driver with a function that paints a single pixel with X and Y coordinates and 0/1 color.<br>
|
||||
Find "SET A FUNCTION NAME HERE!" text in *mctext.c* file and change the function name that suits your driver.<br>
|
||||
**Note:** it is possible that you will have to change parameters in the function to match your driver.
|
||||
|
||||
**Note:** check the "include" in *mctext.h* file to match your HAL. (stm32f1xx_hal.h is fo STM32F1 MCU series)
|
||||
|
||||
Now use **mct_String** function to draw a string of text.
|
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;
|
||||
}
|
||||
}
|
21
STM32_HAL_Lib/mctext.h
Normal file
21
STM32_HAL_Lib/mctext.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* mctext.h
|
||||
*
|
||||
* Created on: May 16, 2025
|
||||
* Author: User
|
||||
*/
|
||||
|
||||
#ifndef INC_MCTEXT_H_
|
||||
#define INC_MCTEXT_H_
|
||||
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
// Draw a string of characters
|
||||
void mct_String(uint8_t x, uint8_t y, const char *c, uint8_t color, const uint8_t *font);
|
||||
|
||||
#endif /* INC_MCTEXT_H_ */
|
Reference in New Issue
Block a user