u8x8_display.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. u8x8_display.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. Abstraction layer for the graphics controller.
  27. Main goal is the placement of a 8x8 pixel block (tile) on the display.
  28. */
  29. #include "u8x8.h"
  30. /*==========================================*/
  31. /* internal library function */
  32. /*
  33. this is a helper function for the U8X8_MSG_DISPLAY_SETUP_MEMORY function.
  34. It can be called within the display callback function to carry out the usual standard tasks.
  35. */
  36. void u8x8_d_helper_display_setup_memory(u8x8_t *u8x8, const u8x8_display_info_t *display_info)
  37. {
  38. /* 1) set display info struct */
  39. u8x8->display_info = display_info;
  40. u8x8->x_offset = u8x8->display_info->default_x_offset;
  41. }
  42. /*
  43. this is a helper function for the U8X8_MSG_DISPLAY_INIT function.
  44. It can be called within the display callback function to carry out the usual standard tasks.
  45. */
  46. void u8x8_d_helper_display_init(u8x8_t *u8x8)
  47. {
  48. /* 2) apply port directions to the GPIO lines and apply default values for the IO lines*/
  49. u8x8_gpio_Init(u8x8);
  50. u8x8_cad_Init(u8x8); /* this will also call U8X8_MSG_BYTE_INIT, byte init will NOT call GPIO_INIT */
  51. /* 3) do reset */
  52. u8x8_gpio_SetReset(u8x8, 1);
  53. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, u8x8->display_info->reset_pulse_width_ms);
  54. u8x8_gpio_SetReset(u8x8, 0);
  55. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, u8x8->display_info->reset_pulse_width_ms);
  56. u8x8_gpio_SetReset(u8x8, 1);
  57. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, u8x8->display_info->post_reset_wait_ms);
  58. }
  59. /*==========================================*/
  60. /* official functions */
  61. uint8_t u8x8_DrawTile(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t cnt, uint8_t *tile_ptr)
  62. {
  63. u8x8_tile_t tile;
  64. tile.x_pos = x;
  65. tile.y_pos = y;
  66. tile.cnt = cnt;
  67. tile.tile_ptr = tile_ptr;
  68. return u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, 1, (void *)&tile);
  69. }
  70. /* should be implemented as macro */
  71. void u8x8_SetupMemory(u8x8_t *u8x8)
  72. {
  73. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SETUP_MEMORY, 0, NULL);
  74. }
  75. /*
  76. This will just init the display interface, compared to InitDisplay, it will not issue a reset and also not upload the init code.
  77. Comparison:
  78. Call u8x8_InitInterface u8x8_InitDisplay
  79. Init Interface yes yes
  80. Reset Display no yes
  81. Upload Display Init Code no yes
  82. u8x8_InitInterface() is an alternative function to u8x8_InitDisplay(). Do not call both.
  83. */
  84. void u8x8_InitInterface(u8x8_t *u8x8)
  85. {
  86. u8x8_gpio_Init(u8x8);
  87. u8x8_cad_Init(u8x8); /* this will also call U8X8_MSG_BYTE_INIT, byte init will NOT call GPIO_INIT */
  88. }
  89. /*
  90. This will sent the display init message to the display.
  91. The display itself will then call u8x8_d_helper_display_init() from above. This includes:
  92. GPIO Init (set port directions)
  93. BYTE init (part of CAD init: which may set some levels)
  94. CAD init (which will set things like I2C default address)
  95. Issue a reset to the display: This will usually turn off the display
  96. Additonally each display will set the init code to the display, which will also turn of the display in most cases (Arduino code disable power save mode later)
  97. Actually this procedure should be better called InitInterfaceAndDisplay, because it actually does both.
  98. (actually u8x8_InitInterface() is not called directly but only u8x8_gpio_Init and u8x8_cad_Init which
  99. in turn is called by u8x8_InitInterface())
  100. InitDisplay is called by the Arduino begin() function
  101. In some cases it is not required to init the display (for example if the display is already running, but the controller comes out of deep sleep mode).
  102. Then InitDisplay can be skipped, but u8x8_InitInterface() (== u8x8_gpio_Init() and u8x8_cad_Init()) need to be executed.
  103. */
  104. void u8x8_InitDisplay(u8x8_t *u8x8)
  105. {
  106. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_INIT, 0, NULL); /* this will call u8x8_d_helper_display_init() and send the init seqence to the display */
  107. /* u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_FLIP_MODE, 0, NULL); */ /* It would make sense to call flip mode 0 here after U8X8_MSG_DISPLAY_INIT */
  108. }
  109. void u8x8_SetPowerSave(u8x8_t *u8x8, uint8_t is_enable)
  110. {
  111. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_POWER_SAVE, is_enable, NULL);
  112. }
  113. void u8x8_SetFlipMode(u8x8_t *u8x8, uint8_t mode)
  114. {
  115. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_FLIP_MODE, mode, NULL);
  116. }
  117. void u8x8_SetContrast(u8x8_t *u8x8, uint8_t value)
  118. {
  119. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_CONTRAST, value, NULL);
  120. }
  121. void u8x8_RefreshDisplay(u8x8_t *u8x8)
  122. {
  123. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_REFRESH, 0, NULL);
  124. }
  125. void u8x8_ClearDisplayWithTile(u8x8_t *u8x8, const uint8_t *buf)
  126. {
  127. u8x8_tile_t tile;
  128. uint8_t h;
  129. tile.x_pos = 0;
  130. tile.cnt = 1;
  131. tile.tile_ptr = (uint8_t *)buf; /* tile_ptr should be const, but isn't */
  132. h = u8x8->display_info->tile_height;
  133. tile.y_pos = 0;
  134. do
  135. {
  136. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, u8x8->display_info->tile_width, (void *)&tile);
  137. tile.y_pos++;
  138. } while( tile.y_pos < h );
  139. }
  140. void u8x8_ClearDisplay(u8x8_t *u8x8)
  141. {
  142. uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  143. u8x8_ClearDisplayWithTile(u8x8, buf);
  144. }
  145. void u8x8_FillDisplay(u8x8_t *u8x8)
  146. {
  147. uint8_t buf[8] = { 255, 255, 255, 255, 255, 255, 255, 255 };
  148. u8x8_ClearDisplayWithTile(u8x8, buf);
  149. }
  150. void u8x8_ClearLine(u8x8_t *u8x8, uint8_t line)
  151. {
  152. uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  153. u8x8_tile_t tile;
  154. if ( line < u8x8->display_info->tile_height )
  155. {
  156. tile.x_pos = 0;
  157. tile.y_pos = line;
  158. tile.cnt = 1;
  159. tile.tile_ptr = (uint8_t *)buf; /* tile_ptr should be const, but isn't */
  160. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, u8x8->display_info->tile_width, (void *)&tile);
  161. }
  162. }