]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/con-utils.c
b8017ed4f7f70a1528ef2e747f1d5d645efb5925
[z180-stamp.git] / avr / con-utils.c
1
2 #include <string.h>
3 #include "common.h"
4
5 #include "serial.h"
6 #include "background.h"
7 #include "con-utils.h"
8
9 uint_fast8_t tstc(void)
10 {
11 bg_shed();
12 return serial_tstc();
13 }
14
15 int my_getchar(uint_fast8_t waitforchar)
16 {
17 int c;
18
19 do {
20 bg_shed();
21 c = serial_getc();
22 } while ((c < 0) && waitforchar);
23
24 return c;
25 }
26
27
28 /* test if ctrl-c was pressed */
29
30 static uint_fast8_t ctrlc_disabled; /* see disable_ctrl() */
31 static uint_fast8_t ctrlc_was_pressed;
32
33 uint_fast8_t ctrlc(void)
34 {
35 if (!ctrlc_disabled) {
36 switch (serial_getc()) {
37 case 0x03: /* ^C - Control C */
38 ctrlc_was_pressed = 1;
39 return 1;
40 default:
41 break;
42 }
43 }
44 return 0;
45 }
46
47 /* Reads user's confirmation.
48 Returns 1 if user's input is "y", "Y", "yes" or "YES"
49 */
50 uint_fast8_t confirm_yesno(void)
51 {
52 unsigned int i;
53 char str_input[5];
54
55 /* Flush input */
56 while (serial_getc())
57 ;
58 i = 0;
59 while (i < sizeof(str_input)) {
60 str_input[i] = my_getchar(1);
61 putchar(str_input[i]);
62 if (str_input[i] == '\r')
63 break;
64 i++;
65 }
66 putchar('\n');
67 if (strncmp(str_input, "y\r", 2) == 0 ||
68 strncmp(str_input, "Y\r", 2) == 0 ||
69 strncmp(str_input, "yes\r", 4) == 0 ||
70 strncmp(str_input, "YES\r", 4) == 0)
71 return 1;
72 return 0;
73 }
74
75 /* pass 1 to disable ctrlc() checking, 0 to enable.
76 * returns previous state
77 */
78 uint_fast8_t disable_ctrlc(uint_fast8_t disable)
79 {
80 uint_fast8_t prev = ctrlc_disabled; /* save previous state */
81
82 ctrlc_disabled = disable;
83 return prev;
84 }
85
86 uint_fast8_t had_ctrlc (void)
87 {
88 return ctrlc_was_pressed;
89 }
90
91 void clear_ctrlc(void)
92 {
93 ctrlc_was_pressed = 0;
94 }
95