-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_console.h
90 lines (63 loc) · 2.53 KB
/
main_console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
static bool serial_getline (char * const serial_buffer_data, const int serial_buffer_max, int * const serial_buffer_size) {
int c;
while ((c = getchar_timeout_us (0)) != PICO_ERROR_TIMEOUT) {
if (c > 0 && c < 255) {
if ((c == '\n' || c == '\r') || (*serial_buffer_size) == (serial_buffer_max - 1)) {
if ((*serial_buffer_size) > 0) {
serial_buffer_data [(*serial_buffer_size)] = '\0';
(*serial_buffer_size) = 0;
return true;
}
} else
serial_buffer_data [(*serial_buffer_size) ++] = (char) c;
}
}
return false;
}
// ------------------------------------------------------------------------------------------------------------------------
static void console_send (const command_t command) {
fprintf (stdout, command.args != NULL ? "%s %s\n" : "%s\n", command.name, command.args);
}
static bool console_recv (command_t * const command, char * const string) {
char * pointer = string;
command->name = pointer;
while (*pointer != '\0' && *pointer != ' ')
pointer ++;
if (*pointer == ' ') {
*pointer ++ = '\0';
while (*pointer == ' ')
pointer ++;
}
if (command->name [0] == '\0' || command->name [0] == ' ')
return false;
command->args = pointer;
return true;
}
static void console_open (void) {
stdio_init_all ();
}
static void console_close (void) {
}
static bool console_timeout (const bool hastimedout) {
static uint32_t prev = 0;
uint32_t curr = (uint32_t) (time_us_64 () / 1000);
if (!hastimedout)
prev = curr;
else if ((curr - prev) > CONSOLE_TIMEOUT)
return true;
return false;
}
static bool console_obtain__command (command_t * const command) {
static char data [CONSOLE_BUFFER];
static int offs = 0;
if (serial_getline (data, CONSOLE_BUFFER, &offs) && console_recv (command, data)) {
console_timeout (false);
return true;
}
return false;
}
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------