Skip to content

Commit

Permalink
Add constants to replace magic numbers. Cleanup code.
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz committed Mar 29, 2024
1 parent 711a759 commit 9f4e709
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 143 deletions.
111 changes: 52 additions & 59 deletions radio/src/gui/128x64/lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,70 +162,72 @@ uint8_t getPatternWidth(const PatternData * pattern)
LcdFlags getCharPattern(PatternData * pattern, unsigned char c, LcdFlags flags)
{
#if !defined(BOOT)
static const uint8_t fontWidth[] = { 5, 3, 5, 8, 10, 22, 5 };
static const uint8_t fontHeight[] = { 7, 5, 6, 12, 16, 38, 7 };

uint32_t fontsize = FONTSIZE(flags);
unsigned char c_remapped = 0;

if (fontsize == DBLSIZE || (flags & BOLD)) {
// To save space only some DBLSIZE and BOLD chars are available
// c has to be remapped. All non existing chars mapped to 0 (space)
if (c>=',' && c<=':')
if (c >= ',' && c <= ':')
c_remapped = c - ',' + 1;
else if (c>='A' && c<='Z')
else if (c >= 'A' && c <= 'Z')
c_remapped = c - 'A' + 16;
else if (c>='a' && c<='z')
else if (c >= 'a' && c <= 'z')
c_remapped = c - 'a' + 42;
else if (c=='_')
else if (c == '_')
c_remapped = 4;
else if (c!=' ')
flags &= ~BOLD;
}

if (fontsize == DBLSIZE) {
pattern->width = 10;
pattern->height = 16;
if (c >= 0x95)
c_remapped = c - 73; // Adjust for language special characters
else if (c >= 0x80)
c_remapped = c - 60; // Adjust for 'extra' characters
pattern->data = &font_10x14[((uint16_t)c_remapped) * 20];
}
else if (fontsize == XXLSIZE) {
pattern->width = 22;
pattern->height = 38;
pattern->data = &font_22x38_num[((uint16_t)c-'0'+5)*110];
}
else if (fontsize == MIDSIZE) {
pattern->width = 8;
pattern->height = 12;
if (c >= 0x95) c -=21; // Adjust for language special characters
pattern->data = &font_8x10[((uint16_t)c-0x20)*16];
}
else if (fontsize == SMLSIZE) {
pattern->width = 5;
pattern->height = 6;
if (c >= 0x95)
c = c - 13; // Adjust for language special characters
pattern->data = &font_4x6[((uint16_t)c - 0x20) * 5];
}
else if (fontsize == TINSIZE) {
pattern->width = 3;
pattern->height = 5;
pattern->data = &font_3x5[((uint16_t)c-0x20)*3];
}
else if (flags & BOLD) {
pattern->width = 5;
pattern->height = 7;
pattern->data = &font_5x7_B[c_remapped*5];
}
else {
pattern->width = 5;
pattern->height = 7;
pattern->data = &font_5x7[(c - 0x20) * 5];
else if (c != ' ')
flags &= ~BOLD; // For BOLD use Standard font if character is not in BOLD
}

uint8_t fontIdx = fontsize >> 8;
if (flags & BOLD) fontIdx = 6;

pattern->width = fontWidth[fontIdx];
pattern->height = fontHeight[fontIdx];
int charSize = (pattern->height + 7) / 8 * pattern->width;

switch (fontIdx) {
case 0: // Standard
pattern->data = &font_5x7[(c - FONT_BASE_START) * charSize];
break;
case 1: // TINSIZE
pattern->data = &font_3x5[((uint16_t)c - FONT_BASE_START) * charSize];
break;
case 2: // SMLSIZE
// Adjust language special characters offset
if (c >= FONT_LANG_START)
c = c - (FONT_SYMS_CNT - FONT_SYMS_CNT_4x6);
pattern->data = &font_4x6[((uint16_t)c - FONT_BASE_START) * charSize];
break;
case 3: // MDLSIZE
// Adjust language special characters offset
if (c >= FONT_LANG_START)
c = c - FONT_SYMS_CNT;
pattern->data = &font_8x10[((uint16_t)c - FONT_BASE_START) * charSize];
break;
case 4: // DBLSIZE
// Adjust language special characters offset and symbols offset
if (c >= FONT_LANG_START)
c_remapped = c - (FONT_BASE_CNT - FONT_BASE_CNT_10x14) - (FONT_SYMS_CNT - FONT_SYMS_CNT_10x14) - FONT_BASE_START;
else if (c >= FONT_SYMS_START)
c_remapped = c - (FONT_BASE_CNT - FONT_BASE_CNT_10x14) - FONT_BASE_START;
pattern->data = &font_10x14[((uint16_t)c_remapped) * charSize];
break;
case 5: // XXLSIZE
pattern->data = &font_22x38_num[((uint16_t)c - '0' + 5) * charSize];
break;
case 6: // BOLD
pattern->data = &font_5x7_B[c_remapped * charSize];
break;
};
#else
pattern->width = 5;
pattern->height = 7;
pattern->data = &font_5x7[(c-0x20)*5];
pattern->data = &font_5x7[(c - FONT_BASE_START) * 5];
#endif
return flags;
}
Expand All @@ -240,14 +242,9 @@ uint8_t getCharWidth(char c, LcdFlags flags)
void lcdDrawChar(coord_t x, coord_t y, uint8_t c, LcdFlags flags)
{
lcdNextPos = x - 1;
#if defined(BOOT)
const uint8_t * data = &font_5x7[(c-0x20)*5];
lcdPutPattern(x, y, data, 5, 7, flags);
#else
PatternData pattern;
flags = getCharPattern(&pattern, c, flags);
lcdPutPattern(x, y, pattern.data, pattern.width, pattern.height, flags);
#endif
}

