forked from olikraus/u8g2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu8g2_input_value.c
150 lines (125 loc) · 4.06 KB
/
u8g2_input_value.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
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 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)
{
uint8_t line_height;
uint8_t height;
u8g2_uint_t pixel_height;
u8g2_uint_t y, yy;
u8g2_uint_t pixel_width;
u8g2_uint_t x, xx;
uint8_t local_value = *value;
//uint8_t r; /* not used ??? */
uint8_t event;
/* only horizontal strings are supported, so force this here */
u8g2_SetFontDirection(u8g2, 0);
/* force baseline position */
u8g2_SetFontPosBaseline(u8g2);
/* calculate line height */
line_height = u8g2_GetAscent(u8g2);
line_height -= u8g2_GetDescent(u8g2);
/* calculate overall height of the input value box */
height = 1; /* value input line */
height += u8x8_GetStringLineCnt(title);
/* calculate the height in pixel */
pixel_height = height;
pixel_height *= line_height;
/* calculate offset from top */
y = 0;
if ( pixel_height < u8g2_GetDisplayHeight(u8g2) )
{
y = u8g2_GetDisplayHeight(u8g2);
y -= pixel_height;
y /= 2;
}
/* calculate offset from left for the label */
x = 0;
pixel_width = u8g2_GetUTF8Width(u8g2, pre);
pixel_width += u8g2_GetUTF8Width(u8g2, "0") * digits;
pixel_width += u8g2_GetUTF8Width(u8g2, post);
if ( pixel_width < u8g2_GetDisplayWidth(u8g2) )
{
x = u8g2_GetDisplayWidth(u8g2);
x -= pixel_width;
x /= 2;
}
/* event loop */
for(;;)
{
u8g2_FirstPage(u8g2);
do
{
/* render */
yy = y;
yy += u8g2_DrawUTF8Lines(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), line_height, title);
xx = x;
xx += u8g2_DrawUTF8(u8g2, xx, yy, pre);
xx += u8g2_DrawUTF8(u8g2, xx, yy, u8x8_u8toa(local_value, digits));
u8g2_DrawUTF8(u8g2, xx, yy, post);
} while( u8g2_NextPage(u8g2) );
#ifdef U8G2_REF_MAN_PIC
return 0;
#endif
for(;;)
{
event = u8x8_GetMenuEvent(u8g2_GetU8x8(u8g2));
if ( event == U8X8_MSG_GPIO_MENU_SELECT )
{
*value = local_value;
return 1;
}
else if ( event == U8X8_MSG_GPIO_MENU_HOME )
{
return 0;
}
else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP )
{
if ( local_value >= hi )
local_value = lo;
else
local_value++;
break;
}
else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN )
{
if ( local_value <= lo )
local_value = hi;
else
local_value--;
break;
}
}
}
/* never reached */
//return r;
}