Finish "Stats by" screen

This commit is contained in:
Alexander Rosenberg 2024-03-31 17:20:07 -07:00
parent c0e51c13ed
commit 1443bcccee
Signed by: school-rpi4
GPG Key ID: 5CCFC80B0B47B04B
4 changed files with 43 additions and 23 deletions

View File

@ -144,5 +144,4 @@ bool lcd_is_busy(LCD *lcd) {
gpio_pin_low(lcd->handle, lcd->en); gpio_pin_low(lcd->handle, lcd->en);
usleep(1); usleep(1);
return busy; return busy;
} }

View File

@ -263,6 +263,7 @@ static void date_sel_clamp_time(DateSelection *ds) {
} }
static void date_sel_cleanup(DateSelection *ds) { static void date_sel_cleanup(DateSelection *ds) {
date_sel_clamp_time(ds);
if (ds->month < 1) { if (ds->month < 1) {
ds->month = 12 - ds->month; ds->month = 12 - ds->month;
--ds->year; --ds->year;
@ -369,6 +370,8 @@ DateSelectionState date_sel_dispatch(DateSelection *ds, SensorState *state) {
static void stats_by_select_start(StatsByScreen *screen, 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) { if (state->up_down || state->down_down || state->sel_down || state->back_down) {
screen->need_redraw = true; screen->need_redraw = true;
}
if (screen->need_redraw) {
UtilDate start, end; UtilDate start, end;
if (!get_database_limits(state->db, PERIOD_LABELS[screen->period], if (!get_database_limits(state->db, PERIOD_LABELS[screen->period],
&start, &end)) { &start, &end)) {
@ -378,8 +381,6 @@ static void stats_by_select_start(StatsByScreen *screen, SensorState *state) {
} }
screen->ds.start_time = start; screen->ds.start_time = start;
screen->ds.end_time = end; screen->ds.end_time = end;
}
if (screen->need_redraw) {
lcd_clear(state->lcd); lcd_clear(state->lcd);
lcd_move_to(state->lcd, 0, 0); lcd_move_to(state->lcd, 0, 0);
lcd_write_string(state->lcd, "Start: "); lcd_write_string(state->lcd, "Start: ");
@ -416,6 +417,14 @@ static void stats_by_show_stats(StatsByScreen *screen, SensorState *state) {
LCD_DISPLAY_ON); LCD_DISPLAY_ON);
lcd_clear(state->lcd); lcd_clear(state->lcd);
lcd_move_to(state->lcd, 0, 0); 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; UtilAverageRange data;
if (!get_average_for_range(state->db, screen->ds.year, screen->ds.month, if (!get_average_for_range(state->db, screen->ds.year, screen->ds.month,
screen->ds.day, screen->offset_scale, screen->ds.day, screen->offset_scale,

View File

@ -85,25 +85,30 @@ static const char *DB_LIMITS_QUERY_STR =
"strftime('%Y', MinUTC, 'unixepoch') as MinUTCYear,\n" "strftime('%Y', MinUTC, 'unixepoch') as MinUTCYear,\n"
"strftime('%m', MinUTC, 'unixepoch') as MinUTCMonth,\n" "strftime('%m', MinUTC, 'unixepoch') as MinUTCMonth,\n"
"strftime('%e', MinUTC, 'unixepoch') as MinUTCDay,\n" "strftime('%e', MinUTC, 'unixepoch') as MinUTCDay,\n"
"strftime('%H', RealMin, 'unixepoch') as MinUTCHour,\n"
"MinLocal,\n" "MinLocal,\n"
"strftime('%Y', MinLocal, 'unixepoch') as MinLocalYear,\n" "strftime('%Y', MinLocal, 'unixepoch') as MinLocalYear,\n"
"strftime('%m', MinLocal, 'unixepoch') as MinLocalMonth,\n" "strftime('%m', MinLocal, 'unixepoch') as MinLocalMonth,\n"
"strftime('%e', MinLocal, 'unixepoch') as MinLocalDay,\n" "strftime('%e', MinLocal, 'unixepoch') as MinLocalDay,\n"
"strftime('%H', RealMin, 'unixepoch', 'localtime') as MinLocalHour,\n"
"MaxUTC,\n" "MaxUTC,\n"
"strftime('%Y', MaxUTC, 'unixepoch') as MaxUTCYear,\n" "strftime('%Y', MaxUTC, 'unixepoch') as MaxUTCYear,\n"
"strftime('%m', MaxUTC, 'unixepoch') as MaxUTCMonth,\n" "strftime('%m', MaxUTC, 'unixepoch') as MaxUTCMonth,\n"
"strftime('%e', MaxUTC, 'unixepoch') as MaxUTCDay,\n" "strftime('%e', MaxUTC, 'unixepoch') as MaxUTCDay,\n"
"strftime('%H', RealMax, 'unixepoch') as MaxUTCHour,\n"
"MaxLocal,\n" "MaxLocal,\n"
"strftime('%Y', MaxLocal, 'unixepoch') as MaxLocalYear,\n" "strftime('%Y', MaxLocal, 'unixepoch') as MaxLocalYear,\n"
"strftime('%m', MaxLocal, 'unixepoch') as MaxLocalMonth,\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" "FROM\n"
"(SELECT\n" "(SELECT\n"
"unixepoch(min(time), 'unixepoch', 'start of ' || ?1) as MinUTC,\n" "RealMax, RealMin,\n"
"unixepoch(max(time), 'unixepoch', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxUTC,\n" "unixepoch(RealMin, 'unixepoch', 'start of ' || ?1) as MinUTC,\n"
"unixepoch(min(time), 'unixepoch', 'localtime', 'start of ' || ?1) as MinLocal,\n" "unixepoch(RealMax, 'unixepoch', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxUTC,\n"
"unixepoch(max(time), 'unixepoch', 'localtime', '+1 ' || ?1, 'start of ' || ?1, '-1 second') as MaxLocal\n" "unixepoch(RealMin, 'unixepoch', 'localtime', 'start of ' || ?1) as MinLocal,\n"
"FROM env_data);"; "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; static sqlite3_stmt *DB_LIMITS_QUERY;
const char *AVG_FOR_RANGE_QUERY_STR = const char *AVG_FOR_RANGE_QUERY_STR =
"SELECT\n" "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_year = sqlite3_column_int64(DB_LIMITS_QUERY, 1);
start->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 2); start->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 2);
start->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 3); start->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 3);
start->local = sqlite3_column_int64(DB_LIMITS_QUERY, 4); start->utc_hour = sqlite3_column_int64(DB_LIMITS_QUERY, 4);
start->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 5); start->local = sqlite3_column_int64(DB_LIMITS_QUERY, 5);
start->local_month = sqlite3_column_int64(DB_LIMITS_QUERY, 6); start->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 6);
start->local_day = sqlite3_column_int64(DB_LIMITS_QUERY, 7); 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) { if (end) {
end->utc = sqlite3_column_int64(DB_LIMITS_QUERY, 8); end->utc = sqlite3_column_int64(DB_LIMITS_QUERY, 10);
end->utc_year = sqlite3_column_int64(DB_LIMITS_QUERY, 9); end->utc_year = sqlite3_column_int64(DB_LIMITS_QUERY, 11);
end->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 10); end->utc_month = sqlite3_column_int64(DB_LIMITS_QUERY, 12);
end->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 11); end->utc_day = sqlite3_column_int64(DB_LIMITS_QUERY, 13);
end->local = sqlite3_column_int64(DB_LIMITS_QUERY, 12); end->utc_hour = sqlite3_column_int64(DB_LIMITS_QUERY, 14);
end->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 13); end->local = sqlite3_column_int64(DB_LIMITS_QUERY, 15);
end->local_month = sqlite3_column_int64(DB_LIMITS_QUERY, 14); end->local_year = sqlite3_column_int64(DB_LIMITS_QUERY, 16);
end->local_day = sqlite3_column_int64(DB_LIMITS_QUERY, 15); 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 { } else {
success = false; success = false;

View File

@ -97,19 +97,22 @@ typedef struct {
int utc_year; int utc_year;
int utc_month; int utc_month;
int utc_day; int utc_day;
int utc_hour;
int64_t local; int64_t local;
int local_year; int local_year;
int local_month; int local_month;
int local_day; int local_day;
int local_hour;
} UtilDate; } UtilDate;
typedef enum { enum {
PERIOD_HOUR = 0, PERIOD_HOUR = 0,
PERIOD_DAY, PERIOD_DAY,
PERIOD_WEEK, PERIOD_WEEK,
PERIOD_MONTH, PERIOD_MONTH,
PERIOD_YEAR, PERIOD_YEAR,
} UtilPeriod; };
typedef int UtilPeriod;
extern const char *PERIOD_LABELS[]; extern const char *PERIOD_LABELS[];
extern const size_t NPERIOD; extern const size_t NPERIOD;