diff --git a/src/lcd.c b/src/lcd.c index 126f51b..22f7d9e 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -144,5 +144,4 @@ bool lcd_is_busy(LCD *lcd) { gpio_pin_low(lcd->handle, lcd->en); usleep(1); return busy; - } diff --git a/src/screen.c b/src/screen.c index 85f174b..c84ed64 100644 --- a/src/screen.c +++ b/src/screen.c @@ -263,6 +263,7 @@ static void date_sel_clamp_time(DateSelection *ds) { } static void date_sel_cleanup(DateSelection *ds) { + date_sel_clamp_time(ds); if (ds->month < 1) { ds->month = 12 - ds->month; --ds->year; @@ -369,6 +370,8 @@ DateSelectionState date_sel_dispatch(DateSelection *ds, SensorState *state) { static void stats_by_select_start(StatsByScreen *screen, SensorState *state) { if (state->up_down || state->down_down || state->sel_down || state->back_down) { screen->need_redraw = true; + } + if (screen->need_redraw) { UtilDate start, end; if (!get_database_limits(state->db, PERIOD_LABELS[screen->period], &start, &end)) { @@ -378,8 +381,6 @@ static void stats_by_select_start(StatsByScreen *screen, SensorState *state) { } screen->ds.start_time = start; screen->ds.end_time = end; - } - if (screen->need_redraw) { lcd_clear(state->lcd); lcd_move_to(state->lcd, 0, 0); lcd_write_string(state->lcd, "Start: "); @@ -416,6 +417,14 @@ static void stats_by_show_stats(StatsByScreen *screen, SensorState *state) { LCD_DISPLAY_ON); lcd_clear(state->lcd); lcd_move_to(state->lcd, 0, 0); + printf("%d\n",screen->ds.start_time.local_hour); + if (screen->period == PERIOD_HOUR && + screen->offset_scale == 0 && + screen->ds.year == screen->ds.start_time.local_year && + screen->ds.month == screen->ds.start_time.local_month && + screen->ds.day == screen->ds.start_time.local_day) { + screen->offset_scale = screen->ds.start_time.local_hour; + } UtilAverageRange data; if (!get_average_for_range(state->db, screen->ds.year, screen->ds.month, screen->ds.day, screen->offset_scale, diff --git a/src/util.c b/src/util.c index 9029104..722d5d1 100644 --- a/src/util.c +++ b/src/util.c @@ -85,25 +85,30 @@ static const char *DB_LIMITS_QUERY_STR = "strftime('%Y', MinUTC, 'unixepoch') as MinUTCYear,\n" "strftime('%m', MinUTC, 'unixepoch') as MinUTCMonth,\n" "strftime('%e', MinUTC, 'unixepoch') as MinUTCDay,\n" + "strftime('%H', RealMin, 'unixepoch') as MinUTCHour,\n" "MinLocal,\n" "strftime('%Y', MinLocal, 'unixepoch') as MinLocalYear,\n" "strftime('%m', MinLocal, 'unixepoch') as MinLocalMonth,\n" "strftime('%e', MinLocal, 'unixepoch') as MinLocalDay,\n" + "strftime('%H', RealMin, 'unixepoch', 'localtime') as MinLocalHour,\n" "MaxUTC,\n" "strftime('%Y', MaxUTC, 'unixepoch') as MaxUTCYear,\n" "strftime('%m', MaxUTC, 'unixepoch') as MaxUTCMonth,\n" "strftime('%e', MaxUTC, 'unixepoch') as MaxUTCDay,\n" + "strftime('%H', RealMax, 'unixepoch') as MaxUTCHour,\n" "MaxLocal,\n" "strftime('%Y', MaxLocal, 'unixepoch') as MaxLocalYear,\n" "strftime('%m', MaxLocal, 'unixepoch') as MaxLocalMonth,\n" - "strftime('%e', MaxLocal, 'unixepoch') as MaxLocalDay\n" + "strftime('%e', MaxLocal, 'unixepoch') as MaxLocalDay,\n" + "strftime('%H', RealMax, 'unixepoch', 'localtime') as MaxLocalHour\n" "FROM\n" "(SELECT\n" - "unixepoch(min(time), 'unixepoch', 'start of ' || ?1) as MinUTC,\n" - "unixepoch(max(time), 'unixepoch', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxUTC,\n" - "unixepoch(min(time), 'unixepoch', 'localtime', 'start of ' || ?1) as MinLocal,\n" - "unixepoch(max(time), 'unixepoch', 'localtime', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxLocal\n" - "FROM env_data);"; + "RealMax, RealMin,\n" + "unixepoch(RealMin, 'unixepoch', 'start of ' || ?1) as MinUTC,\n" + "unixepoch(RealMax, 'unixepoch', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxUTC,\n" + "unixepoch(RealMin, 'unixepoch', 'localtime', 'start of ' || ?1) as MinLocal,\n" + "unixepoch(RealMax, 'unixepoch', 'localtime', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxLocal\n" + "FROM (SELECT max(time) as RealMax, min(time) as RealMin FROM env_data));"; static sqlite3_stmt *DB_LIMITS_QUERY; const char *AVG_FOR_RANGE_QUERY_STR = "SELECT\n" @@ -159,20 +164,24 @@ bool get_database_limits(sqlite3 *db, const char *period, UtilDate *start, start->utc_year = sqlite3_column_int64(DB_LIMITS_QUERY, 1); start->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 2); start->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 3); - start->local = sqlite3_column_int64(DB_LIMITS_QUERY, 4); - start->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 5); - start->local_month = sqlite3_column_int64(DB_LIMITS_QUERY, 6); - start->local_day = sqlite3_column_int64(DB_LIMITS_QUERY, 7); + start->utc_hour = sqlite3_column_int64(DB_LIMITS_QUERY, 4); + start->local = sqlite3_column_int64(DB_LIMITS_QUERY, 5); + start->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 6); + start->local_month = sqlite3_column_int64(DB_LIMITS_QUERY, 7); + start->local_day = sqlite3_column_int64(DB_LIMITS_QUERY, 8); + start->local_hour = sqlite3_column_int64(DB_LIMITS_QUERY, 9); } if (end) { - end->utc = sqlite3_column_int64(DB_LIMITS_QUERY, 8); - end->utc_year = sqlite3_column_int64(DB_LIMITS_QUERY, 9); - end->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 10); - end->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 11); - end->local = sqlite3_column_int64(DB_LIMITS_QUERY, 12); - end->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 13); - end->local_month = sqlite3_column_int64(DB_LIMITS_QUERY, 14); - end->local_day = sqlite3_column_int64(DB_LIMITS_QUERY, 15); + end->utc = sqlite3_column_int64(DB_LIMITS_QUERY, 10); + end->utc_year = sqlite3_column_int64(DB_LIMITS_QUERY, 11); + end->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 12); + end->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 13); + end->utc_hour = sqlite3_column_int64(DB_LIMITS_QUERY, 14); + end->local = sqlite3_column_int64(DB_LIMITS_QUERY, 15); + end->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 16); + end->local_month = sqlite3_column_int64(DB_LIMITS_QUERY, 17); + end->local_day = sqlite3_column_int64(DB_LIMITS_QUERY, 18); + end->local_hour = sqlite3_column_int64(DB_LIMITS_QUERY, 19); } } else { success = false; diff --git a/src/util.h b/src/util.h index 786d646..e4fb646 100644 --- a/src/util.h +++ b/src/util.h @@ -97,19 +97,22 @@ typedef struct { int utc_year; int utc_month; int utc_day; + int utc_hour; int64_t local; int local_year; int local_month; int local_day; + int local_hour; } UtilDate; -typedef enum { +enum { PERIOD_HOUR = 0, PERIOD_DAY, PERIOD_WEEK, PERIOD_MONTH, PERIOD_YEAR, -} UtilPeriod; +}; +typedef int UtilPeriod; extern const char *PERIOD_LABELS[]; extern const size_t NPERIOD;