]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/con-utils.c
Merge branch 'fatfs-integration' into fatcommands
[z180-stamp.git] / avr / con-utils.c
CommitLineData
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
L
8#include <string.h>
9#include <avr/wdt.h>
d684c216 10
c748023e 11#include "config.h"
d684c216 12#include "serial.h"
72f58822 13#include "background.h"
d684c216
L
14#include "con-utils.h"
15
d684c216
L
16uint_fast8_t tstc(void)
17{
72f58822 18 bg_shed();
d684c216
L
19 return serial_tstc();
20}
21
424b184a 22int my_getchar(uint_fast8_t waitforchar)
d684c216
L
23{
24 int c;
c79c80b4 25
72f58822
L
26 do {
27 bg_shed();
28 c = serial_getc();
424b184a 29 } while ((c < 0) && waitforchar);
72f58822 30
c748023e
L
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
d684c216
L
56 return c;
57}
58
59
60/* test if ctrl-c was pressed */
61
72f58822
L
62static uint_fast8_t ctrlc_disabled; /* see disable_ctrl() */
63static uint_fast8_t ctrlc_was_pressed;
d684c216
L
64
65uint_fast8_t ctrlc(void)
66{
a8b4c964 67 bg_shed();
d684c216
L
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*/
1cdd02d8
L
83uint_fast8_t confirm_yes(void)
84{
85 uint_fast8_t checkch, ch;
86
87 checkch = ch = my_getchar(1);
88 putchar(ch);
89 while (ch != '\r') {
90 ch = my_getchar(1);
91 putchar(ch);
92 }
93 putchar('\n');
94
95 return (checkch == 'y');
96}
97
98#if 0
d684c216
L
99uint_fast8_t confirm_yesno(void)
100{
101 unsigned int i;
102 char str_input[5];
103
104 /* Flush input */
1cdd02d8 105 while (serial_getc() < 0)
d684c216
L
106 ;
107 i = 0;
108 while (i < sizeof(str_input)) {
424b184a 109 str_input[i] = my_getchar(1);
d684c216
L
110 putchar(str_input[i]);
111 if (str_input[i] == '\r')
112 break;
113 i++;
114 }
115 putchar('\n');
116 if (strncmp(str_input, "y\r", 2) == 0 ||
117 strncmp(str_input, "Y\r", 2) == 0 ||
118 strncmp(str_input, "yes\r", 4) == 0 ||
119 strncmp(str_input, "YES\r", 4) == 0)
120 return 1;
121 return 0;
122}
1cdd02d8 123#endif
d684c216
L
124
125/* pass 1 to disable ctrlc() checking, 0 to enable.
126 * returns previous state
127 */
128uint_fast8_t disable_ctrlc(uint_fast8_t disable)
129{
130 uint_fast8_t prev = ctrlc_disabled; /* save previous state */
131
132 ctrlc_disabled = disable;
133 return prev;
134}
135
136uint_fast8_t had_ctrlc (void)
137{
138 return ctrlc_was_pressed;
139}
140
141void clear_ctrlc(void)
142{
143 ctrlc_was_pressed = 0;
144}