From 8321059b35b43653abaa1bb52016568cc58e94d2 Mon Sep 17 00:00:00 2001 From: olikraus Date: Mon, 13 Jun 2016 18:55:59 +0200 Subject: [PATCH] input value --- ChangeLog | 2 +- csrc/u8g2_input_value.c | 123 +++++++++++++++++++++++++++++++ csrc/u8x8_input_value.c | 17 ++--- csrc/u8x8_string.c | 18 ++--- sys/sdl/input_value_8x8/Makefile | 12 +++ sys/sdl/input_value_8x8/main.c | 26 +++++++ 6 files changed, 176 insertions(+), 22 deletions(-) create mode 100644 csrc/u8g2_input_value.c create mode 100644 sys/sdl/input_value_8x8/Makefile create mode 100644 sys/sdl/input_value_8x8/main.c diff --git a/ChangeLog b/ChangeLog index 35e55b48..dde0a0dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,4 +13,4 @@ https://github.com/olikraus/u8g2 ChangeLog 2016-06-12 v2.0.4 olikraus@gmail.com * Support for ST7565 2016-xx-xx v2.0.5 olikraus@gmail.com - \ No newline at end of file + * User interface functions \ No newline at end of file diff --git a/csrc/u8g2_input_value.c b/csrc/u8g2_input_value.c new file mode 100644 index 00000000..d9910d44 --- /dev/null +++ b/csrc/u8g2_input_value.c @@ -0,0 +1,123 @@ +/* + + u8g2_input_value.c + + Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) + + Copyright (c) 2016, olikraus@gmail.com + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list + of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or other + materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "u8g2.h" + +/* + return: + 0: value is not changed (HOME/Break Button pressed) + 1: value has been updated +*/ + +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) +{ + uint8_t height; + uint8_t y; + uint8_t width; + uint8_t x; + uint8_t local_value = *value; + uint8_t r; + uint8_t event; + + /* calculate overall height of the input value box */ + height = 1; /* value input line */ + height += u8x8_GetStringLineCnt(title); + + /* calculate offset from top */ + y = 0; + if ( height < u8g2_GetDisplayHeight(u8g2) ) + { + y = u8g2_GetDisplayHeight(u8g2); + y -= height; + y /= 2; + } + + /* calculate offset from left for the label */ + x = 0; + width = u8x8_GetUTF8Len(u8x8, pre); + width += digits; + width += u8x8_GetUTF8Len(u8x8, post); + if ( width < u8x8_GetCols(u8x8) ) + { + x = u8x8_GetCols(u8x8); + x -= width; + x /= 2; + } + + /* render */ + u8x8_ClearDisplay(u8x8); /* required, because not everything is filled */ + u8x8_SetInverseFont(u8x8, 0); + y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title); + x += u8x8_DrawUTF8(u8x8, x, y, pre); + u8x8_DrawUTF8(u8x8, x+digits, y, post); + u8x8_SetInverseFont(u8x8, 1); + + /* event loop */ + u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); + for(;;) + { + event = u8x8_GetMenuEvent(u8x8); + if ( event == U8X8_MSG_GPIO_MENU_SELECT ) + { + *value = local_value; + r = 1; + break; + } + else if ( event == U8X8_MSG_GPIO_MENU_HOME ) + { + r = 0; + break; + } + else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP ) + { + if ( local_value >= hi ) + local_value = lo; + else + local_value++; + u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); + } + else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN ) + { + if ( local_value <= lo ) + local_value = hi; + else + local_value--; + u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); + } + } + + u8x8_SetInverseFont(u8x8, 0); + return r; +} diff --git a/csrc/u8x8_input_value.c b/csrc/u8x8_input_value.c index 09c78491..c75e3c7f 100644 --- a/csrc/u8x8_input_value.c +++ b/csrc/u8x8_input_value.c @@ -69,16 +69,11 @@ uint8_t u8x8_UserInterfaceInputValue(u8x8_t *u8x8, const char *title, const char width = u8x8_GetUTF8Len(u8x8, pre); width += digits; width += u8x8_GetUTF8Len(u8x8, post); - if ( *pre == '\t' ) + if ( width < u8x8_GetCols(u8x8) ) { - width--; - pre++; - if ( width < u8x8_GetCols(u8x8) ) - { - x = u8x8_GetCols(u8x8); - x -= width; - x /= 2; - } + x = u8x8_GetCols(u8x8); + x -= width; + x /= 2; } /* render */ @@ -105,7 +100,7 @@ uint8_t u8x8_UserInterfaceInputValue(u8x8_t *u8x8, const char *title, const char r = 0; break; } - else if ( event == U8X8_MSG_GPIO_MENU_NEXT ) + else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP ) { if ( local_value >= hi ) local_value = lo; @@ -113,7 +108,7 @@ uint8_t u8x8_UserInterfaceInputValue(u8x8_t *u8x8, const char *title, const char local_value++; u8x8_DrawUTF8(u8x8, x, y, u8x8_u8toa(local_value, digits)); } - else if ( event == U8X8_MSG_GPIO_MENU_PREV ) + else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN ) { if ( local_value <= lo ) local_value = hi; diff --git a/csrc/u8x8_string.c b/csrc/u8x8_string.c index a0ebbf70..a2b2357e 100644 --- a/csrc/u8x8_string.c +++ b/csrc/u8x8_string.c @@ -112,7 +112,7 @@ void u8x8_CopyStringLine(char *dest, uint8_t line_idx, const char *str) /* Draw a string Extend the string to size "w" - Center the string, if the first char is a '\t' (center with respect to "w") + Center the string within "w" return the size of the string */ @@ -122,17 +122,15 @@ uint8_t u8x8_DrawUTF8Line(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const c uint8_t cx, dx; d = 0; - if ( *s == '\t' ) + + lw = u8x8_GetUTF8Len(u8x8, s); + if ( lw < w ) { - s++; /* skip '\t' */ - lw = u8x8_GetUTF8Len(u8x8, s); - if ( lw < w ) - { - d = w; - d -=lw; - d /= 2; - } + d = w; + d -=lw; + d /= 2; } + cx = x; dx = cx + d; while( cx < dx ) diff --git a/sys/sdl/input_value_8x8/Makefile b/sys/sdl/input_value_8x8/Makefile new file mode 100644 index 00000000..9f409a66 --- /dev/null +++ b/sys/sdl/input_value_8x8/Makefile @@ -0,0 +1,12 @@ +CFLAGS = -g -Wall -I../../../csrc/. `sdl-config --cflags` + +SRC = $(shell ls ../../../csrc/*.c) $(shell ls ../common/*.c ) main.c + +OBJ = $(SRC:.c=.o) + +helloworld: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) `sdl-config --libs` -o u8g2_sdl + +clean: + -rm $(OBJ) u8g2_sdl + diff --git a/sys/sdl/input_value_8x8/main.c b/sys/sdl/input_value_8x8/main.c new file mode 100644 index 00000000..dba762a4 --- /dev/null +++ b/sys/sdl/input_value_8x8/main.c @@ -0,0 +1,26 @@ +/* + Example for the + u8x8_UserInterfaceInputValue + procedure. + +*/ + + +#include "u8x8.h" + +u8x8_t u8x8; + + +int main(void) +{ + uint8_t value = 0; + u8x8_Setup_SDL_128x64(&u8x8); + u8x8_InitDisplay(&u8x8); + + u8x8_SetFont(&u8x8, u8x8_font_amstrad_cpc_extended_f); + + u8x8_UserInterfaceInputValue(&u8x8, "Title\n-----\n", "X=", &value, 0, 19, 2, "m"); + + return 0; +} +