first commit

This commit is contained in:
valeh
2020-12-22 14:30:09 +02:00
commit 26b0ba5954
1832 changed files with 17777948 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
CFLAGS = -g -Wall -I../../../csrc/. `sdl2-config --cflags`
SRC = $(shell ls ../../../csrc/*.c) $(shell ls ../common/*.c ) main.c menu.c gui.c datecalc.c
OBJ = $(SRC:.c=.o)
helloworld: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) `sdl2-config --libs` -o u8g2_sdl
clean:
-rm $(OBJ) u8g2_sdl

View File

@@ -0,0 +1,330 @@
/*
datecalc.c
Written 1996/96 by Oliver Kraus
Published by Heinz Heise Verlag 1997 (c't 15/97)
Completly rewritten and put under GPL 2011 by Oliver Kraus
(c) 2011 by Oliver Kraus (olikraus@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Development goals:
- English version
- Optimized for 8 bit microcontroller
Definitions:
Short Name: y
Long Name: year
Range: 2000...
Short Name: ydn
Long Name: year day number
Range: 1..366
Short Name: cdn
Long Name: century day number
Range: 1...65535
Short Name: ymd
Long Name: Year, Month, Day
Range: 2000...65535, 1..12, 1..31
Conversions
ymd --> y, ydn
get_year_day_number()
y, ydn --> ymd
get_month_by_year_day_number()
get_day_by_year_day_number()
y, ydn --> cdn
to_century_day_number();
cdn --> y, ydn
from_century_day_number();
*/
#include <stdint.h>
/*
Prototype:
uint8_t is_leap_year(uint16_t y)
Description:
Calculate leap year
Arguments:
y year, e.g. 2011 for year 2011
Result:
0 not a leap year
1 leap year
*/
static uint8_t is_leap_year(uint16_t y)
{
if (
((y % 4 == 0) && (y % 100 != 0)) ||
(y % 400 == 0)
)
return 1;
return 0;
}
/*
Prototype:
uint16_t get_year_day_number(uint16_t y, uint8_t m, uint8_t d)
Description:
Calculate the day number within a year. 1st of Jan has the number 1.
"Robertson" Algorithm
Arguments:
y year, e.g. 2011 for year 2011
m month with 1 = january to 12 = december
d day starting with 1
Result:
The "day number" within the year: 1 for the 1st of Jan.
See also:
get_month_by_day_number()
*/
uint16_t get_year_day_number(uint16_t y, uint8_t m, uint8_t d)
{
uint8_t tmp1;
uint16_t tmp2;
tmp1 = 0;
if ( m >= 3 )
tmp1++;
tmp2 = m;
tmp2 +=2;
tmp2 *=611;
tmp2 /= 20;
tmp2 += d;
tmp2 -= 91;
tmp1 <<=1;
tmp2 -= tmp1;
if ( tmp1 != 0 )
tmp2 += is_leap_year(y);
return tmp2;
}
/*
Prototype:
uint8_t get_month_by_year_day_number(uint16_t y, uint16_t ydn)
Description:
Get the month from year and day number within a year.
"R. A. Stone" Algorithm
Arguments:
y year, e.g. 2011 for year 2011
ydn year day number (1st of Jan has the number 1)
Result:
The month within the year: 1 for January.
See also:
get_year_day_number()
*/
static uint16_t corrected_year_day_number(uint16_t y, uint16_t ydn)
{
uint8_t a;
a = is_leap_year(y);
if ( ydn > 59+a )
{
ydn += 2;
ydn -= a;
}
ydn += 91;
return ydn;
}
uint8_t get_month_by_year_day_number(uint16_t y, uint16_t ydn)
{
uint8_t a;
ydn = corrected_year_day_number(y, ydn);
ydn *= 20;
ydn /= 611;
a = ydn;
a -= 2;
return a;
}
/*
Prototype:
uint8_t get_day_by_year_day_number(uint16_t y, uint16_t ydn)
Description:
Get the day within month from year and day number within a year.
"R. A. Stone" Algorithm
Arguments:
y year, e.g. 2011 for year 2011
ydn year day number (1st of Jan has the number 1)
Result:
The day within a month: 1 for the first day of a month.
See also:
get_year_day_number()
*/
uint8_t get_day_by_year_day_number(uint16_t y, uint16_t ydn)
{
uint8_t m;
uint16_t tmp;
m = get_month_by_year_day_number(y, ydn);
m += 2;
ydn = corrected_year_day_number(y, ydn);
tmp = 611;
tmp *= m;
tmp /= 20;
ydn -= tmp;
return ydn;
}
/*
Prototype:
uint8_t get_weekday_by_year_day_number(uint16_t y, uint16_t ydn)
Description:
Get the day within week from year and day number within a year.
"Zeller" Algorithm
Arguments:
y year, e.g. 2011 for year 2011
ydn year day number (1st of Jan has the number 1)
Result:
The day within a week: 0..6 with 0 = Sunday, 1 = Monday, ...
See also:
get_year_day_number()
*/
uint8_t get_weekday_by_year_day_number(uint16_t y, uint16_t ydn)
{
uint8_t j, c, tmp8;
uint16_t tmp16;
y--;
j = y % 100;
c = y / 100;
tmp16 = c;
tmp16 *= 5;
tmp16 += ydn;
tmp8 = j;
j >>= 2;
c >>= 2;
tmp8 += j;
tmp8 += c;
tmp8 += 28;
tmp16 += tmp8;
tmp16 %= 7;
return tmp16;
}
/*
Prototype:
uint16_t to_century_day_number(uint16_t y, uint16_t ydn)
Description:
Calculate days since January, 1st, 2000
Arguments:
y year, e.g. 2011 for year 2011
ydn year day number (1st of Jan has the number 1)
*/
uint16_t to_century_day_number(uint16_t y, uint16_t ydn)
{
uint16_t cdn;
cdn = ydn;
cdn--;
while( y > 2000 )
{
y--;
cdn += 365;
cdn += is_leap_year(y);
}
return cdn;
}
void from_century_day_number(uint16_t cdn, uint16_t *year, uint16_t *ydn)
{
uint16_t y, days_per_year;
y = 2000;
for(;;)
{
days_per_year = 365;
days_per_year += is_leap_year(y);
if ( cdn >= days_per_year )
{
cdn -= days_per_year;
y++;
}
else
break;
}
cdn++;
*year = y;
*ydn = cdn;
}
/*
Calculate the seconds after 2000-01-01 00:00. The largest possible
time is 2136-02-07 06:28:15
*/
uint32_t to_time(uint16_t cdn, uint8_t h, uint8_t m, uint8_t s)
{
uint32_t t;
t = cdn;
t *= 24;
t += h;
t *= 60;
t += m;
t *= 60;
t += s;
return t;
}
void from_time(uint32_t t, uint16_t *cdn, uint8_t *h, uint8_t *m, uint8_t *s)
{
*s = t % 60;
t /= 60;
*m = t % 60;
t /= 60;
*h = t % 24;
t /= 24;
*cdn = t;
}
uint32_t to_sec_since_2000(uint16_t y, uint8_t mo, uint8_t d, uint8_t h, uint8_t mi, uint8_t s)
{
uint16_t ydn = get_year_day_number(y, mo, d);
uint16_t cdn = to_century_day_number(y, ydn);
return to_time(cdn, h, mi, s);
}
/*
Calculate the minutes after 2000-01-01 00:00.
*/
uint32_t to_minutes(uint16_t cdn, uint8_t h, uint8_t m)
{
uint32_t t;
t = cdn;
t *= 24;
t += h;
t *= 60;
t += m;
return t;
}
void from_minutes(uint32_t t, uint16_t *cdn, uint8_t *h, uint8_t *m)
{
*m = t % 60;
t /= 60;
*h = t % 24;
t /= 24;
*cdn = t;
}
uint32_t to_minutes_since_2000(uint16_t y, uint8_t mo, uint8_t d, uint8_t h, uint8_t mi)
{
uint16_t ydn = get_year_day_number(y, mo, d);
uint16_t cdn = to_century_day_number(y, ydn);
return to_minutes(cdn, h, mi);
}

View File

@@ -0,0 +1,82 @@
/*
datecalc.c
Written 1996/96 by Oliver Kraus
Published by Heinz Heise Verlag 1997 (c't 15/97)
Completly rewritten and put under GPL 2011 by Oliver Kraus
(c) 2011 by Oliver Kraus (olikraus@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Development goals:
- English version
- Optimized for 8 bit microcontroller
Definitions:
Short Name: y
Long Name: year
Range: 2000...
Short Name: ydn
Long Name: year day number
Range: 1..366
Short Name: cdn
Long Name: century day number
Range: 1...65535
Short Name: ymd
Long Name: Year, Month, Day
Range: 2000...65535, 1..12, 1..31
Conversions
ymd --> y, ydn
get_year_day_number()
y, ydn --> ymd
get_month_by_year_day_number()
get_day_by_year_day_number()
y, ydn --> cdn
to_century_day_number();
cdn --> y, ydn
from_century_day_number();
*/
#ifndef _DATECALC_H
#define _DATECALC_H
#include <stdint.h>
uint16_t get_year_day_number(uint16_t y, uint8_t m, uint8_t d);
uint8_t get_month_by_year_day_number(uint16_t y, uint16_t ydn);
uint8_t get_day_by_year_day_number(uint16_t y, uint16_t ydn);
uint8_t get_weekday_by_year_day_number(uint16_t y, uint16_t ydn);
uint16_t to_century_day_number(uint16_t y, uint16_t ydn);
void from_century_day_number(uint16_t cdn, uint16_t *year, uint16_t *ydn);
uint32_t to_time(uint16_t cdn, uint8_t h, uint8_t m, uint8_t s);
void from_time(uint32_t t, uint16_t *cdn, uint8_t *h, uint8_t *m, uint8_t *s);
uint32_t to_sec_since_2000(uint16_t y, uint8_t mo, uint8_t d, uint8_t h, uint8_t mi, uint8_t s);
uint32_t to_minutes(uint16_t cdn, uint8_t h, uint8_t m);
void from_minutes(uint32_t t, uint16_t *cdn, uint8_t *h, uint8_t *m);
uint32_t to_minutes_since_2000(uint16_t y, uint8_t mo, uint8_t d, uint8_t h, uint8_t mi);
#endif

View File

@@ -0,0 +1,684 @@
/*
gui.c
*/
#include "gui.h"
#include "datecalc.h"
#include <string.h>
/*============================================*/
extern const me_t melist_display_time[];
extern const me_t melist_top_menu[];
extern const me_t melist_active_alarm_menu[];
extern const me_t melist_setup_menu[];
extern const me_t melist_alarm_menu[];
/*============================================*/
uint8_t gui_alarm_index = 0;
gui_alarm_t gui_alarm_current;
gui_alarm_t gui_alarm_list[GUI_ALARM_CNT];
char gui_alarm_str[GUI_ALARM_CNT][8];
const char weekdaystr[7][4] = {
"Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"
};
gui_data_t gui_data;
menu_t gui_menu;
/*============================================*/
void gui_alarm_calc_next_wd_alarm(uint8_t idx, uint16_t current_week_time_in_minutes)
{
uint8_t i;
uint16_t week_time_abs;
uint16_t week_time_diff; /* difference to current_week_time_in_minutes */
uint16_t best_diff = 0x0ffff;
gui_alarm_list[idx].na_week_time_in_minutes = 0x0ffff; /* not found */
gui_alarm_list[idx].na_minutes_diff = 0x0ffff; /* not found */
gui_alarm_list[idx].na_wd = 7; /* not found */
//printf("gui_alarm_calc_next_wd_alarm: %d\n", idx);
if ( gui_alarm_list[idx].enable != 0 )
{
//printf("gui_alarm_calc_next_wd_alarm: %d enabled\n", idx);
for( i = 0; i < 7; i++ )
{
if ( gui_alarm_list[idx].wd[i] != 0 )
{
//printf("gui_alarm_calc_next_wd_alarm: %d i=%d gui_alarm_list[idx].skip_wd=%d \n", idx, i, gui_alarm_list[idx].skip_wd);
if ( gui_alarm_list[idx].skip_wd != i+1 )
{
week_time_abs = i;
week_time_abs *= 24;
week_time_abs += gui_alarm_list[idx].h;
week_time_abs *= 60;
week_time_abs += gui_alarm_list[idx].m;
week_time_abs += gui_alarm_list[idx].snooze_count*(uint16_t)SNOOZE_MINUTES;
if ( current_week_time_in_minutes <= week_time_abs )
week_time_diff = week_time_abs - current_week_time_in_minutes;
else
week_time_diff = week_time_abs + 7*24*60 - current_week_time_in_minutes;
//printf("gui_alarm_calc_next_wd_alarm: %d week_time_abs=%d current_week_time_in_minutes=%d week_time_diff=%d\n", idx, week_time_abs, current_week_time_in_minutes,week_time_diff);
if ( best_diff > week_time_diff )
{
best_diff = week_time_diff;
/* found for this alarm */
gui_alarm_list[idx].na_minutes_diff = week_time_diff;
gui_alarm_list[idx].na_week_time_in_minutes = week_time_abs;
gui_alarm_list[idx].na_h = gui_alarm_list[idx].h;
gui_alarm_list[idx].na_m = gui_alarm_list[idx].m;
gui_alarm_list[idx].na_wd = i;
}
}
}
}
}
//printf("gui_alarm_calc_next_wd_alarm: %d na_minutes_diff=%d\n", idx, gui_alarm_list[idx].na_minutes_diff);
//printf("gui_alarm_calc_next_wd_alarm: %d na_wd=%d\n", idx, gui_alarm_list[idx].na_wd);
}
void gui_alarm_calc_str_time(uint8_t idx) U8G2_NOINLINE;
void gui_alarm_calc_str_time(uint8_t idx)
{
gui_alarm_str[idx][0] = ' ';
strcpy(gui_alarm_str[idx]+1, u8x8_u8toa(gui_alarm_list[idx].h, 2));
strcpy(gui_alarm_str[idx]+4, u8x8_u8toa(gui_alarm_list[idx].m, 2));
gui_alarm_str[idx][3] = ':';
if ( gui_alarm_list[idx].enable == 0 )
{
gui_alarm_str[idx][0] = '(';
gui_alarm_str[idx][6] = ')';
gui_alarm_str[idx][7] = '\0';
}
}
/* adjust day/month and calculates the weekday */
void gui_date_adjust(void) U8G2_NOINLINE;
void gui_date_adjust(void)
{
uint16_t ydn;
//uint16_t cdn;
uint16_t year;
if ( gui_data.month == 0 )
gui_data.month++;
if ( gui_data.day == 0 )
gui_data.day++;
year = 2000+gui_data.year_t*10 + gui_data.year_o;
ydn = get_year_day_number(year, gui_data.month, gui_data.day);
gui_data.month = get_month_by_year_day_number(year, ydn);
gui_data.day = get_day_by_year_day_number(year, ydn);
gui_data.weekday = get_weekday_by_year_day_number(year, ydn); /* 0 = Sunday */
/* adjust the weekday so that 0 will be Monday */
gui_data.weekday += 6;
if ( gui_data.weekday >= 7 )
gui_data.weekday -= 7;
//cdn = to_century_day_number(y, ydn);
//to_minutes(cdn, h, m);
}
/*
calculate the minute within the week.
this must be called after gui_date_adjust(), because the weekday is used here
*/
void gui_calc_week_time(void)
{
gui_data.week_time = gui_data.weekday;
gui_data.week_time *= 24;
gui_data.week_time += gui_data.h;
gui_data.week_time *= 60;
gui_data.week_time += gui_data.mt * 10 + gui_data.mo;
}
/*
calculate the next alarm.
this must be called after gui_calc_week_time() because, we need week_time
*/
void gui_calc_next_alarm(void)
{
uint8_t i;
uint8_t lowest_i;
uint16_t lowest_diff;
/* step 1: Calculate the difference to current weektime for each alarm */
/* result is stored in gui_alarm_list[i].na_minutes_diff */
for( i = 0; i < GUI_ALARM_CNT; i++ )
gui_alarm_calc_next_wd_alarm(i, gui_data.week_time);
/* step 2: find the index with the lowest difference */
lowest_diff = 0x0ffff;
lowest_i = GUI_ALARM_CNT;
for( i = 0; i < GUI_ALARM_CNT; i++ )
{
if ( lowest_diff > gui_alarm_list[i].na_minutes_diff )
{
lowest_diff = gui_alarm_list[i].na_minutes_diff;
lowest_i = i;
}
}
/* step 3: store the result */
gui_data.next_alarm_index = lowest_i; /* this can be GUI_ALARM_CNT */
//printf("gui_calc_next_alarm gui_data.next_alarm_index=%d\n", gui_data.next_alarm_index);
/* calculate the is_skip_possible and the is_alarm flag */
gui_data.is_skip_possible = 0;
if ( lowest_i < GUI_ALARM_CNT )
{
if ( gui_alarm_list[lowest_i].na_minutes_diff <= 1 )
{
if ( gui_data.is_alarm == 0 )
{
gui_data.is_alarm = 1;
gui_data.active_alarm_idx = lowest_i;
menu_SetMEList(&gui_menu, melist_active_alarm_menu, 0);
}
}
else
{
/* valid next alarm time */
if ( gui_alarm_list[lowest_i].skip_wd == 0 )
{
/* skip flag not yet set */
if ( gui_alarm_list[lowest_i].na_minutes_diff <= (uint16_t)60*(uint16_t)ALLOW_SKIP_HOURS )
{
/* within the limit before alarm */
gui_data.is_skip_possible = 1;
}
}
}
}
}
/* recalculate all internal data */
/* additionally the active alarm menu might be set by this function */
void gui_Recalculate(void)
{
int i;
gui_date_adjust();
gui_calc_week_time();
gui_calc_next_alarm();
for ( i = 0; i < GUI_ALARM_CNT; i++ )
{
gui_alarm_calc_str_time(i);
}
}
void gui_Init(u8g2_t *u8g2)
{
menu_Init(&gui_menu, u8g2);
menu_SetMEList(&gui_menu, melist_display_time, 0);
gui_Recalculate();
}
void gui_Draw(void)
{
menu_Draw(&gui_menu);
}
void gui_Next(void)
{
menu_NextFocus(&gui_menu);
}
void gui_Select(void)
{
menu_Select(&gui_menu);
}
/*============================================*/
const static uint8_t ok_xbm[] = { /* 16x16 */
0xfe, 0x7f, 0x03, 0xc0, 0x01, 0x80, 0x01, 0xb8, 0x01, 0x9c, 0x01, 0x8e,
0x01, 0x87, 0x01, 0x87, 0x9d, 0x83, 0xb9, 0x83, 0xf1, 0x81, 0xe1, 0x81,
0xc1, 0x80, 0x01, 0x80, 0x03, 0xc0, 0xfe, 0x7f };
const static uint8_t alarm_xbm[] = { /* 12x12 */
0x00, 0x00, 0x0c, 0x06, 0xf6, 0x0d, 0x1a, 0x0b, 0x4c, 0x06, 0x44, 0x04,
0xc4, 0x05, 0x04, 0x04, 0x0c, 0x06, 0x18, 0x03, 0xf0, 0x01, 0x00, 0x00 };
int me_action_to_top_menu(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_top_menu, 0);
return 1;
}
return 0;
}
int me_action_save_time(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_top_menu, 0); /* first set the normal menu */
gui_Recalculate(); /* because it might be overwritten with the alarm menu */
return 1;
}
return 0;
}
#define ME_TIME_Y 19
#define ME_TIME_XO 11
const me_t melist_setup_time[] =
{
{ me_cb_0_23, &gui_data.h, NULL, ME_TIME_XO+2,ME_TIME_Y },
{ me_cb_num_label, NULL, ":", ME_TIME_XO+30,ME_TIME_Y-3 },
{ me_cb_0_5, &gui_data.mt, NULL, ME_TIME_XO+39,ME_TIME_Y },
{ me_cb_0_9, &gui_data.mo, NULL, ME_TIME_XO+52,ME_TIME_Y },
{ me_cb_num_label, NULL, ":", ME_TIME_XO+67,ME_TIME_Y-3 },
{ me_cb_0_5, &gui_data.st, NULL, ME_TIME_XO+67+9,ME_TIME_Y },
{ me_cb_0_9, &gui_data.so, NULL, ME_TIME_XO+80+9,ME_TIME_Y },
{ me_cb_button_full_line, (void *)me_action_save_time, "Speichern", 40,30 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* Display Time */
void gui_alarm_to_str(uint8_t idx)
{
strcpy(gui_data.s, weekdaystr[gui_alarm_list[gui_data.next_alarm_index].na_wd]);
gui_data.s[2] = ',';
gui_data.s[3] = ' ';
strcpy(gui_data.s+4, u8x8_u8toa(gui_alarm_list[gui_data.next_alarm_index].na_h, 2));
gui_data.s[6] = ':';
strcpy(gui_data.s+7, u8x8_u8toa(gui_alarm_list[gui_data.next_alarm_index].na_m, 2));
gui_data.s[9] = '\0';
}
int me_action_handle_display_time(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_DRAW )
{
char s[14];
u8g2_uint_t x = 4;
u8g2_SetFont(menu->u8g2, MENU_NORMAL_FONT);
if ( gui_data.next_alarm_index < GUI_ALARM_CNT )
{
u8g2_DrawXBM(menu->u8g2, 67, 20, 12, 12, (const uint8_t *)(alarm_xbm));
gui_alarm_to_str(gui_data.next_alarm_index);
u8g2_DrawUTF8(menu->u8g2, 81, 30, gui_data.s);
}
else
{
x= 34;
}
strcpy(s, weekdaystr[gui_data.weekday]);
s[2] = ',';
s[3] = ' ';
strcpy(s+4, u8x8_u8toa(gui_data.day, 2));
s[6] = '.';
strcpy(s+7, u8x8_u8toa(gui_data.month, 2));
s[9] = '.';
s[10] = gui_data.year_t+'0';
s[11] = gui_data.year_o+'0';
s[12] = '\0';
u8g2_DrawUTF8(menu->u8g2, x, 30, s);
return 1;
}
else if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_top_menu, 0);
return 1;
}
return 0;
}
#define ME_TIME_DXO 30
const me_t melist_display_time[] =
{
{ me_cb_0_23_ro, &gui_data.h, NULL, ME_TIME_DXO+2,ME_TIME_Y },
{ me_cb_num_label, NULL, ":", ME_TIME_DXO+30,ME_TIME_Y-3 },
{ me_cb_0_9_ro, &gui_data.mt, NULL, ME_TIME_DXO+39,ME_TIME_Y },
{ me_cb_0_9_ro, &gui_data.mo, NULL, ME_TIME_DXO+52,ME_TIME_Y },
{ me_cb_button_empty, (void *)me_action_handle_display_time, NULL, 0, 0 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* Date Edit Dialog */
int me_action_save_date(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_top_menu, 0); /* first set the normal menu */
gui_Recalculate(); /* because it might be overwritten with the alarm menu */
return 1;
}
return 0;
}
const me_t melist_setup_date[] =
{
{ me_cb_1_31, &gui_data.day, NULL, ME_TIME_XO+2,ME_TIME_Y },
{ me_cb_num_label, NULL, ".", ME_TIME_XO+30,ME_TIME_Y },
{ me_cb_1_12, &gui_data.month, NULL, ME_TIME_XO+39,ME_TIME_Y },
{ me_cb_num_label, NULL, ".", ME_TIME_XO+67,ME_TIME_Y },
{ me_cb_0_9, &gui_data.year_t, NULL, ME_TIME_XO+67+9,ME_TIME_Y },
{ me_cb_0_9, &gui_data.year_o, NULL, ME_TIME_XO+80+9,ME_TIME_Y },
{ me_cb_button_full_line, (void *)me_action_save_date, "Speichern", 40,30 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* Alarm Edit Dialog */
#define ME_ALARM_TIME_XO 28
#define ME_ALARM_TIME_Y 20
#define ME_ALARM_WD_Y 29
#define ME_ALARM_WD_XO 8
int me_action_alarm_done(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
gui_alarm_list[gui_alarm_index] = gui_alarm_current;
gui_alarm_list[gui_alarm_index].skip_wd = 0; /* clear the skip alarm (if any) */
gui_alarm_list[gui_alarm_index].snooze_count = 0; /* clear snooze (if any) */
//gui_alarm_calc_str_time(gui_alarm_index);
menu_SetMEList(menu, melist_alarm_menu, gui_alarm_index); /* first set the normal menu */
gui_Recalculate(); /* because it might be overwritten with the alarm menu */
return 1;
}
return 0;
}
const me_t melist_setup_alarm[] =
{
{ me_cb_big_toggle, &(gui_alarm_current.enable), NULL, 4 , 6},
{ me_cb_0_23, &(gui_alarm_current.h), NULL, ME_ALARM_TIME_XO+2,ME_ALARM_TIME_Y },
{ me_cb_num_label, NULL, ":", ME_ALARM_TIME_XO+30,ME_ALARM_TIME_Y-3 },
{ me_cb_0_55, &(gui_alarm_current.m), NULL, ME_ALARM_TIME_XO+39,ME_ALARM_TIME_Y },
{ me_cb_wd_toggle, &(gui_alarm_current.wd[0]), (void *)weekdaystr[0], ME_ALARM_WD_XO+17*0, ME_ALARM_WD_Y},
{ me_cb_wd_toggle, &(gui_alarm_current.wd[1]), (void *)weekdaystr[1], ME_ALARM_WD_XO+17*1, ME_ALARM_WD_Y},
{ me_cb_wd_toggle, &(gui_alarm_current.wd[2]), (void *)weekdaystr[2], ME_ALARM_WD_XO+17*2, ME_ALARM_WD_Y},
{ me_cb_wd_toggle, &(gui_alarm_current.wd[3]), (void *)weekdaystr[3], ME_ALARM_WD_XO+17*3, ME_ALARM_WD_Y},
{ me_cb_wd_toggle, &(gui_alarm_current.wd[4]), (void *)weekdaystr[4], ME_ALARM_WD_XO+17*4, ME_ALARM_WD_Y},
{ me_cb_wd_toggle, &(gui_alarm_current.wd[5]), (void *)weekdaystr[5], ME_ALARM_WD_XO+17*5, ME_ALARM_WD_Y},
{ me_cb_wd_toggle, &(gui_alarm_current.wd[6]), (void *)weekdaystr[6], ME_ALARM_WD_XO+17*6, ME_ALARM_WD_Y},
{ me_cb_16x16_bitmap_button, (void *)me_action_alarm_done, (void *)ok_xbm, ME_ALARM_TIME_XO+80,ME_ALARM_TIME_Y-17 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* Alarm Setup Menu */
static int me_action_alarm_common(menu_t *menu, const me_t *me, uint8_t msg) U8G2_NOINLINE;
static int me_action_alarm_common(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
gui_alarm_current = gui_alarm_list[gui_alarm_index];
menu_SetMEList(menu, melist_setup_alarm, 0);
return 1;
}
return 0;
}
int me_action_alarm1(menu_t *menu, const me_t *me, uint8_t msg)
{
gui_alarm_index = 0;
return me_action_alarm_common(menu, me, msg);
}
int me_action_alarm2(menu_t *menu, const me_t *me, uint8_t msg)
{
gui_alarm_index = 1;
return me_action_alarm_common(menu, me, msg);
}
int me_action_alarm3(menu_t *menu, const me_t *me, uint8_t msg)
{
gui_alarm_index = 2;
return me_action_alarm_common(menu, me, msg);
}
int me_action_alarm4(menu_t *menu, const me_t *me, uint8_t msg)
{
gui_alarm_index = 3;
return me_action_alarm_common(menu, me, msg);
}
int me_action_handle_alarm_list(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_DRAW )
{
uint8_t y, x;
uint8_t ym;
u8g2_SetFont(menu->u8g2, MENU_SMALL_FONT);
for( y = 0; y < 4; y++ )
{
ym = y*8+7;
for( x = 0; x < 7; x++ )
{
u8g2_DrawGlyph(menu->u8g2, 0, ym, y+'1');
u8g2_DrawGlyph(menu->u8g2, 5, ym, ':');
u8g2_DrawStr(menu->u8g2, 9, ym, gui_alarm_str[y]);
if ( gui_alarm_list[y].wd[x] )
{
u8g2_DrawStr(menu->u8g2, 40+x*12, ym, weekdaystr[x]);
}
}
}
}
else if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_alarm_menu, 4);
return 1;
}
return 0;
}
const me_t melist_alarm_list_menu[] =
{
{ me_cb_button_empty, (void *)me_action_handle_alarm_list, NULL, 0, 0 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
int me_action_goto_alarm_list(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_alarm_list_menu, 0);
return 1;
}
return 0;
}
const me_t melist_alarm_menu[] =
{
{ me_cb_button_half_line, (void *)me_action_alarm1, gui_alarm_str[0], 0,10 },
{ me_cb_button_half_line, (void *)me_action_alarm2, gui_alarm_str[1], 64,10 },
{ me_cb_button_half_line, (void *)me_action_alarm3, gui_alarm_str[2], 0,20 },
{ me_cb_button_half_line, (void *)me_action_alarm4, gui_alarm_str[3], 64,20 },
{ me_cb_button_half_line, (void *)me_action_goto_alarm_list, "Liste", 0,30 },
{ me_cb_button_half_line, (void *)me_action_to_top_menu, "Zurück", 64,30 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* Setup Menu */
int me_action_setup_time(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_setup_time, 0);
return 1;
}
return 0;
}
int me_action_setup_date(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_setup_date, 0);
return 1;
}
return 0;
}
const me_t melist_setup_menu[] =
{
{ me_cb_button_half_line, (void *)me_action_setup_time, "Uhrzeit", 0,10 },
{ me_cb_button_half_line, (void *)me_action_setup_date, "Datum", 64,10 },
{ me_cb_button_half_line, (void *)NULL, "Power", 0,20 },
{ me_cb_button_half_line, (void *)me_action_goto_alarm_list, "Alarme", 64,20 },
{ me_cb_button_full_line, (void *)me_action_to_top_menu, "Zurück", 40,30 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* Alarm Menu */
int me_action_deactivate_alarm(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_display_time, 0);
return 1;
}
return 0;
}
int me_action_do_snooze(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_display_time, 0);
return 1;
}
return 0;
}
const me_t melist_active_alarm_menu[] =
{
{ me_cb_button_full_line, (void *)0, "", 3,10 },
{ me_cb_button_full_line, (void *)me_action_deactivate_alarm, "Alarm abschalten", 3,20 },
{ me_cb_button_full_line, (void *)me_action_do_snooze, "5 Min schlummern", 3,30 },
{ me_cb_null, NULL, NULL, 0, 0 },
};
/*============================================*/
/* toplevel menu */
int me_action_to_display_time(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_display_time, 0);
return 1;
}
return 0;
}
int me_action_to_alarm_menu(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_alarm_menu, 0);
return 1;
}
return 0;
}
int me_action_to_setup_menu(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_SELECT )
{
menu_SetMEList(menu, melist_setup_menu, 0);
return 1;
}
return 0;
}
int me_cb_button_skip_alarm(menu_t *menu, const me_t *me, uint8_t msg)
{
int r = 0;
u8g2_uint_t x;
switch(msg)
{
case ME_MSG_IS_FOCUS:
return gui_data.is_skip_possible;
case ME_MSG_DRAW_FOCUS:
menu_DrawBoxFocus(menu,
0,
me->y - u8g2_GetAscent(menu->u8g2)-1,
u8g2_GetDisplayWidth(menu->u8g2) ,
u8g2_GetAscent(menu->u8g2) - u8g2_GetDescent(menu->u8g2) +1);
r = 1;
break;
case ME_MSG_DRAW:
if ( gui_data.is_skip_possible )
{
u8g2_SetFont(menu->u8g2, MENU_NORMAL_FONT);
gui_alarm_to_str(gui_data.next_alarm_index);
x = u8g2_DrawUTF8(menu->u8g2, me->x, me->y, gui_data.s );
u8g2_DrawUTF8(menu->u8g2, me->x+x, me->y, " deaktvieren" );
}
r = 1;
break;
case ME_MSG_SELECT:
//printf("me_cb_button_skip_alarm ME_MSG_SELECT\n");
gui_alarm_list[gui_data.next_alarm_index].skip_wd =
gui_alarm_list[gui_data.next_alarm_index].na_wd + 1;
gui_alarm_list[gui_data.next_alarm_index].snooze_count = 0; /* clear snooze (if any) */
menu_SetMEList(menu, melist_display_time, 0); /* first set the normal menu */
gui_Recalculate(); /* it might be changed here to the alarm menu */
r = 1;
break;
}
return r;
}
const me_t melist_top_menu[] =
{
{ me_cb_button_half_line, (void *)me_action_to_display_time, "Zurück", 0,10 },
{ me_cb_button_half_line, (void *)me_action_to_alarm_menu, "Alarm", 64,10 },
{ me_cb_button_skip_alarm, NULL, NULL, 3,20 },
{ me_cb_button_full_line, (void *)me_action_to_setup_menu, "Weitere Funktionen", 3,30 },
{ me_cb_null, NULL, NULL, 0, 0 },
};

