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