u8g2_setup.c 11 KB

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