yocto_rangefinder.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_rangefinder.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindRangeFinder(), the high-level API for RangeFinder functions
6  *
7  * - - - - - - - - - License information: - - - - - - - - -
8  *
9  * Copyright (C) 2011 and beyond by Yoctopuce Sarl, Switzerland.
10  *
11  * Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
12  * non-exclusive license to use, modify, copy and integrate this
13  * file into your software for the sole purpose of interfacing
14  * with Yoctopuce products.
15  *
16  * You may reproduce and distribute copies of this file in
17  * source or object form, as long as the sole purpose of this
18  * code is to interface with Yoctopuce products. You must retain
19  * this notice in the distributed source file.
20  *
21  * You should refer to Yoctopuce General Terms and Conditions
22  * for additional information regarding your rights and
23  * obligations.
24  *
25  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 'AS IS' WITHOUT
26  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
27  * WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
28  * FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
29  * EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
30  * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
31  * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
32  * SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
33  * LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
34  * CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
35  * BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
36  * WARRANTY, OR OTHERWISE.
37  *
38  *********************************************************************/
39 
40 
41 #define _CRT_SECURE_NO_DEPRECATE //do not use windows secure crt
42 #include "yocto_rangefinder.h"
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <math.h>
48 #include <stdlib.h>
49 #define __FILE_ID__ "rangefinder"
50 
51 YRangeFinder::YRangeFinder(const string& func): YSensor(func)
52 //--- (YRangeFinder initialization)
53  ,_rangeFinderMode(RANGEFINDERMODE_INVALID)
54  ,_hardwareCalibration(HARDWARECALIBRATION_INVALID)
55  ,_currentTemperature(CURRENTTEMPERATURE_INVALID)
56  ,_command(COMMAND_INVALID)
57  ,_valueCallbackRangeFinder(NULL)
58  ,_timedReportCallbackRangeFinder(NULL)
59 //--- (end of YRangeFinder initialization)
60 {
61  _className="RangeFinder";
62 }
63 
65 {
66 //--- (YRangeFinder cleanup)
67 //--- (end of YRangeFinder cleanup)
68 }
69 //--- (YRangeFinder implementation)
70 // static attributes
74 
76 {
77  if(json_val->has("rangeFinderMode")) {
78  _rangeFinderMode = (Y_RANGEFINDERMODE_enum)json_val->getInt("rangeFinderMode");
79  }
80  if(json_val->has("hardwareCalibration")) {
81  _hardwareCalibration = json_val->getString("hardwareCalibration");
82  }
83  if(json_val->has("currentTemperature")) {
84  _currentTemperature = floor(json_val->getDouble("currentTemperature") * 1000.0 / 65536.0 + 0.5) / 1000.0;
85  }
86  if(json_val->has("command")) {
87  _command = json_val->getString("command");
88  }
89  return YSensor::_parseAttr(json_val);
90 }
91 
92 
106 int YRangeFinder::set_unit(const string& newval)
107 {
108  string rest_val;
109  int res;
111  try {
112  rest_val = newval;
113  res = _setAttr("unit", rest_val);
114  } catch (std::exception) {
116  throw;
117  }
119  return res;
120 }
121 
132 {
135  try {
138  {
141  }
142  }
143  }
144  res = _rangeFinderMode;
145  } catch (std::exception) {
147  throw;
148  }
150  return res;
151 }
152 
167 {
168  string rest_val;
169  int res;
171  try {
172  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
173  res = _setAttr("rangeFinderMode", rest_val);
174  } catch (std::exception) {
176  throw;
177  }
179  return res;
180 }
181 
183 {
184  string res;
186  try {
189  {
192  }
193  }
194  }
195  res = _hardwareCalibration;
196  } catch (std::exception) {
198  throw;
199  }
201  return res;
202 }
203 
204 int YRangeFinder::set_hardwareCalibration(const string& newval)
205 {
206  string rest_val;
207  int res;
209  try {
210  rest_val = newval;
211  res = _setAttr("hardwareCalibration", rest_val);
212  } catch (std::exception) {
214  throw;
215  }
217  return res;
218 }
219 
228 {
229  double res = 0.0;
231  try {
234  {
237  }
238  }
239  }
240  res = _currentTemperature;
241  } catch (std::exception) {
243  throw;
244  }
246  return res;
247 }
248 
250 {
251  string res;
253  try {
256  {
259  }
260  }
261  }
262  res = _command;
263  } catch (std::exception) {
265  throw;
266  }
268  return res;
269 }
270 
271 int YRangeFinder::set_command(const string& newval)
272 {
273  string rest_val;
274  int res;
276  try {
277  rest_val = newval;
278  res = _setAttr("command", rest_val);
279  } catch (std::exception) {
281  throw;
282  }
284  return res;
285 }
286 
315 {
316  YRangeFinder* obj = NULL;
317  int taken = 0;
318  if (YAPI::_apiInitialized) {
320  taken = 1;
321  }try {
322  obj = (YRangeFinder*) YFunction::_FindFromCache("RangeFinder", func);
323  if (obj == NULL) {
324  obj = new YRangeFinder(func);
325  YFunction::_AddToCache("RangeFinder", func, obj);
326  }
327  } catch (std::exception) {
329  throw;
330  }
332  return obj;
333 }
334 
347 {
348  string val;
349  if (callback != NULL) {
351  } else {
353  }
354  _valueCallbackRangeFinder = callback;
355  // Immediately invoke value callback with current value
356  if (callback != NULL && this->isOnline()) {
357  val = _advertisedValue;
358  if (!(val == "")) {
359  this->_invokeValueCallback(val);
360  }
361  }
362  return 0;
363 }
364 
366 {
367  if (_valueCallbackRangeFinder != NULL) {
368  _valueCallbackRangeFinder(this, value);
369  } else {
371  }
372  return 0;
373 }
374 
387 {
388  YSensor* sensor = NULL;
389  sensor = this;
390  if (callback != NULL) {
392  } else {
394  }
396  return 0;
397 }
398 
400 {
401  if (_timedReportCallbackRangeFinder != NULL) {
402  _timedReportCallbackRangeFinder(this, value);
403  } else {
405  }
406  return 0;
407 }
408 
418 {
419  string hwcal;
420  hwcal = this->get_hardwareCalibration();
421  if (!((hwcal).substr(0, 1) == "@")) {
422  return YAPI_INVALID_DOUBLE;
423  }
424  return atoi(((hwcal).substr(1, (int)(hwcal).length())).c_str());
425 }
426 
437 {
438  return this->set_command("T");
439 }
440 
451 {
452  return this->set_command("S");
453 }
454 
468 {
469  int distmm = 0;
470  if (this->get_unit() == "\"") {
471  distmm = (int) floor(targetDist * 25.4+0.5);
472  } else {
473  distmm = (int) floor(targetDist+0.5);
474  }
475  return this->set_command(YapiWrapper::ysprintf("O%d",distmm));
476 }
477 
491 {
492  int distmm = 0;
493  if (this->get_unit() == "\"") {
494  distmm = (int) floor(targetDist * 25.4+0.5);
495  } else {
496  distmm = (int) floor(targetDist+0.5);
497  }
498  return this->set_command(YapiWrapper::ysprintf("X%d",distmm));
499 }
500 
510 {
511  return this->set_hardwareCalibration("");
512 }
513 
515 {
516  string hwid;
517 
518  if(YISERR(_nextFunction(hwid)) || hwid=="") {
519  return NULL;
520  }
521  return YRangeFinder::FindRangeFinder(hwid);
522 }
523 
525 {
526  vector<YFUN_DESCR> v_fundescr;
527  YDEV_DESCR ydevice;
528  string serial, funcId, funcName, funcVal, errmsg;
529 
530  if(YISERR(YapiWrapper::getFunctionsByClass("RangeFinder", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
531  v_fundescr.size() == 0 ||
532  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
533  return NULL;
534  }
535  return YRangeFinder::FindRangeFinder(serial+"."+funcId);
536 }
537 
538 //--- (end of YRangeFinder implementation)
539 
540 //--- (YRangeFinder functions)
541 //--- (end of YRangeFinder functions)
string _advertisedValue
Definition: yocto_api.h:1569
virtual double get_hardwareCalibrationTemperature(void)
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
string get_unit(void)
Definition: yocto_api.cpp:7061
string get_command(void)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
virtual int _invokeTimedReportCallback(YMeasure value)
Y_RANGEFINDERMODE_enum get_rangeFinderMode(void)
string _hardwareCalibration
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int registerValueCallback(YRangeFinderValueCallback callback)
virtual int triggerOffsetCalibration(double targetDist)
YRangeFinderTimedReportCallback _timedReportCallbackRangeFinder
static const string HARDWARECALIBRATION_INVALID
YRangeFinderValueCallback _valueCallbackRangeFinder
virtual int cancelCoverGlassCalibrations(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
string get_hardwareCalibration(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
virtual int triggerTemperatureCalibration(void)
int set_rangeFinderMode(Y_RANGEFINDERMODE_enum newval)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int _invokeValueCallback(string value)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
virtual int registerTimedReportCallback(YRangeFinderTimedReportCallback callback)
void(* YRangeFinderTimedReportCallback)(YRangeFinder *func, YMeasure measure)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
YRangeFinder * nextRangeFinder(void)
static YRangeFinder * FindRangeFinder(string func)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
Y_RANGEFINDERMODE_enum _rangeFinderMode
static const double CURRENTTEMPERATURE_INVALID
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_api.cpp:7936
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
static YRangeFinder * FirstRangeFinder(void)
virtual int _parseAttr(YJSONObject *json_val)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
double get_currentTemperature(void)
static const string COMMAND_INVALID
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
bool isOnline(void)
Definition: yocto_api.cpp:3291
static const Y_RANGEFINDERMODE_enum RANGEFINDERMODE_INVALID
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
int set_command(const string &newval)
int set_hardwareCalibration(const string &newval)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
int set_unit(const string &newval)
string _className
Definition: yocto_api.h:1558
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int triggerSpadCalibration(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
double _currentTemperature
YRangeFinder(const string &func)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
void(* YRangeFinderValueCallback)(YRangeFinder *func, const string &functionValue)
virtual int triggerXTalkCalibration(double targetDist)
Y_RANGEFINDERMODE_enum


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:11