u8log_u8g2.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. u8log_u8g2.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2018, 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. /*
  29. Draw the u8log text at the specified x/y position.
  30. x/y position is the reference position of the first char of the first line.
  31. the line height is
  32. u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2) + line_height_offset;
  33. line_height_offset can be set with u8log_SetLineHeightOffset()
  34. Use
  35. u8g2_SetFontRefHeightText(u8g2_t *u8g2);
  36. u8g2_SetFontRefHeightExtendedText(u8g2_t *u8g2);
  37. u8g2_SetFontRefHeightAll(u8g2_t *u8g2);
  38. to change the return values for u8g2_GetAscent and u8g2_GetDescent
  39. */
  40. void u8g2_DrawLog(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8log_t *u8log)
  41. {
  42. u8g2_uint_t disp_x, disp_y;
  43. uint8_t buf_x, buf_y;
  44. uint8_t c;
  45. disp_y = y;
  46. u8g2_SetFontDirection(u8g2, 0);
  47. for( buf_y = 0; buf_y < u8log->height; buf_y++ )
  48. {
  49. disp_x = x;
  50. for( buf_x = 0; buf_x < u8log->width; buf_x++ )
  51. {
  52. c = u8log->screen_buffer[buf_y * u8log->width + buf_x];
  53. disp_x += u8g2_DrawGlyph(u8g2, disp_x, disp_y, c);
  54. }
  55. disp_y += u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2);
  56. disp_y += u8log->line_height_offset;
  57. }
  58. }
  59. /*
  60. u8lib callback for u8g2
  61. Only font direction 0 is supported: u8g2_SetFontDirection(u8g2, 0)
  62. Use
  63. u8g2_SetFontRefHeightText(u8g2_t *u8g2);
  64. u8g2_SetFontRefHeightExtendedText(u8g2_t *u8g2);
  65. u8g2_SetFontRefHeightAll(u8g2_t *u8g2);
  66. to change the top offset and the line height and
  67. u8log_SetLineHeightOffset(u8log_t *u8log, int8_t line_height_offset)
  68. to change the line height.
  69. */
  70. void u8log_u8g2_cb(u8log_t * u8log)
  71. {
  72. u8g2_t *u8g2 = (u8g2_t *)(u8log->aux_data);
  73. if ( u8log->is_redraw_line || u8log->is_redraw_all )
  74. {
  75. u8g2_FirstPage(u8g2);
  76. do
  77. {
  78. u8g2_DrawLog( u8g2, 0, u8g2_GetAscent(u8g2), u8log);
  79. }
  80. while( u8g2_NextPage(u8g2) );
  81. }
  82. }