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 }
LogStream.h
CameraDescriptor
Definition: FrameGrabberInterfaces.h:35
CameraDescriptor::busType
BusType busType
Definition: FrameGrabberInterfaces.h:36
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Value::asVocab
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::dev::FrameGrabberControls_Sender::hasManual
bool hasManual(int feature, bool *hasManual) override
Check if the requested feature has the 'manual' mode.
Definition: FrameGrabberControlImpl.cpp:152
yarp::os::Bottle::clear
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
yarp::dev::IFrameGrabberControls::getActive
virtual bool getActive(int feature, bool *isActive)=0
Get the current status of the feature, on or off.
VOCAB_CAMERA_DESCRIPTION
constexpr yarp::conf::vocab32_t VOCAB_CAMERA_DESCRIPTION
Definition: FrameGrabberInterfaces.h:65
VOCAB_FRAMEGRABBER_CONTROL
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_CONTROL
Definition: FrameGrabberInterfaces.h:63
yarp::dev::FrameGrabberControls_Sender::setActive
bool setActive(int feature, bool onoff) override
Set the requested feature on or off.
Definition: FrameGrabberControlImpl.cpp:115
yarp::dev::IFrameGrabberControls::hasFeature
virtual bool hasFeature(int feature, bool *hasFeature)=0
Check if camera has the requested feature (saturation, brightness ...
yarp::dev::IFrameGrabberControls::getCameraDescription
virtual bool getCameraDescription(CameraDescriptor *camera)=0
Get a basic description of the camera hw.
yarp::dev::FrameGrabberControls_Parser::FrameGrabberControls_Parser
FrameGrabberControls_Parser()
Definition: FrameGrabberControlImpl.cpp:216
yarp::dev::FrameGrabberControls_Sender::setOnePush
bool setOnePush(int feature) override
Set the requested feature to a value (saturation, brightness ...
Definition: FrameGrabberControlImpl.cpp:202
VOCAB_GET
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:16
VOCAB_FEATURE2
constexpr yarp::conf::vocab32_t VOCAB_FEATURE2
Definition: FrameGrabberInterfaces.h:68
VOCAB_ONOFF
constexpr yarp::conf::vocab32_t VOCAB_ONOFF
Definition: FrameGrabberInterfaces.h:69
VOCAB_ACTIVE
constexpr yarp::conf::vocab32_t VOCAB_ACTIVE
Definition: FrameGrabberInterfaces.h:73
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::os::Bottle::addFloat64
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
yError
#define yError(...)
Definition: Log.h:282
FrameGrabberControlImpl.h
CameraDescriptor::deviceDescription
std::string deviceDescription
Definition: FrameGrabberInterfaces.h:37
yarp::dev::FrameGrabberControls_Sender::hasOnePush
bool hasOnePush(int feature, bool *hasOnePush) override
Check if the requested feature has the 'onePush' mode.
Definition: FrameGrabberControlImpl.cpp:165
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::IFrameGrabberControls::setFeature
virtual bool setFeature(int feature, double value)=0
Set the requested feature to a value (saturation, brightness ...
VOCAB_HAS
constexpr yarp::conf::vocab32_t VOCAB_HAS
Definition: FrameGrabberInterfaces.h:66
yarp::os::Vocab::decode
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
yarp::dev::FrameGrabberControls_Sender::setFeature
bool setFeature(int feature, double value) override
Set the requested feature to a value (saturation, brightness ...
Definition: FrameGrabberControlImpl.cpp:52
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
VOCAB_ONEPUSH
constexpr yarp::conf::vocab32_t VOCAB_ONEPUSH
Definition: FrameGrabberInterfaces.h:72
yarp::dev::FrameGrabberControls_Sender::hasFeature
bool hasFeature(int feature, bool *hasFeature) override
Check if camera has the requested feature (saturation, brightness ...
Definition: FrameGrabberControlImpl.cpp:39
BusType
BusType
Definition: FrameGrabberInterfaces.h:23
VOCAB_SET
constexpr yarp::conf::vocab32_t VOCAB_SET
Definition: GenericVocabs.h:15
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::dev::IFrameGrabberControls::setMode
virtual bool setMode(int feature, FeatureMode mode)=0
Set the requested mode for the feature.
yarp::dev::FrameGrabberControls_Parser::respond
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
Definition: FrameGrabberControlImpl.cpp:235
yarp::dev::FrameGrabberControls_Sender::getMode
bool getMode(int feature, FeatureMode *mode) override
Get the current mode for the feature.
Definition: FrameGrabberControlImpl.cpp:189
yarp::dev::FrameGrabberControls_Sender::hasAuto
bool hasAuto(int feature, bool *hasAuto) override
Check if the requested feature has the 'auto' mode.
Definition: FrameGrabberControlImpl.cpp:139
yarp::dev::IFrameGrabberControls::hasManual
virtual bool hasManual(int feature, bool *hasManual)=0
Check if the requested feature has the 'manual' mode.
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::dev::IFrameGrabberControls::getFeature
virtual bool getFeature(int feature, double *value)=0
Get the current value for the requested feature.
yarp::dev::FrameGrabberControls_Sender::getFeature
bool getFeature(int feature, double *value) override
Get the current value for the requested feature.
Definition: FrameGrabberControlImpl.cpp:75
yarp::os::Bottle::addInt32
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:143
yarp::dev::IFrameGrabberControls::hasAuto
virtual bool hasAuto(int feature, bool *hasAuto)=0
Check if the requested feature has the 'auto' mode.
yarp::dev::IFrameGrabberControls
Control interface for frame grabber devices.
Definition: FrameGrabberInterfaces.h:365
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
VOCAB_MANUAL
constexpr yarp::conf::vocab32_t VOCAB_MANUAL
Definition: FrameGrabberInterfaces.h:71
yarp::os::Bottle::addVocab
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
yarp::dev::FrameGrabberControls_Sender::getCameraDescription
bool getCameraDescription(CameraDescriptor *camera) override
Get a basic description of the camera hw.
Definition: FrameGrabberControlImpl.cpp:24
yarp::dev::IFrameGrabberControls::getMode
virtual bool getMode(int feature, FeatureMode *mode)=0
Get the current mode for the feature.
yarp::os::Value::asInt32
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
VOCAB_AUTO
constexpr yarp::conf::vocab32_t VOCAB_AUTO
Definition: FrameGrabberInterfaces.h:70
yarp::os::Port::write
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:430
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
FeatureMode
FeatureMode
Definition: FrameGrabberInterfaces.h:29
VOCAB_MODE
constexpr yarp::conf::vocab32_t VOCAB_MODE
Definition: FrameGrabberInterfaces.h:74
yInfo
#define yInfo(...)
Definition: Log.h:260
yarp::dev::IFrameGrabberControls::hasOnePush
virtual bool hasOnePush(int feature, bool *hasOnePush)=0
Check if the requested feature has the 'onePush' mode.
yDebug
#define yDebug(...)
Definition: Log.h:237
yarp::dev::IFrameGrabberControls::setOnePush
virtual bool setOnePush(int feature)=0
Set the requested feature to a value (saturation, brightness ...
VOCAB_IS
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:17
GenericVocabs.h
yarp::dev::FrameGrabberControls_Parser::configure
bool configure(IFrameGrabberControls *interface)
Definition: FrameGrabberControlImpl.cpp:218
VOCAB_FEATURE
constexpr yarp::conf::vocab32_t VOCAB_FEATURE
Definition: FrameGrabberInterfaces.h:67
yarp::dev::IFrameGrabberControls::hasOnOff
virtual bool hasOnOff(int feature, bool *HasOnOff)=0
Check if the camera has the ability to turn on/off the requested feature.
yarp::os::Value::asFloat64
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
yarp::dev::IFrameGrabberControls::setActive
virtual bool setActive(int feature, bool onoff)=0
Set the requested feature on or off.
yarp::dev::FrameGrabberControls_Sender::getActive
bool getActive(int feature, bool *isActive) override
Get the current status of the feature, on or off.
Definition: FrameGrabberControlImpl.cpp:126
yarp::dev::FrameGrabberControls_Sender::setMode
bool setMode(int feature, FeatureMode mode) override
Set the requested mode for the feature.
Definition: FrameGrabberControlImpl.cpp:178
yarp::dev::FrameGrabberControls_Sender::hasOnOff
bool hasOnOff(int feature, bool *HasOnOff) override
Check if the camera has the ability to turn on/off the requested feature.
Definition: FrameGrabberControlImpl.cpp:102
yarp::os::Port::isOpen
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:671