View File

@@ -0,0 +1,70 @@
/*
gui.h
*/
#ifndef _GUI_H
#define _GUI_H
#include "menu.h"
#define GUI_ALARM_CNT 4
#define SNOOZE_MINUTES 10
#define ALLOW_SKIP_HOURS 4
struct _gui_data
{
uint16_t week_time; /* calculated: derived from h, mt, mo and weekday */
uint8_t h, mt, mo, st, so; /* input: current time */
uint8_t day; /* input: 1 .. 31 current day in month */
uint8_t month; /* input: 1..12 */
uint8_t year_t, year_o; /* input: current year */
uint8_t weekday; /* calculated: 0 = Monday */
uint8_t next_alarm_index; /* calculated: index for the next alarm or GUI_ALARM_CNT if there is no next alarm */
uint8_t is_skip_possible; /* calculated: whether the next alarm (next_alarm_index) can be skipped */
uint8_t is_alarm; /* input/calculated: set by the software, has to be reset by the user */
uint8_t active_alarm_idx; /* input/calculated: set by the software, has to be reset by the user */
char s[16]; /* string buffer */
};
typedef struct _gui_data gui_data_t;
struct _gui_alarm_struct
{
/* next alarm, all na_ fields are derived from the alarm information */
uint16_t na_week_time_in_minutes;
uint16_t na_minutes_diff; /* calculated: time in minutes until next alarm, 0x0ffff = no alarm */
uint8_t na_h; /* calculated */
uint8_t na_m; /* calculated */
uint8_t na_wd; /* calculated: 0...7, 0=monday, 7=no alarm */
/* alarm information */
uint8_t enable; /* input */
uint8_t snooze_count; /* input */
uint8_t skip_wd; /* input 0 = no skip, 1 = Monday, ...*/
uint8_t h; /* input */
uint8_t m; /* input */
uint8_t wd[7]; /* input: 0 or 1, 0=weekday not selected */
};
typedef struct _gui_alarm_struct gui_alarm_t;
extern const me_t melist_display_time[];
extern const me_t melist_top_menu[];
extern const me_t melist_active_alarm_menu[];
extern const me_t melist_setup_menu[];
extern const me_t melist_alarm_menu[];
extern gui_data_t gui_data;
void gui_Recalculate(void);
void gui_Init(u8g2_t *u8g2);
void gui_Draw(void);
void gui_Next(void);
void gui_Select(void);
#endif

