selection list
This commit is contained in:
parent
b0909d1c49
commit
5ed9b21d52
|
@ -666,7 +666,7 @@ uint16_t u8x8_utf8_next(u8x8_t *u8x8, uint8_t b);
|
|||
void u8x8_SetFont(u8x8_t *u8x8, const uint8_t *font_8x8);
|
||||
void u8x8_DrawGlyph(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding);
|
||||
uint8_t u8x8_DrawString(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s);
|
||||
uint8_t u8x8_DrawUTF8(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s);
|
||||
uint8_t u8x8_DrawUTF8(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s); /* return number of glyps */
|
||||
uint8_t u8x8_GetUTF8Len(u8x8_t *u8x8, const char *s);
|
||||
#define u8x8_SetInverseFont(u8x8, b) (u8x8)->is_font_inverse_mode = (b)
|
||||
|
||||
|
@ -676,6 +676,8 @@ uint8_t u8x8_GetUTF8Len(u8x8_t *u8x8, const char *s);
|
|||
|
||||
uint8_t u8x8_GetStringLineCnt(const char *str);
|
||||
const char *u8x8_GetStringLine(uint8_t line_idx, const char *str );
|
||||
uint8_t u8x8_DrawUTF8Line(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const char *s);
|
||||
uint8_t u8x8_DrawUTF8Lines(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const char *s);
|
||||
|
||||
|
||||
/*==========================================*/
|
||||
|
@ -730,12 +732,15 @@ struct _u8sl_struct
|
|||
uint8_t total; /* total number of elements in the menu */
|
||||
uint8_t first_pos; /* position of the first visible line */
|
||||
uint8_t current_pos; /* current cursor position, starts at 0 */
|
||||
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
};
|
||||
typedef struct _u8sl_struct u8sl_t;
|
||||
|
||||
typedef void (*u8x8_sl_cb)(u8x8_t *u8x8, u8sl_t *u8sl, uint8_t idx, const void *aux);
|
||||
|
||||
uint8_t u8x8_UserInterfaceSelectionList(u8x8_t *u8x8, uint8_t start_pos, const char *sl);
|
||||
uint8_t u8x8_UserInterfaceSelectionList(u8x8_t *u8x8, const char *title, uint8_t start_pos, const char *sl);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
|
||||
u8x8_message.h
|
||||
|
||||
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 "u8x8.h"
|
||||
|
||||
void u8x8_draw_button(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t is_highlight, const char *s)
|
||||
{
|
||||
if ( is_highlight )
|
||||
u8x8_SetInverseFont(u8x8, 1);
|
||||
else
|
||||
u8x8_SetInverseFont(u8x8, 0);
|
||||
u8x8_DrawUTF8(u8x8, x, y, s);
|
||||
}
|
||||
|
||||
void u8x8_draw_button_line(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, uint8_t cursor, const char *s)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
|
||||
u8x8_sl.h
|
||||
u8x8_selection_list.h
|
||||
|
||||
selection list with scroll option
|
||||
|
||||
|
@ -87,12 +87,9 @@ void u8x8_DrawSelectionList(u8x8_t *u8x8, u8sl_t *u8sl, u8x8_sl_cb sl_cb, const
|
|||
void u8x8_sl_string_line_cb(u8x8_t *u8x8, u8sl_t *u8sl, uint8_t idx, const void *aux)
|
||||
{
|
||||
const char *s;
|
||||
uint8_t col;
|
||||
uint8_t row;
|
||||
/* calculate offset from display upper border */
|
||||
row = u8x8_GetRows(u8x8);
|
||||
row -= u8sl->visible;
|
||||
row /= 2;
|
||||
row = u8sl->y;
|
||||
|
||||
/* calculate target pos */
|
||||
row += idx;
|
||||
|
@ -110,29 +107,42 @@ void u8x8_sl_string_line_cb(u8x8_t *u8x8, u8sl_t *u8sl, uint8_t idx, const void
|
|||
/* draw the line */
|
||||
if ( s == NULL )
|
||||
s = "";
|
||||
col = u8x8_DrawUTF8(u8x8, 0, row, s);
|
||||
|
||||
/* fill up the line with some more spaced */
|
||||
while( col < u8x8_GetCols(u8x8) )
|
||||
u8x8_DrawUTF8(u8x8, col++, row, " ");
|
||||
u8x8_DrawUTF8Line(u8x8, u8sl->x, row, u8x8_GetCols(u8x8), s);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
title: NULL for no title, valid str for title line. Can contain mutliple lines, separated by '\n'
|
||||
start_pos: default position for the cursor
|
||||
sl: string list (list of strings separated by \n)
|
||||
returns start_pos if user has pressed the home key
|
||||
returns the selected line if user has pressed the select key
|
||||
*/
|
||||
uint8_t u8x8_UserInterfaceSelectionList(u8x8_t *u8x8, uint8_t start_pos, const char *sl)
|
||||
uint8_t u8x8_UserInterfaceSelectionList(u8x8_t *u8x8, const char *title, uint8_t start_pos, const char *sl)
|
||||
{
|
||||
u8sl_t u8sl;
|
||||
uint8_t event;
|
||||
uint8_t title_lines;
|
||||
|
||||
u8sl.visible = u8x8_GetRows(u8x8);
|
||||
u8sl.total = u8x8_GetStringLineCnt(sl);
|
||||
u8sl.first_pos = 0;
|
||||
u8sl.current_pos = start_pos;
|
||||
u8sl.x = 0;
|
||||
u8sl.y = 0;
|
||||
|
||||
//u8x8_ClearDisplay(u8x8); /* not required because all is 100% filled */
|
||||
|
||||
if ( title != NULL )
|
||||
{
|
||||
title_lines = u8x8_DrawUTF8Lines(u8x8, u8sl.x, u8sl.y, u8x8_GetCols(u8x8), title);
|
||||
u8sl.y+=title_lines;
|
||||
u8sl.visible-=title_lines;
|
||||
}
|
||||
|
||||
if ( u8sl.current_pos >= u8sl.total )
|
||||
u8sl.current_pos = u8sl.total-1;
|
||||
|
||||
|
||||
u8x8_DrawSelectionList(u8x8, &u8sl, u8x8_sl_string_line_cb, sl);
|
||||
|
||||
for(;;)
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
u8x8_string.c
|
||||
|
||||
font procedures, directly interfaces display procedures
|
||||
string line procedures
|
||||
|
||||
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
|
||||
|
||||
|
@ -41,6 +41,8 @@ uint8_t u8x8_GetStringLineCnt(const char *str)
|
|||
{
|
||||
char e;
|
||||
uint8_t line_cnt = 1;
|
||||
if ( str == NULL )
|
||||
return 0;
|
||||
for(;;)
|
||||
{
|
||||
e = *str;
|
||||
|
@ -86,3 +88,64 @@ const char *u8x8_GetStringLine(uint8_t line_idx, const char *str )
|
|||
return NULL; /* line not found */
|
||||
}
|
||||
|
||||
/*
|
||||
Draw a string
|
||||
Extend the string to size "w"
|
||||
Center the string, if the first char is a '\t' (center with respect to "w")
|
||||
return the size of the string
|
||||
|
||||
*/
|
||||
uint8_t u8x8_DrawUTF8Line(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const char *s)
|
||||
{
|
||||
uint8_t d, lw;
|
||||
uint8_t cx, dx;
|
||||
|
||||
|
||||
d = 0;
|
||||
if ( *s == '\t' )
|
||||
{
|
||||
s++; /* skip '\t' */
|
||||
lw = u8x8_GetUTF8Len(u8x8, s);
|
||||
if ( lw < w )
|
||||
{
|
||||
d = w;
|
||||
d -=lw;
|
||||
d /= 2;
|
||||
}
|
||||
}
|
||||
cx = x;
|
||||
dx = cx + d;
|
||||
while( cx < dx )
|
||||
{
|
||||
u8x8_DrawUTF8(u8x8, cx, y, " ");
|
||||
cx++;
|
||||
}
|
||||
cx += u8x8_DrawUTF8(u8x8, cx, y, s);
|
||||
dx = x + w;
|
||||
while( cx < dx )
|
||||
{
|
||||
u8x8_DrawUTF8(u8x8, cx, y, " ");
|
||||
cx++;
|
||||
}
|
||||
cx -= x;
|
||||
return cx;
|
||||
}
|
||||
|
||||
/*
|
||||
draw several lines at position x,y.
|
||||
lines are stored in s and must be separated with '\n'.
|
||||
lines can be centered with respect to "w" if the first char in the line is a '\t'
|
||||
returns the number of lines in s
|
||||
*/
|
||||
uint8_t u8x8_DrawUTF8Lines(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const char *s)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t cnt;
|
||||
cnt = u8x8_GetStringLineCnt(s);
|
||||
for( i = 0; i < cnt; i++ )
|
||||
{
|
||||
u8x8_DrawUTF8Line(u8x8, x, y, w, u8x8_GetStringLine(i, s));
|
||||
y++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
|
|
@ -57,8 +57,8 @@
|
|||
//U8X8_SSD1306_128X64_NONAME_8080 u8x8(13, 11, 2, 3, 4, 5, 6, A4, /*enable=*/ 7, /*cs=*/ 10, /*dc=*/ 9, /*reset=*/ 8);
|
||||
//U8X8_ST7920_192X32_8080 u8x8(8, 9, 10, 11, 4, 5, 6, 7, /*enable=*/ 18, /*cs=*/ U8X8_PIN_NONE, /*dc=*/ 17, /*reset=*/ U8X8_PIN_NONE);
|
||||
//U8X8_ST7920_192X32_6800 u8x8(8, 9, 10, 11, 4, 5, 6, 7, /*enable=*/ 18, /*cs=*/ U8X8_PIN_NONE, /*dc=*/ 17, /*reset=*/ U8X8_PIN_NONE);
|
||||
//U8X8_UC1701_DOGS102_4W_SW_SPI u8x8(/* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
|
||||
U8X8_UC1701_DOGS102_4W_HW_SPI u8x8(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
|
||||
U8X8_UC1701_DOGS102_4W_SW_SPI u8x8(/* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
|
||||
//U8X8_UC1701_DOGS102_4W_HW_SPI u8x8(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
|
||||
|
||||
|
||||
|
||||
|
@ -70,12 +70,27 @@ void setup(void) {
|
|||
u8x8.setFont(u8x8_font_chroma48medium8_r);
|
||||
}
|
||||
|
||||
const char *string_list = "000\n111\n222\n333\n444\n555\n666\n777\n888\n999";
|
||||
const char *string_list =
|
||||
"\tAltocumulus\n"
|
||||
"\tAltostratus\n"
|
||||
"\tCirrocumulus\n"
|
||||
"\tCirrostratus\n"
|
||||
"\tCirrus\n"
|
||||
"\tCumulonimbus\n"
|
||||
"\tCumulus\n"
|
||||
"\tNimbostratus\n"
|
||||
"\tStratocumulus\n"
|
||||
"\tStratus";
|
||||
|
||||
uint8_t current_selection = 0;
|
||||
|
||||
|
||||
|
||||
void loop(void) {
|
||||
current_selection = u8x8_UserInterfaceSelectionList(u8x8.getU8x8(), current_selection, string_list);
|
||||
current_selection = u8x8_UserInterfaceSelectionList(
|
||||
u8x8.getU8x8(),
|
||||
"\tCloud Types\n\t-----------",
|
||||
current_selection,
|
||||
string_list);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue