Text and font support

This commit is contained in:
Clément Grennerat
2025-09-14 22:37:33 +02:00
parent 10b9a14a75
commit 5fd9a4a86f
10 changed files with 1271 additions and 193 deletions
+4 -68
View File
@@ -24,11 +24,9 @@
//
#include "LCD_driver.h"
#include "5x5_font.h"
#include "stm32f1xx.h"
void LCD_Write_Command(uint8_t Command) {
while ((SPI1->SR & SPI_SR_BSY) != 0); // Wait that everything is sent before changing the RS pin
while ((SPI1->SR & SPI_SR_BSY) != 0);// Wait that everything is sent before changing the RS pin
CMD
;
while ((SPI1->SR & SPI_SR_TXE) == 0);
@@ -36,14 +34,14 @@ void LCD_Write_Command(uint8_t Command) {
}
/* Send Data (char) to LCD via SPI bus */
void LCD_Write_Data(uint8_t Data) {
while ((SPI1->SR & SPI_SR_BSY) != 0); // Wait that everything is sent before changing the RS pin
while ((SPI1->SR & SPI_SR_BSY) != 0);// Wait that everything is sent before changing the RS pin
DATA
;
while ((SPI1->SR & SPI_SR_TXE) == 0);
SPI1->DR = Data;
}
void LCD_Write_Data16(uint16_t data) {
while ((SPI1->SR & SPI_SR_BSY) != 0); // Wait that everything is sent before changing the RS pin
while ((SPI1->SR & SPI_SR_BSY) != 0);// Wait that everything is sent before changing the RS pin
DATA
;
while ((SPI1->SR & SPI_SR_TXE) == 0);
@@ -126,15 +124,12 @@ void LCD_Init(void) {
BLACK);
LCD_Draw_Rectangle(1, LCD_HEIGHT - 21, 20, 20, BLUE);
// Test drawing text
char text[] = "test bro";
LCD_Draw_Text(&text, 30, 100, WHITE, 20, RED);
}
//INTERNAL FUNCTIONS OF THE LIBRARY
void LCD_Draw_Colour_Burst(uint16_t color, uint32_t size) {
while ((SPI1->SR & SPI_SR_BSY) != 0); // Wait that everything is sent before changing the RS pin
while ((SPI1->SR & SPI_SR_BSY) != 0);// Wait that everything is sent before changing the RS pin
DATA
;
for (uint32_t j = 0; j < size; j++) {
@@ -149,7 +144,6 @@ void LCD_Fill_Screen(uint16_t color) {
LCD_Draw_Rectangle(0, 0, LCD_WIDTH, LCD_HEIGHT, color);
}
void LCD_Draw_Pixel(uint16_t x, uint16_t y, uint16_t color) {
if ((x >= LCD_WIDTH) || (y >= LCD_HEIGHT)) return;
@@ -234,7 +228,6 @@ void LCD_Draw_Hollow_Circle(
}
}
void LCD_Draw_Filled_Circle(
uint16_t X,
uint16_t Y,
@@ -356,63 +349,6 @@ void LCD_Draw_Filled_Rectangle_Coord(
LCD_Draw_Rectangle(X0_true, Y0_true, X_length, Y_length, Colour);
}
/*Draws a character (fonts imported from fonts.h) at X,Y location with specified font colour, size and Background colour*/
/*See fonts.h implementation of font on what is required for changing to a different font when switching fonts libraries*/
void LCD_Draw_Char(
char Character,
uint16_t X,
uint16_t Y,
uint16_t Colour,
uint16_t Size,
uint16_t Background_Colour) {
uint8_t function_char;
uint8_t i, j;
function_char = Character;
if (function_char < ' ') {
Character = 0;
} else {
function_char -= 32;
}
char temp[CHAR_WIDTH];
for (uint8_t k = 0; k < CHAR_WIDTH; k++) {
temp[k] = font[function_char][k];
}
// Draw pixels
LCD_Draw_Rectangle(X, Y, CHAR_WIDTH * Size, CHAR_HEIGHT * Size,
Background_Colour);
for (j = 0; j < CHAR_WIDTH; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (temp[j] & (1 << i)) {
if (Size == 1) {
LCD_Draw_Pixel(X + j, Y + i, Colour);
} else {
LCD_Draw_Rectangle(X + (j * Size), Y + (i * Size), Size, Size,
Colour);
}
}
}
}
}
/*Draws an array of characters (fonts imported from fonts.h) at X,Y location with specified font colour, size and Background colour*/
/*See fonts.h implementation of font on what is required for changing to a different font when switching fonts libraries*/
void LCD_Draw_Text(
const char *Text,
uint16_t X,
uint16_t Y,
uint16_t Colour,
uint16_t Size,
uint16_t Background_Colour) {
while (*Text) {
LCD_Draw_Char(*Text++, X, Y, Colour, Size, Background_Colour);
X += CHAR_WIDTH * Size;
}
}
/*Dessine une image dans une zone de l'ecran, aux coordonnées X et Y*/
//CONVERTISSEUR: http://www.digole.com/tools/PicturetoC_Hex_converter.php
//65K colour (2Bytes / Pixel)
+19 -1
View File
@@ -1,5 +1,7 @@
#include "dashboard.h"
#include "LCD_driver.h"
#include "font.h"
#include "monomaniacone20pt.h"
#include "monomaniacone72pt.h"
void run_dashboard_loop() {
@@ -14,6 +16,22 @@ void init() {
LCD_Init();
// draw base
// Test drawing text
//char text[] = "test bro";
//LCD_Draw_Text(&text, 30, 100, WHITE, 20, RED);
GFX_DrawChar(40, 40, '!', &monomaniacone20pt, BLACK, WHITE);
GFX_DrawChar(60, 40, '2', &monomaniacone20pt, BLACK, WHITE);
GFX_DrawChar(80, 40, '3', &monomaniacone20pt, BLACK, WHITE);
GFX_DrawChar(100, 40, '4', &monomaniacone20pt, BLACK, WHITE);
GFX_DrawChar(120, 40, '5', &monomaniacone20pt, BLACK, WHITE);
GFX_DrawText(470/2, 150, "BONJOUR !", &monomaniacone20pt, RED, BLACK, 1);
GFX_DrawText(470/2, 250, "01234", &monomaniacone72pt, ORANGE, BLACK, 1);
}
void update_values() {
// UART send 0201 04 4084 03
+113
View File
@@ -0,0 +1,113 @@
#include "font.h"
#include "LCD_driver.h"
void GFX_DrawChar(
uint16_t x,
uint16_t y,
char c,
const GFXfont *font,
uint16_t fg_color,
uint16_t bg_color) {
if (c < font->firstChar || c > font->lastChar) {
return;
}
uint16_t glyphIndex = c - font->firstChar;
const GFXglyph *glyph = &font->glyphs[glyphIndex];
int16_t startX = x + glyph->xOffset;
int16_t startY = y + glyph->yOffset;
if (startX + glyph->width <= 0 || startX >= LCD_WIDTH) return;
if (startY + glyph->height <= 0 || startY >= LCD_HEIGHT) return;
// LCD_Set_Address(startX, startY, startX + glyph->width - 1, startY + glyph->height - 1);
uint16_t pendingPixelCount = 0;
uint8_t pendingPixelBit = 0;
uint16_t bitPos = 0;
for (uint8_t row = 0; row < glyph->height; row++) {
for (uint8_t col = 0; col < glyph->width; col++) {
uint16_t byteIndex = glyph->bitmapOffset + (bitPos / 8);
uint8_t bitIndex = bitPos % 8;
uint8_t pixelBit = font->bitmaps[byteIndex] & (0x80 >> bitIndex);
// if(pixelBit == pendingPixelBit) {
// pendingPixelCount++;
// }else {
// if(pendingPixelCount != 0) {
// LCD_Draw_Colour_Burst(pendingPixelBit ? fg_color : bg_color, pendingPixelCount);
// pendingPixelCount = 0;
// }
// pendingPixelBit = pixelBit;
// }
uint16_t pixelColor = pixelBit ? fg_color : bg_color;
int16_t absX = startX + col;
int16_t absY = startY + row;
if (absX >= 0 && absX < LCD_WIDTH && absY >= 0 && absY < LCD_HEIGHT) {
LCD_Draw_Pixel(absX, absY, pixelColor);
}
bitPos++;
}
}
if(pendingPixelCount != 0) {
LCD_Draw_Colour_Burst(pendingPixelBit ? fg_color : bg_color, pendingPixelCount);
}
}
uint16_t GFX_GetTextWidth(const char *text, const GFXfont *font) {
uint16_t width = 0;
while (*text != '\0') {
if (*text >= font->firstChar && *text <= font->lastChar) {
uint16_t glyphIndex = *text - font->firstChar;
width += font->glyphs[glyphIndex].advance;
} else {
// Default advance for unsupported characters
width += 10;
}
text++;
}
return width;
}
void GFX_DrawText(uint16_t x, uint16_t y, const char *text, const GFXfont *font,
uint16_t fg_color, uint16_t bg_color, uint8_t alignment) {
// Adjust x position based on alignment
switch (alignment) {
case 1: // Center
x -= GFX_GetTextWidth(text, font) / 2;
break;
case 2: // Right
x -= GFX_GetTextWidth(text, font);
break;
// Default: Left alignment (no adjustment)
}
// Draw each character
uint16_t cursorX = x;
uint16_t cursorY = y;
while (*text != '\0') {
GFX_DrawChar(cursorX, cursorY, *text, font, fg_color, bg_color);
// Move cursor by the glyph's advance width
if (*text >= font->firstChar && *text <= font->lastChar) {
uint16_t glyphIndex = *text - font->firstChar;
cursorX += font->glyphs[glyphIndex].advance;
} else {
// Default advance for unsupported characters
cursorX += 10;
}
text++;
}
}