u8x8_display.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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);
  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. void u8x8_InitDisplay(u8x8_t *u8x8)
  76. {
  77. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_INIT, 0, NULL);
  78. }
  79. void u8x8_SetPowerSave(u8x8_t *u8x8, uint8_t is_enable)
  80. {
  81. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_POWER_SAVE, is_enable, NULL);
  82. }
  83. void u8x8_SetFlipMode(u8x8_t *u8x8, uint8_t mode)
  84. {
  85. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_FLIP_MODE, mode, NULL);
  86. }
  87. void u8x8_SetContrast(u8x8_t *u8x8, uint8_t value)
  88. {
  89. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_CONTRAST, value, NULL);
  90. }
  91. void u8x8_RefreshDisplay(u8x8_t *u8x8)
  92. {
  93. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_REFRESH, 0, NULL);
  94. }
  95. void u8x8_ClearDisplayWithTile(u8x8_t *u8x8, const uint8_t *buf)
  96. {
  97. u8x8_tile_t tile;
  98. uint8_t h;
  99. tile.x_pos = 0;
  100. tile.cnt = 1;
  101. tile.tile_ptr = (uint8_t *)buf; /* tile_ptr should be const, but isn't */
  102. h = u8x8->display_info->tile_height;
  103. tile.y_pos = 0;
  104. do
  105. {
  106. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, u8x8->display_info->tile_width, (void *)&tile);
  107. tile.y_pos++;
  108. } while( tile.y_pos < h );
  109. }
  110. void u8x8_ClearDisplay(u8x8_t *u8x8)
  111. {
  112. uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  113. u8x8_ClearDisplayWithTile(u8x8, buf);
  114. }
  115. void u8x8_FillDisplay(u8x8_t *u8x8)
  116. {
  117. uint8_t buf[8] = { 255, 255, 255, 255, 255, 255, 255, 255 };
  118. u8x8_ClearDisplayWithTile(u8x8, buf);
  119. }
  120. void u8x8_ClearLine(u8x8_t *u8x8, uint8_t line)
  121. {
  122. uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  123. u8x8_tile_t tile;
  124. if ( line < u8x8->display_info->tile_height )
  125. {
  126. tile.x_pos = 0;
  127. tile.y_pos = line;
  128. tile.cnt = 1;
  129. tile.tile_ptr = (uint8_t *)buf; /* tile_ptr should be const, but isn't */
  130. u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, u8x8->display_info->tile_width, (void *)&tile);
  131. }
  132. }