Skip to content

Commit fe92cf6

Browse files
committed
Add a function to draw a single character
1 parent 6ab6689 commit fe92cf6

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

lib/oled/frame/frame.c

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -198,49 +198,63 @@ void frame_init(void)
198198
/**
199199
* @brief Renders a text string on the OLED display at a specified position.
200200
*
201-
* This function draws the given null-terminated string starting at the coordinates specified by `position`. It calculates affected display pages based on character height and handles multi-page rendering for characters that span page boundaries. For each character in the string, the corresponding font bitmap is fetched and individual bytes are combined with the optional background pattern (if enabled) to preserve display background beneath the text. Bit-shifting is applied to properly align the character pixels vertically on partial pages. The function outputs the composed columns of pixel data to the OLED display using the lower-level `oled_column` function.
201+
* This function draws the given null-terminated string starting at the coordinates specified by `position`. It calculates affected display pages based on the character height and handles multi-page rendering for characters that span page boundaries. For each character in the string, the corresponding font bitmap is fetched and individual bytes are combined with the optional background pattern (if enabled) to preserve display background beneath the text. Bit-shifting is applied to properly align the character pixels vertically on partial pages. The function outputs the composed columns of pixel data to the OLED display using the lower-level `oled_column` function.
202202
*
203203
* @param text Pointer to the null-terminated string to be drawn.
204204
* @param position The starting position (x, y) on the display for the text.
205205
*/
206206
void frame_draw_text(const char *text, DRAWING_Position position)
207207
{
208-
unsigned char start_page = (position.y/OLED_PAGE_SIZE);
209-
unsigned char end_page = ((position.y + FONT_HEIGHT)/OLED_PAGE_SIZE);
208+
unsigned char current_character = 0;
209+
210+
while (*(text + current_character) != '\0')
211+
{
212+
frame_draw_char(*(text + current_character), position);
213+
214+
position.x += FONT_WIDTH;
215+
current_character++;
216+
}
217+
}
218+
219+
/**
220+
* @brief Renders a single character on the OLED display at a specified position.
221+
*
222+
* This function draws a character bitmap starting at the coordinates specified by `position`. It determines which display pages are affected based on the font height and divides the character rendering across multiple OLED memory pages if necessary. For each column of the character, the corresponding font data is fetched from the font table and optionally combined with a predefined background buffer (if enabled). Bit shifting is applied to align the character correctly on display pages that do not align with the character boundaries. The final pixel data for each column is sent to the OLED driver via the `oled_column` function.
223+
*
224+
* @param character The ASCII character to be drawn.
225+
* @param position The starting position (x, y) on the display for the character.
226+
*/
227+
void frame_draw_char(const char character, DRAWING_Position position)
228+
{
229+
unsigned char start_page = (position.y/OLED_PAGE_SIZE);
230+
unsigned char end_page = ((position.y + FONT_HEIGHT)/OLED_PAGE_SIZE);
210231

211232
for (unsigned char page=start_page; page <= end_page; page++)
212233
{
213-
unsigned char current_character = 0;
214-
215-
while (*(text + current_character) != '\0')
234+
unsigned char *font = font_getchararray(character);
235+
unsigned char y_position = (position.y%OLED_PAGE_SIZE);
236+
237+
for (unsigned char x=0; x < FONT_WIDTH; x++)
216238
{
217-
unsigned char *font = font_getchararray(*(text + current_character));
218-
unsigned char y_position = (position.y%OLED_PAGE_SIZE);
219-
220-
for (unsigned char x=0; x < FONT_WIDTH; x++)
239+
unsigned char font_row = *(font + x);
240+
unsigned char frame_row = (position.x + x);
241+
242+
unsigned char temp =
243+
#ifdef FRAME_SPECIFIC_BACKGROUND
244+
pgm_read_byte(&frame_background[page][frame_row]);
245+
#else
246+
0x00;
247+
#endif
248+
249+
if(page == start_page)
250+
{
251+
temp |= (font_row<<(y_position - OLED_PAGE_SIZE + FONT_HEIGHT));
252+
}
253+
else
221254
{
222-
unsigned char font_row = *(font + x);
223-
unsigned char frame_row = (position.x + current_character * FONT_WIDTH + x);
224-
225-
unsigned char temp =
226-
#ifdef FRAME_SPECIFIC_BACKGROUND
227-
pgm_read_byte(&frame_background[page][frame_row]);
228-
#else
229-
0x00;
230-
#endif
231-
232-
if(page == start_page)
233-
{
234-
temp |= (font_row<<(y_position - OLED_PAGE_SIZE + FONT_HEIGHT));
235-
}
236-
else
237-
{
238-
temp |= (font_row>>(OLED_PAGE_SIZE + (OLED_PAGE_SIZE - FONT_HEIGHT) - y_position));
239-
}
240-
241-
oled_column(temp, frame_row, page);
255+
temp |= (font_row>>(OLED_PAGE_SIZE + (OLED_PAGE_SIZE - FONT_HEIGHT) - y_position));
242256
}
243-
current_character++;
257+
oled_column(temp, frame_row, page);
244258
}
245259
}
246260
}

lib/oled/frame/frame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
typedef enum FRAME_Number_Operation_t FRAME_Number_Operation;
135135

136136
void frame_init(void);
137+
void frame_draw_char(const char character, DRAWING_Position position);
137138
void frame_draw_text(const char *text, DRAWING_Position position);
138139
void frame_draw_number(const void* number, unsigned char length, NUMBER_Type type, NUMBER_Radix radix, DRAWING_Position position);
139140
void frame_draw_number_uint(const unsigned int number, unsigned char length, NUMBER_Radix radix, DRAWING_Position position);

0 commit comments

Comments
 (0)