View File

@@ -0,0 +1,108 @@
/*
main.c
*/
#include "gui.h"
#include <stdio.h>
u8g2_t u8g2;
int main(void)
{
int x, y;
int k;
int i;
u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
//u8g2_SetFont(&u8g2, u8g2_font_helvB18_tf);
u8g2_SetFont(&u8g2, u8g2_font_ncenR08_tf);
gui_Init(&u8g2);
x = 50;
y = 50;
for(;;)
{
u8g2_FirstPage(&u8g2);
i = 0;
do
{
gui_Draw();
u8g2_SetFontDirection(&u8g2, 0);
u8g2_SetFont(&u8g2, u8g2_font_ncenR08_tf);
u8g2_DrawStr(&u8g2, x, y, "-- 123");
i++;
} while( u8g2_NextPage(&u8g2) );
do
{
k = u8g_sdl_get_key();
} while( k < 0 );
if ( k == 'n' )
gui_Next();
if ( k == 's' )
gui_Select();
if ( k == 'm' )
{
gui_data.mo++;
if ( gui_data.mo >= 10 )
{
gui_data.mo = 0;
gui_data.mt++;
if ( gui_data.mt >= 60 )
{
gui_data.h++;
if ( gui_data.h >= 24 )
{
gui_data.h = 0;
gui_data.day++;
}
}
}
gui_Recalculate();
}
if ( k == 'h' )
{
gui_data.h++;
if ( gui_data.h >= 24 )
{
gui_data.h = 0;
gui_data.day++;
}
gui_Recalculate();
}
if ( k == 273 ) y -= 1;
if ( k == 274 ) y += 1;
if ( k == 276 ) x -= 1;
if ( k == 275 ) x += 1;
if ( k == 'q' ) break;
}
//u8x8_Set8x8Font(u8g2_GetU8x8(&u8g2), bdf_font);
//u8x8_Draw8x8String(u8g2_GetU8x8(&u8g2), 0, 0, "Hello World!");
return 0;
}

