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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
/**
* \file
*
* \brief Generic monochrome LCD graphic primitives
*
* Copyright (c) 2011-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
*
*/
/**
* \ingroup asfdoc_common2_gfx_mono_generic_group
* @{
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include "gfx_mono_generic.h"
/**
* \brief Draw a horizontal line, one pixel wide (generic implementation)
*
* \note This function does a very simple bounds checking that does not
* check if the line is placed outside the screen. If you supply an
* x- or y-coordinate outside the display the behaviour is undefined,
* and you risk overwriting portions of internal SRAM.
*
* \param[in] x X coordinate of leftmost pixel.
* \param[in] y Y coordinate of the line.
* \param[in] length Length of the line in pixels.
* \param[in] color Pixel operation of the line.
*/
void gfx_mono_generic_draw_horizontal_line(gfx_coord_t x, gfx_coord_t y,
gfx_coord_t length, enum gfx_mono_color color)
{
uint8_t page;
uint8_t pixelmask;
uint8_t temp;
/* Clip line length if too long */
if (x + length > GFX_MONO_LCD_WIDTH) {
length = GFX_MONO_LCD_WIDTH - x;
}
page = y / 8;
pixelmask = (1 << (y - (page * 8)));
if (length == 0) {
/* Nothing to do. Move along. */
return;
}
switch (color) {
case GFX_PIXEL_SET:
while (length-- > 0) {
temp = gfx_mono_get_byte(page, x + length);
temp |= pixelmask;
gfx_mono_put_byte(page, x + length, temp);
}
break;
case GFX_PIXEL_CLR:
while (length-- > 0) {
temp = gfx_mono_get_byte(page, x + length);
temp &= ~pixelmask;
gfx_mono_put_byte(page, x + length, temp);
}
break;
case GFX_PIXEL_XOR:
while (length-- > 0) {
temp = gfx_mono_get_byte(page, x + length);
temp ^= pixelmask;
gfx_mono_put_byte(page, x + length, temp);
}
break;
default:
break;
}
}
/**
* \brief Draw a vertical line, one pixel wide (generic implementation)
*
* \note This function does a very simple bounds checking that does not
* check if the line is placed outside the screen. If you supply an
* x- or y-coordinate outside the display the behaviour is undefined,
* and you risk overwriting portions of internal SRAM.
*
* \param[in] x X coordinate of the line.
* \param[in] y Y coordinate of the topmost pixel.
* \param[in] length Length of the line in pixels.
* \param[in] color Pixel operation of the line.
*/
void gfx_mono_generic_draw_vertical_line(gfx_coord_t x, gfx_coord_t y,
gfx_coord_t length, enum gfx_mono_color color)
{
if (length == 0) {
return;
}
gfx_coord_t y2 = y + length - 1;
if (y == y2) {
gfx_mono_draw_pixel(x, y, color);
return;
}
if (y2 >= GFX_MONO_LCD_HEIGHT - 1) {
y2 = GFX_MONO_LCD_HEIGHT - 1;
}
gfx_coord_t y1page = y / 8;
gfx_coord_t y2page = y2 / 8;
uint8_t y1bitpos = y & 0x07;
uint8_t y2bitpos = y2 & 0x07;
uint8_t y1pixelmask = 0xFF << y1bitpos;
uint8_t y2pixelmask = 0xFF >> (7 - y2bitpos);
/* The pixels are on the same page; combine masks */
if (y1page == y2page) {
uint8_t pixelmask = y1pixelmask & y2pixelmask;
gfx_mono_mask_byte(y1page, x, pixelmask, color);
} else {
gfx_mono_mask_byte(y1page, x, y1pixelmask, color);
while (++y1page < y2page) {
gfx_mono_mask_byte(y1page, x, 0xFF, color);
}
gfx_mono_mask_byte(y2page, x, y2pixelmask, color);
}
}
/**
* \brief Draw a line between two arbitrary points (generic implementation).
*
* \param[in] x1 Start X coordinate.
* \param[in] y1 Start Y coordinate.
* \param[in] x2 End X coordinate.
* \param[in] y2 End Y coordinate.
* \param[in] color Pixel operation of the line.
*/
void gfx_mono_generic_draw_line(gfx_coord_t x1, gfx_coord_t y1,
gfx_coord_t x2, gfx_coord_t y2,
enum gfx_mono_color color)
{
uint8_t i;
uint8_t x;
uint8_t y;
int8_t xinc;
int8_t yinc;
int8_t dx;
int8_t dy;
int8_t e;
/* swap x1,y1 with x2,y2 */
if (x1 > x2) {
dx = x1;
x1 = x2;
x2 = dx;
dy = y1;
y1 = y2;
y2 = dy;
}
dx = x2 - x1;
dy = y2 - y1;
x = x1;
y = y1;
if (dx < 0) {
xinc = -1;
dx = -dx;
} else {
xinc = 1;
}
if (dy < 0) {
yinc = -1;
dy = -dy;
} else {
yinc = 1;
}
if (dx > dy) {
e = dy - dx;
for (i = 0; i <= dx; i++) {
gfx_mono_draw_pixel(x, y, color);
if (e >= 0) {
e -= dx;
y += yinc;
}
e += dy;
x += xinc;
}
} else {
e = dx - dy;
for (i = 0; i <= dy; i++) {
gfx_mono_draw_pixel(x, y, color);
if (e >= 0) {
e -= dy;
x += xinc;
}
e += dx;
y += yinc;
}
}
}
/**
* \brief Draw an outline of a rectangle (generic implementation).
*
* \param[in] x X coordinate of the left side.
* \param[in] y Y coordinate of the top side.
* \param[in] width Width of the rectangle.
* \param[in] height Height of the rectangle.
* \param[in] color Pixel operation of the line.
*/
void gfx_mono_generic_draw_rect(gfx_coord_t x, gfx_coord_t y,
gfx_coord_t width, gfx_coord_t height,
enum gfx_mono_color color)
{
gfx_mono_draw_horizontal_line(x, y, width, color);
gfx_mono_draw_horizontal_line(x, y + height - 1, width, color);
gfx_mono_draw_vertical_line(x, y, height, color);
gfx_mono_draw_vertical_line(x + width - 1, y, height, color);
}
/**
* \brief Draw a filled rectangle (generic implementation).
*
* \param[in] x X coordinate of the left side.
* \param[in] y Y coordinate of the top side.
* \param[in] width Width of the rectangle.
* \param[in] height Height of the rectangle.
* \param[in] color Pixel operation of the line
*/
void gfx_mono_generic_draw_filled_rect(gfx_coord_t x, gfx_coord_t y,
gfx_coord_t width, gfx_coord_t height,
enum gfx_mono_color color)
{
if (height == 0) {
/* Nothing to do. Move along. */
return;
}
while (height-- > 0) {
gfx_mono_draw_horizontal_line(x, y + height, width, color);
}
}
/**
* \brief Draw an outline of a circle or arc (generic implementation).
*
* The radius is the distance from the center to the circumference,
* which means that the total width or height of a circle will be
* (radius*2+1).
*
* The octant_mask parameter is a bitmask that decides which octants of
* the circle to draw. Use the GFX_OCTANTn, GFX_QUADRANTn, GFX_xHALF and
* GFX_WHOLE constants and OR them together if required. Radius equal to
* zero gives a single pixel.
*
* \param[in] x X coordinate of center.
* \param[in] y Y coordinate of center.
* \param[in] radius Circle radius in pixels.
* \param[in] color Pixel operation.
* \param[in] octant_mask Bitmask indicating which octants to draw.
*/
void gfx_mono_generic_draw_circle(gfx_coord_t x, gfx_coord_t y,
gfx_coord_t radius, enum gfx_mono_color color,
uint8_t octant_mask)
{
gfx_coord_t offset_x;
gfx_coord_t offset_y;
int16_t error;
/* Draw only a pixel if radius is zero. */
if (radius == 0) {
gfx_mono_draw_pixel(x, y, color);
return;
}
/* Set up start iterators. */
offset_x = 0;
offset_y = radius;
error = 3 - 2 * radius;
/* Iterate offsetX from 0 to radius. */
while (offset_x <= offset_y) {
/* Draw one pixel for each octant enabled in octant_mask. */
if (octant_mask & GFX_OCTANT0) {
gfx_mono_draw_pixel(x + offset_y, y - offset_x, color);
}
if (octant_mask & GFX_OCTANT1) {
gfx_mono_draw_pixel(x + offset_x, y - offset_y, color);
}
if (octant_mask & GFX_OCTANT2) {
gfx_mono_draw_pixel(x - offset_x, y - offset_y, color);
}
if (octant_mask & GFX_OCTANT3) {
gfx_mono_draw_pixel(x - offset_y, y - offset_x, color);
}
if (octant_mask & GFX_OCTANT4) {
gfx_mono_draw_pixel(x - offset_y, y + offset_x, color);
}
if (octant_mask & GFX_OCTANT5) {
gfx_mono_draw_pixel(x - offset_x, y + offset_y, color);
}
if (octant_mask & GFX_OCTANT6) {
gfx_mono_draw_pixel(x + offset_x, y + offset_y, color);
}
if (octant_mask & GFX_OCTANT7) {
gfx_mono_draw_pixel(x + offset_y, y + offset_x, color);
}
/* Update error value and step offset_y when required. */
if (error < 0) {
error += ((offset_x << 2) + 6);
} else {
error += (((offset_x - offset_y) << 2) + 10);
--offset_y;
}
/* Next X. */
++offset_x;
}
}
/**
* \brief Draw a filled circle or sector (generic implementation).
*
* The radius is the distance from the center to the circumference,
* which means that the total width or height of a circle will be
* (radius*2+1).
*
* The quadrant_mask parameter is a bitmask that decides which quadrants
* of the circle to draw. Use the GFX_QUADRANTn, GFX_xHALF and
* GFX_WHOLE constants and OR them together if required. Radius equal to
* zero gives a single pixel.
*
* \note This function only supports quadrants while gfx_draw_circle()
* supports octants. This is to improve performance on drawing
* filled circles.
*
* \param[in] x X coordinate of center.
* \param[in] y Y coordinate of center.
* \param[in] radius Circle radius in pixels.
* \param[in] color Pixel operation.
* \param[in] quadrant_mask Bitmask indicating which quadrants to draw.
*/
void gfx_mono_generic_draw_filled_circle(gfx_coord_t x, gfx_coord_t y,
gfx_coord_t radius, enum gfx_mono_color color,
uint8_t quadrant_mask)
{
gfx_coord_t offset_x;
gfx_coord_t offset_y;
int16_t error;
/* Draw only a pixel if radius is zero. */
if (radius == 0) {
gfx_mono_draw_pixel(x, y, color);
return;
}
/* Set up start iterators. */
offset_x = 0;
offset_y = radius;
error = 3 - 2 * radius;
/* Iterate offset_x from 0 to radius. */
while (offset_x <= offset_y) {
/* Draw vertical lines tracking each quadrant. */
if (quadrant_mask & GFX_QUADRANT0) {
gfx_mono_draw_vertical_line(x + offset_y,
y - offset_x, offset_x + 1, color);
gfx_mono_draw_vertical_line(x + offset_x,
y - offset_y, offset_y + 1, color);
}
if (quadrant_mask & GFX_QUADRANT1) {
gfx_mono_draw_vertical_line(x - offset_y,
y - offset_x, offset_x + 1, color);
gfx_mono_draw_vertical_line(x - offset_x,
y - offset_y, offset_y + 1, color);
}
if (quadrant_mask & GFX_QUADRANT2) {
gfx_mono_draw_vertical_line(x - offset_y,
y, offset_x + 1, color);
gfx_mono_draw_vertical_line(x - offset_x,
y, offset_y + 1, color);
}
if (quadrant_mask & GFX_QUADRANT3) {
gfx_mono_draw_vertical_line(x + offset_y,
y, offset_x + 1, color);
gfx_mono_draw_vertical_line(x + offset_x,
y, offset_y + 1, color);
}
/* Update error value and step offset_y when required. */
if (error < 0) {
error += ((offset_x << 2) + 6);
} else {
error += (((offset_x - offset_y) << 2) + 10);
--offset_y;
}
/* Next X. */
++offset_x;
}
}
/**
* \brief Put bitmap from FLASH or RAM to display
*
* This function will output bitmap data from FLASH or RAM.
* The bitmap y-coordinate will be aligned with display pages, rounded down.
* Ie: placing a bitmap at x=10, y=5 will put the bitmap at x = 10,y = 0 and
* placing a bitmap at x = 10, y = 10 will put the bitmap at x = 10, y = 8
*
*/
void gfx_mono_generic_put_bitmap(struct gfx_mono_bitmap *bitmap, gfx_coord_t x,
gfx_coord_t y)
{
gfx_coord_t num_pages = bitmap->height / 8;
gfx_coord_t page = y / 8;
gfx_coord_t column;
gfx_coord_t i;
gfx_mono_color_t temp;
switch (bitmap->type) {
case GFX_MONO_BITMAP_PROGMEM:
for (i = 0; i < num_pages; i++) {
for (column = 0; column < bitmap->width; column++) {
temp = PROGMEM_READ_BYTE(bitmap->data.progmem
+ (i * bitmap->width)
+ column);
gfx_mono_put_byte(i + page, column + x, temp);
}
}
break;
case GFX_MONO_BITMAP_RAM:
for (i = 0; i < num_pages; i++) {
gfx_mono_put_page(bitmap->data.pixmap
+ (i * bitmap->width), page + i, x,
bitmap->width);
}
break;
default:
break;
}
}
/** @} */
|