-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaestroCmd.cpp
135 lines (122 loc) · 3.36 KB
/
MaestroCmd.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
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
/*
* Copyright (C) 2016 Mikhail Sapozhnikov
*
* This file is part of ship-control.
*
* ship-control is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ship-control is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ship-control. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "MaestroCmd.hpp"
#include <unistd.h>
#include <cstring>
namespace shipcontrol
{
// creates command with up to 3 data bytes
MaestroCmd::MaestroCmd(int fd, MaestroCmdCode code,
unsigned char byte1,
unsigned char byte2,
unsigned char byte3) :
_fd(fd)
{
_cmd[0] = static_cast<unsigned char>(code);
_cmd[1] = byte1;
_cmd[2] = byte2;
_cmd[3] = byte3;
_log = Log::getInstance();
}
// execute command and get response
unsigned char *MaestroCmd::send()
{
// calculate command length
unsigned char len = cmdLen();
std::memset((void *)_rsp, 0, MAESTRO_RSP_LEN);
if (_fd == -1)
{
goto exit;
}
if (len < 1)
{
goto exit;
}
if (write(_fd, (const void *)_cmd, len) == -1)
{
_log->write(LogLevel::ERROR,
"MaestroCmd couldn't send command to Maestro, error code %d\n",
errno);
goto exit;
}
_log->write(LogLevel::DEBUG, "MaestroCmd sent command: ");
for (int i = 0; i < len; i++)
{
_log->write(LogLevel::DEBUG, "0x%x", _cmd[i]);
}
_log->write(LogLevel::DEBUG, "\n");
// calculate response length
len = rspLen();
if (len > 0)
{
if (read(_fd, (void *)_rsp, len) == -1)
{
_log->write(LogLevel::ERROR,
"MaestroCmd couldn't read response from Maestro, error code %d\n",
errno);
goto exit;
}
_log->write(LogLevel::DEBUG, "MaestroCmd received response: ");
for (int i = 0; i < len; i++)
{
_log->write(LogLevel::DEBUG, "0x%x ", _rsp[i]);
}
_log->write(LogLevel::DEBUG, "\n");
}
exit:
return _rsp;
}
unsigned char MaestroCmd::cmdLen()
{
switch (static_cast<MaestroCmdCode>(_cmd[0]))
{
case MaestroCmdCode::SETTARGET:
case MaestroCmdCode::SETSPEED:
case MaestroCmdCode::SETACCEL:
return 4;
case MaestroCmdCode::GETPOS:
return 2;
case MaestroCmdCode::GETMOVINGSTATE:
case MaestroCmdCode::GETERRORS:
case MaestroCmdCode::GOHOME:
return 1;
default:
return 0;
}
}
unsigned char MaestroCmd::rspLen()
{
switch (static_cast<MaestroCmdCode>(_cmd[0]))
{
case MaestroCmdCode::SETTARGET:
case MaestroCmdCode::SETSPEED:
case MaestroCmdCode::SETACCEL:
case MaestroCmdCode::GOHOME:
return 0;
case MaestroCmdCode::GETPOS:
case MaestroCmdCode::GETERRORS:
return 2;
case MaestroCmdCode::GETMOVINGSTATE:
return 1;
default:
return 0;
}
}
} // namespace shipcontrol