void lcdDrawChar(coord_t x, coord_t y, uint8_t c)
Expand All @@ -260,11 +257,7 @@ uint8_t getTextWidth(const char * s, uint8_t len, LcdFlags flags)
uint8_t width = 0;
if (len == 0) len = strlen(s);
while (len--) {
#if !defined(BOOT)
unsigned char c = map_utf8_char(s, len);
#else
unsigned char c = *s;
#endif
if (!c) {
break;
}
Expand Down
6 changes: 1 addition & 5 deletions radio/src/gui/128x64/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
* GNU General Public License for more details.
*/

#ifndef _LCD_H_
#define _LCD_H_
#pragma once

#include <inttypes.h>

Expand Down Expand Up @@ -94,7 +93,6 @@ extern coord_t lcdNextPos;
#define IS_IN_DISPLAY(p) ((p) >= displayBuf && (p) < DISPLAY_END)
#define ASSERT_IN_DISPLAY(p) assert((p) >= displayBuf && (p) < DISPLAY_END)


void lcdDrawChar(coord_t x, coord_t y, uint8_t c);
void lcdDrawChar(coord_t x, coord_t y, uint8_t c, LcdFlags flags);
void lcdDrawCenteredText(coord_t y, const char * s, LcdFlags flags = 0);
Expand Down Expand Up @@ -190,5 +188,3 @@ inline pixel_t getPixel(uint8_t x, uint8_t y)
}

uint8_t getTextWidth(const char * s, uint8_t len=0, LcdFlags flags=0);

