| 1 | /* |
| 2 | |
| 3 | u8g2_input_value.c |
| 4 | |
| 5 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 6 | |
| 7 | Copyright (c) 2016, olikraus@gmail.com |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without modification, |
| 11 | are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, this list |
| 14 | of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 17 | list of conditions and the following disclaimer in the documentation and/or other |
| 18 | materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 22 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 32 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | |
| 34 | */ |
| 35 | |
| 36 | #include "u8g2.h" |
| 37 | |
| 38 | /* |
| 39 | return: |
| 40 | 0: value is not changed (HOME/Break Button pressed) |
| 41 | 1: value has been updated |
| 42 | */ |
| 43 | |
| 44 | 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) |
| 45 | { |
| 46 | uint8_t line_height; |
| 47 | uint8_t height; |
| 48 | u8g2_uint_t pixel_height; |
| 49 | u8g2_uint_t y, yy; |
| 50 | u8g2_uint_t pixel_width; |
| 51 | u8g2_uint_t x, xx; |
| 52 | |
| 53 | uint8_t local_value = *value; |
| 54 | //uint8_t r; /* not used ??? */ |
| 55 | uint8_t event; |
| 56 | |
| 57 | /* only horizontal strings are supported, so force this here */ |
| 58 | u8g2_SetFontDirection(u8g2, 0); |
| 59 | |
| 60 | /* force baseline position */ |
| 61 | u8g2_SetFontPosBaseline(u8g2); |
| 62 | |
| 63 | /* calculate line height */ |
| 64 | line_height = u8g2_GetAscent(u8g2); |
| 65 | line_height -= u8g2_GetDescent(u8g2); |
| 66 | |
| 67 | |
| 68 | /* calculate overall height of the input value box */ |
| 69 | height = 1; /* value input line */ |
| 70 | height += u8x8_GetStringLineCnt(title); |
| 71 | |
| 72 | /* calculate the height in pixel */ |
| 73 | pixel_height = height; |
| 74 | pixel_height *= line_height; |
| 75 | |
| 76 | |
| 77 | /* calculate offset from top */ |
| 78 | y = 0; |
| 79 | if ( pixel_height < u8g2_GetDisplayHeight(u8g2) ) |
| 80 | { |
| 81 | y = u8g2_GetDisplayHeight(u8g2); |
| 82 | y -= pixel_height; |
| 83 | y /= 2; |
| 84 | } |
| 85 | |
| 86 | /* calculate offset from left for the label */ |
| 87 | x = 0; |
| 88 | pixel_width = u8g2_GetUTF8Width(u8g2, pre); |
| 89 | pixel_width += u8g2_GetUTF8Width(u8g2, "0") * digits; |
| 90 | pixel_width += u8g2_GetUTF8Width(u8g2, post); |
| 91 | if ( pixel_width < u8g2_GetDisplayWidth(u8g2) ) |
| 92 | { |
| 93 | x = u8g2_GetDisplayWidth(u8g2); |
| 94 | x -= pixel_width; |
| 95 | x /= 2; |
| 96 | } |
| 97 | |
| 98 | /* event loop */ |
| 99 | for(;;) |
| 100 | { |
| 101 | u8g2_FirstPage(u8g2); |
| 102 | do |
| 103 | { |
| 104 | /* render */ |
| 105 | yy = y; |
| 106 | yy += u8g2_DrawUTF8Lines(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), line_height, title); |
| 107 | xx = x; |
| 108 | xx += u8g2_DrawUTF8(u8g2, xx, yy, pre); |
| 109 | xx += u8g2_DrawUTF8(u8g2, xx, yy, u8x8_u8toa(local_value, digits)); |
| 110 | u8g2_DrawUTF8(u8g2, xx, yy, post); |
| 111 | } while( u8g2_NextPage(u8g2) ); |
| 112 | |
| 113 | #ifdef U8G2_REF_MAN_PIC |
| 114 | return 0; |
| 115 | #endif |
| 116 | |
| 117 | for(;;) |
| 118 | { |
| 119 | event = u8x8_GetMenuEvent(u8g2_GetU8x8(u8g2)); |
| 120 | if ( event == U8X8_MSG_GPIO_MENU_SELECT ) |
| 121 | { |
| 122 | *value = local_value; |
| 123 | return 1; |
| 124 | } |
| 125 | else if ( event == U8X8_MSG_GPIO_MENU_HOME ) |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP ) |
| 130 | { |
| 131 | if ( local_value >= hi ) |
| 132 | local_value = lo; |
| 133 | else |
| 134 | local_value++; |
| 135 | break; |
| 136 | } |
| 137 | else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN ) |
| 138 | { |
| 139 | if ( local_value <= lo ) |
| 140 | local_value = hi; |
| 141 | else |
| 142 | local_value--; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* never reached */ |
| 149 | //return r; |
| 150 | } |