u8g2_buffer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. u8g2_buffer.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. /*============================================*/
  30. void u8g2_ClearBuffer(u8g2_t *u8g2)
  31. {
  32. size_t cnt;
  33. cnt = u8g2_GetU8x8(u8g2)->display_info->tile_width;
  34. cnt *= u8g2->tile_buf_height;
  35. cnt *= 8;
  36. memset(u8g2->tile_buf_ptr, 0, cnt);
  37. }
  38. /*============================================*/
  39. static void u8g2_send_tile_row(u8g2_t *u8g2, uint8_t src_tile_row, uint8_t dest_tile_row)
  40. {
  41. uint8_t *ptr;
  42. uint16_t offset;
  43. uint8_t w;
  44. w = u8g2_GetU8x8(u8g2)->display_info->tile_width;
  45. offset = src_tile_row;
  46. ptr = u8g2->tile_buf_ptr;
  47. offset *= w;
  48. offset *= 8;
  49. ptr += offset;
  50. u8x8_DrawTile(u8g2_GetU8x8(u8g2), 0, dest_tile_row, w, ptr);
  51. }
  52. /*
  53. write the buffer to the display RAM.
  54. For most displays, this will make the content visible to the user.
  55. Some displays (like the SSD1606) require a u8x8_RefreshDisplay()
  56. */
  57. static void u8g2_send_buffer(u8g2_t *u8g2) U8X8_NOINLINE;
  58. static void u8g2_send_buffer(u8g2_t *u8g2)
  59. {
  60. uint8_t src_row;
  61. uint8_t src_max;
  62. uint8_t dest_row;
  63. uint8_t dest_max;
  64. src_row = 0;
  65. src_max = u8g2->tile_buf_height;
  66. dest_row = u8g2->tile_curr_row;
  67. dest_max = u8g2_GetU8x8(u8g2)->display_info->tile_height;
  68. do
  69. {
  70. u8g2_send_tile_row(u8g2, src_row, dest_row);
  71. src_row++;
  72. dest_row++;
  73. } while( src_row < src_max && dest_row < dest_max );
  74. }
  75. /* same as u8g2_send_buffer but also send the DISPLAY_REFRESH message (used by SSD1606) */
  76. void u8g2_SendBuffer(u8g2_t *u8g2)
  77. {
  78. u8g2_send_buffer(u8g2);
  79. u8x8_RefreshDisplay( u8g2_GetU8x8(u8g2) );
  80. }
  81. /*============================================*/
  82. void u8g2_SetBufferCurrTileRow(u8g2_t *u8g2, uint8_t row)
  83. {
  84. u8g2->tile_curr_row = row;
  85. u8g2->cb->update_dimension(u8g2);
  86. u8g2->cb->update_page_win(u8g2);
  87. }
  88. void u8g2_FirstPage(u8g2_t *u8g2)
  89. {
  90. if ( u8g2->is_auto_page_clear )
  91. {
  92. u8g2_ClearBuffer(u8g2);
  93. }
  94. u8g2_SetBufferCurrTileRow(u8g2, 0);
  95. }
  96. uint8_t u8g2_NextPage(u8g2_t *u8g2)
  97. {
  98. uint8_t row;
  99. u8g2_send_buffer(u8g2);
  100. row = u8g2->tile_curr_row;
  101. row += u8g2->tile_buf_height;
  102. if ( row >= u8g2_GetU8x8(u8g2)->display_info->tile_height )
  103. {
  104. u8x8_RefreshDisplay( u8g2_GetU8x8(u8g2) );
  105. return 0;
  106. }
  107. if ( u8g2->is_auto_page_clear )
  108. {
  109. u8g2_ClearBuffer(u8g2);
  110. }
  111. u8g2_SetBufferCurrTileRow(u8g2, row);
  112. return 1;
  113. }
  114. /*============================================*/
  115. /*
  116. Description:
  117. Update a sub area of the display, given by tile position, width and height.
  118. The arguments are "tile" coordinates. Any u8g2 rotation is ignored.
  119. This procedure only checks whether full buffer mode is active.
  120. There is no error checking for the arguments: It is the responsibility of the
  121. user to ensure, that the provided arguments are correct.
  122. Limitations:
  123. - Only available in full buffer mode (will not do anything in page mode)
  124. - Tile positions and sizes (pixel position divided by 8)
  125. - Any display rotation/mirror is ignored
  126. - Only works with displays, which support U8x8 API
  127. - Will not send the e-paper refresh message (will probably not work with e-paper devices)
  128. */
  129. void u8g2_UpdateDisplayArea(u8g2_t *u8g2, uint8_t tx, uint8_t ty, uint8_t tw, uint8_t th)
  130. {
  131. uint16_t page_size;
  132. uint8_t *ptr;
  133. /* check, whether we are in full buffer mode */
  134. if ( u8g2->tile_buf_height != u8g2_GetU8x8(u8g2)->display_info->tile_height )
  135. return; /* not in full buffer mode, do nothing */
  136. page_size = u8g2->pixel_buf_width; /* 8*u8g2->u8g2_GetU8x8(u8g2)->display_info->tile_width */
  137. ptr = u8g2_GetBufferPtr(u8g2);
  138. ptr += tx*8;
  139. ptr += page_size*ty;
  140. while( th > 0 )
  141. {
  142. u8x8_DrawTile( u8g2_GetU8x8(u8g2), tx, ty, tw, ptr );
  143. ptr += page_size;
  144. ty++;
  145. th--;
  146. }
  147. }
  148. /* same as sendBuffer, but does not send the ePaper refresh message */
  149. void u8g2_UpdateDisplay(u8g2_t *u8g2)
  150. {
  151. u8g2_send_buffer(u8g2);
  152. }
  153. /*============================================*/
  154. /* vertical_top memory architecture */
  155. void u8g2_WriteBufferPBM(u8g2_t *u8g2, void (*out)(const char *s))
  156. {
  157. u8x8_capture_write_pbm_pre(u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), out);
  158. u8x8_capture_write_pbm_buffer(u8g2_GetBufferPtr(u8g2), u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), u8x8_capture_get_pixel_1, out);
  159. }
  160. void u8g2_WriteBufferXBM(u8g2_t *u8g2, void (*out)(const char *s))
  161. {
  162. u8x8_capture_write_xbm_pre(u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), out);
  163. u8x8_capture_write_xbm_buffer(u8g2_GetBufferPtr(u8g2), u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), u8x8_capture_get_pixel_1, out);
  164. }
  165. /* horizontal right memory architecture */
  166. /* SH1122, LD7032, ST7920, ST7986, LC7981, T6963, SED1330, RA8835, MAX7219, LS0 */
  167. void u8g2_WriteBufferPBM2(u8g2_t *u8g2, void (*out)(const char *s))
  168. {
  169. u8x8_capture_write_pbm_pre(u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), out);
  170. u8x8_capture_write_pbm_buffer(u8g2_GetBufferPtr(u8g2), u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), u8x8_capture_get_pixel_2, out);
  171. }
  172. void u8g2_WriteBufferXBM2(u8g2_t *u8g2, void (*out)(const char *s))
  173. {
  174. u8x8_capture_write_xbm_pre(u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), out);
  175. u8x8_capture_write_xbm_buffer(u8g2_GetBufferPtr(u8g2), u8g2_GetBufferTileWidth(u8g2), u8g2_GetBufferTileHeight(u8g2), u8x8_capture_get_pixel_2, out);
  176. }