View File

@@ -0,0 +1,566 @@
/*
menu.c
*/
#include "menu.h"
/*================================================*/
void menu_DrawEdgePixel(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h)
{
w--;
h--;
u8g2_DrawPixel(menu->u8g2, x,y);
u8g2_DrawPixel(menu->u8g2, x+w,y);
u8g2_DrawPixel(menu->u8g2, x,y+h);
u8g2_DrawPixel(menu->u8g2, x+w,y+h);
}
void menu_ClearEdgePixel(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h)
{
u8g2_SetDrawColor(menu->u8g2, 0);
menu_DrawEdgePixel(menu, x, y, w, h);
u8g2_SetDrawColor(menu->u8g2, 1);
}
void menu_DrawBoxFocus(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h)
{
u8g2_SetDrawColor(menu->u8g2, 2);
u8g2_DrawBox(menu->u8g2, x, y, w, h);
menu_ClearEdgePixel(menu, x, y, w, h);
}
void menu_DrawFrameFocus(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h)
{
menu_DrawEdgePixel(menu, x, y, w, h);
x--;
y--;
w+=2;
h+=2;
u8g2_DrawFrame(menu->u8g2, x, y, w, h);
menu_ClearEdgePixel(menu, x, y, w, h);
}
/*================================================*/
/* this function must be the last function in the list. it also marks the end of a list */
int me_cb_null(menu_t *menu, const me_t *me, uint8_t msg)
{
return 0;
}
/*
Name: me_cb_wd_toggle
Val: uint8_t *
Arg: Not used
*/
int me_cb_big_toggle(menu_t *menu, const me_t *me, uint8_t msg)
{
uint8_t val = *(uint8_t *)(me->val);
u8g2_uint_t x, y, w, h, w2;
w = 16;
w2 = 6;
h = 10;
x = me->x;
y = me->y;
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawFrameFocus(menu, x-1, y-1, w+2, h+2);
return 1;
case ME_MSG_SELECT:
{
val++;
if ( val > 1 )
val = 0;
*(uint8_t *)(me->val) = val;
}
return 1;
case ME_MSG_DRAW:
menu_DrawFrameFocus(menu, x+1,y+1,w-2,h-2);
if ( val == 0 )
{
menu_DrawFrameFocus(menu, x+3,y+3,w2-2,h-6);
}
else
{
menu_DrawBoxFocus(menu, x+w/2,y+2,w2,h-4);
}
return 1;
}
return 0;
}
/*
Name: me_cb_wd_toggle
Val: uint8_t *
Arg: char *
*/
int me_cb_wd_toggle(menu_t *menu, const me_t *me, uint8_t msg)
{
uint8_t val = *(uint8_t *)(me->val);
u8g2_uint_t x, y, w, h;
u8g2_SetFont(menu->u8g2, MENU_SMALL_FONT);
w = 13;
h = u8g2_GetAscent(menu->u8g2)+2;
x = me->x-2;
y = me->y - u8g2_GetAscent(menu->u8g2)-1;
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawFrameFocus(menu, x, y, w, h);
return 1;
case ME_MSG_SELECT:
{
val++;
if ( val > 1 )
val = 0;
*(uint8_t *)(me->val) = val;
}
return 1;
case ME_MSG_DRAW:
u8g2_DrawUTF8(menu->u8g2, me->x, me->y, (const char *)(me->arg));
if ( val > 0 )
{
menu_DrawBoxFocus(menu, x,y,w,h);
}
//u8g2_DrawRFrame(menu->u8g2, x, y, w, h, 1);
return 1;
}
return 0;
}
/*
Name: me_cb_0_9
Val: uint8_t *
*/
int me_cb_0_9(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawBoxFocus(menu,
me->x,
me->y - u8g2_GetAscent(menu->u8g2)-1,
u8g2_GetGlyphWidth(menu->u8g2, '0'),
u8g2_GetAscent(menu->u8g2) + 2);
return 1;
case ME_MSG_SELECT:
{
uint8_t val = *(uint8_t *)(me->val);
val++;
if ( val > 9 )
val = 0;
*(uint8_t *)(me->val) = val;
}
return 1;
case ME_MSG_DRAW:
u8g2_SetFont(menu->u8g2, MENU_BIG_NUM);
u8g2_DrawGlyph(menu->u8g2, me->x, me->y, *(uint8_t *)(me->val) + '0');
return 1;
}
return 0;
}
int me_cb_0_9_ro(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_IS_FOCUS )
return 0;
return me_cb_0_9(menu, me, msg);
}
/*
Name: me_cb_0_5
Val: uint8_t *
*/
int me_cb_0_5(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_SELECT:
{
uint8_t val = *(uint8_t *)(me->val);
val++;
if ( val > 5 )
val = 0;
*(uint8_t *)(me->val) = val;
}
return 1;
}
return me_cb_0_9(menu, me, msg);
}
/*
Name: me_cb_0_23
Val: uint8_t *
*/
int me_cb_0_23(menu_t *menu, const me_t *me, uint8_t msg)
{
char s[4];
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawBoxFocus(menu,
me->x,
me->y - u8g2_GetAscent(menu->u8g2)-1,
u8g2_GetGlyphWidth(menu->u8g2, '0')*2,
u8g2_GetAscent(menu->u8g2) + 2);
return 1;
case ME_MSG_SELECT:
{
uint8_t val = *(uint8_t *)(me->val);
val++;
if ( val > 23 )
val = 0;
*(uint8_t *)(me->val) = val;
}
return 1;
case ME_MSG_DRAW:
u8g2_SetFont(menu->u8g2, MENU_BIG_NUM);
s[0] = *(uint8_t *)(me->val);
s[1] = s[0];
s[1] %= 10;
s[1] += '0';
s[0] /= 10;
s[0] += '0';
s[2] = '\0';
u8g2_DrawUTF8(menu->u8g2, me->x, me->y, s);
return 1;
}
return 0;
}
int me_cb_0_23_ro(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_IS_FOCUS )
return 0;
return me_cb_0_23(menu, me, msg);
}
/*
Name: me_cb_0_55
Val: uint8_t *
*/
int me_cb_0_55(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_SELECT:
{
uint8_t val = *(uint8_t *)(me->val);
val+=5;
if ( val > 55 )
val = 0;
*(uint8_t *)(me->val) = val;
}
return 1;
}
return me_cb_0_23(menu, me, msg);
}
/*
Name: me_cb_1_12
Val: uint8_t *
*/
int me_cb_1_12(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_SELECT:
{
uint8_t val = *(uint8_t *)(me->val);
val++;
if ( val > 12 )
val = 1;
*(uint8_t *)(me->val) = val;
}
return 1;
}
return me_cb_0_23(menu, me, msg);
}
/*
Name: me_cb_1_31
Val: uint8_t *
*/
int me_cb_1_31(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_SELECT:
{
uint8_t val = *(uint8_t *)(me->val);
val++;
if ( val > 31 )
val = 1;
*(uint8_t *)(me->val) = val;
}
return 1;
}
return me_cb_0_23(menu, me, msg);
}
/*
Name: me_cb_label
can not get focus
Arg: char *
*/
int me_cb_num_label(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_IS_FOCUS:
case ME_MSG_DRAW_FOCUS:
case ME_MSG_SELECT:
break;
case ME_MSG_DRAW:
u8g2_SetFont(menu->u8g2, MENU_BIG_NUM);
u8g2_DrawUTF8(menu->u8g2, me->x, me->y, (char *)(me->arg) );
return 1;
}
return 0;
}
/*
Name: me_cb_button_full_line
Val: callback function
Arg: char *
*/
int me_cb_button_full_line(menu_t *menu, const me_t *me, uint8_t msg)
{
int r = 0;
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawBoxFocus(menu,
0,
me->y - u8g2_GetAscent(menu->u8g2)-1,
u8g2_GetDisplayWidth(menu->u8g2) ,
u8g2_GetAscent(menu->u8g2) - u8g2_GetDescent(menu->u8g2) +1);
r = 1;
break;
case ME_MSG_DRAW:
u8g2_SetFont(menu->u8g2, MENU_NORMAL_FONT);
u8g2_DrawUTF8(menu->u8g2, me->x, me->y, (char *)(me->arg) );
r = 1;
break;
}
/* pass all messages except for the IS_FOCUS also to the callback function */
if ( me->val != NULL )
return ((me_cb)(me->val))(menu, me, msg) | r;
return r;
}
/*
Name: me_cb_button_full_line
Val: callback function
Arg: char *
*/
int me_cb_button_half_line(menu_t *menu, const me_t *me, uint8_t msg)
{
int r = 0;
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawBoxFocus(menu,
me->x,
me->y - u8g2_GetAscent(menu->u8g2)-1,
u8g2_GetDisplayWidth(menu->u8g2)/2,
u8g2_GetAscent(menu->u8g2) - u8g2_GetDescent(menu->u8g2) +1);
r = 1;
break;
case ME_MSG_DRAW:
u8g2_SetFont(menu->u8g2, MENU_NORMAL_FONT);
u8g2_DrawUTF8(menu->u8g2, me->x+4, me->y, (char *)(me->arg) );
r = 1;
break;
}
/* pass all messages except for the IS_FOCUS also to the callback function */
if ( me->val != NULL )
return ((me_cb)(me->val))(menu, me, msg) | r;
return r;
}
/*
Name: me_cb_button_empty
Val: callback function
Arg: not used
*/
int me_cb_button_empty(menu_t *menu, const me_t *me, uint8_t msg)
{
if ( msg == ME_MSG_IS_FOCUS )
return 1;
if ( me->val != NULL )
return ((me_cb)(me->val))(menu, me, msg);
return 0;
}
/*
Name: me_cb_label
can not get focus
Arg: char *
*/
int me_cb_label(menu_t *menu, const me_t *me, uint8_t msg)
{
switch(msg)
{
case ME_MSG_IS_FOCUS:
case ME_MSG_DRAW_FOCUS:
case ME_MSG_SELECT:
break;
case ME_MSG_DRAW:
u8g2_SetFont(menu->u8g2, MENU_NORMAL_FONT);
u8g2_DrawUTF8(menu->u8g2, me->x, me->y, (char *)(me->arg) );
return 1;
}
return 0;
}
/*
Name: me_cb_button_full_line
Val: callback function
Arg: bitmap
*/
int me_cb_16x16_bitmap_button(menu_t *menu, const me_t *me, uint8_t msg)
{
int r = 0;
switch(msg)
{
case ME_MSG_IS_FOCUS:
return 1;
case ME_MSG_DRAW_FOCUS:
menu_DrawFrameFocus(menu,
me->x-1,
me->y-1,
16+2,
16+2);
r = 1;
break;
case ME_MSG_DRAW:
u8g2_DrawXBM(menu->u8g2, me->x, me->y, 16, 16, (const uint8_t *)(me->arg));
r = 1;
break;
}
/* pass all messages except for the IS_FOCUS also to the callback function */
if ( me->val != NULL )
return ((me_cb)(me->val))(menu, me, msg) | r;
return r;
}
/*================================================*/
/* call menu element from menu->current_index */
int menu_CallME(menu_t *menu, uint8_t msg)
{
const me_t *me;
me = menu->me_list+menu->current_index;
return me->cb(menu, me, msg);
}
/* stay on current focus if valid, move to next valid focus */
static void menu_CalcNextValidFocus(menu_t *menu) U8G2_NOINLINE;
static void menu_CalcNextValidFocus(menu_t *menu)
{
for(;;)
{
menu->current_index = menu->focus_index;
if ( menu->current_index >= menu->me_count )
break;
if ( menu_CallME(menu, ME_MSG_IS_FOCUS) != 0 )
break;
menu->focus_index++;
}
}
/* advance current focus to the next element */
void menu_NextFocus(menu_t *menu)
{
menu->focus_index++;
if ( menu->focus_index >= menu->me_count )
menu->focus_index = 0;
menu_CalcNextValidFocus(menu);
}
/* send select message to the element which has the current focus */
void menu_Select(menu_t *menu)
{
menu->current_index = menu->focus_index;
menu_CallME(menu, ME_MSG_SELECT);
}
void menu_SetMEList(menu_t *menu, const me_t *me_list, uint16_t initial_focus)
{
menu->me_list = me_list;
menu->me_count = 0;
while( me_list[menu->me_count].cb != me_cb_null )
menu->me_count++;
menu->focus_index = 0;
menu_CalcNextValidFocus(menu);
while( initial_focus > 0 )
{
menu_NextFocus(menu);
initial_focus--;
}
menu->radio_index = menu->me_count;
}
me_t melist_emty[] =
{
{ me_cb_null, NULL, 0, 0 }
};
void menu_Init(menu_t *menu, u8g2_t *u8g2)
{
menu->u8g2 = u8g2;
menu_SetMEList(menu, melist_emty, 0);
}
void menu_Draw(menu_t *menu)
{
for( menu->current_index = 0; menu->current_index < menu->me_count; menu->current_index++ )
{
menu_CallME(menu, ME_MSG_DRAW);
if ( menu->current_index == menu->focus_index )
{
menu_CallME(menu, ME_MSG_DRAW_FOCUS);
}
}
u8g2_DrawHLine(menu->u8g2, 0, 32, 128);
}

