u8g2_input_value.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. u8g2_input_value.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 "u8g2.h"
  28. /*
  29. return:
  30. 0: value is not changed (HOME/Break Button pressed)
  31. 1: value has been updated
  32. */
  33. uint8_t u8g2_UserInterfaceInputValue(u8g2_t *u8g2, const char *title, const char *pre, uint8_t *value, uint8_t lo, uint8_t hi, uint8_t digits, const char *post)
  34. {
  35. uint8_t line_height;
  36. uint8_t height;
  37. u8g2_uint_t pixel_height;
  38. u8g2_uint_t y, yy;
  39. u8g2_uint_t pixel_width;
  40. u8g2_uint_t x, xx;
  41. uint8_t local_value = *value;
  42. //uint8_t r; /* not used ??? */
  43. uint8_t event;
  44. /* only horizontal strings are supported, so force this here */
  45. u8g2_SetFontDirection(u8g2, 0);
  46. /* force baseline position */
  47. u8g2_SetFontPosBaseline(u8g2);
  48. /* calculate line height */
  49. line_height = u8g2_GetAscent(u8g2);
  50. line_height -= u8g2_GetDescent(u8g2);
  51. /* calculate overall height of the input value box */
  52. height = 1; /* value input line */
  53. height += u8x8_GetStringLineCnt(title);
  54. /* calculate the height in pixel */
  55. pixel_height = height;
  56. pixel_height *= line_height;
  57. /* calculate offset from top */
  58. y = 0;
  59. if ( pixel_height < u8g2_GetDisplayHeight(u8g2) )
  60. {
  61. y = u8g2_GetDisplayHeight(u8g2);
  62. y -= pixel_height;
  63. y /= 2;
  64. }
  65. /* calculate offset from left for the label */
  66. x = 0;
  67. pixel_width = u8g2_GetUTF8Width(u8g2, pre);
  68. pixel_width += u8g2_GetUTF8Width(u8g2, "0") * digits;
  69. pixel_width += u8g2_GetUTF8Width(u8g2, post);
  70. if ( pixel_width < u8g2_GetDisplayWidth(u8g2) )
  71. {
  72. x = u8g2_GetDisplayWidth(u8g2);
  73. x -= pixel_width;
  74. x /= 2;
  75. }
  76. /* event loop */
  77. for(;;)
  78. {
  79. u8g2_FirstPage(u8g2);
  80. do
  81. {
  82. /* render */
  83. yy = y;
  84. yy += u8g2_DrawUTF8Lines(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), line_height, title);
  85. xx = x;
  86. xx += u8g2_DrawUTF8(u8g2, xx, yy, pre);
  87. xx += u8g2_DrawUTF8(u8g2, xx, yy, u8x8_u8toa(local_value, digits));
  88. u8g2_DrawUTF8(u8g2, xx, yy, post);
  89. } while( u8g2_NextPage(u8g2) );
  90. #ifdef U8G2_REF_MAN_PIC
  91. return 0;
  92. #endif
  93. for(;;)
  94. {
  95. event = u8x8_GetMenuEvent(u8g2_GetU8x8(u8g2));
  96. if ( event == U8X8_MSG_GPIO_MENU_SELECT )
  97. {
  98. *value = local_value;
  99. return 1;
  100. }
  101. else if ( event == U8X8_MSG_GPIO_MENU_HOME )
  102. {
  103. return 0;
  104. }
  105. else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP )
  106. {
  107. if ( local_value >= hi )
  108. local_value = lo;
  109. else
  110. local_value++;
  111. break;
  112. }
  113. else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN )
  114. {
  115. if ( local_value <= lo )
  116. local_value = hi;
  117. else
  118. local_value--;
  119. break;
  120. }
  121. }
  122. }
  123. /* never reached */
  124. //return r;
  125. }