input value

This commit is contained in:
olikraus 2016-06-13 18:55:59 +02:00
parent 4d0a8765ee
commit 8321059b35
6 changed files with 176 additions and 22 deletions

View File

@ -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
* User interface functions

123
csrc/u8g2_input_value.c Normal file
View File

@ -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;
}

View File

@ -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;

View File

@ -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 )

View File

@ -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

View File

@ -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;
}