#endif // _LCD_H_
109 changes: 51 additions & 58 deletions radio/src/gui/212x64/lcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ void lcdPutPattern(coord_t x, coord_t y, const uint8_t * pattern, uint8_t width,
LcdFlags getCharPattern(PatternData * pattern, unsigned char c, LcdFlags flags)
{
#if !defined(BOOT)
static const uint8_t fontWidth[] = { 5, 3, 5, 8, 10, 22, 5 };
static const uint8_t fontHeight[] = { 7, 5, 6, 12, 16, 38, 7 };

uint32_t fontsize = FONTSIZE(flags);
unsigned char c_remapped = 0;

Expand All @@ -175,62 +178,61 @@ LcdFlags getCharPattern(PatternData * pattern, unsigned char c, LcdFlags flags)
// c has to be remapped. All non existing chars mapped to 0 (space)
if (c >= ',' && c <= ':')
c_remapped = c - ',' + 1;
else if (c>='A' && c<='Z')
else if (c >= 'A' && c <= 'Z')
c_remapped = c - 'A' + 16;
else if (c>='a' && c<='z')
else if (c >= 'a' && c <= 'z')
c_remapped = c - 'a' + 42;
else if (c=='_')
else if (c == '_')
c_remapped = 4;
else if (c!=' ')
flags &= ~BOLD;
else if (c != ' ')
flags &= ~BOLD; // For BOLD use Standard font if character is not in BOLD
}

if (fontsize == DBLSIZE) {
pattern->width = 10;
pattern->height = 16;
if (c >= 0x95)
c_remapped = c - 73; // Adjust for language special characters
else if (c >= 0x80)
c_remapped = c - 60; // Adjust for 'extra' characters
pattern->data = &font_10x14[((uint16_t)c_remapped) * 20];
}
else if (fontsize == XXLSIZE) {
pattern->width = 22;
pattern->height = 38;
pattern->data = &font_22x38_num[((uint16_t) c - '0' + 5) * 110];
}
else if (fontsize == MIDSIZE) {
pattern->width = 8;
pattern->height = 12;
if (c >= 0x95) c -=21; // Adjust for language special characters
pattern->data = &font_8x10[((uint16_t)c-0x20)*16];
}
else if (fontsize == SMLSIZE) {
pattern->width = 5;
pattern->height = 6;
if (c >= 0x95)
c = c - 13; // Adjust for language special characters
pattern->data = &font_4x6[((uint16_t)c - 0x20) * 5];
}
else if (fontsize == TINSIZE) {
pattern->width = 3;
pattern->height = 5;
pattern->data = &font_3x5[((uint16_t) c - 0x20) * 3];
}
else if (flags & BOLD) {
pattern->width = 5;
pattern->height = 7;
pattern->data = &font_5x7_B[c_remapped*5];
}
else {
pattern->width = 5;
pattern->height = 7;
pattern->data = &font_5x7[(c - 0x20) * 5];
}
uint8_t fontIdx = fontsize >> 8;
if (flags & BOLD) fontIdx = 6;

pattern->width = fontWidth[fontIdx];
pattern->height = fontHeight[fontIdx];
int charSize = (pattern->height + 7) / 8 * pattern->width;

switch (fontIdx) {
case 0: // Standard
pattern->data = &font_5x7[(c - FONT_BASE_START) * charSize];
break;
case 1: // TINSIZE
pattern->data = &font_3x5[((uint16_t)c - FONT_BASE_START) * charSize];
break;
case 2: // SMLSIZE
// Adjust language special characters offset
if (c >= FONT_LANG_START)
c = c - (FONT_SYMS_CNT - FONT_SYMS_CNT_4x6);
pattern->data = &font_4x6[((uint16_t)c - FONT_BASE_START) * charSize];
break;
case 3: // MDLSIZE
// Adjust language special characters offset
if (c >= FONT_LANG_START)
c = c - FONT_SYMS_CNT;
pattern->data = &font_8x10[((uint16_t)c - FONT_BASE_START) * charSize];
break;
case 4: // DBLSIZE
// Adjust language special characters offset and symbols offset
if (c >= FONT_LANG_START)
c_remapped = c - (FONT_BASE_CNT - FONT_BASE_CNT_10x14) - (FONT_SYMS_CNT - FONT_SYMS_CNT_10x14) - FONT_BASE_START;
else if (c >= FONT_SYMS_START)
c_remapped = c - (FONT_BASE_CNT - FONT_BASE_CNT_10x14) - FONT_BASE_START;
pattern->data = &font_10x14[((uint16_t)c_remapped) * charSize];
break;
case 5: // XXLSIZE
pattern->data = &font_22x38_num[((uint16_t)c - '0' + 5) * charSize];
break;
case 6: // BOLD
pattern->data = &font_5x7_B[c_remapped * charSize];
break;
};
#else
pattern->width = 5;
pattern->height = 7;
pattern->data = &font_5x7[(c - 0x20) * 5];
pattern->data = &font_5x7[(c - FONT_BASE_START) * 5];
#endif
return flags;
}
Expand All @@ -245,14 +247,9 @@ uint8_t getCharWidth(char c, LcdFlags flags)
void lcdDrawChar(coord_t x, coord_t y, uint8_t c, LcdFlags flags)
{
lcdNextPos = x - 1;
#if defined(BOOT)
const uint8_t * data = &font_5x7[(c-0x20)*5];
lcdPutPattern(x, y, data, 5, 7, flags);
#else
PatternData pattern;
flags = getCharPattern(&pattern, c, flags);
lcdPutPattern(x, y, pattern.data, pattern.width, pattern.height, flags);
#endif
}

