feature: font_gfx
This commit is contained in:
92
firmware/shared_libs/display/font_gfx.c
Normal file
92
firmware/shared_libs/display/font_gfx.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* fonts.c
|
||||
*
|
||||
* Created on: 23 maj 2021
|
||||
* Author: bartool
|
||||
*/
|
||||
|
||||
// #include "main.h"
|
||||
#include "fonts.h"
|
||||
|
||||
static uint8_t font_string_lenght_px(const FONT_INFO *font, uint8_t *text);
|
||||
|
||||
/**
|
||||
* @brief Write string at at the specified (x,y) position.
|
||||
* @param disp A pointer to display struct
|
||||
* @param font A pointer to font struct.
|
||||
* @param text A pointer to string data.
|
||||
* @param pos_x Top left corner x coordinate
|
||||
* @param pos_yTop left corner y coordinate
|
||||
* @param color Color of pixel BM_NORMAL or BM_INVERSE for bitmap.
|
||||
* @return uint8_t
|
||||
*/
|
||||
uint8_t DISP_writeString(GFX_display_t *disp, const FONT_INFO *font, uint8_t *text, uint8_t pos_x, uint8_t pos_y, GFX_BitmapColor_t color)
|
||||
{
|
||||
uint8_t actual_char, char_nr;
|
||||
GFX_bitmap_t char_bitmap;
|
||||
const FONT_CHAR_INFO *charinfo;
|
||||
|
||||
uint8_t height = font->heightPixels;
|
||||
uint8_t x = pos_x;
|
||||
uint8_t width = font_string_lenght_px(font, text);
|
||||
|
||||
switch (color)
|
||||
{
|
||||
case BM_INVERSE:
|
||||
// SSD1306_clear_buffer(width + 2, height + 2, pos_x - 1, pos_y - 1, WHITE);
|
||||
// ST7565_writeBitmap(st7565_buffor, NULL, width + 2, height + 2, pos_x - 1, pos_y - 1, WHITE);
|
||||
DISP_drawFillRect(disp, pos_x - 1, pos_y - 1, width + 2, height + 2);
|
||||
break;
|
||||
default:
|
||||
// SSD1306_clear_buffer(width + 2, height + 2, pos_x - 1, pos_y - 1, BLACK);
|
||||
// ST7565_writeBitmap(st7565_buffor, NULL, width + 2, height + 2, pos_x - 1, pos_y - 1, BLACK);
|
||||
DISP_clearRegion(disp, pos_x - 1, pos_y - 1, width + 2, height + 2);
|
||||
break;
|
||||
}
|
||||
|
||||
while (*text)
|
||||
{
|
||||
actual_char = *text++;
|
||||
if (actual_char < font->startChar || actual_char > font->endChar)
|
||||
{
|
||||
if (actual_char == ' ')
|
||||
{
|
||||
x += font->spacePixels;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
char_nr = actual_char - font->startChar;
|
||||
charinfo = &font->charInfo[char_nr];
|
||||
|
||||
char_bitmap.height = height,
|
||||
char_bitmap.width = charinfo->widthBits,
|
||||
char_bitmap.bitmap = font->data + charinfo->offset,
|
||||
|
||||
DISP_drawBitmap(disp, &char_bitmap, x, pos_y, color);
|
||||
|
||||
x += charinfo->widthBits + font->interspacePixels;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
static uint8_t font_string_lenght_px(const FONT_INFO *font, uint8_t *text)
|
||||
{
|
||||
uint8_t width = 0;
|
||||
while (*text)
|
||||
{
|
||||
uint8_t actual_char = *text++;
|
||||
uint8_t char_nr = actual_char - font->startChar;
|
||||
if (actual_char < font->startChar || actual_char > font->endChar)
|
||||
{
|
||||
if (actual_char == ' ')
|
||||
{
|
||||
width += font->spacePixels;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
width += font->charInfo[char_nr].widthBits + font->interspacePixels;
|
||||
}
|
||||
return width;
|
||||
}
|
||||
28
firmware/shared_libs/display/font_gfx.h
Normal file
28
firmware/shared_libs/display/font_gfx.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "main.h"
|
||||
#include "display_gfx.h"
|
||||
// This structure describes a single character's display information
|
||||
typedef struct
|
||||
{
|
||||
const uint8_t widthBits; // width, in bits (or pixels), of the character
|
||||
const uint16_t offset; // offset of the character's bitmap, in bytes, into the the GFX_font_t's data array
|
||||
|
||||
} GFX_fontChar_t;
|
||||
|
||||
// Describes a single font
|
||||
typedef struct
|
||||
{
|
||||
// uint8_t DownSpace; // Downs Space in pixels
|
||||
uint8_t heightPixels; // height, in pages (8 pixels), of the font's characters
|
||||
uint8_t startChar; // the first character in the font (e.g. in charInfo and data)
|
||||
uint8_t endChar;
|
||||
uint8_t interspacePixels; // number of pixels of interspace between characters
|
||||
uint8_t spacePixels; // number of pixels of space character
|
||||
const GFX_fontChar_t *charInfo; // pointer to array of char information
|
||||
const uint8_t *data; // pointer to generated array of character visual representation
|
||||
// char * FontFileName; // (Pointer) Font filename saved on SD card or 0 (null) otherwise
|
||||
// uint8_t bitOrientation; // bits and byte orientation 0-T2B, 1-L2R
|
||||
} GFX_font_t;
|
||||
|
||||
uint8_t DISP_writeString(GFX_display_t *disp, const GFX_font_t *font, uint8_t *text, uint8_t pos_x, uint8_t pos_y, GFX_BitmapColor_t color);
|
||||
Reference in New Issue
Block a user