yocto_altitude.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_altitude.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindAltitude(), the high-level API for Altitude 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_altitude.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__ "altitude"
50 
51 YAltitude::YAltitude(const string& func): YSensor(func)
52 //--- (YAltitude initialization)
53  ,_qnh(QNH_INVALID)
54  ,_technology(TECHNOLOGY_INVALID)
55  ,_valueCallbackAltitude(NULL)
56  ,_timedReportCallbackAltitude(NULL)
57 //--- (end of YAltitude initialization)
58 {
59  _className="Altitude";
60 }
61 
63 {
64 //--- (YAltitude cleanup)
65 //--- (end of YAltitude cleanup)
66 }
67 //--- (YAltitude implementation)
68 // static attributes
71 
73 {
74  if(json_val->has("qnh")) {
75  _qnh = floor(json_val->getDouble("qnh") * 1000.0 / 65536.0 + 0.5) / 1000.0;
76  }
77  if(json_val->has("technology")) {
78  _technology = json_val->getString("technology");
79  }
80  return YSensor::_parseAttr(json_val);
81 }
82 
83 
94 int YAltitude::set_currentValue(double newval)
95 {
96  string rest_val;
97  int res;
99  try {
100  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
101  res = _setAttr("currentValue", rest_val);
102  } catch (std::exception) {
104  throw;
105  }
107  return res;
108 }
109 
123 int YAltitude::set_qnh(double newval)
124 {
125  string rest_val;
126  int res;
128  try {
129  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
130  res = _setAttr("qnh", rest_val);
131  } catch (std::exception) {
133  throw;
134  }
136  return res;
137 }
138 
148 double YAltitude::get_qnh(void)
149 {
150  double res = 0.0;
152  try {
155  {
157  return YAltitude::QNH_INVALID;
158  }
159  }
160  }
161  res = _qnh;
162  } catch (std::exception) {
164  throw;
165  }
167  return res;
168 }
169 
180 {
181  string res;
183  try {
186  {
189  }
190  }
191  }
192  res = _technology;
193  } catch (std::exception) {
195  throw;
196  }
198  return res;
199 }
200 
229 {
230  YAltitude* obj = NULL;
231  int taken = 0;
232  if (YAPI::_apiInitialized) {
234  taken = 1;
235  }try {
236  obj = (YAltitude*) YFunction::_FindFromCache("Altitude", func);
237  if (obj == NULL) {
238  obj = new YAltitude(func);
239  YFunction::_AddToCache("Altitude", func, obj);
240  }
241  } catch (std::exception) {
243  throw;
244  }
246  return obj;
247 }
248 
261 {
262  string val;
263  if (callback != NULL) {
265  } else {
267  }
268  _valueCallbackAltitude = callback;
269  // Immediately invoke value callback with current value
270  if (callback != NULL && this->isOnline()) {
271  val = _advertisedValue;
272  if (!(val == "")) {
273  this->_invokeValueCallback(val);
274  }
275  }
276  return 0;
277 }
278 
280 {
281  if (_valueCallbackAltitude != NULL) {
282  _valueCallbackAltitude(this, value);
283  } else {
285  }
286  return 0;
287 }
288 
301 {
302  YSensor* sensor = NULL;
303  sensor = this;
304  if (callback != NULL) {
306  } else {
308  }
309  _timedReportCallbackAltitude = callback;
310  return 0;
311 }
312 
314 {
315  if (_timedReportCallbackAltitude != NULL) {
316  _timedReportCallbackAltitude(this, value);
317  } else {
319  }
320  return 0;
321 }
322 
324 {
325  string hwid;
326 
327  if(YISERR(_nextFunction(hwid)) || hwid=="") {
328  return NULL;
329  }
330  return YAltitude::FindAltitude(hwid);
331 }
332 
334 {
335  vector<YFUN_DESCR> v_fundescr;
336  YDEV_DESCR ydevice;
337  string serial, funcId, funcName, funcVal, errmsg;
338 
339  if(YISERR(YapiWrapper::getFunctionsByClass("Altitude", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
340  v_fundescr.size() == 0 ||
341  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
342  return NULL;
343  }
344  return YAltitude::FindAltitude(serial+"."+funcId);
345 }
346 
347 //--- (end of YAltitude implementation)
348 
349 //--- (YAltitude functions)
350 //--- (end of YAltitude functions)
YAltitude * nextAltitude(void)
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
virtual int _parseAttr(YJSONObject *json_val)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int registerValueCallback(YAltitudeValueCallback callback)
int set_currentValue(double newval)
string _technology
YAltitude(const string &func)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
double get_qnh(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
double _qnh
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
void(* YAltitudeTimedReportCallback)(YAltitude *func, YMeasure measure)
static YAltitude * FindAltitude(string func)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
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
#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
virtual int _invokeValueCallback(string value)
virtual int _invokeTimedReportCallback(YMeasure value)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
string get_technology(void)
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
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static YAltitude * FirstAltitude(void)
string _className
Definition: yocto_api.h:1558
static const string TECHNOLOGY_INVALID
void(* YAltitudeValueCallback)(YAltitude *func, const string &functionValue)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
YAltitudeValueCallback _valueCallbackAltitude
static const double QNH_INVALID
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int registerTimedReportCallback(YAltitudeTimedReportCallback callback)
bool has(const string &key)
Definition: yocto_api.cpp:735
YAltitudeTimedReportCallback _timedReportCallbackAltitude
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
int set_qnh(double newval)


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