-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgfx.c
300 lines (252 loc) · 8.4 KB
/
gfx.c
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
#include "gfx.h"
#include "fonts/adafruit_gfx_library/glcdfont.c"
// Helper function to create "sorted" points, out_p1 will have the smallest x
// and y coordinates from p1 and p2 and out_p2 the biggest. Useful for, e.g.
// drawing rectangles
error_t gfx_min_max_point(const gfx_point *p1, const gfx_point *p2,
gfx_point *out_p1, gfx_point *out_p2) {
if (!p1 || !p2 || !out_p1 || !out_p2) {
return E_NULL_PTR;
}
out_p1->x = min(p1->x, p2->x);
out_p1->y = min(p1->y, p2->y);
out_p2->x = max(p1->x, p2->x);
out_p2->y = max(p1->y, p2->y);;
return E_SUCCESS;
}
error_t gfx_init(gfx_context *ctx, uint8_t *buffer, uint16_t width,
uint16_t height) {
if (!ctx || !buffer) {
return E_NULL_PTR;
}
ctx->buffer = buffer;
ctx->width = width;
ctx->height = height;
ctx->word_wrap = true;
return E_SUCCESS;
}
// print checkers on display
error_t gfx_demo_checker(gfx_context *ctx) {
// fill buffer
for (uint16_t i = 0; i < (ctx->height * (ctx->width / 8)); ++i) {
if (i % 2 == 0)
ctx->buffer[i] = 0x55;
else
ctx->buffer[i] = 0xAA;
}
return E_SUCCESS;
}
error_t gfx_clear(gfx_context *ctx) {
memset(ctx->buffer, 0, ctx->width * ctx->height / 8);
return E_SUCCESS;
}
error_t gfx_draw_pixel(gfx_context *ctx, uint16_t x, uint16_t y) {
if (x >= ctx->width || y >= ctx->height)
return E_SUCCESS;
ctx->buffer[x + (y / 8) * ctx->width] |= (1u << (y & 7u));
return E_SUCCESS;
}
error_t gfx_clear_pixel(gfx_context *ctx, uint16_t x, uint16_t y) {
if (x >= ctx->width || y >= ctx->height)
return E_SUCCESS;
ctx->buffer[x + (y / 8) * ctx->width] &= ~(1u << (y & 7u));
return E_SUCCESS;
}
error_t gfx_draw_gfx_pixel(gfx_context *ctx, enum gfx_color color,
const gfx_point *pt) {
if (color == COLOR_BG) {
check_error(gfx_clear_pixel(ctx, pt->x, pt->y));
} else {
check_error(gfx_draw_pixel(ctx, pt->x, pt->y));
}
return E_SUCCESS;
}
error_t gfx_draw_char(gfx_context *ctx, enum gfx_color color, uint16_t x,
uint16_t y, char c) {
// extern uint8_t font[];
for (uint8_t i = 0; i < 5; i++) { // Char bitmap = 5 columns
uint8_t line = font[c * 5 + i];
for (uint8_t j = 0; j < 8; j++, line >>= 1u) {
if (line & 1u) {
if (color == COLOR_BG) {
check_error(gfx_clear_pixel(ctx, x + i, y + j));
} else {
check_error(gfx_draw_pixel(ctx, x + i, y + j));
}
}
}
}
return E_SUCCESS;
}
error_t gfx_draw_text(gfx_context *ctx, enum gfx_color color, uint16_t x,
uint16_t y, const char *string) {
uint16_t _x = x;
uint16_t _y = y;
uint8_t a = 0;
while (string[a]) {
if ((_x + 5) >= ctx->width) {
_x = 0;
_y += 8;
}
check_error(gfx_draw_char(ctx, color, _x, _y, string[a++]));
_x += 5;
}
return E_SUCCESS;
}
static error_t _gfx_draw_line_low(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1, const gfx_point *p2) {
int16_t d_x = p2->x - p1->x;
int16_t d_y = p2->y - p1->y;
int8_t y_i = 1;
if (d_y < 0) {
y_i = -1;
d_y = -d_y;
}
int16_t d = 2 * d_y - d_x;
int16_t y = p1->y;
for (uint16_t x = p1->x; x < p2->x; ++x) {
check_error(gfx_draw_gfx_pixel(ctx, color, &(gfx_point){.x = x, .y = y}));
if (d > 0) {
y += y_i;
d -= 2 * d_x;
}
d += 2 * d_y;
}
return E_SUCCESS;
}
static error_t _gfx_draw_line_high(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1, const gfx_point *p2) {
int16_t d_x = p2->x - p1->x;
int16_t d_y = p2->y - p1->y;
int8_t x_i = 1;
if (d_x < 0) {
x_i = -1;
d_x = -d_x;
}
int16_t d = 2 * d_x - d_y;
int16_t x = p1->x;
for (uint16_t y = p1->y; y < p2->y; ++y) {
check_error(gfx_draw_gfx_pixel(ctx, color, &(gfx_point){.x = x, .y = y}));
if (d > 0) {
x += x_i;
d -= 2 * d_y;
}
d += 2 * d_x;
}
return E_SUCCESS;
}
static error_t _gfx_draw_line_horizontal(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1,
const gfx_point *p2) {
if (p1->y != p2->y) {
return E_VALUE_INVALID;
}
for (uint16_t x = p1->x; x <= p2->x; ++x) {
check_error(
gfx_draw_gfx_pixel(ctx, color, &(gfx_point){.x = x, .y = p1->y}));
}
return E_SUCCESS;
}
static error_t _gfx_draw_line_vertical(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1,
const gfx_point *p2) {
if (p1->x != p2->x) {
return E_VALUE_INVALID;
}
for (uint16_t y = p1->y; y <= p2->y; ++y) {
check_error(
gfx_draw_gfx_pixel(ctx, color, &(gfx_point){.x = p1->x, .y = y}));
}
return E_SUCCESS;
}
error_t gfx_draw_line(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1, const gfx_point *p2) {
gfx_point _p1, _p2;
check_error(gfx_min_max_point(p1, p2, &_p1, &_p2));
// Special case for horizontal / vertical lines
if (_p1.y == _p2.y) {
return _gfx_draw_line_horizontal(ctx, color, &_p1, &_p2);
} else if (_p1.x == _p2.x) {
return _gfx_draw_line_vertical(ctx, color, &_p1, &_p2);
}
// Use Bresenham algorithm
if (fast_abs(p2->y - p1->y) < fast_abs(p2->x - p1->x)) {
if (p1->x > p2->x) {
return _gfx_draw_line_low(ctx, color, p2, p1);
} else {
return _gfx_draw_line_low(ctx, color, p1, p2);
}
} else {
if (p1->y > p2->y) {
return _gfx_draw_line_high(ctx, color, p2, p1);
} else {
return _gfx_draw_line_high(ctx, color, p1, p2);
}
}
}
error_t gfx_draw_rectangle(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1, const gfx_point *p2) {
gfx_point _p1, _p2;
check_error(gfx_min_max_point(p1, p2, &_p1, &_p2));
gfx_point corner_1 = {.x = _p2.x, .y = _p1.y};
gfx_point corner_2 = {.x = _p1.x, .y = _p2.y};
check_error(_gfx_draw_line_horizontal(ctx, color, &_p1, &corner_1));
check_error(_gfx_draw_line_horizontal(ctx, color, &corner_2, &_p2));
check_error(_gfx_draw_line_vertical(ctx, color, &_p1, &corner_2));
check_error(_gfx_draw_line_vertical(ctx, color, &corner_1, &_p2));
return E_SUCCESS;
}
error_t gfx_draw_circle(gfx_context *ctx, enum gfx_color color,
const gfx_point *center, uint16_t radius) {
int16_t f = 1 - radius;
int16_t d_x = 0;
int16_t d_y = -2 * radius;
int16_t x = 0;
int16_t y = radius;
gfx_draw_gfx_pixel(ctx, color,
&(gfx_point){.x = center->x, .y = center->y + radius});
gfx_draw_gfx_pixel(ctx, color,
&(gfx_point){.x = center->x, .y = center->y - radius});
gfx_draw_gfx_pixel(ctx, color,
&(gfx_point){.x = center->x + radius, .y = center->y});
gfx_draw_gfx_pixel(ctx, color,
&(gfx_point){.x = center->x - radius, .y = center->y});
while (x < y) {
if (f >= 0) {
--y;
d_y += 2;
f += d_y;
}
++x;
d_x += 2;
f += d_x + 1;
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x + x, .y = center->y + y}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x - x, .y = center->y + y}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x + x, .y = center->y - y}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x - x, .y = center->y - y}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x + y, .y = center->y + x}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x - y, .y = center->y + x}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x + y, .y = center->y - x}));
check_error(gfx_draw_gfx_pixel(
ctx, color, &(gfx_point){.x = center->x - y, .y = center->y - x}));
}
return E_SUCCESS;
}
error_t gfx_fill_rectangle(gfx_context *ctx, enum gfx_color color,
const gfx_point *p1, const gfx_point *p2) {
gfx_point _p1, _p2;
check_error(gfx_min_max_point(p1, p2, &_p1, &_p2));
for (uint16_t y = _p1.y; y <= _p2.y; ++y) {
check_error(_gfx_draw_line_horizontal(ctx, color,
&(gfx_point){.x = _p1.x, .y = y},
&(gfx_point){.x = _p2.x, .y = y}));
}
return E_SUCCESS;
}