application: add auto-shutdown

This commit is contained in:
King Kévin 2019-12-21 14:23:35 +01:00
parent 2e9ad46a76
commit 1e808f66b2
1 changed files with 24 additions and 2 deletions

View File

@ -69,6 +69,9 @@ static time_t time_start = 0;
volatile bool rtc_internal_tick_flag = false; /**< flag set when internal RTC ticked */
/** @} */
/** activity timeout before switching off (in seconds) */
#define SHUTDOWN_TIMEOUT 60
size_t putc(char c)
{
size_t length = 0; // number of characters printed
@ -1040,25 +1043,44 @@ void main(void)
rcc_periph_clock_enable(RCC_GPIOG); // enable clock to all GPIO port domains since we use them all
usb_pins_float(); // pull all pins to floating
// find cable after power up
command_find("lcd");
// setup terminal
terminal_prefix = ""; // set default prefix
terminal_process = &process_command; // set central function to process commands
terminal_setup(); // start terminal
// start main loop
uint32_t last_connect_time = rtc_get_counter_val(); // last time a USB cable has been connected/disconnected
bool interactive = false; // if there is user activity on the serial port
bool action = false; // if an action has been performed don't go to sleep
while (true) { // infinite loop
iwdg_reset(); // kick the dog
if (user_input_available) { // user input is available
action = true; // action has been performed
//led_toggle(); // toggle LED
if (!interactive) { // the user takes control
interactive = true; // remember to not shut down anymore
// show interactive mode on LCD
const char* lcd_interactive_line1 = "interactive mode";
const char* lcd_interactive_line2 = "over serial port";
lcd_hd44780_clear_display();
lcd_hd44780_write_line(false, lcd_interactive_line1, strlen(lcd_interactive_line1));
lcd_hd44780_write_line(true, lcd_interactive_line2, strlen(lcd_interactive_line2));
}
char c = user_input_get(); // store receive character
terminal_send(c); // send received character to terminal
}
if (rtc_internal_tick_flag) { // the internal RTC ticked
rtc_internal_tick_flag = false; // reset flag
action = true; // action has been performed
//led_toggle(); // toggle LED (good to indicate if main function is stuck)
while (!interactive && rtc_get_counter_val() >= last_connect_time + SHUTDOWN_TIMEOUT) { // time to shut down
display_off(); // cut power to displays (at stop D+ pull-up to indicate disconnect)
SCB_SCR |= SCB_SCR_SLEEPDEEP; // Cortex-M3 standby setting
pwr_set_standby_mode(); // power setting
pwr_clear_wakeup_flag(); // clear wake-up flag to be able to sleep
__WFI(); // go to standby (e.g. shut down)
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;