add monitor sample
@@ -5,6 +5,9 @@ | ||
5 | 5 | \author Satofumi KAMIMURA |
6 | 6 | |
7 | 7 | $Id$ |
8 | + | |
9 | + \todo 記録データにコメントを付加できるようにする | |
10 | + \todo ログデータから、各データ毎に1行目を除去するスクリプトを作成する | |
8 | 11 | */ |
9 | 12 | |
10 | 13 | #include "MonitorDataHandler.h" |
@@ -30,9 +33,10 @@ | ||
30 | 33 | } |
31 | 34 | |
32 | 35 | virtual int fetchFirstLine(const int size) = 0; |
33 | - virtual void fetch(char* data, int size) = 0; | |
34 | - virtual void fetch(std::string& data) = 0; | |
35 | - virtual void fetch(bool& data) = 0; | |
36 | + virtual int fetch(char* data, int size) = 0; | |
37 | + virtual int fetch(std::string& data) = 0; | |
38 | + virtual int fetch(bool& data) = 0; | |
39 | + virtual int fetch(int& data) = 0; | |
36 | 40 | }; |
37 | 41 | |
38 | 42 |
@@ -66,23 +70,37 @@ | ||
66 | 70 | } |
67 | 71 | |
68 | 72 | |
69 | - void fetch(char* data, int size) | |
73 | + int fetch(char* data, int size) | |
70 | 74 | { |
71 | 75 | fout_.write(data, size); |
72 | 76 | fout_ << std::endl; |
77 | + | |
78 | + return size; | |
73 | 79 | } |
74 | 80 | |
75 | 81 | |
76 | - void fetch(std::string& data) | |
82 | + int fetch(std::string& data) | |
77 | 83 | { |
78 | 84 | fout_ << data << std::endl; |
85 | + | |
86 | + return data.size(); | |
79 | 87 | } |
80 | 88 | |
81 | 89 | |
82 | - void fetch(bool& data) | |
90 | + int fetch(bool& data) | |
83 | 91 | { |
84 | 92 | fout_ << data << std::endl; |
93 | + | |
94 | + return data; | |
85 | 95 | } |
96 | + | |
97 | + | |
98 | + int fetch(int& data) | |
99 | + { | |
100 | + fout_ << data << std::endl; | |
101 | + | |
102 | + return data; | |
103 | + } | |
86 | 104 | }; |
87 | 105 | |
88 | 106 |
@@ -109,16 +127,19 @@ | ||
109 | 127 | |
110 | 128 | int fetchFirstLine(const int size) |
111 | 129 | { |
130 | + static_cast<void>(size); | |
131 | + | |
112 | 132 | // 1行目の読み出し |
113 | 133 | std::string line; |
114 | 134 | getline(fin_, line); |
115 | 135 | std::vector<std::string> tokens; |
116 | 136 | if (split(tokens, line, ",") != 2) { |
137 | + fprintf(stderr, "mismatch log.\n"); | |
117 | 138 | return 0; |
118 | 139 | } |
119 | 140 | |
120 | 141 | int ticks = atoi(tokens[0].c_str()); |
121 | - fprintf(stderr, "ticks: %d\n", ticks); | |
142 | + static_cast<void>(ticks); | |
122 | 143 | // !!! 指定 ticks まで待たせる |
123 | 144 | // !!! 指定時間まで delay() し、再評価を行うメソッドをここに配置する |
124 | 145 |
@@ -134,20 +155,33 @@ | ||
134 | 155 | } |
135 | 156 | |
136 | 157 | |
137 | - void fetch(char* data, int size) | |
158 | + int fetch(char* data, int size) | |
138 | 159 | { |
139 | - fin_.read(data, size); | |
160 | + if (data) { | |
161 | + fin_.read(data, size); | |
162 | + } else { | |
163 | + // 読み捨てる | |
164 | + // !!! 固定長バッファでの読み出しに変更すべき | |
165 | + for (int i = 0; i < size; ++i) { | |
166 | + char ch; | |
167 | + fin_.read(&ch, 1); | |
168 | + } | |
169 | + } | |
140 | 170 | skipEndl(); |
171 | + | |
172 | + return size; | |
141 | 173 | } |
142 | 174 | |
143 | 175 | |
144 | - void fetch(std::string& data) | |
176 | + int fetch(std::string& data) | |
145 | 177 | { |
146 | 178 | getline(fin_, data); |
179 | + | |
180 | + return data.size(); | |
147 | 181 | } |
148 | 182 | |
149 | 183 | |
150 | - void fetch(bool& data) | |
184 | + int fetch(bool& data) | |
151 | 185 | { |
152 | 186 | char ch; |
153 | 187 | fin_.read(&ch, 1); |
@@ -154,7 +188,17 @@ | ||
154 | 188 | skipEndl(); |
155 | 189 | |
156 | 190 | data = (ch == 0) ? false : true; |
191 | + return data; | |
157 | 192 | } |
193 | + | |
194 | + | |
195 | + int fetch(int& data) | |
196 | + { | |
197 | + fin_ >> data; | |
198 | + skipEndl(); | |
199 | + | |
200 | + return data; | |
201 | + } | |
158 | 202 | }; |
159 | 203 | }; |
160 | 204 |
@@ -181,26 +225,33 @@ | ||
181 | 225 | } |
182 | 226 | |
183 | 227 | |
184 | - void fetch(char* data, int size) | |
228 | + int fetch(char* data, int size) | |
185 | 229 | { |
186 | 230 | int data_size = handler_->fetchFirstLine(size); |
187 | - handler_->fetch(data, data_size); | |
231 | + return handler_->fetch(data, data_size); | |
188 | 232 | } |
189 | 233 | |
190 | 234 | |
191 | - void fetch(std::string& data) | |
235 | + int fetch(std::string& data) | |
192 | 236 | { |
193 | 237 | int size = data.size(); |
194 | 238 | handler_->fetchFirstLine(size); |
195 | - handler_->fetch(data); | |
239 | + return handler_->fetch(data); | |
196 | 240 | } |
197 | 241 | |
198 | 242 | |
199 | - void fetch(bool& data) | |
243 | + int fetch(bool& data) | |
200 | 244 | { |
201 | 245 | handler_->fetchFirstLine(1); |
202 | - handler_->fetch(data); | |
246 | + return handler_->fetch(data); | |
203 | 247 | } |
248 | + | |
249 | + | |
250 | + int fetch(int& data) | |
251 | + { | |
252 | + handler_->fetchFirstLine(DummySize); | |
253 | + return handler_->fetch(data); | |
254 | + } | |
204 | 255 | }; |
205 | 256 | |
206 | 257 |
@@ -216,19 +267,25 @@ | ||
216 | 267 | } |
217 | 268 | |
218 | 269 | |
219 | -void MonitorDataHandler::fetch(char* data, int size) | |
270 | +int MonitorDataHandler::fetch(char* data, int size) | |
220 | 271 | { |
221 | - pimpl->fetch(data, size); | |
272 | + return pimpl->fetch(data, size); | |
222 | 273 | } |
223 | 274 | |
224 | 275 | |
225 | -void MonitorDataHandler::fetch(std::string& data) | |
276 | +int MonitorDataHandler::fetch(std::string& data) | |
226 | 277 | { |
227 | - pimpl->fetch(data); | |
278 | + return pimpl->fetch(data); | |
228 | 279 | } |
229 | 280 | |
230 | 281 | |
231 | -void MonitorDataHandler::fetch(bool& data) | |
282 | +int MonitorDataHandler::fetch(bool& data) | |
232 | 283 | { |
233 | - pimpl->fetch(data); | |
284 | + return pimpl->fetch(data); | |
234 | 285 | } |
286 | + | |
287 | + | |
288 | +int MonitorDataHandler::fetch(int& data) | |
289 | +{ | |
290 | + return pimpl->fetch(data); | |
291 | +} |
@@ -35,7 +35,7 @@ | ||
35 | 35 | char buffer[BufferSize]; |
36 | 36 | snprintf(buffer, BufferSize, "_%d", id); |
37 | 37 | |
38 | - return (std::string(baseName) + buffer).c_str(); | |
38 | + return (std::string(baseName) + buffer + ".log").c_str(); | |
39 | 39 | } |
40 | 40 | }; |
41 | 41 |
@@ -19,7 +19,7 @@ | ||
19 | 19 | /*! |
20 | 20 | \brief モニタ対応の通信インターフェース |
21 | 21 | */ |
22 | - class mConnection : Connection | |
22 | + class mConnection : public Connection | |
23 | 23 | { |
24 | 24 | mConnection(void); |
25 | 25 | mConnection(const mConnection& rhs); |
@@ -0,0 +1,51 @@ | ||
1 | +/*! | |
2 | + \file | |
3 | + \brief mUrgCtrl の動作サンプル | |
4 | + | |
5 | + \author Satofumi KAMIMURA | |
6 | + | |
7 | + $Id$ | |
8 | +*/ | |
9 | + | |
10 | +#include "mConnection.h" | |
11 | +#include "SerialCtrl.h" | |
12 | +#include "ConnectionUtils.h" | |
13 | +#include "MonitorModeManager.h" | |
14 | + | |
15 | +using namespace qrk; | |
16 | + | |
17 | + | |
18 | +int main(int argc, char *argv[]) | |
19 | +{ | |
20 | + MonitorModeManager::singleton()->setMode(argc, argv); | |
21 | + | |
22 | + SerialCtrl serial; | |
23 | + mConnection connection(&serial); | |
24 | + | |
25 | + // 接続 | |
26 | + if (! connection.connect("/dev/ttyACM0", 19200)) { | |
27 | + printf("SerialCtrl::connect: %s\n", connection.what()); | |
28 | + exit(1); | |
29 | + } | |
30 | + | |
31 | + // 文字列送信 | |
32 | + printf("--- send data ---\n"); | |
33 | + connection.send("V\r", 2); | |
34 | + | |
35 | + // 文字列受信 | |
36 | + printf("--- recv data ---\n"); | |
37 | + enum { | |
38 | + BufferSize = 255, | |
39 | + Timeout = 1000, | |
40 | + }; | |
41 | + char buffer[BufferSize]; | |
42 | + while (1) { | |
43 | + int recv_size = readline(&connection, buffer, BufferSize, Timeout); | |
44 | + if (recv_size <= 0) { | |
45 | + break; | |
46 | + } | |
47 | + printf("%s\n", buffer); | |
48 | + } | |
49 | + | |
50 | + return 0; | |
51 | +} |
@@ -4,18 +4,19 @@ | ||
4 | 4 | |
5 | 5 | # Compile options |
6 | 6 | CC = g++ |
7 | -CXXFLAGS = -g -O0 -Wall -Werror ${INCLUDES} | |
8 | -INCLUDES = -I.. | |
7 | +CXXFLAGS = -g -O0 -Wall -Werror ${INCLUDES} `sdl-config --cflags` | |
8 | +INCLUDES = -I.. -I../../connection | |
9 | 9 | LDFLAGS = |
10 | -LDLIBS = | |
10 | +LDLIBS = `sdl-config --libs` | |
11 | 11 | |
12 | 12 | # Target |
13 | 13 | TARGET = \ |
14 | + mConnectionSample \ | |
14 | 15 | |
15 | 16 | all : ${TARGET} |
16 | 17 | |
17 | 18 | clean : |
18 | - ${RM} *.o ${TARGET} | |
19 | + ${RM} *.o ${TARGET} mConnection_0.log | |
19 | 20 | |
20 | 21 | depend : |
21 | 22 | makedepend -Y -- ${INCLUDES} -- ${wildcard *.h *.cpp} |
@@ -22,7 +23,7 @@ | ||
22 | 23 | |
23 | 24 | .PHONY : all clean depend |
24 | 25 | ###################################################################### |
25 | -REQUIRE_LIBS = ../monitor.a | |
26 | +REQUIRE_LIBS = ../monitor.a ../../connection/connection.a ../../common/common.a ../../system/sdl/system_sdl.a | |
26 | 27 | |
27 | 28 | ${REQUIRE_LIBS} : |
28 | 29 | cd ${@D}/ && ${MAKE} ${@F} |
@@ -30,3 +31,8 @@ | ||
30 | 31 | ${TARGET} : ${REQUIRE_LIBS} |
31 | 32 | |
32 | 33 | # DO NOT DELETE |
34 | + | |
35 | +mConnectionSample.o: ../mConnection.h ../../connection/Connection.h | |
36 | +mConnectionSample.o: ../../connection/SerialCtrl.h | |
37 | +mConnectionSample.o: ../../connection/ConnectionUtils.h | |
38 | +mConnectionSample.o: ../MonitorModeManager.h |
@@ -37,15 +37,16 @@ | ||
37 | 37 | MonitorModeManager::MonitorMode mode); |
38 | 38 | ~MonitorDataHandler(void); |
39 | 39 | |
40 | - void fetch(char* data, int size); | |
40 | + int fetch(char* data, int size); | |
41 | 41 | |
42 | 42 | /*! |
43 | 43 | \attention 改行が複数ある文字列データは、扱えない |
44 | 44 | */ |
45 | - void fetch(std::string& data); | |
46 | - // !!! | |
45 | + int fetch(std::string& data); | |
47 | 46 | |
48 | - void fetch(bool& data); | |
47 | + int fetch(bool& data); | |
48 | + | |
49 | + int fetch(int& data); | |
49 | 50 | }; |
50 | 51 | }; |
51 | 52 |
@@ -56,7 +56,7 @@ | ||
56 | 56 | return false; |
57 | 57 | |
58 | 58 | } else { |
59 | - return false; | |
59 | + return con_->connect(device, baudrate); | |
60 | 60 | } |
61 | 61 | } |
62 | 62 |
@@ -64,7 +64,8 @@ | ||
64 | 64 | const char* what(void) |
65 | 65 | { |
66 | 66 | // エラーメッセージも記録と再生の対象とする |
67 | - if (mode_ != MonitorModeManager::Simulation) { | |
67 | + if ((mode_ == MonitorModeManager::Record) || | |
68 | + (mode_ == MonitorModeManager::Play)) { | |
68 | 69 | std::string error_message = std::string(con_->what()); |
69 | 70 | handler_->fetch(error_message); |
70 | 71 | return error_message.c_str(); |
@@ -77,6 +78,108 @@ | ||
77 | 78 | return con_->what(); |
78 | 79 | } |
79 | 80 | } |
81 | + | |
82 | + | |
83 | + bool changeBaudrate(long baudrate) | |
84 | + { | |
85 | + // 戻り値のみを記録対象とする | |
86 | + if ((mode_ == MonitorModeManager::Record) || | |
87 | + (mode_ == MonitorModeManager::Play)) { | |
88 | + bool ret = con_->changeBaudrate(baudrate); | |
89 | + handler_->fetch(ret); | |
90 | + return ret; | |
91 | + | |
92 | + } else if (mode_ == MonitorModeManager::Simulation) { | |
93 | + // !!! | |
94 | + return false; | |
95 | + | |
96 | + } else { | |
97 | + return con_->changeBaudrate(baudrate); | |
98 | + } | |
99 | + } | |
100 | + | |
101 | + | |
102 | + bool isConnected(void) | |
103 | + { | |
104 | + // !!! 戻り値のみを記録対象とする | |
105 | + return false; | |
106 | + | |
107 | + if ((mode_ == MonitorModeManager::Record) || | |
108 | + (mode_ == MonitorModeManager::Play)) { | |
109 | + bool ret = con_->isConnected(); | |
110 | + handler_->fetch(ret); | |
111 | + return ret; | |
112 | + | |
113 | + } else if (mode_ == MonitorModeManager::Simulation) { | |
114 | + // !!! | |
115 | + return false; | |
116 | + | |
117 | + } else { | |
118 | + return con_->isConnected(); | |
119 | + } | |
120 | + } | |
121 | + | |
122 | + | |
123 | + int send(const char* data, size_t count) | |
124 | + { | |
125 | + if (mode_ == MonitorModeManager::Record) { | |
126 | + // 記録する必要はないが、デバッグ情報として使うために残しておく | |
127 | + int send_size = con_->send(data, count); | |
128 | + return handler_->fetch(const_cast<char*>(data), send_size); | |
129 | + | |
130 | + } else if (mode_ == MonitorModeManager::Play) { | |
131 | + return handler_->fetch(NULL, 0); | |
132 | + | |
133 | + } else if (mode_ == MonitorModeManager::Simulation) { | |
134 | + // 対応するポートに対して接続を行う | |
135 | + // !!! | |
136 | + return false; | |
137 | + | |
138 | + } else { | |
139 | + return con_->send(data, count); | |
140 | + } | |
141 | + } | |
142 | + | |
143 | + | |
144 | + int recv(char* data, size_t count, int timeout) | |
145 | + { | |
146 | + if (mode_ == MonitorModeManager::Record) { | |
147 | + int recv_size = con_->recv(data, count, timeout); | |
148 | + return handler_->fetch(data, recv_size); | |
149 | + | |
150 | + } else if (mode_ == MonitorModeManager::Play) { | |
151 | + return handler_->fetch(data, count); | |
152 | + | |
153 | + } else if (mode_ == MonitorModeManager::Simulation) { | |
154 | + // 対応するポートに対して接続を行う | |
155 | + // !!! | |
156 | + return false; | |
157 | + | |
158 | + } else { | |
159 | + return con_->recv(data, count, timeout); | |
160 | + } | |
161 | + } | |
162 | + | |
163 | + | |
164 | + size_t size(void) | |
165 | + { | |
166 | + // !!! 記録対象 | |
167 | + return con_->size(); | |
168 | + | |
169 | + if ((mode_ == MonitorModeManager::Record) || | |
170 | + (mode_ == MonitorModeManager::Play)) { | |
171 | + int data_size = con_->size(); | |
172 | + return handler_->fetch(data_size); | |
173 | + | |
174 | + } else if (mode_ == MonitorModeManager::Simulation) { | |
175 | + // 対応するポートに対して接続を行う | |
176 | + // !!! | |
177 | + return 0; | |
178 | + | |
179 | + } else { | |
180 | + return con_->size(); | |
181 | + } | |
182 | + } | |
80 | 183 | }; |
81 | 184 | |
82 | 185 |
@@ -99,67 +202,58 @@ | ||
99 | 202 | bool mConnection::connect(const char* device, long baudrate) |
100 | 203 | { |
101 | 204 | return pimpl->connect(device, baudrate); |
102 | -}; | |
205 | +} | |
103 | 206 | |
104 | 207 | |
105 | 208 | void mConnection::disconnect(void) |
106 | 209 | { |
107 | - // !!! | |
210 | + pimpl->con_->disconnect(); | |
108 | 211 | } |
109 | 212 | |
110 | 213 | |
111 | 214 | bool mConnection::changeBaudrate(long baudrate) |
112 | 215 | { |
113 | - // !!! ボーレート処理はシミュレートしない | |
114 | - // !!! 戻り値のみを記録対象とする | |
115 | - | |
116 | - return false; | |
216 | + return pimpl->changeBaudrate(baudrate); | |
117 | 217 | } |
118 | 218 | |
119 | 219 | |
120 | 220 | bool mConnection::isConnected(void) |
121 | 221 | { |
122 | - // !!! 戻り値のみを記録対象とする | |
123 | - | |
124 | - return false; | |
222 | + return pimpl->isConnected(); | |
125 | 223 | } |
126 | 224 | |
127 | 225 | |
128 | 226 | int mConnection::send(const char* data, size_t count) |
129 | 227 | { |
130 | - // !!! | |
131 | - | |
132 | - return false; | |
228 | + return pimpl->send(data, count); | |
133 | 229 | } |
134 | 230 | |
135 | 231 | |
136 | 232 | int mConnection::recv(char* data, size_t count, int timeout) |
137 | 233 | { |
138 | - // !!! | |
139 | - return -1; | |
234 | + return pimpl->recv(data, count, timeout); | |
140 | 235 | } |
141 | 236 | |
142 | 237 | |
143 | -size_t Connection::size(void) | |
238 | +size_t mConnection::size(void) | |
144 | 239 | { |
145 | - // !!! | |
146 | - return 99; | |
240 | + return pimpl->size(); | |
147 | 241 | } |
148 | 242 | |
149 | 243 | |
150 | -void Connection::flush(void) | |
244 | +void mConnection::flush(void) | |
151 | 245 | { |
152 | - // !!! | |
246 | + pimpl->con_->flush(); | |
153 | 247 | } |
154 | 248 | |
155 | 249 | |
156 | -void Connection::clear(void) | |
250 | +void mConnection::clear(void) | |
157 | 251 | { |
158 | - // !!! | |
252 | + pimpl->con_->clear(); | |
159 | 253 | } |
160 | 254 | |
161 | 255 | |
162 | -void Connection::ungetc(const char ch) | |
256 | +void mConnection::ungetc(const char ch) | |
163 | 257 | { |
164 | - // !!! | |
258 | + pimpl->con_->ungetc(ch); | |
165 | 259 | } |