rpi4b-temp-humidity/src/ui/powerscreen.c

85 lines
2.9 KiB
C

/*
* powerscreen.h - Screen for rebooting or halting
* Copyright (C) 2024 Alexander Rosenberg
*
* 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. See the LICENSE file for more information.
*/
#include "powerscreen.h"
#include <err.h>
#include <unistd.h>
#include <sys/reboot.h>
static bool power_screen_dispatch(PowerScreen *screen,
SensorState *state) {
if (state->back_down) {
lcd_display_control(state->lcd, LCD_CURSOR_NO_BLINK, LCD_CURSOR_OFF,
LCD_DISPLAY_ON);
screen->choice = POWER_SCREEN_RESTART_PROGRAM;
return true;
}
if (state->sel_down) {
switch (screen->choice) {
case POWER_SCREEN_RESTART_PROGRAM:
request_restart();
break;
case POWER_SCREEN_REBOOT:
LOG_VERBOSE("Rebooting...");
reboot(RB_AUTOBOOT);
abort();
// how did we even get here???
break;
case POWER_SCREEN_POWEROFF:
LOG_VERBOSE("Powering off...");
reboot(RB_POWEROFF);
abort();
// how did we even get here???
break;
default:
warnx("invalid power screen choice");
lcd_display_control(state->lcd, LCD_CURSOR_NO_BLINK, LCD_CURSOR_OFF,
LCD_DISPLAY_ON);
return true;
}
}
screen->choice = (screen->choice + state->up_down - state->down_down) %
POWER_SCREEN_NCHOICE;
if (screen->choice < 0) {
screen->choice = POWER_SCREEN_NCHOICE + screen->choice;
}
if (state->force_draw || state->up_down || state->down_down) {
lcd_clear(state->lcd);
lcd_move_to(state->lcd, 0, 0);
lcd_write_string(state->lcd, "Type:");
lcd_move_to(state->lcd, 1, 0);
lcd_write_char(state->lcd, '>');
switch (screen->choice) {
case POWER_SCREEN_RESTART_PROGRAM:
lcd_write_string(state->lcd, "REINIT");
break;
case POWER_SCREEN_REBOOT:
lcd_write_string(state->lcd, "REBOOT");
break;
case POWER_SCREEN_POWEROFF:
lcd_write_string(state->lcd, "POWEROFF");
break;
}
lcd_display_control(state->lcd, LCD_CURSOR_BLINK, LCD_CURSOR_ON,
LCD_DISPLAY_ON);
lcd_move_to(state->lcd, 1, 0);
}
return false;
}
PowerScreen *power_screen_new(void) {
PowerScreen *s = malloc_checked(sizeof(PowerScreen));
screen_init(&s->parent, "Power options",
(ScreenDispatchFunc) power_screen_dispatch,
(ScreenCleanupFunc) free);
s->choice = POWER_SCREEN_RESTART_PROGRAM;
return s;
}