-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathMeadeParser.cpp
More file actions
143 lines (131 loc) · 3.64 KB
/
MeadeParser.cpp
File metadata and controls
143 lines (131 loc) · 3.64 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @file MeadeParser.cpp
* @brief Top-level entry points for the Meade LX200 command parser.
*
* Contains `parseMeadeCommand` (classifier) and `dispatchMeadeCommand`
* (unified parse + dispatch). Family-specific handlers live in their own
* files (MeadeParserGet.cpp, MeadeParserSet.cpp, etc.).
*/
#include "MeadeParser.hpp"
#include "MeadeParserHelpers.hpp"
#include <stddef.h>
namespace oat
{
namespace core
{
namespace meade
{
// ---------------------------------------------------------------------------
// Top-level parser
// ---------------------------------------------------------------------------
bool parseMeadeCommand(const char *input, MeadeParseResult &result)
{
if (input == nullptr || input[0] != ':')
{
return false;
}
// Copy the input into a stack buffer with whitespace stripped and the
// optional trailing `#` removed. Using a fixed-capacity char buffer keeps
// the parser usable on bare AVR builds that lack libstdc++ (`std::string`).
char normalized[MeadeResponse::Capacity];
size_t nlen = 0;
for (const char *cursor = input; *cursor != '\0' && nlen + 1 < sizeof(normalized); ++cursor)
{
if (*cursor != ' ')
{
normalized[nlen++] = *cursor;
}
}
normalized[nlen] = '\0';
if (nlen < 2)
{
return false;
}
if (normalized[nlen - 1] == '#')
{
--nlen;
normalized[nlen] = '\0';
}
if (nlen < 2)
{
return false;
}
const char family = normalized[1];
switch (family)
{
case 'S':
case 'M':
case 'G':
case 'g':
case 'C':
case 'h':
case 'I':
case 'Q':
case 'R':
case 'D':
case 'X':
case 'F':
result.valid = true;
result.family = family;
result.payload.assign(normalized + 2);
return true;
default:
return false;
}
}
// ---------------------------------------------------------------------------
// Unified dispatch — parse + classify + dispatch in one call
// ---------------------------------------------------------------------------
void dispatchMeadeCommand(MeadeResponse &r, const char *input, IMeadeHandlers &h)
{
r.clear();
MeadeParseResult parsed;
if (!parseMeadeCommand(input, parsed))
{
return;
}
switch (parsed.family)
{
case 'S':
handleMeadeSet(r, parsed.payload.c_str(), h);
break;
case 'M':
handleMeadeMovement(r, parsed.payload.c_str(), h);
break;
case 'G':
handleMeadeGet(r, parsed.payload.c_str(), h);
break;
case 'g':
handleMeadeGps(r, parsed.payload.c_str(), h);
break;
case 'C':
handleMeadeSyncControl(r, parsed.payload.c_str(), h);
break;
case 'h':
handleMeadeHome(r, parsed.payload.c_str(), h);
break;
case 'I':
handleMeadeInit(r, parsed.payload.c_str(), h);
break;
case 'Q':
handleMeadeQuit(r, parsed.payload.c_str(), h);
break;
case 'R':
handleMeadeSetSlewRate(r, parsed.payload.c_str(), h);
break;
case 'D':
handleMeadeDistance(r, parsed.payload.c_str(), h);
break;
case 'X':
handleMeadeExtra(r, parsed.payload.c_str(), h);
break;
case 'F':
handleMeadeFocus(r, parsed.payload.c_str(), h);
break;
default:
break;
}
}
} // namespace meade
} // namespace core
} // namespace oat