|
1 /* |
|
2 |
|
3 u8g2_setup.c |
|
4 |
|
5 Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
|
6 |
|
7 Copyright (c) 2016, olikraus@gmail.com |
|
8 All rights reserved. |
|
9 |
|
10 Redistribution and use in source and binary forms, with or without modification, |
|
11 are permitted provided that the following conditions are met: |
|
12 |
|
13 * Redistributions of source code must retain the above copyright notice, this list |
|
14 of conditions and the following disclaimer. |
|
15 |
|
16 * Redistributions in binary form must reproduce the above copyright notice, this |
|
17 list of conditions and the following disclaimer in the documentation and/or other |
|
18 materials provided with the distribution. |
|
19 |
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
|
21 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
|
22 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
30 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
32 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
33 |
|
34 */ |
|
35 |
|
36 #include "u8g2.h" |
|
37 #include <string.h> |
|
38 #include <assert.h> |
|
39 |
|
40 |
|
41 /*============================================*/ |
|
42 |
|
43 |
|
44 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
45 |
|
46 void u8g2_SetMaxClipWindow(u8g2_t *u8g2) |
|
47 { |
|
48 u8g2->clip_x0 = 0; |
|
49 u8g2->clip_y0 = 0; |
|
50 u8g2->clip_x1 = (u8g2_uint_t)~(u8g2_uint_t)0; |
|
51 u8g2->clip_y1 = (u8g2_uint_t)~(u8g2_uint_t)0; |
|
52 |
|
53 u8g2->cb->update_page_win(u8g2); |
|
54 } |
|
55 |
|
56 void u8g2_SetClipWindow(u8g2_t *u8g2, u8g2_uint_t clip_x0, u8g2_uint_t clip_y0, u8g2_uint_t clip_x1, u8g2_uint_t clip_y1 ) |
|
57 { |
|
58 u8g2->clip_x0 = clip_x0; |
|
59 u8g2->clip_y0 = clip_y0; |
|
60 u8g2->clip_x1 = clip_x1; |
|
61 u8g2->clip_y1 = clip_y1; |
|
62 u8g2->cb->update_page_win(u8g2); |
|
63 } |
|
64 #endif |
|
65 |
|
66 /*============================================*/ |
|
67 /* |
|
68 This procedure is called after setting up the display (u8x8 structure). |
|
69 --> This is the central init procedure for u8g2 object |
|
70 */ |
|
71 void u8g2_SetupBuffer(u8g2_t *u8g2, uint8_t *buf, uint8_t tile_buf_height, u8g2_draw_ll_hvline_cb ll_hvline_cb, const u8g2_cb_t *u8g2_cb) |
|
72 { |
|
73 u8g2->font = NULL; |
|
74 //u8g2->kerning = NULL; |
|
75 //u8g2->get_kerning_cb = u8g2_GetNullKerning; |
|
76 |
|
77 //u8g2->ll_hvline = u8g2_ll_hvline_vertical_top_lsb; |
|
78 u8g2->ll_hvline = ll_hvline_cb; |
|
79 |
|
80 u8g2->tile_buf_ptr = buf; |
|
81 u8g2->tile_buf_height = tile_buf_height; |
|
82 |
|
83 u8g2->tile_curr_row = 0; |
|
84 |
|
85 u8g2->font_decode.is_transparent = 0; /* issue 443 */ |
|
86 u8g2->bitmap_transparency = 0; |
|
87 |
|
88 u8g2->draw_color = 1; |
|
89 u8g2->is_auto_page_clear = 1; |
|
90 |
|
91 u8g2->cb = u8g2_cb; |
|
92 u8g2->cb->update_dimension(u8g2); |
|
93 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
94 u8g2_SetMaxClipWindow(u8g2); /* assign a clip window and call the update() procedure */ |
|
95 #else |
|
96 u8g2->cb->update_page_win(u8g2); |
|
97 #endif |
|
98 |
|
99 u8g2_SetFontPosBaseline(u8g2); /* issue 195 */ |
|
100 |
|
101 #ifdef U8G2_WITH_FONT_ROTATION |
|
102 u8g2->font_decode.dir = 0; |
|
103 #endif |
|
104 } |
|
105 |
|
106 /* |
|
107 Usually the display rotation is set initially, but it could be done later also |
|
108 u8g2_cb can be U8G2_R0..U8G2_R3 |
|
109 */ |
|
110 void u8g2_SetDisplayRotation(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb) |
|
111 { |
|
112 u8g2->cb = u8g2_cb; |
|
113 u8g2->cb->update_dimension(u8g2); |
|
114 u8g2->cb->update_page_win(u8g2); |
|
115 } |
|
116 |
|
117 |
|
118 /*============================================*/ |
|
119 /* |
|
120 update dimension: |
|
121 calculate the following variables: |
|
122 u8g2_uint_t buf_x0; left corner of the buffer |
|
123 u8g2_uint_t buf_x1; right corner of the buffer (excluded) |
|
124 u8g2_uint_t buf_y0; |
|
125 u8g2_uint_t buf_y1; |
|
126 */ |
|
127 |
|
128 static void u8g2_update_dimension_common(u8g2_t *u8g2) |
|
129 { |
|
130 const u8x8_display_info_t *display_info = u8g2_GetU8x8(u8g2)->display_info; |
|
131 u8g2_uint_t t; |
|
132 |
|
133 t = u8g2->tile_buf_height; |
|
134 t *= 8; |
|
135 u8g2->pixel_buf_height = t; |
|
136 |
|
137 t = display_info->tile_width; |
|
138 #ifndef U8G2_16BIT |
|
139 if ( t >= 32 ) |
|
140 t = 31; |
|
141 #endif |
|
142 t *= 8; |
|
143 u8g2->pixel_buf_width = t; |
|
144 |
|
145 t = u8g2->tile_curr_row; |
|
146 t *= 8; |
|
147 u8g2->pixel_curr_row = t; |
|
148 |
|
149 t = u8g2->tile_buf_height; |
|
150 /* handle the case, where the buffer is larger than the (remaining) part of the display */ |
|
151 if ( t + u8g2->tile_curr_row > display_info->tile_height ) |
|
152 t = display_info->tile_height - u8g2->tile_curr_row; |
|
153 t *= 8; |
|
154 |
|
155 u8g2->buf_y0 = u8g2->pixel_curr_row; |
|
156 u8g2->buf_y1 = u8g2->buf_y0; |
|
157 u8g2->buf_y1 += t; |
|
158 |
|
159 |
|
160 #ifdef U8G2_16BIT |
|
161 u8g2->width = display_info->pixel_width; |
|
162 u8g2->height = display_info->pixel_height; |
|
163 #else |
|
164 u8g2->width = 240; |
|
165 if ( display_info->pixel_width <= 240 ) |
|
166 u8g2->width = display_info->pixel_width; |
|
167 u8g2->height = display_info->pixel_height; |
|
168 #endif |
|
169 |
|
170 } |
|
171 |
|
172 /*==========================================================*/ |
|
173 /* apply clip window */ |
|
174 |
|
175 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
176 static void u8g2_apply_clip_window(u8g2_t *u8g2) |
|
177 { |
|
178 /* check aganst the current user_??? window */ |
|
179 if ( u8g2_IsIntersection(u8g2, u8g2->clip_x0, u8g2->clip_y0, u8g2->clip_x1, u8g2->clip_y1) == 0 ) |
|
180 { |
|
181 u8g2->is_page_clip_window_intersection = 0; |
|
182 } |
|
183 else |
|
184 { |
|
185 u8g2->is_page_clip_window_intersection = 1; |
|
186 |
|
187 if ( u8g2->user_x0 < u8g2->clip_x0 ) |
|
188 u8g2->user_x0 = u8g2->clip_x0; |
|
189 if ( u8g2->user_x1 > u8g2->clip_x1 ) |
|
190 u8g2->user_x1 = u8g2->clip_x1; |
|
191 if ( u8g2->user_y0 < u8g2->clip_y0 ) |
|
192 u8g2->user_y0 = u8g2->clip_y0; |
|
193 if ( u8g2->user_y1 > u8g2->clip_y1 ) |
|
194 u8g2->user_y1 = u8g2->clip_y1; |
|
195 } |
|
196 } |
|
197 #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
|
198 |
|
199 /*==========================================================*/ |
|
200 |
|
201 |
|
202 void u8g2_update_dimension_r0(u8g2_t *u8g2) |
|
203 { |
|
204 u8g2_update_dimension_common(u8g2); |
|
205 } |
|
206 |
|
207 void u8g2_update_page_win_r0(u8g2_t *u8g2) |
|
208 { |
|
209 u8g2->user_x0 = 0; |
|
210 u8g2->user_x1 = u8g2->width; /* pixel_buf_width replaced with width */ |
|
211 |
|
212 u8g2->user_y0 = u8g2->buf_y0; |
|
213 u8g2->user_y1 = u8g2->buf_y1; |
|
214 |
|
215 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
216 u8g2_apply_clip_window(u8g2); |
|
217 #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
|
218 } |
|
219 |
|
220 |
|
221 void u8g2_update_dimension_r1(u8g2_t *u8g2) |
|
222 { |
|
223 u8g2_update_dimension_common(u8g2); |
|
224 |
|
225 u8g2->height = u8g2_GetU8x8(u8g2)->display_info->pixel_width; |
|
226 u8g2->width = u8g2_GetU8x8(u8g2)->display_info->pixel_height; |
|
227 |
|
228 } |
|
229 |
|
230 void u8g2_update_page_win_r1(u8g2_t *u8g2) |
|
231 { |
|
232 u8g2->user_x0 = u8g2->buf_y0; |
|
233 u8g2->user_x1 = u8g2->buf_y1; |
|
234 |
|
235 u8g2->user_y0 = 0; |
|
236 u8g2->user_y1 = u8g2->height; /* pixel_buf_width replaced with height (which is the real pixel width) */ |
|
237 |
|
238 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
239 u8g2_apply_clip_window(u8g2); |
|
240 #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
|
241 } |
|
242 |
|
243 void u8g2_update_dimension_r2(u8g2_t *u8g2) |
|
244 { |
|
245 u8g2_update_dimension_common(u8g2); |
|
246 } |
|
247 |
|
248 void u8g2_update_page_win_r2(u8g2_t *u8g2) |
|
249 { |
|
250 u8g2->user_x0 = 0; |
|
251 u8g2->user_x1 = u8g2->width; /* pixel_buf_width replaced with width */ |
|
252 |
|
253 /* there are ases where the height is not a multiple of 8. */ |
|
254 /* in such a case u8g2->buf_y1 might be heigher than u8g2->height */ |
|
255 u8g2->user_y0 = 0; |
|
256 if ( u8g2->height >= u8g2->buf_y1 ) |
|
257 u8g2->user_y0 = u8g2->height - u8g2->buf_y1; |
|
258 u8g2->user_y1 = u8g2->height - u8g2->buf_y0; |
|
259 |
|
260 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
261 u8g2_apply_clip_window(u8g2); |
|
262 #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
|
263 } |
|
264 |
|
265 |
|
266 void u8g2_update_dimension_r3(u8g2_t *u8g2) |
|
267 { |
|
268 u8g2_update_dimension_common(u8g2); |
|
269 |
|
270 u8g2->height = u8g2_GetU8x8(u8g2)->display_info->pixel_width; |
|
271 u8g2->width = u8g2_GetU8x8(u8g2)->display_info->pixel_height; |
|
272 |
|
273 } |
|
274 |
|
275 void u8g2_update_page_win_r3(u8g2_t *u8g2) |
|
276 { |
|
277 /* there are ases where the height is not a multiple of 8. */ |
|
278 /* in such a case u8g2->buf_y1 might be heigher than u8g2->width */ |
|
279 u8g2->user_x0 = 0; |
|
280 if ( u8g2->width >= u8g2->buf_y1 ) |
|
281 u8g2->user_x0 = u8g2->width - u8g2->buf_y1; |
|
282 u8g2->user_x1 = u8g2->width - u8g2->buf_y0; |
|
283 |
|
284 u8g2->user_y0 = 0; |
|
285 u8g2->user_y1 = u8g2->height; /* pixel_buf_width replaced with height (pixel_width) */ |
|
286 |
|
287 #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
|
288 u8g2_apply_clip_window(u8g2); |
|
289 #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
|
290 } |
|
291 |
|
292 |
|
293 /*============================================*/ |
|
294 extern void u8g2_draw_hv_line_2dir(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir); |
|
295 |
|
296 |
|
297 void u8g2_draw_l90_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
|
298 { |
|
299 #ifdef __unix |
|
300 assert( dir <= 1 ); |
|
301 #endif |
|
302 u8g2_draw_hv_line_2dir(u8g2, x, y, len, dir); |
|
303 } |
|
304 |
|
305 void u8g2_draw_l90_mirrorr_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
|
306 { |
|
307 u8g2_uint_t xx; |
|
308 xx = u8g2->width; |
|
309 xx -= x; |
|
310 if ( (dir & 1) == 0 ) |
|
311 { |
|
312 xx -= len; |
|
313 } |
|
314 else |
|
315 { |
|
316 xx--; |
|
317 } |
|
318 u8g2_draw_hv_line_2dir(u8g2, xx, y, len, dir); |
|
319 } |
|
320 |
|
321 /* dir = 0 or 1 */ |
|
322 void u8g2_draw_l90_r1(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
|
323 { |
|
324 u8g2_uint_t xx, yy; |
|
325 |
|
326 #ifdef __unix |
|
327 assert( dir <= 1 ); |
|
328 #endif |
|
329 |
|
330 yy = x; |
|
331 |
|
332 xx = u8g2->height; |
|
333 xx -= y; |
|
334 xx--; |
|
335 |
|
336 dir ++; |
|
337 if ( dir == 2 ) |
|
338 { |
|
339 xx -= len; |
|
340 xx++; |
|
341 dir = 0; |
|
342 } |
|
343 |
|
344 u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir); |
|
345 } |
|
346 |
|
347 void u8g2_draw_l90_r2(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
|
348 { |
|
349 u8g2_uint_t xx, yy; |
|
350 |
|
351 /* |
|
352 yy = u8g2->height; |
|
353 yy -= y; |
|
354 yy--; |
|
355 |
|
356 xx = u8g2->width; |
|
357 xx -= x; |
|
358 xx--; |
|
359 |
|
360 if ( dir == 0 ) |
|
361 { |
|
362 xx -= len; |
|
363 xx++; |
|
364 } |
|
365 else if ( dir == 1 ) |
|
366 { |
|
367 yy -= len; |
|
368 yy++; |
|
369 } |
|
370 */ |
|
371 |
|
372 yy = u8g2->height; |
|
373 yy -= y; |
|
374 |
|
375 xx = u8g2->width; |
|
376 xx -= x; |
|
377 |
|
378 if ( dir == 0 ) |
|
379 { |
|
380 yy--; |
|
381 xx -= len; |
|
382 } |
|
383 else if ( dir == 1 ) |
|
384 { |
|
385 xx--; |
|
386 yy -= len; |
|
387 } |
|
388 |
|
389 u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir); |
|
390 } |
|
391 |
|
392 void u8g2_draw_l90_r3(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
|
393 { |
|
394 u8g2_uint_t xx, yy; |
|
395 |
|
396 xx = y; |
|
397 |
|
398 yy = u8g2->width; |
|
399 yy -= x; |
|
400 |
|
401 if ( dir == 0 ) |
|
402 { |
|
403 yy--; |
|
404 yy -= len; |
|
405 yy++; |
|
406 dir = 1; |
|
407 } |
|
408 else |
|
409 { |
|
410 yy--; |
|
411 dir = 0; |
|
412 } |
|
413 |
|
414 |
|
415 u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir); |
|
416 } |
|
417 |
|
418 |
|
419 |
|
420 /*============================================*/ |
|
421 const u8g2_cb_t u8g2_cb_r0 = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_l90_r0 }; |
|
422 const u8g2_cb_t u8g2_cb_r1 = { u8g2_update_dimension_r1, u8g2_update_page_win_r1, u8g2_draw_l90_r1 }; |
|
423 const u8g2_cb_t u8g2_cb_r2 = { u8g2_update_dimension_r2, u8g2_update_page_win_r2, u8g2_draw_l90_r2 }; |
|
424 const u8g2_cb_t u8g2_cb_r3 = { u8g2_update_dimension_r3, u8g2_update_page_win_r3, u8g2_draw_l90_r3 }; |
|
425 |
|
426 const u8g2_cb_t u8g2_cb_mirror = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_l90_mirrorr_r0 }; |
|
427 |
|
428 /*============================================*/ |
|
429 /* setup for the null device */ |
|
430 |
|
431 /* setup for the null (empty) device */ |
|
432 void u8g2_Setup_null(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb) |
|
433 { |
|
434 static uint8_t buf[8]; |
|
435 u8g2_SetupDisplay(u8g2, u8x8_d_null_cb, u8x8_cad_empty, byte_cb, gpio_and_delay_cb); |
|
436 u8g2_SetupBuffer(u8g2, buf, 1, u8g2_ll_hvline_vertical_top_lsb, rotation); |
|
437 } |
|
438 |
|
439 |
|
440 |
|
441 |