u8x8_message.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. u8x8_message.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. */
  27. #include "u8x8.h"
  28. uint8_t u8x8_draw_button_line(u8x8_t *u8x8, uint8_t y, uint8_t w, uint8_t cursor, const char *s)
  29. {
  30. uint8_t i;
  31. uint8_t cnt;
  32. uint8_t total;
  33. uint8_t d;
  34. uint8_t x;
  35. cnt = u8x8_GetStringLineCnt(s);
  36. /* calculate the width of the button */
  37. total = 0;
  38. for( i = 0; i < cnt; i++ )
  39. {
  40. total += u8x8_GetUTF8Len(u8x8, u8x8_GetStringLineStart(i, s));
  41. }
  42. total += (cnt-1); /* had one space between the buttons */
  43. /* calculate the left offset */
  44. d = 0;
  45. if ( total < w )
  46. {
  47. d = w;
  48. d -= total;
  49. d /= 2;
  50. }
  51. /* draw the buttons */
  52. x = d;
  53. u8x8_SetInverseFont(u8x8, 0);
  54. for( i = 0; i < cnt; i++ )
  55. {
  56. if ( i == cursor )
  57. u8x8_SetInverseFont(u8x8, 1);
  58. x+=u8x8_DrawUTF8(u8x8, x, y, u8x8_GetStringLineStart(i, s));
  59. u8x8_SetInverseFont(u8x8, 0);
  60. x+=u8x8_DrawUTF8(u8x8, x, y, " ");
  61. }
  62. /* return the number of buttons */
  63. return cnt;
  64. }
  65. /*
  66. title1: Multiple lines,separated by '\n'
  67. title2: A single line/string which is terminated by '\0' or '\n' . "title2" accepts the return value from u8x8_GetStringLineStart()
  68. title3: Multiple lines,separated by '\n'
  69. buttons: one more more buttons separated by '\n' and terminated with '\0'
  70. */
  71. uint8_t u8x8_UserInterfaceMessage(u8x8_t *u8x8, const char *title1, const char *title2, const char *title3, const char *buttons)
  72. {
  73. uint8_t height;
  74. uint8_t y;
  75. uint8_t cursor = 0;
  76. uint8_t button_cnt;
  77. uint8_t event;
  78. u8x8_SetInverseFont(u8x8, 0);
  79. /* calculate overall height of the message box */
  80. height = 1; /* button line */
  81. height += u8x8_GetStringLineCnt(title1);
  82. if ( title2 != NULL )
  83. height ++;
  84. height += u8x8_GetStringLineCnt(title3);
  85. /* calculate offset from top */
  86. y = 0;
  87. if ( height < u8x8_GetRows(u8x8) )
  88. {
  89. y = u8x8_GetRows(u8x8);
  90. y -= height;
  91. y /= 2;
  92. }
  93. /* draw message box */
  94. u8x8_ClearDisplay(u8x8); /* required, because not everything is filled */
  95. y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title1);
  96. if ( title2 != NULL )
  97. {
  98. u8x8_DrawUTF8Line(u8x8, 0, y, u8x8_GetCols(u8x8), title2);
  99. y++;
  100. }
  101. y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title3);
  102. button_cnt = u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons);
  103. for(;;)
  104. {
  105. event = u8x8_GetMenuEvent(u8x8);
  106. if ( event == U8X8_MSG_GPIO_MENU_SELECT )
  107. return cursor+1;
  108. else if ( event == U8X8_MSG_GPIO_MENU_HOME )
  109. break;
  110. else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP )
  111. {
  112. cursor++;
  113. if ( cursor >= button_cnt )
  114. cursor = 0;
  115. u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons);
  116. }
  117. else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN )
  118. {
  119. if ( cursor == 0 )
  120. cursor = button_cnt;
  121. cursor--;
  122. u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons);
  123. }
  124. }
  125. return 0;
  126. }