1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/**
* \file
*
* \brief Font and text drawing routines
*
* Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include "stddef.h"
#include "assert.h"
#include "gfx_mono.h"
#include "gfx_mono_text.h"
#include <asf.h>
#ifndef CONFIG_FONT_PIXELS_PER_BYTE
# define CONFIG_FONT_PIXELS_PER_BYTE 8
#endif
#define EXTMEM_BUF_SIZE 20
#if defined(CONFIG_HUGEMEM) || defined(__DOXYGEN__)
/**
* \internal
* \brief Helper function that draws a character from a font in hugemem
* to the display
*
* This function will first calculate the start offset in the font character
* data before iterating over the specific character data.
*
* Only pixels in the character that should be enabled are done so, the caller
* is required to prepare the drawing area before printing a character to it.
* This is done by the gfx_mono_draw_string() and
* gfx_mono_draw_progmem_string() functions.
*
* \param[in] ch Character to be drawn
* \param[in] x X coordinate on screen.
* \param[in] y Y coordinate on screen.
* \param[in] font Font to draw character in
*/
static void gfx_mono_draw_char_hugemem(const char ch, const gfx_coord_t x,
const gfx_coord_t y, const struct font *font)
{
uint8_t i;
uint8_t char_row_size;
uint8_t glyph_size;
uint16_t glyph_data_offset;
uint8_t char_buff[EXTMEM_BUF_SIZE];
uint8_t buffer_pos;
uint8_t rows_left;
/* Sanity check on parameters, assert if font is NULL. */
Assert(font != NULL);
gfx_coord_t inc_x = x;
gfx_coord_t inc_y = y;
char_row_size = font->width / CONFIG_FONT_PIXELS_PER_BYTE;
if (font->width % CONFIG_FONT_PIXELS_PER_BYTE) {
char_row_size++;
}
glyph_size = char_row_size * font->height;
glyph_data_offset = glyph_size * ((uint8_t)ch - font->first_char);
buffer_pos = EXTMEM_BUF_SIZE;
rows_left = font->height;
do {
static uint8_t glyph_byte = 0;
uint8_t pixelsToDraw = font->width;
for (i = 0; i < pixelsToDraw; i++) {
if (i % CONFIG_FONT_PIXELS_PER_BYTE == 0) {
/* Read another byte from hugemem */
if (buffer_pos >= EXTMEM_BUF_SIZE) {
hugemem_ptr_t source
= font->data.hugemem;
source = (hugemem_ptr_t)
((uint32_t)source +
glyph_data_offset);
hugemem_read_block(char_buff, source,
EXTMEM_BUF_SIZE);
glyph_data_offset += EXTMEM_BUF_SIZE;
buffer_pos = 0;
}
glyph_byte = char_buff[buffer_pos];
buffer_pos++;
}
/* Draw bit of glyph to screen */
if ((glyph_byte & 0x80)) {
gfx_mono_draw_pixel(inc_x, inc_y,
GFX_PIXEL_SET);
}
inc_x += 1;
glyph_byte <<= 1;
}
inc_y += 1;
inc_x = x;
} while (--rows_left > 0);
}
#endif
/**
* \internal
* \brief Helper function that draws a character from a font in progmem
* to the display
*
* This function will first calculate the start offset in the font character
* data before iterating over the specific character data.
*
* Only pixels in the character that should be enabled are done so, the caller
* is required to prepare the drawing area before printing a character to it.
* This is done by the gfx_mono_draw_string() and
* gfx_mono_draw_progmem_string() functions.
*
* \param[in] ch Character to be drawn
* \param[in] x X coordinate on screen.
* \param[in] y Y coordinate on screen.
* \param[in] font Font to draw character in
*/
static void gfx_mono_draw_char_progmem(const char ch, const gfx_coord_t x,
const gfx_coord_t y, const struct font *font)
{
uint8_t PROGMEM_PTR_T glyph_data;
uint16_t glyph_data_offset;
uint8_t char_row_size;
uint8_t rows_left;
uint8_t i;
/* Sanity check on parameters, assert if font is NULL. */
Assert(font != NULL);
gfx_coord_t inc_x = x;
gfx_coord_t inc_y = y;
char_row_size = font->width / CONFIG_FONT_PIXELS_PER_BYTE;
if (font->width % CONFIG_FONT_PIXELS_PER_BYTE) {
char_row_size++;
}
glyph_data_offset = char_row_size * font->height *
((uint8_t)ch - font->first_char);
glyph_data = font->data.progmem + glyph_data_offset;
rows_left = font->height;
do {
uint8_t glyph_byte = 0;
uint8_t pixelsToDraw = font->width;
for (i = 0; i < pixelsToDraw; i++) {
if (i % CONFIG_FONT_PIXELS_PER_BYTE == 0) {
glyph_byte = PROGMEM_READ_BYTE(glyph_data);
glyph_data++;
}
if ((glyph_byte & 0x80)) {
gfx_mono_draw_pixel(inc_x, inc_y,
GFX_PIXEL_SET);
}
inc_x += 1;
glyph_byte <<= 1;
}
inc_y += 1;
inc_x = x;
rows_left--;
} while (rows_left > 0);
}
/**
* \brief Draws a character to the display
*
* \param[in] c Character to be drawn
* \param[in] x X coordinate on screen.
* \param[in] y Y coordinate on screen.
* \param[in] font Font to draw character in
*/
void gfx_mono_draw_char(const char c, const gfx_coord_t x, const gfx_coord_t y,
const struct font *font)
{
gfx_mono_draw_filled_rect(x, y, font->width, font->height,
GFX_PIXEL_CLR);
switch (font->type) {
case FONT_LOC_PROGMEM:
gfx_mono_draw_char_progmem(c, x, y, font);
break;
#ifdef CONFIG_HUGEMEM
case FONT_LOC_HUGEMEM:
gfx_mono_draw_char_hugemem(c, x, y, font);
break;
#endif
default:
/* Unsupported mode, call assert */
Assert(false);
break;
}
}
/**
* \brief Draws a string to the display
*
* This function will draw a string located in memory to the display.
*
* \param[in] str Pointer to string
* \param[in] x X coordinate on screen.
* \param[in] y Y coordinate on screen.
* \param[in] font Font to draw string in
*/
void gfx_mono_draw_string(const char *str, gfx_coord_t x, gfx_coord_t y,
const struct font *font)
{
/* Save X in order to know where to return to on CR. */
const gfx_coord_t start_of_string_position_x = x;
/* Sanity check on parameters, assert if str or font is NULL. */
Assert(str != NULL);
Assert(font != NULL);
/* Draw characters until trailing null byte */
do {
/* Handle '\n' as newline, draw normal characters. */
if (*str == '\n') {
x = start_of_string_position_x;
y += font->height + 1;
} else if (*str == '\r') {
/* Skip '\r' characters. */
} else {
gfx_mono_draw_char(*str, x, y, font);
x += font->width;
}
} while (*(++str));
}
/**
* \brief Draws a string located in program memory to the display
*
* This function will draw a string located in program memory to the display,
* this differs from gfx_mono_draw_string() by using constant string data from
* the program memory instead of string data in RAM.
*
* Using program memory for constant strings will reduce the applications need
* for RAM, and thus lower the overall size footprint.
*
* \param[in] str Pointer to string located in program memory
* \param[in] x X coordinate on screen.
* \param[in] y Y coordinate on screen.
* \param[in] font Font to draw string in
*/
void gfx_mono_draw_progmem_string(char PROGMEM_PTR_T str, gfx_coord_t x,
gfx_coord_t y, const struct font *font)
{
char temp_char;
/* Sanity check on parameters, assert if str or font is NULL. */
Assert(str != NULL);
Assert(font != NULL);
/* Save X in order to know where to return to on CR. */
const gfx_coord_t start_of_string_position_x = x;
/* Draw characters until trailing null byte */
temp_char = PROGMEM_READ_BYTE((uint8_t PROGMEM_PTR_T)str);
while (temp_char) {
/* Handle '\n' as newline, draw normal characters. */
if (temp_char == '\n') {
x = start_of_string_position_x;
y += font->height + 1;
} else if (temp_char == '\r') {
/* Skip '\r' characters. */
} else {
gfx_mono_draw_char(temp_char, x, y, font);
x += font->width;
}
temp_char = PROGMEM_READ_BYTE((uint8_t PROGMEM_PTR_T)(++str));
}
}
/**
* \brief Computes the bounding box of a string
*
* \note If string is empty the returned width will be 1 pixel and the height
* equal to the font height.
*
* \param[in] str String to calculate bounding box for
* \param[in] font Font used
* \param[in] width Pointer to width result
* \param[in] height Pointer to height result
*/
void gfx_mono_get_string_bounding_box(const char *str, const struct font *font,
gfx_coord_t *width, gfx_coord_t *height)
{
gfx_coord_t font_width = font->width;
gfx_coord_t font_height = font->height;
gfx_coord_t max_width = 1;
gfx_coord_t max_height = font_height;
gfx_coord_t x = 0;
/* Sanity check on parameters, assert if str or font is NULL. */
Assert(str != NULL);
Assert(font != NULL);
/* Handle each character until trailing null byte */
do {
/* Handle '\n' as newline, draw normal characters. */
if (*str == '\n') {
x = 0;
max_height += font_height;
} else if (*str == '\r') {
/* Skip '\r' characters. */
} else {
x += font_width;
if (x > max_width) {
max_width = x;
}
}
} while (*(++str));
/* Return values through references */
*width = max_width;
*height = max_height;
}
/**
* \brief Computes the bounding box of a string located in program memory
*
* \note If string is empty the returned width will be 1 pixel and the height
* equal to the font height.
*
* \param[in] str String in program memory to calculate bounding box for
* \param[in] font Font used
* \param[in] width Pointer to width result
* \param[in] height Pointer to height result
*/
void gfx_mono_get_progmem_string_bounding_box(char PROGMEM_PTR_T str,
const struct font *font, gfx_coord_t *width,
gfx_coord_t *height)
{
gfx_coord_t font_width = font->width;
gfx_coord_t font_height = font->height;
char temp_char;
gfx_coord_t max_width = 1;
gfx_coord_t max_height = font_height;
gfx_coord_t x = 0;
/* Sanity check on parameters, assert if str or font is NULL. */
Assert(str != NULL);
Assert(font != NULL);
/* Handle each character until trailing null byte */
temp_char = PROGMEM_READ_BYTE((uint8_t PROGMEM_PTR_T)str);
while (temp_char) {
/* Handle '\n' as newline, draw normal characters. */
if (temp_char == '\n') {
x = 0;
max_height += font_height;
} else if (*str == '\r') {
/* Skip '\r' characters. */
} else {
x += font_width;
if (x > max_width) {
max_width = x;
}
}
temp_char = PROGMEM_READ_BYTE((uint8_t PROGMEM_PTR_T)(++str));
}
/* Return values through references */
*width = max_width;
*height = max_height;
}
|