YARP
Yet Another Robot Platform
FrameGrabberControlImpl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/LogStream.h>
11 #include <yarp/dev/GenericVocabs.h>
12 
13 using namespace yarp::os;
14 using namespace yarp::dev;
15 
16 //
17 // Interface helper implementation
18 //
19 
20 // Sender
21 FrameGrabberControls_Sender::FrameGrabberControls_Sender(Port& port) : _port(port) {};
22 
23 
25 {
26  yarp::os::Bottle cmd, response;
28  cmd.addVocab(VOCAB_GET);
30  yInfo() << _port.isOpen();
31  bool ret = _port.write(cmd,response);
32 
33  // response should be [fgc2] [camd] [is] [busType] [description]
34  camera->busType = (BusType) response.get(3).asInt32();
35  camera->deviceDescription = response.get(4).asString();
36  return ret;
37 }
38 
39 bool FrameGrabberControls_Sender::hasFeature(int feature, bool* hasFeature)
40 {
41  yarp::os::Bottle cmd, response;
43  cmd.addVocab(VOCAB_HAS);
45  cmd.addInt32(feature);
46  bool ret = _port.write(cmd,response);
47 
48  *hasFeature = response.get(4).asInt32() !=0? true:false;
49  return ret;
50 }
51 
52 bool FrameGrabberControls_Sender::setFeature(int feature, double value)
53 {
54  yarp::os::Bottle cmd, response;
56  cmd.addVocab(VOCAB_SET);
58  cmd.addInt32(feature);
59  cmd.addFloat64(value);
60  return _port.write(cmd,response);
61 }
62 
63 bool FrameGrabberControls_Sender::setFeature(int feature, double value1, double value2)
64 {
65  yarp::os::Bottle cmd, response;
67  cmd.addVocab(VOCAB_SET);
69  cmd.addInt32(feature);
70  cmd.addFloat64(value1);
71  cmd.addFloat64(value2);
72  return _port.write(cmd,response);
73 }
74 
75 bool FrameGrabberControls_Sender::getFeature(int feature, double* value)
76 {
77  yarp::os::Bottle cmd, response;
79  cmd.addVocab(VOCAB_GET);
81  cmd.addInt32(feature);
82  bool ret = _port.write(cmd,response);
83 
84  *value = response.get(3).asFloat64();
85  return ret;
86 }
87 
88 bool FrameGrabberControls_Sender::getFeature(int feature, double *value1, double *value2)
89 {
90  yarp::os::Bottle cmd, response;
92  cmd.addVocab(VOCAB_GET);
94  cmd.addInt32(feature);
95  bool ret = _port.write(cmd,response);
96 
97  *value1 = response.get(3).asFloat64();
98  *value2 = response.get(4).asFloat64();
99  return ret;
100 }
101 
102 bool FrameGrabberControls_Sender::hasOnOff(int feature, bool* _hasOnOff)
103 {
104  yarp::os::Bottle cmd, response;
106  cmd.addVocab(VOCAB_HAS);
107  cmd.addVocab(VOCAB_ONOFF);
108  cmd.addInt32(feature);
109  bool ret = _port.write(cmd,response);
110 
111  *_hasOnOff = response.get(4).asInt32() !=0? true:false;
112  return ret;
113 }
114 
115 bool FrameGrabberControls_Sender::setActive(int feature, bool onoff)
116 {
117  yarp::os::Bottle cmd, response;
119  cmd.addVocab(VOCAB_SET);
120  cmd.addVocab(VOCAB_ACTIVE);
121  cmd.addInt32(feature);
122  cmd.addInt32(onoff);
123  return _port.write(cmd,response);
124 }
125 
126 bool FrameGrabberControls_Sender::getActive(int feature, bool* _isActive)
127 {
128  yarp::os::Bottle cmd, response;
130  cmd.addVocab(VOCAB_GET);
131  cmd.addVocab(VOCAB_ACTIVE);
132  cmd.addInt32(feature);
133  bool ret = _port.write(cmd,response);
134 
135  *_isActive = response.get(3).asInt32() !=0? true:false;
136  return ret;
137 }
138 
139 bool FrameGrabberControls_Sender::hasAuto(int feature, bool* _hasAuto)
140 {
141  yarp::os::Bottle cmd, response;
143  cmd.addVocab(VOCAB_HAS);
144  cmd.addVocab(VOCAB_AUTO);
145  cmd.addInt32(feature);
146  bool ret = _port.write(cmd,response);
147 
148  *_hasAuto = response.get(4).asInt32() !=0? true:false;;
149  return ret;
150 }
151 
152 bool FrameGrabberControls_Sender::hasManual(int feature, bool* _hasManual)
153 {
154  yarp::os::Bottle cmd, response;
156  cmd.addVocab(VOCAB_HAS);
157  cmd.addVocab(VOCAB_MANUAL);
158  cmd.addInt32(feature);
159  bool ret = _port.write(cmd,response);
160 
161  *_hasManual = response.get(4).asInt32() !=0? true:false;
162  return ret;
163 }
164 
165 bool FrameGrabberControls_Sender::hasOnePush(int feature, bool* _hasOnePush)
166 {
167  yarp::os::Bottle cmd, response;
169  cmd.addVocab(VOCAB_HAS);
170  cmd.addVocab(VOCAB_ONEPUSH);
171  cmd.addInt32(feature);
172  bool ret = _port.write(cmd,response);
173 
174  *_hasOnePush = response.get(4).asInt32() !=0? true:false;
175  return ret;
176 }
177 
179 {
180  yarp::os::Bottle cmd, response;
182  cmd.addVocab(VOCAB_SET);
183  cmd.addVocab(VOCAB_MODE);
184  cmd.addInt32(feature);
185  cmd.addInt32(mode);
186  return _port.write(cmd,response);
187 }
188 
190 {
191  yarp::os::Bottle cmd, response;
193  cmd.addVocab(VOCAB_GET);
194  cmd.addVocab(VOCAB_MODE);
195  cmd.addInt32(feature);
196  bool ret = _port.write(cmd,response);
197 
198  *mode = (FeatureMode) response.get(3).asInt32();
199  return ret;
200 }
201 
203 {
204  yarp::os::Bottle cmd, response;
206  cmd.addVocab(VOCAB_SET);
207  cmd.addVocab(VOCAB_ONEPUSH);
208  cmd.addInt32(feature);
209  return _port.write(cmd,response);
210 }
211 
212 //
213 // Parser
214 //
215 
217 
219 {
220  bool ret = false;
221  if(interface)
222  {
223  fgCtrl = interface;
224  ret = true;
225  }
226  else
227  {
228  fgCtrl = nullptr;
229  ret = false;
230  }
231  return ret;
232 }
233 
234 
236 {
237  bool ok = false;
238  int action = cmd.get(1).asVocab();
239  int param = cmd.get(2).asVocab();
240 
241 // yTrace() << "cmd received\n\t" << cmd.toString().c_str();
242 
243 
244  if(!fgCtrl)
245  {
246  yError() << " Selected camera device has no IFrameGrabberControl interface";
247  return false;
248  }
249 
250  response.clear();
251 
252  switch (action)
253  {
254  case VOCAB_HAS:
255  {
257  response.addVocab(VOCAB_HAS);
258  response.addVocab(VOCAB_FEATURE);
259  response.addInt32(param);
260 
261  switch (param)
262  {
263  case VOCAB_FEATURE:
264  {
265  bool _hasFeat;
266  ok = fgCtrl->hasFeature(cmd.get(3).asInt32(), &_hasFeat);
267  response.addInt32(_hasFeat);
268  } break;
269 
270  case VOCAB_ONOFF:
271  {
272  bool _hasOnOff;
273  ok = fgCtrl->hasOnOff(cmd.get(3).asInt32(), &_hasOnOff);
274  response.addInt32(_hasOnOff);
275  } break;
276 
277  case VOCAB_AUTO:
278  {
279  bool _hasAuto;
280 
281  ok = fgCtrl->hasAuto(cmd.get(3).asInt32(), &_hasAuto);
282  response.addInt32(_hasAuto);
283  } break;
284 
285  case VOCAB_MANUAL:
286  {
287  bool _hasManual;
288  ok = fgCtrl->hasManual(cmd.get(3).asInt32(), &_hasManual);
289  response.addInt32(_hasManual);
290  } break;
291 
292  case VOCAB_ONEPUSH:
293  {
294  bool _hasOnePush;
295  ok = fgCtrl->hasOnePush(cmd.get(3).asInt32(), &_hasOnePush);
296  response.addInt32(_hasOnePush);
297  } break;
298 
299  default:
300  {
301  yError() << "Unknown command 'HAS " << Vocab::decode(param) << "' received on IFrameGrabber2 interface";
302  response.clear();
303  ok = false;
304  } break;
305  } break; // end switch (param)
306 
307  } break; // end VOCAB_HAS
308 
309  case VOCAB_SET:
310  {
311  switch (param)
312  {
313  case VOCAB_FEATURE:
314  {
315  ok = fgCtrl->setFeature(cmd.get(3).asInt32(), cmd.get(4).asFloat64());
316  } break;
317 
318  case VOCAB_FEATURE2:
319  {
320  ok = fgCtrl->setFeature(cmd.get(3).asInt32(), cmd.get(4).asFloat64(), cmd.get(5).asFloat64());
321  } break;
322 
323  case VOCAB_ACTIVE:
324  {
325  ok = fgCtrl->setActive(cmd.get(3).asInt32(), cmd.get(4).asInt32());
326  } break;
327 
328  case VOCAB_MODE:
329  {
330  ok = fgCtrl->setMode(cmd.get(3).asInt32(), (FeatureMode) cmd.get(4).asInt32());
331  } break;
332 
333  case VOCAB_ONEPUSH:
334  {
335  ok = fgCtrl->setOnePush(cmd.get(3).asInt32());
336  } break;
337 
338  default:
339  {
340  yError() << "Unknown command 'SET " << Vocab::decode(param) << "' received on IFrameGrabber2 interface";
341  response.clear();
342  ok = false;
343  }
344  } break; // end switch (param)
345 
346  } break; // end VOCAB_SET
347 
348  case VOCAB_GET:
349  {
351  response.addVocab(param);
352  response.addVocab(VOCAB_IS);
353  switch (param)
354  {
356  {
357  CameraDescriptor camera;
358  ok = fgCtrl->getCameraDescription(&camera);
359  response.addInt32(camera.busType);
360  response.addString(camera.deviceDescription);
361  yDebug() << "Response is " << response.toString();
362  } break;
363 
364  case VOCAB_FEATURE:
365  {
366  double value;
367  ok = fgCtrl->getFeature(cmd.get(3).asInt32(), &value);
368  response.addFloat64(value);
369  } break;
370 
371  case VOCAB_FEATURE2:
372  {
373  double value1, value2;
374  ok = fgCtrl->getFeature(cmd.get(3).asInt32(), &value1, &value2);
375  response.addFloat64(value1);
376  response.addFloat64(value2);
377  } break;
378 
379  case VOCAB_ACTIVE:
380  {
381  bool _isActive;
382  ok = fgCtrl->getActive(cmd.get(3).asInt32(), &_isActive);
383  response.addInt32(_isActive);
384  } break;
385 
386  case VOCAB_MODE:
387  {
388  FeatureMode _mode;
389  ok = fgCtrl->getMode(cmd.get(3).asInt32(), &_mode);
390  response.addInt32(_mode);
391  } break;
392 
393  default:
394  {
395  yError() << "Unknown command 'GET " << Vocab::decode(param) << "' received on IFrameGrabber2 interface";
396  response.clear();
397  ok = false;
398  }
399 
400  } break; // end switch (param)
401 
402  } break; // end VOCAB_GET
403  }
404 // yTrace() << "response is\n\t" << response.toString().c_str();
405  return ok;
406 }
constexpr yarp::conf::vocab32_t VOCAB_ACTIVE
constexpr yarp::conf::vocab32_t VOCAB_FEATURE2
constexpr yarp::conf::vocab32_t VOCAB_FEATURE
constexpr yarp::conf::vocab32_t VOCAB_MANUAL
constexpr yarp::conf::vocab32_t VOCAB_ONEPUSH
constexpr yarp::conf::vocab32_t VOCAB_MODE
constexpr yarp::conf::vocab32_t VOCAB_ONOFF
constexpr yarp::conf::vocab32_t VOCAB_CAMERA_DESCRIPTION
constexpr yarp::conf::vocab32_t VOCAB_HAS
constexpr yarp::conf::vocab32_t VOCAB_AUTO
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_CONTROL
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:17
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:16
constexpr yarp::conf::vocab32_t VOCAB_SET
Definition: GenericVocabs.h:15
bool ret
#define yInfo(...)
Definition: Log.h:260
#define yError(...)
Definition: Log.h:282
#define yDebug(...)
Definition: Log.h:237
bool configure(IFrameGrabberControls *interface)
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ...
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ...
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ...
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
bool hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
Control interface for frame grabber devices.
virtual bool setFeature(int feature, double value)=0
Set the requested feature to a value (saturation, brightness ...
virtual bool setOnePush(int feature)=0
Set the requested feature to a value (saturation, brightness ...
virtual bool getFeature(int feature, double *value)=0
Get the current value for the requested feature.
virtual bool hasManual(int feature, bool *hasManual)=0
Check if the requested feature has the 'manual' mode.
virtual bool setMode(int feature, FeatureMode mode)=0
Set the requested mode for the feature.
virtual bool hasOnOff(int feature, bool *HasOnOff)=0
Check if the camera has the ability to turn on/off the requested feature.
virtual bool hasAuto(int feature, bool *hasAuto)=0
Check if the requested feature has the 'auto' mode.
virtual bool getMode(int feature, FeatureMode *mode)=0
Get the current mode for the feature.
virtual bool getCameraDescription(CameraDescriptor *camera)=0
Get a basic description of the camera hw.
virtual bool getActive(int feature, bool *isActive)=0
Get the current status of the feature, on or off.
virtual bool setActive(int feature, bool onoff)=0
Set the requested feature on or off.
virtual bool hasFeature(int feature, bool *hasFeature)=0
Check if camera has the requested feature (saturation, brightness ...
virtual bool hasOnePush(int feature, bool *hasOnePush)=0
Check if the requested feature has the 'onePush' mode.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition: Bottle.cpp:161
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:143
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
A mini-server for network communication.
Definition: Port.h:50
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:430
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:671
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
An interface for the device drivers.
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
An interface to the operating system, including Port based communication.