void lcdDrawChar(coord_t x, coord_t y, uint8_t c)
Expand All @@ -264,11 +261,7 @@ uint8_t getTextWidth(const char * s, uint8_t len, LcdFlags flags)
{
uint8_t width = 0;
for (int i = 0; len == 0 || i < len; ++i) {
#if !defined(BOOT)
unsigned char c = *s;
#else
unsigned char c = *s;
#endif
unsigned char c = map_utf8_char(s, len);
if (!c) {
break;
}
Expand Down
6 changes: 1 addition & 5 deletions radio/src/gui/212x64/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
* GNU General Public License for more details.
*/

#ifndef _LCD_H_
#define _LCD_H_
#pragma once

#include <inttypes.h>

Expand Down Expand Up @@ -255,6 +254,3 @@ class RleBitmap

coord_t pos;
};


#endif // _LCD_H_
1 change: 1 addition & 0 deletions radio/src/gui/common/stdlcd/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
(1) - reduced character set, contains only space, upper case letters, numbers, '-' and '.'.
(2) - reduced character set, contains only space, alphanumeric, ',', '.', '-', ':' and '_'.
(3) - reduced character set, contains only space, numbers, ',', '.', '-', ':' and '_'.
Note: to display ' ' use '+', to display '_' use '/'.
(4) - arrows
(5) - arrows and symbols (e.g. delta, stick, switch, etc)
(6) - uses the 5x7 arrow and symbol font characters (not bold)
Expand Down
29 changes: 17 additions & 12 deletions radio/src/gui/common/stdlcd/fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
* GNU General Public License for more details.
*/

#ifndef _FONTS_H_
#define _FONTS_H_
#pragma once

extern const unsigned char font_5x7[];
extern const unsigned char font_10x14[];
constexpr int FONT_BASE_START = 0x20;
constexpr int FONT_SYMS_START = 0x80;
constexpr int FONT_LANG_START = 0x95;

constexpr int FONT_BASE_CNT = 96; // Number of characters in standard 5x7 font
constexpr int FONT_BASE_CNT_10x14 = 68; // Number of characters in compressed 10x14 font

#if !defined(BOOT)
extern const unsigned char font_5x7_B[];
extern const unsigned char font_3x5[];
extern const unsigned char font_4x6[];
extern const unsigned char font_8x10[];
extern const unsigned char font_22x38_num[];
#endif
constexpr int FONT_SYMS_CNT = 21; // Max # of extra symbols (based on 5x7 standard font)
constexpr int FONT_SYMS_CNT_4x6 = 8; // # of extra symbols in 4x6 font (adjust if font is changed)
constexpr int FONT_SYMS_CNT_10x14 = 8; // # of extra symbols in 10x14 font (adjust if font is changed)

#endif // _FONTS_H_
extern const unsigned char font_5x7[];
extern const unsigned char font_5x7_B[];
extern const unsigned char font_3x5[];
extern const unsigned char font_4x6[];
extern const unsigned char font_8x10[];
extern const unsigned char font_10x14[];
extern const unsigned char font_22x38_num[];
Loading

0 comments on commit 9f4e709

Please sign in to comment.