u8g2_setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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->draw_color = 1;
  67. u8g2->is_auto_page_clear = 1;
  68. u8g2->cb = u8g2_cb;
  69. u8g2->cb->update_dimension(u8g2);
  70. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  71. u8g2_SetMaxClipWindow(u8g2); /* assign a clip window and call the update() procedure */
  72. #else
  73. u8g2->cb->update_page_win(u8g2);
  74. #endif
  75. u8g2_SetFontPosBaseline(u8g2); /* issue 195 */
  76. #ifdef U8G2_WITH_FONT_ROTATION
  77. u8g2->font_decode.dir = 0;
  78. #endif
  79. }
  80. /*
  81. Usually the display rotation is set initially, but it could be done later also
  82. u8g2_cb can be U8G2_R0..U8G2_R3
  83. */
  84. void u8g2_SetDisplayRotation(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  85. {
  86. u8g2->cb = u8g2_cb;
  87. u8g2->cb->update_dimension(u8g2);
  88. u8g2->cb->update_page_win(u8g2);
  89. }
  90. /*============================================*/
  91. void u8g2_SendF(u8g2_t * u8g2, const char *fmt, ...)
  92. {
  93. va_list va;
  94. va_start(va, fmt);
  95. u8x8_cad_vsendf(u8g2_GetU8x8(u8g2), fmt, va);
  96. va_end(va);
  97. }
  98. /*============================================*/
  99. /*
  100. update dimension:
  101. calculate the following variables:
  102. u8g2_uint_t buf_x0; left corner of the buffer
  103. u8g2_uint_t buf_x1; right corner of the buffer (excluded)
  104. u8g2_uint_t buf_y0;
  105. u8g2_uint_t buf_y1;
  106. */
  107. static void u8g2_update_dimension_common(u8g2_t *u8g2)
  108. {
  109. const u8x8_display_info_t *display_info = u8g2_GetU8x8(u8g2)->display_info;
  110. u8g2_uint_t t;
  111. t = u8g2->tile_buf_height;
  112. t *= 8;
  113. u8g2->pixel_buf_height = t;
  114. t = display_info->tile_width;
  115. #ifndef U8G2_16BIT
  116. if ( t >= 32 )
  117. t = 31;
  118. #endif
  119. t *= 8;
  120. u8g2->pixel_buf_width = t;
  121. t = u8g2->tile_curr_row;
  122. t *= 8;
  123. u8g2->pixel_curr_row = t;
  124. t = u8g2->tile_buf_height;
  125. /* handle the case, where the buffer is larger than the (remaining) part of the display */
  126. if ( t + u8g2->tile_curr_row > display_info->tile_height )
  127. t = display_info->tile_height - u8g2->tile_curr_row;
  128. t *= 8;
  129. u8g2->buf_y0 = u8g2->pixel_curr_row;
  130. u8g2->buf_y1 = u8g2->buf_y0;
  131. u8g2->buf_y1 += t;
  132. #ifdef U8G2_16BIT
  133. u8g2->width = display_info->pixel_width;
  134. u8g2->height = display_info->pixel_height;
  135. #else
  136. u8g2->width = 240;
  137. if ( display_info->pixel_width <= 240 )
  138. u8g2->width = display_info->pixel_width;
  139. u8g2->height = display_info->pixel_height;
  140. #endif
  141. }
  142. /*==========================================================*/
  143. /* apply clip window */
  144. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  145. static void u8g2_apply_clip_window(u8g2_t *u8g2)
  146. {
  147. /* check aganst the current user_??? window */
  148. if ( u8g2_IsIntersection(u8g2, u8g2->clip_x0, u8g2->clip_y0, u8g2->clip_x1, u8g2->clip_y1) == 0 )
  149. {
  150. u8g2->is_page_clip_window_intersection = 0;
  151. }
  152. else
  153. {
  154. u8g2->is_page_clip_window_intersection = 1;
  155. if ( u8g2->user_x0 < u8g2->clip_x0 )
  156. u8g2->user_x0 = u8g2->clip_x0;
  157. if ( u8g2->user_x1 > u8g2->clip_x1 )
  158. u8g2->user_x1 = u8g2->clip_x1;
  159. if ( u8g2->user_y0 < u8g2->clip_y0 )
  160. u8g2->user_y0 = u8g2->clip_y0;
  161. if ( u8g2->user_y1 > u8g2->clip_y1 )
  162. u8g2->user_y1 = u8g2->clip_y1;
  163. }
  164. }
  165. #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */
  166. /*==========================================================*/
  167. void u8g2_update_dimension_r0(u8g2_t *u8g2)
  168. {
  169. u8g2_update_dimension_common(u8g2);
  170. }
  171. void u8g2_update_page_win_r0(u8g2_t *u8g2)
  172. {
  173. u8g2->user_x0 = 0;
  174. u8g2->user_x1 = u8g2->width; /* pixel_buf_width replaced with width */
  175. u8g2->user_y0 = u8g2->buf_y0;
  176. u8g2->user_y1 = u8g2->buf_y1;
  177. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  178. u8g2_apply_clip_window(u8g2);
  179. #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */
  180. }
  181. void u8g2_update_dimension_r1(u8g2_t *u8g2)
  182. {
  183. u8g2_update_dimension_common(u8g2);
  184. u8g2->height = u8g2_GetU8x8(u8g2)->display_info->pixel_width;
  185. u8g2->width = u8g2_GetU8x8(u8g2)->display_info->pixel_height;
  186. }
  187. void u8g2_update_page_win_r1(u8g2_t *u8g2)
  188. {
  189. u8g2->user_x0 = u8g2->buf_y0;
  190. u8g2->user_x1 = u8g2->buf_y1;
  191. u8g2->user_y0 = 0;
  192. u8g2->user_y1 = u8g2->height; /* pixel_buf_width replaced with height (which is the real pixel width) */
  193. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  194. u8g2_apply_clip_window(u8g2);
  195. #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */
  196. }
  197. void u8g2_update_dimension_r2(u8g2_t *u8g2)
  198. {
  199. u8g2_update_dimension_common(u8g2);
  200. }
  201. void u8g2_update_page_win_r2(u8g2_t *u8g2)
  202. {
  203. u8g2->user_x0 = 0;
  204. u8g2->user_x1 = u8g2->width; /* pixel_buf_width replaced with width */
  205. /* there are ases where the height is not a multiple of 8. */
  206. /* in such a case u8g2->buf_y1 might be heigher than u8g2->height */
  207. u8g2->user_y0 = 0;
  208. if ( u8g2->height >= u8g2->buf_y1 )
  209. u8g2->user_y0 = u8g2->height - u8g2->buf_y1;
  210. u8g2->user_y1 = u8g2->height - u8g2->buf_y0;
  211. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  212. u8g2_apply_clip_window(u8g2);
  213. #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */
  214. }
  215. void u8g2_update_dimension_r3(u8g2_t *u8g2)
  216. {
  217. u8g2_update_dimension_common(u8g2);
  218. u8g2->height = u8g2_GetU8x8(u8g2)->display_info->pixel_width;
  219. u8g2->width = u8g2_GetU8x8(u8g2)->display_info->pixel_height;
  220. }
  221. void u8g2_update_page_win_r3(u8g2_t *u8g2)
  222. {
  223. /* there are ases where the height is not a multiple of 8. */
  224. /* in such a case u8g2->buf_y1 might be heigher than u8g2->width */
  225. u8g2->user_x0 = 0;
  226. if ( u8g2->width >= u8g2->buf_y1 )
  227. u8g2->user_x0 = u8g2->width - u8g2->buf_y1;
  228. u8g2->user_x1 = u8g2->width - u8g2->buf_y0;
  229. u8g2->user_y0 = 0;
  230. u8g2->user_y1 = u8g2->height; /* pixel_buf_width replaced with height (pixel_width) */
  231. #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT
  232. u8g2_apply_clip_window(u8g2);
  233. #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */
  234. }
  235. /*============================================*/
  236. 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);
  237. void u8g2_draw_l90_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  238. {
  239. #ifdef __unix
  240. assert( dir <= 1 );
  241. #endif
  242. u8g2_draw_hv_line_2dir(u8g2, x, y, len, dir);
  243. }
  244. void u8g2_draw_l90_mirrorr_r0(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  245. {
  246. u8g2_uint_t xx;
  247. xx = u8g2->width;
  248. xx -= x;
  249. if ( (dir & 1) == 0 )
  250. {
  251. xx -= len;
  252. }
  253. else
  254. {
  255. xx--;
  256. }
  257. u8g2_draw_hv_line_2dir(u8g2, xx, y, len, dir);
  258. }
  259. /* dir = 0 or 1 */
  260. void u8g2_draw_l90_r1(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  261. {
  262. u8g2_uint_t xx, yy;
  263. #ifdef __unix
  264. assert( dir <= 1 );
  265. #endif
  266. yy = x;
  267. xx = u8g2->height;
  268. xx -= y;
  269. xx--;
  270. dir ++;
  271. if ( dir == 2 )
  272. {
  273. xx -= len;
  274. xx++;
  275. dir = 0;
  276. }
  277. u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir);
  278. }
  279. void u8g2_draw_l90_r2(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  280. {
  281. u8g2_uint_t xx, yy;
  282. /*
  283. yy = u8g2->height;
  284. yy -= y;
  285. yy--;
  286. xx = u8g2->width;
  287. xx -= x;
  288. xx--;
  289. if ( dir == 0 )
  290. {
  291. xx -= len;
  292. xx++;
  293. }
  294. else if ( dir == 1 )
  295. {
  296. yy -= len;
  297. yy++;
  298. }
  299. */
  300. yy = u8g2->height;
  301. yy -= y;
  302. xx = u8g2->width;
  303. xx -= x;
  304. if ( dir == 0 )
  305. {
  306. yy--;
  307. xx -= len;
  308. }
  309. else if ( dir == 1 )
  310. {
  311. xx--;
  312. yy -= len;
  313. }
  314. u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir);
  315. }
  316. void u8g2_draw_l90_r3(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  317. {
  318. u8g2_uint_t xx, yy;
  319. xx = y;
  320. yy = u8g2->width;
  321. yy -= x;
  322. if ( dir == 0 )
  323. {
  324. yy--;
  325. yy -= len;
  326. yy++;
  327. dir = 1;
  328. }
  329. else
  330. {
  331. yy--;
  332. dir = 0;
  333. }
  334. u8g2_draw_hv_line_2dir(u8g2, xx, yy, len, dir);
  335. }
  336. /*============================================*/
  337. const u8g2_cb_t u8g2_cb_r0 = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_l90_r0 };
  338. const u8g2_cb_t u8g2_cb_r1 = { u8g2_update_dimension_r1, u8g2_update_page_win_r1, u8g2_draw_l90_r1 };
  339. const u8g2_cb_t u8g2_cb_r2 = { u8g2_update_dimension_r2, u8g2_update_page_win_r2, u8g2_draw_l90_r2 };
  340. const u8g2_cb_t u8g2_cb_r3 = { u8g2_update_dimension_r3, u8g2_update_page_win_r3, u8g2_draw_l90_r3 };
  341. const u8g2_cb_t u8g2_cb_mirror = { u8g2_update_dimension_r0, u8g2_update_page_win_r0, u8g2_draw_l90_mirrorr_r0 };
  342. /*============================================*/
  343. /* setup for the null device */
  344. /* setup for the null (empty) device */
  345. void u8g2_Setup_null(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
  346. {
  347. static uint8_t buf[8];
  348. u8g2_SetupDisplay(u8g2, u8x8_d_null_cb, u8x8_cad_empty, byte_cb, gpio_and_delay_cb);
  349. u8g2_SetupBuffer(u8g2, buf, 1, u8g2_ll_hvline_vertical_top_lsb, rotation);
  350. }