u8x8_selection_list.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. u8x8_selection_list.c
  3. selection list with scroll option
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "u8x8.h"
  29. /*
  30. increase the cursor position
  31. */
  32. void u8sl_Next(u8sl_t *u8sl)
  33. {
  34. u8sl->current_pos++;
  35. if ( u8sl->current_pos >= u8sl->total )
  36. {
  37. u8sl->current_pos = 0;
  38. u8sl->first_pos = 0;
  39. }
  40. else
  41. {
  42. if ( u8sl->first_pos + u8sl->visible <= u8sl->current_pos + 1 )
  43. {
  44. u8sl->first_pos = u8sl->current_pos - u8sl->visible + 1;
  45. }
  46. }
  47. }
  48. void u8sl_Prev(u8sl_t *u8sl)
  49. {
  50. if ( u8sl->current_pos == 0 )
  51. {
  52. u8sl->current_pos = u8sl->total - 1;
  53. u8sl->first_pos = 0;
  54. if ( u8sl->total > u8sl->visible )
  55. u8sl->first_pos = u8sl->total - u8sl->visible;
  56. }
  57. else
  58. {
  59. u8sl->current_pos--;
  60. if ( u8sl->first_pos > u8sl->current_pos )
  61. u8sl->first_pos = u8sl->current_pos;
  62. }
  63. }
  64. void u8x8_DrawSelectionList(u8x8_t *u8x8, u8sl_t *u8sl, u8x8_sl_cb sl_cb, const void *aux)
  65. {
  66. uint8_t i;
  67. for( i = 0; i < u8sl->visible; i++ )
  68. {
  69. sl_cb(u8x8, u8sl, i+u8sl->first_pos, aux);
  70. }
  71. }
  72. /* selection list with string line */
  73. void u8x8_sl_string_line_cb(u8x8_t *u8x8, u8sl_t *u8sl, uint8_t idx, const void *aux)
  74. {
  75. const char *s;
  76. uint8_t row;
  77. /* calculate offset from display upper border */
  78. row = u8sl->y;
  79. /* calculate target pos */
  80. row += idx;
  81. row -= u8sl->first_pos;
  82. /* check whether this is the current cursor line */
  83. if ( idx == u8sl->current_pos )
  84. u8x8_SetInverseFont(u8x8, 1);
  85. else
  86. u8x8_SetInverseFont(u8x8, 0);
  87. /* get the line from the array */
  88. s = u8x8_GetStringLineStart(idx, (const char *)aux);
  89. /* draw the line */
  90. if ( s == NULL )
  91. s = "";
  92. u8x8_DrawUTF8Line(u8x8, u8sl->x, row, u8x8_GetCols(u8x8), s);
  93. u8x8_SetInverseFont(u8x8, 0);
  94. }
  95. /*
  96. title: NULL for no title, valid str for title line. Can contain mutliple lines, separated by '\n'
  97. start_pos: default position for the cursor (starts with 1)
  98. sl: string list (list of strings separated by \n)
  99. returns 0 if user has pressed the home key
  100. returns the selected line+1 if user has pressed the select key (e.g. 1 for the first line)
  101. */
  102. uint8_t u8x8_UserInterfaceSelectionList(u8x8_t *u8x8, const char *title, uint8_t start_pos, const char *sl)
  103. {
  104. u8sl_t u8sl;
  105. uint8_t event;
  106. uint8_t title_lines;
  107. if ( start_pos > 0 )
  108. start_pos--;
  109. u8sl.visible = u8x8_GetRows(u8x8);
  110. u8sl.total = u8x8_GetStringLineCnt(sl);
  111. u8sl.first_pos = 0;
  112. u8sl.current_pos = start_pos;
  113. u8sl.x = 0;
  114. u8sl.y = 0;
  115. //u8x8_ClearDisplay(u8x8); /* not required because all is 100% filled */
  116. u8x8_SetInverseFont(u8x8, 0);
  117. if ( title != NULL )
  118. {
  119. title_lines = u8x8_DrawUTF8Lines(u8x8, u8sl.x, u8sl.y, u8x8_GetCols(u8x8), title);
  120. u8sl.y+=title_lines;
  121. u8sl.visible-=title_lines;
  122. }
  123. if ( u8sl.current_pos >= u8sl.total )
  124. u8sl.current_pos = u8sl.total-1;
  125. u8x8_DrawSelectionList(u8x8, &u8sl, u8x8_sl_string_line_cb, sl);
  126. for(;;)
  127. {
  128. event = u8x8_GetMenuEvent(u8x8);
  129. if ( event == U8X8_MSG_GPIO_MENU_SELECT )
  130. return u8sl.current_pos+1;
  131. else if ( event == U8X8_MSG_GPIO_MENU_HOME )
  132. return 0;
  133. else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_DOWN )
  134. {
  135. u8sl_Next(&u8sl);
  136. u8x8_DrawSelectionList(u8x8, &u8sl, u8x8_sl_string_line_cb, sl);
  137. }
  138. else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_UP )
  139. {
  140. u8sl_Prev(&u8sl);
  141. u8x8_DrawSelectionList(u8x8, &u8sl, u8x8_sl_string_line_cb, sl);
  142. }
  143. }
  144. }