| 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->font_height_mode = 0; /* issue 2046 */ |
| 89 | u8g2->draw_color = 1; |
| 90 | u8g2->is_auto_page_clear = 1; |
| 91 | |
| 92 | u8g2->cb = u8g2_cb; |
| 93 | u8g2->cb->update_dimension(u8g2); |
| 94 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 95 | u8g2_SetMaxClipWindow(u8g2); /* assign a clip window and call the update() procedure */ |
| 96 | #else |
| 97 | u8g2->cb->update_page_win(u8g2); |
| 98 | #endif |
| 99 | |
| 100 | u8g2_SetFontPosBaseline(u8g2); /* issue 195 */ |
| 101 | |
| 102 | #ifdef U8G2_WITH_FONT_ROTATION |
| 103 | u8g2->font_decode.dir = 0; |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | Usually the display rotation is set initially, but it could be done later also |
| 109 | u8g2_cb can be U8G2_R0..U8G2_R3 |
| 110 | */ |
| 111 | void u8g2_SetDisplayRotation(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb) |
| 112 | { |
| 113 | u8g2->cb = u8g2_cb; |
| 114 | u8g2->cb->update_dimension(u8g2); |
| 115 | u8g2->cb->update_page_win(u8g2); |
| 116 | } |
| 117 | |
| 118 | /*============================================*/ |
| 119 | |
| 120 | void u8g2_SendF(u8g2_t * u8g2, const char *fmt, ...) |
| 121 | { |
| 122 | va_list va; |
| 123 | va_start(va, fmt); |
| 124 | u8x8_cad_vsendf(u8g2_GetU8x8(u8g2), fmt, va); |
| 125 | va_end(va); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /*============================================*/ |
| 130 | /* |
| 131 | update dimension: |
| 132 | calculate the following variables: |
| 133 | u8g2_uint_t buf_x0; left corner of the buffer |
| 134 | u8g2_uint_t buf_x1; right corner of the buffer (excluded) |
| 135 | u8g2_uint_t buf_y0; |
| 136 | u8g2_uint_t buf_y1; |
| 137 | */ |
| 138 | |
| 139 | static void u8g2_update_dimension_common(u8g2_t *u8g2) |
| 140 | { |
| 141 | const u8x8_display_info_t *display_info = u8g2_GetU8x8(u8g2)->display_info; |
| 142 | u8g2_uint_t t; |
| 143 | |
| 144 | t = u8g2->tile_buf_height; |
| 145 | t *= 8; |
| 146 | u8g2->pixel_buf_height = t; |
| 147 | |
| 148 | t = display_info->tile_width; |
| 149 | #ifndef U8G2_16BIT |
| 150 | if ( t >= 32 ) |
| 151 | t = 31; |
| 152 | #endif |
| 153 | t *= 8; |
| 154 | u8g2->pixel_buf_width = t; |
| 155 | |
| 156 | t = u8g2->tile_curr_row; |
| 157 | t *= 8; |
| 158 | u8g2->pixel_curr_row = t; |
| 159 | |
| 160 | t = u8g2->tile_buf_height; |
| 161 | /* handle the case, where the buffer is larger than the (remaining) part of the display */ |
| 162 | if ( t + u8g2->tile_curr_row > display_info->tile_height ) |
| 163 | t = display_info->tile_height - u8g2->tile_curr_row; |
| 164 | t *= 8; |
| 165 | |
| 166 | u8g2->buf_y0 = u8g2->pixel_curr_row; |
| 167 | u8g2->buf_y1 = u8g2->buf_y0; |
| 168 | u8g2->buf_y1 += t; |
| 169 | |
| 170 | |
| 171 | #ifdef U8G2_16BIT |
| 172 | u8g2->width = display_info->pixel_width; |
| 173 | u8g2->height = display_info->pixel_height; |
| 174 | #else |
| 175 | u8g2->width = 240; |
| 176 | if ( display_info->pixel_width <= 240 ) |
| 177 | u8g2->width = display_info->pixel_width; |
| 178 | u8g2->height = display_info->pixel_height; |
| 179 | #endif |
| 180 | |
| 181 | } |
| 182 | |
| 183 | /*==========================================================*/ |
| 184 | /* apply clip window */ |
| 185 | |
| 186 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 187 | static void u8g2_apply_clip_window(u8g2_t *u8g2) |
| 188 | { |
| 189 | /* check aganst the current user_??? window */ |
| 190 | if ( u8g2_IsIntersection(u8g2, u8g2->clip_x0, u8g2->clip_y0, u8g2->clip_x1, u8g2->clip_y1) == 0 ) |
| 191 | { |
| 192 | u8g2->is_page_clip_window_intersection = 0; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | u8g2->is_page_clip_window_intersection = 1; |
| 197 | |
| 198 | if ( u8g2->user_x0 < u8g2->clip_x0 ) |
| 199 | u8g2->user_x0 = u8g2->clip_x0; |
| 200 | if ( u8g2->user_x1 > u8g2->clip_x1 ) |
| 201 | u8g2->user_x1 = u8g2->clip_x1; |
| 202 | if ( u8g2->user_y0 < u8g2->clip_y0 ) |
| 203 | u8g2->user_y0 = u8g2->clip_y0; |
| 204 | if ( u8g2->user_y1 > u8g2->clip_y1 ) |
| 205 | u8g2->user_y1 = u8g2->clip_y1; |
| 206 | } |
| 207 | } |
| 208 | #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
| 209 | |
| 210 | /*==========================================================*/ |
| 211 | |
| 212 | |
| 213 | void u8g2_update_dimension_r0(u8g2_t *u8g2) |
| 214 | { |
| 215 | u8g2_update_dimension_common(u8g2); |
| 216 | } |
| 217 | |
| 218 | void u8g2_update_page_win_r0(u8g2_t *u8g2) |
| 219 | { |
| 220 | u8g2->user_x0 = 0; |
| 221 | u8g2->user_x1 = u8g2->width; /* pixel_buf_width replaced with width */ |
| 222 | |
| 223 | u8g2->user_y0 = u8g2->buf_y0; |
| 224 | u8g2->user_y1 = u8g2->buf_y1; |
| 225 | |
| 226 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 227 | u8g2_apply_clip_window(u8g2); |
| 228 | #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void u8g2_update_dimension_r1(u8g2_t *u8g2) |
| 233 | { |
| 234 | u8g2_update_dimension_common(u8g2); |
| 235 | |
| 236 | u8g2->height = u8g2_GetU8x8(u8g2)->display_info->pixel_width; |
| 237 | u8g2->width = u8g2_GetU8x8(u8g2)->display_info->pixel_height; |
| 238 | |
| 239 | } |
| 240 | |
| 241 | void u8g2_update_page_win_r1(u8g2_t *u8g2) |
| 242 | { |
| 243 | u8g2->user_x0 = u8g2->buf_y0; |
| 244 | u8g2->user_x1 = u8g2->buf_y1; |
| 245 | |
| 246 | u8g2->user_y0 = 0; |
| 247 | u8g2->user_y1 = u8g2->height; /* pixel_buf_width replaced with height (which is the real pixel width) */ |
| 248 | |
| 249 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 250 | u8g2_apply_clip_window(u8g2); |
| 251 | #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
| 252 | } |
| 253 | |
| 254 | void u8g2_update_dimension_r2(u8g2_t *u8g2) |
| 255 | { |
| 256 | u8g2_update_dimension_common(u8g2); |
| 257 | } |
| 258 | |
| 259 | void u8g2_update_page_win_r2(u8g2_t *u8g2) |
| 260 | { |
| 261 | u8g2->user_x0 = 0; |
| 262 | u8g2->user_x1 = u8g2->width; /* pixel_buf_width replaced with width */ |
| 263 | |
| 264 | /* there are ases where the height is not a multiple of 8. */ |
| 265 | /* in such a case u8g2->buf_y1 might be heigher than u8g2->height */ |
| 266 | u8g2->user_y0 = 0; |
| 267 | if ( u8g2->height >= u8g2->buf_y1 ) |
| 268 | u8g2->user_y0 = u8g2->height - u8g2->buf_y1; |
| 269 | u8g2->user_y1 = u8g2->height - u8g2->buf_y0; |
| 270 | |
| 271 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 272 | u8g2_apply_clip_window(u8g2); |
| 273 | #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void u8g2_update_dimension_r3(u8g2_t *u8g2) |
| 278 | { |
| 279 | u8g2_update_dimension_common(u8g2); |
| 280 | |
| 281 | u8g2->height = u8g2_GetU8x8(u8g2)->display_info->pixel_width; |
| 282 | u8g2->width = u8g2_GetU8x8(u8g2)->display_info->pixel_height; |
| 283 | |
| 284 | } |
| 285 | |
| 286 | void u8g2_update_page_win_r3(u8g2_t *u8g2) |
| 287 | { |
| 288 | /* there are ases where the height is not a multiple of 8. */ |
| 289 | /* in such a case u8g2->buf_y1 might be heigher than u8g2->width */ |
| 290 | u8g2->user_x0 = 0; |
| 291 | if ( u8g2->width >= u8g2->buf_y1 ) |
| 292 | u8g2->user_x0 = u8g2->width - u8g2->buf_y1; |
| 293 | u8g2->user_x1 = u8g2->width - u8g2->buf_y0; |
| 294 | |
| 295 | u8g2->user_y0 = 0; |
| 296 | u8g2->user_y1 = u8g2->height; /* pixel_buf_width replaced with height (pixel_width) */ |
| 297 | |
| 298 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 299 | u8g2_apply_clip_window(u8g2); |
| 300 | #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /*============================================*/ |
| 305 | 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); |
| 306 | |
| 307 | |
| 308 | void u8g2_draw_l90_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 309 | { |
| 310 | #ifdef __unix |
| 311 | assert( dir <= 1 ); |
| 312 | #endif |
| 313 | u8g2_draw_hv_line_2dir(u8g2, x, y, len, dir); |
| 314 | } |
| 315 | |
| 316 | void u8g2_draw_l90_mirrorr_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 317 | { |
| 318 | u8g2_uint_t xx; |
| 319 | xx = u8g2->width; |
| 320 | xx -= x; |
| 321 | if ( (dir & 1) == 0 ) |
| 322 | { |
| 323 | xx -= len; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | xx--; |
| 328 | } |
| 329 | u8g2_draw_hv_line_2dir(u8g2, xx, y, len, dir); |
| 330 | } |
| 331 | |
| 332 | void u8g2_draw_mirror_vertical_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 333 | { |
| 334 | u8g2_uint_t yy; |
| 335 | yy = u8g2->height; |
| 336 | yy -= y; |
| 337 | if ( (dir & 1) == 1 ) |
| 338 | { |
| 339 | yy -= len; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | yy--; |
| 344 | } |
| 345 | u8g2_draw_hv_line_2dir(u8g2, x, yy, len, dir); |
| 346 | } |
| 347 | |
| 348 | /* dir = 0 or 1 */ |
| 349 | void u8g2_draw_l90_r1(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 350 | { |
| 351 | u8g2_uint_t xx, yy; |
| 352 | |
| 353 | #ifdef __unix |
| 354 | assert( dir <= 1 ); |
| 355 | #endif |
| 356 | |
| 357 | yy = x; |
| 358 | |
| 359 | xx = u8g2->height; |
| 360 | xx -= y; |
| 361 | xx--; |
| 362 | |
| 363 | dir ++; |
| 364 | if ( dir == 2 ) |
| 365 | { |
| 366 | xx -= len; |
| 367 | xx++; |
| 368 | dir = 0; |
| 369 | } |
| 370 | |
| 371 | u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir); |
| 372 | } |
| 373 | |
| 374 | void u8g2_draw_l90_r2(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 375 | { |
| 376 | u8g2_uint_t xx, yy; |
| 377 | |
| 378 | /* |
| 379 | yy = u8g2->height; |
| 380 | yy -= y; |
| 381 | yy--; |
| 382 | |
| 383 | xx = u8g2->width; |
| 384 | xx -= x; |
| 385 | xx--; |
| 386 | |
| 387 | if ( dir == 0 ) |
| 388 | { |
| 389 | xx -= len; |
| 390 | xx++; |
| 391 | } |
| 392 | else if ( dir == 1 ) |
| 393 | { |
| 394 | yy -= len; |
| 395 | yy++; |
| 396 | } |
| 397 | */ |
| 398 | |
| 399 | yy = u8g2->height; |
| 400 | yy -= y; |
| 401 | |
| 402 | xx = u8g2->width; |
| 403 | xx -= x; |
| 404 | |
| 405 | if ( dir == 0 ) |
| 406 | { |
| 407 | yy--; |
| 408 | xx -= len; |
| 409 | } |
| 410 | else if ( dir == 1 ) |
| 411 | { |
| 412 | xx--; |
| 413 | yy -= len; |
| 414 | } |
| 415 | |
| 416 | u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir); |
| 417 | } |
| 418 | |
| 419 | void u8g2_draw_l90_r3(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 420 | { |
| 421 | u8g2_uint_t xx, yy; |
| 422 | |
| 423 | xx = y; |
| 424 | |
| 425 | yy = u8g2->width; |
| 426 | yy -= x; |
| 427 | |
| 428 | if ( dir == 0 ) |
| 429 | { |
| 430 | yy--; |
| 431 | yy -= len; |
| 432 | yy++; |
| 433 | dir = 1; |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | yy--; |
| 438 | dir = 0; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | |
| 447 | /*============================================*/ |
| 448 | const u8g2_cb_t u8g2_cb_r0 = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_l90_r0 }; |
| 449 | const u8g2_cb_t u8g2_cb_r1 = { u8g2_update_dimension_r1, u8g2_update_page_win_r1, u8g2_draw_l90_r1 }; |
| 450 | const u8g2_cb_t u8g2_cb_r2 = { u8g2_update_dimension_r2, u8g2_update_page_win_r2, u8g2_draw_l90_r2 }; |
| 451 | const u8g2_cb_t u8g2_cb_r3 = { u8g2_update_dimension_r3, u8g2_update_page_win_r3, u8g2_draw_l90_r3 }; |
| 452 | |
| 453 | const u8g2_cb_t u8g2_cb_mirror = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_l90_mirrorr_r0 }; |
| 454 | const u8g2_cb_t u8g2_cb_mirror_vertical = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_mirror_vertical_r0 }; |
| 455 | |
| 456 | /*============================================*/ |
| 457 | /* setup for the null device */ |
| 458 | |
| 459 | /* setup for the null (empty) device */ |
| 460 | void u8g2_Setup_null(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb) |
| 461 | { |
| 462 | static uint8_t buf[8]; |
| 463 | u8g2_SetupDisplay(u8g2, u8x8_d_null_cb, u8x8_cad_empty, byte_cb, gpio_and_delay_cb); |
| 464 | u8g2_SetupBuffer(u8g2, buf, 1, u8g2_ll_hvline_vertical_top_lsb, rotation); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | |
| 469 | |