]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
d684c216 | 6 | |
72f58822 | 7 | #include "common.h" |
c748023e | 8 | #include <avr/wdt.h> |
d684c216 | 9 | |
c748023e | 10 | #include "config.h" |
d684c216 | 11 | #include "serial.h" |
72f58822 | 12 | #include "background.h" |
d684c216 L |
13 | #include "con-utils.h" |
14 | ||
d684c216 L |
15 | uint_fast8_t tstc(void) |
16 | { | |
72f58822 | 17 | bg_shed(); |
d684c216 L |
18 | return serial_tstc(); |
19 | } | |
20 | ||
424b184a | 21 | int my_getchar(uint_fast8_t waitforchar) |
d684c216 L |
22 | { |
23 | int c; | |
c79c80b4 | 24 | |
72f58822 L |
25 | do { |
26 | bg_shed(); | |
27 | c = serial_getc(); | |
424b184a | 28 | } while ((c < 0) && waitforchar); |
72f58822 | 29 | |
c748023e L |
30 | #ifdef CONFIG_SYS_FBOOTSIG |
31 | if (c < 0) | |
32 | return c; | |
33 | ||
34 | static const FLASH unsigned char bootsig[] = {CONFIG_SYS_FBOOTSIG}; | |
35 | static uint8_t pb; | |
36 | unsigned char uc = c; | |
37 | ||
38 | ||
39 | if (bootsig[pb] == 0) { | |
40 | if (uc == 0xff) { | |
41 | wdt_enable(WDTO_15MS); | |
42 | for(;;) | |
43 | ; | |
44 | } else | |
45 | pb = 0; | |
46 | ||
47 | } else { | |
48 | if (bootsig[pb] == uc) | |
49 | pb++; | |
50 | else | |
51 | pb = 0; | |
52 | } | |
53 | #endif | |
54 | ||
d684c216 L |
55 | return c; |
56 | } | |
57 | ||
58 | ||
59 | /* test if ctrl-c was pressed */ | |
60 | ||
72f58822 L |
61 | static uint_fast8_t ctrlc_disabled; /* see disable_ctrl() */ |
62 | static uint_fast8_t ctrlc_was_pressed; | |
d684c216 L |
63 | |
64 | uint_fast8_t ctrlc(void) | |
65 | { | |
a8b4c964 | 66 | bg_shed(); |
d684c216 L |
67 | if (!ctrlc_disabled) { |
68 | switch (serial_getc()) { | |
69 | case 0x03: /* ^C - Control C */ | |
70 | ctrlc_was_pressed = 1; | |
71 | return 1; | |
72 | default: | |
73 | break; | |
74 | } | |
75 | } | |
76 | return 0; | |
77 | } | |
78 | ||
79 | /* Reads user's confirmation. | |
80 | Returns 1 if user's input is "y", "Y", "yes" or "YES" | |
81 | */ | |
82 | uint_fast8_t confirm_yesno(void) | |
83 | { | |
84 | unsigned int i; | |
85 | char str_input[5]; | |
86 | ||
87 | /* Flush input */ | |
88 | while (serial_getc()) | |
89 | ; | |
90 | i = 0; | |
91 | while (i < sizeof(str_input)) { | |
424b184a | 92 | str_input[i] = my_getchar(1); |
d684c216 L |
93 | putchar(str_input[i]); |
94 | if (str_input[i] == '\r') | |
95 | break; | |
96 | i++; | |
97 | } | |
98 | putchar('\n'); | |
99 | if (strncmp(str_input, "y\r", 2) == 0 || | |
100 | strncmp(str_input, "Y\r", 2) == 0 || | |
101 | strncmp(str_input, "yes\r", 4) == 0 || | |
102 | strncmp(str_input, "YES\r", 4) == 0) | |
103 | return 1; | |
104 | return 0; | |
105 | } | |
106 | ||
107 | /* pass 1 to disable ctrlc() checking, 0 to enable. | |
108 | * returns previous state | |
109 | */ | |
110 | uint_fast8_t disable_ctrlc(uint_fast8_t disable) | |
111 | { | |
112 | uint_fast8_t prev = ctrlc_disabled; /* save previous state */ | |
113 | ||
114 | ctrlc_disabled = disable; | |
115 | return prev; | |
116 | } | |
117 | ||
118 | uint_fast8_t had_ctrlc (void) | |
119 | { | |
120 | return ctrlc_was_pressed; | |
121 | } | |
122 | ||
123 | void clear_ctrlc(void) | |
124 | { | |
125 | ctrlc_was_pressed = 0; | |
126 | } |