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