u8log.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. u8log.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 <stdint.h>
  28. #include <string.h>
  29. #include "u8x8.h"
  30. /*
  31. static uint8_t u8log_is_on_screen(u8log_t *u8log, uint8_t x, uint8_t y)
  32. {
  33. if ( x >= u8log->width )
  34. return 0;
  35. if ( y >= u8log->height )
  36. return 0;
  37. return 1;
  38. }
  39. */
  40. static void u8log_clear_screen(u8log_t *u8log)
  41. {
  42. uint8_t *dest = u8log->screen_buffer;
  43. uint16_t cnt = u8log->height;
  44. cnt *= u8log->width;
  45. do
  46. {
  47. *dest++ = ' ';
  48. cnt--;
  49. } while( cnt > 0 );
  50. }
  51. /* scroll the content of the complete buffer, set redraw_line to 255 */
  52. static void u8log_scroll_up(u8log_t *u8log)
  53. {
  54. uint8_t *dest = u8log->screen_buffer;
  55. uint8_t *src = dest+u8log->width;
  56. uint16_t cnt = u8log->height;
  57. cnt--;
  58. cnt *= u8log->width;
  59. do
  60. {
  61. *dest++ = *src++;
  62. cnt--;
  63. } while( cnt > 0 );
  64. cnt = u8log->width;
  65. do
  66. {
  67. *dest++ = ' ';
  68. cnt--;
  69. } while(cnt > 0);
  70. if ( u8log->is_redraw_line_for_each_char )
  71. u8log->is_redraw_all = 1;
  72. else
  73. u8log->is_redraw_all_required_for_next_nl = 1;
  74. }
  75. /*
  76. Place the cursor on the screen. This will also scroll, if required
  77. */
  78. static void u8log_cursor_on_screen(u8log_t *u8log)
  79. {
  80. //printf("u8log_cursor_on_screen, cursor_y=%d\n", u8log->cursor_y);
  81. if ( u8log->cursor_x >= u8log->width )
  82. {
  83. u8log->cursor_x = 0;
  84. u8log->cursor_y++;
  85. }
  86. while ( u8log->cursor_y >= u8log->height )
  87. {
  88. u8log_scroll_up(u8log);
  89. u8log->cursor_y--;
  90. }
  91. }
  92. /*
  93. Write a printable, single char on the screen, do any kind of scrolling
  94. */
  95. static void u8log_write_to_screen(u8log_t *u8log, uint8_t c)
  96. {
  97. u8log_cursor_on_screen(u8log);
  98. u8log->screen_buffer[u8log->cursor_y * u8log->width + u8log->cursor_x] = c;
  99. u8log->cursor_x++;
  100. if ( u8log->is_redraw_line_for_each_char )
  101. {
  102. u8log->is_redraw_line = 1;
  103. u8log->redraw_line = u8log->cursor_y;
  104. }
  105. }
  106. /*
  107. Handle control codes or write the char to the screen.
  108. Supported control codes are:
  109. \n 10 Goto first position of the next line. Line is marked for redraw.
  110. \r 13 Goto first position in the same line. Line is marked for redraw.
  111. \t 9 Jump to the next tab position
  112. \f 12 Clear the screen and mark redraw for whole screen
  113. any other char Write char to screen. Line redraw mark depends on
  114. is_redraw_line_for_each_char flag.
  115. */
  116. void u8log_write_char(u8log_t *u8log, uint8_t c)
  117. {
  118. switch(c)
  119. {
  120. case '\n': // 10
  121. u8log->is_redraw_line = 1;
  122. u8log->redraw_line = u8log->cursor_y;
  123. if ( u8log->is_redraw_all_required_for_next_nl )
  124. u8log->is_redraw_all = 1;
  125. u8log->is_redraw_all_required_for_next_nl = 0;
  126. u8log->cursor_y++;
  127. u8log->cursor_x = 0;
  128. break;
  129. case '\r': // 13
  130. u8log->is_redraw_line = 1;
  131. u8log->redraw_line = u8log->cursor_y;
  132. u8log->cursor_x = 0;
  133. break;
  134. case '\t': // 9
  135. u8log->cursor_x = (u8log->cursor_x + 8) & 0xf8;
  136. break;
  137. case '\f': // 12
  138. u8log_clear_screen(u8log);
  139. u8log->is_redraw_all = 1;
  140. u8log->cursor_x = 0;
  141. u8log->cursor_y = 0;
  142. break;
  143. default:
  144. u8log_write_to_screen(u8log, c);
  145. break;
  146. }
  147. }
  148. void u8log_Init(u8log_t *u8log, uint8_t width, uint8_t height, uint8_t *buf)
  149. {
  150. memset(u8log, 0, sizeof(u8log_t));
  151. u8log->width = width;
  152. u8log->height = height;
  153. u8log->screen_buffer = buf;
  154. u8log_clear_screen(u8log);
  155. }
  156. void u8log_SetCallback(u8log_t *u8log, u8log_cb cb, void *aux_data)
  157. {
  158. u8log->cb = cb;
  159. u8log->aux_data = aux_data;
  160. }
  161. void u8log_SetRedrawMode(u8log_t *u8log, uint8_t is_redraw_line_for_each_char)
  162. {
  163. u8log->is_redraw_line_for_each_char = is_redraw_line_for_each_char;
  164. }
  165. /* offset can be negative or positive, it is 0 by default */
  166. void u8log_SetLineHeightOffset(u8log_t *u8log, int8_t line_height_offset)
  167. {
  168. u8log->line_height_offset = line_height_offset;
  169. }
  170. void u8log_WriteChar(u8log_t *u8log, uint8_t c)
  171. {
  172. u8log_write_char(u8log, c);
  173. if ( u8log->is_redraw_line || u8log->is_redraw_all )
  174. {
  175. if ( u8log->cb != 0 )
  176. {
  177. u8log->cb(u8log);
  178. }
  179. u8log->is_redraw_line = 0;
  180. u8log->is_redraw_all = 0;
  181. }
  182. }
  183. void u8log_WriteString(u8log_t *u8log, const char *s)
  184. {
  185. while( *s != '\0' )
  186. {
  187. u8log_WriteChar(u8log, *s);
  188. s++;
  189. }
  190. }
  191. static void u8log_WriteHexHalfByte(u8log_t *u8log, uint8_t b) U8X8_NOINLINE;
  192. static void u8log_WriteHexHalfByte(u8log_t *u8log, uint8_t b)
  193. {
  194. b &= 0x0f;
  195. if ( b < 10 )
  196. u8log_WriteChar(u8log, b+'0');
  197. else
  198. u8log_WriteChar(u8log, b+'a'-10);
  199. }
  200. void u8log_WriteHex8(u8log_t *u8log, uint8_t b)
  201. {
  202. u8log_WriteHexHalfByte(u8log, b >> 4);
  203. u8log_WriteHexHalfByte(u8log, b);
  204. }
  205. void u8log_WriteHex16(u8log_t *u8log, uint16_t v)
  206. {
  207. u8log_WriteHex8(u8log, v>>8);
  208. u8log_WriteHex8(u8log, v);
  209. }
  210. void u8log_WriteHex32(u8log_t *u8log, uint32_t v)
  211. {
  212. u8log_WriteHex16(u8log, v>>16);
  213. u8log_WriteHex16(u8log, v);
  214. }
  215. /* v = value, d = number of digits (1..3) */
  216. void u8log_WriteDec8(u8log_t *u8log, uint8_t v, uint8_t d)
  217. {
  218. u8log_WriteString(u8log, u8x8_u8toa(v, d));
  219. }
  220. /* v = value, d = number of digits (1..5) */
  221. void u8log_WriteDec16(u8log_t *u8log, uint16_t v, uint8_t d)
  222. {
  223. u8log_WriteString(u8log, u8x8_u16toa(v, d));
  224. }