| 1 | /* |
| 2 | |
| 3 | u8x8_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 "u8x8.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 u8x8_UserInterfaceInputValue(u8x8_t *u8x8, 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 height; |
| 47 | uint8_t y; |
| 48 | uint8_t width; |
| 49 | uint8_t x; |
| 50 | uint8_t local_value = *value; |
| 51 | uint8_t r; |
| 52 | uint8_t event; |
| 53 | |
| 54 | /* calculate overall height of the input value box */ |
| 55 | height = 1; /* button line */ |
| 56 | height += u8x8_GetStringLineCnt(title); |
| 57 | |
| 58 | /* calculate offset from top */ |
| 59 | y = 0; |
| 60 | if ( height < u8x8_GetRows(u8x8) ) |
| 61 | { |
| 62 | y = u8x8_GetRows(u8x8); |
| 63 | y -= height; |
| 64 | y /= 2; |
| 65 | } |
| 66 | |
| 67 | /* calculate offset from left for the label */ |
| 68 | x = 0; |
| 69 | width = u8x8_GetUTF8Len(u8x8, pre); |
| 70 | width += digits; |
| 71 | width += u8x8_GetUTF8Len(u8x8, post); |
| 72 | if ( width < u8x8_GetCols(u8x8) ) |
| 73 | { |
| 74 | x = u8x8_GetCols(u8x8); |
| 75 | x -= width; |
| 76 | x /= 2; |
| 77 | } |
| 78 | |
| 79 | /* render */ |
| 80 | u8x8_ClearDisplay(u8x8); /* required, because not everything is filled */ |
| 81 | u8x8_SetInverseFont(u8x8, 0); |
| 82 | y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title); |
| 83 | x += u8x8_DrawUTF8(u8x8, x, y, pre); |
| 84 | u8x8_DrawUTF8(u8x8, x+digits, y, post); |
| 85 | u8x8_SetInverseFont(u8x8, 1); |
| 86 | |
| 87 | /* event loop */ |
| 88 | u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); |
| 89 | for(;;) |
| 90 | { |
| 91 | event = u8x8_GetMenuEvent(u8x8); |
| 92 | if ( event == U8X8_MSG_GPIO_MENU_SELECT ) |
| 93 | { |
| 94 | *value = local_value; |
| 95 | r = 1; |
| 96 | break; |
| 97 | } |
| 98 | else if ( event == U8X8_MSG_GPIO_MENU_HOME ) |
| 99 | { |
| 100 | r = 0; |
| 101 | break; |
| 102 | } |
| 103 | else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP ) |
| 104 | { |
| 105 | if ( local_value >= hi ) |
| 106 | local_value = lo; |
| 107 | else |
| 108 | local_value++; |
| 109 | u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); |
| 110 | } |
| 111 | else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN ) |
| 112 | { |
| 113 | if ( local_value <= lo ) |
| 114 | local_value = hi; |
| 115 | else |
| 116 | local_value--; |
| 117 | u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | u8x8_SetInverseFont(u8x8, 0); |
| 122 | return r; |
| 123 | } |