diff options
Diffstat (limited to 'avr/con-utils.c')
-rw-r--r-- | avr/con-utils.c | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/avr/con-utils.c b/avr/con-utils.c new file mode 100644 index 0000000..5ea19fd --- /dev/null +++ b/avr/con-utils.c @@ -0,0 +1,126 @@ +/* + * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "common.h" +#include <avr/wdt.h> + +#include "config.h" +#include "serial.h" +#include "background.h" +#include "con-utils.h" + +uint_fast8_t tstc(void) +{ + bg_shed(); + return serial_tstc(); +} + +int my_getchar(uint_fast8_t waitforchar) +{ + int c; + + do { + bg_shed(); + c = serial_getc(); + } while ((c < 0) && waitforchar); + +#ifdef CONFIG_SYS_FBOOTSIG + if (c < 0) + return c; + + static const FLASH unsigned char bootsig[] = {CONFIG_SYS_FBOOTSIG}; + static uint8_t pb; + unsigned char uc = c; + + + if (bootsig[pb] == 0) { + if (uc == 0xff) { + wdt_enable(WDTO_15MS); + for(;;) + ; + } else + pb = 0; + + } else { + if (bootsig[pb] == uc) + pb++; + else + pb = 0; + } +#endif + + return c; +} + + +/* test if ctrl-c was pressed */ + +static uint_fast8_t ctrlc_disabled; /* see disable_ctrl() */ +static uint_fast8_t ctrlc_was_pressed; + +uint_fast8_t ctrlc(void) +{ + bg_shed(); + if (!ctrlc_disabled) { + switch (serial_getc()) { + case 0x03: /* ^C - Control C */ + ctrlc_was_pressed = 1; + return 1; + default: + break; + } + } + return 0; +} + +/* Reads user's confirmation. + Returns 1 if user's input is "y", "Y", "yes" or "YES" +*/ +uint_fast8_t confirm_yesno(void) +{ + unsigned int i; + char str_input[5]; + + /* Flush input */ + while (serial_getc()) + ; + i = 0; + while (i < sizeof(str_input)) { + str_input[i] = my_getchar(1); + putchar(str_input[i]); + if (str_input[i] == '\r') + break; + i++; + } + putchar('\n'); + if (strncmp(str_input, "y\r", 2) == 0 || + strncmp(str_input, "Y\r", 2) == 0 || + strncmp(str_input, "yes\r", 4) == 0 || + strncmp(str_input, "YES\r", 4) == 0) + return 1; + return 0; +} + +/* pass 1 to disable ctrlc() checking, 0 to enable. + * returns previous state + */ +uint_fast8_t disable_ctrlc(uint_fast8_t disable) +{ + uint_fast8_t prev = ctrlc_disabled; /* save previous state */ + + ctrlc_disabled = disable; + return prev; +} + +uint_fast8_t had_ctrlc (void) +{ + return ctrlc_was_pressed; +} + +void clear_ctrlc(void) +{ + ctrlc_was_pressed = 0; +} |