View File

@@ -0,0 +1,87 @@
#ifndef _MENU_H
#define _MENU_H
#include "u8g2.h"
#define MENU_SMALL_FONT u8g2_font_baby_tr
#define MENU_NORMAL_FONT u8g2_font_ncenR08_tf
#define MENU_BIG_NUM u8g2_font_ncenR18_tf
typedef struct _menu_struct menu_t;
typedef struct _me_struct me_t;
typedef int (*me_cb)(menu_t *menu, const me_t *me, uint8_t msg);
struct _me_struct
{
me_cb cb;
void *val;
void *arg;
u8g2_uint_t x;
u8g2_uint_t y;
};
/* return 1, if this element can have focus */
#define ME_MSG_IS_FOCUS 1
/* draw focus graphics for the element */
#define ME_MSG_DRAW_FOCUS 2
/* user has pressed the select key */
#define ME_MSG_SELECT 3
/* advice for drawing */
#define ME_MSG_DRAW 4
struct _menu_struct
{
u8g2_t *u8g2;
volatile uint16_t current_index; /* element which is processed right now */
uint16_t focus_index; /* element which has the focus at the moment */
uint16_t radio_index; /* if elements for a radio selection, then this is set by the cb */
uint16_t me_count; /* total number of elements in the list */
/* pointer to the list of menu elements */
const me_t *me_list;
};
void menu_SetEdgePixel(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h) U8G2_NOINLINE;
void menu_ClearEdgePixel(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h) U8G2_NOINLINE;
void menu_DrawBoxFocus(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h) U8G2_NOINLINE;
void menu_DrawFrameFocus(menu_t *menu, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h) U8G2_NOINLINE;
void menu_Init(menu_t *menu, u8g2_t *u8g2);
void menu_SetMEList(menu_t *menu, const me_t *me_list, uint16_t initial_focus);
void menu_Draw(menu_t *menu);
void menu_NextFocus(menu_t *menu);
void menu_Select(menu_t *menu);
int me_cb_null(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_big_toggle(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_wd_toggle(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_0_5(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_0_9(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_0_9_ro(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_0_23(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_0_23_ro(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_0_55(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_1_12(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_1_31(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_num_label(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_button_full_line(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_button_half_line(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_button_empty(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_label(menu_t *menu, const me_t *me, uint8_t msg);
int me_cb_16x16_bitmap_button(menu_t *menu, const me_t *me, uint8_t msg);
#endif