u8g2-copy/sys/sdl/clock/guifn.c

232 lines
6.1 KiB
C
Raw Normal View History

2017-04-30 18:08:44 +08:00
/*
guifn.c
*/
#include "gui.h"
#include "datecalc.h"
#include <string.h>
/*============================================*/
gui_alarm_t gui_alarm_list[GUI_ALARM_CNT];
char gui_alarm_str[GUI_ALARM_CNT][8];
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)
{
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)
{
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);
}