-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.cpp
47 lines (33 loc) · 1002 Bytes
/
Command.cpp
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
#include "Command.h"
#include "error.h"
#include <stdlib.h>
#include <errno.h>
Command::Command(uint8_t type) : type(type) {}
PositionCommand::PositionCommand(uint8_t type, int servoId, long p) : Command(type), servoId(servoId), p(p) {}
PositionCommand::PositionCommand(int servoId, long p)
: PositionCommand(Command::POSITION, servoId, p) {}
PositionCommand * PositionCommand::fromString(char *string) {
errno = 0;
if (string[0] != CMD_CODE_POSITION) {
return 0;
}
char *paramStart = string + 1;
char *nextParam = paramStart;
int servoId = strtol(paramStart, &nextParam, 10);
if ((nextParam == paramStart) || (errno == ERANGE)) {
errno = EFORMAT;
return 0;
}
paramStart = nextParam + 1;
long p = strtol(paramStart, &nextParam, 10);
if ((nextParam == paramStart) || (errno == ERANGE)) {
errno = EFORMAT;
return 0;
}
if (nextParam[0] != CMD_BREAK) {
errno = ETOOBIG;
return 0;
}
PositionCommand *command = new PositionCommand(servoId, p);
return command;
}