yocto_compass.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_compass.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindCompass(), the high-level API for Compass 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_compass.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__ "compass"
50 
51 YCompass::YCompass(const string& func): YSensor(func)
52 //--- (YCompass initialization)
53  ,_bandwidth(BANDWIDTH_INVALID)
54  ,_axis(AXIS_INVALID)
55  ,_magneticHeading(MAGNETICHEADING_INVALID)
56  ,_valueCallbackCompass(NULL)
57  ,_timedReportCallbackCompass(NULL)
58 //--- (end of YCompass initialization)
59 {
60  _className="Compass";
61 }
62 
64 {
65 //--- (YCompass cleanup)
66 //--- (end of YCompass cleanup)
67 }
68 //--- (YCompass implementation)
69 // static attributes
71 
73 {
74  if(json_val->has("bandwidth")) {
75  _bandwidth = json_val->getInt("bandwidth");
76  }
77  if(json_val->has("axis")) {
78  _axis = (Y_AXIS_enum) json_val->getInt("axis");
79  }
80  if(json_val->has("magneticHeading")) {
81  _magneticHeading = floor(json_val->getDouble("magneticHeading") * 1000.0 / 65536.0 + 0.5) / 1000.0;
82  }
83  return YSensor::_parseAttr(json_val);
84 }
85 
86 
95 {
96  int res = 0;
98  try {
101  {
104  }
105  }
106  }
107  res = _bandwidth;
108  } catch (std::exception) {
110  throw;
111  }
113  return res;
114 }
115 
126 int YCompass::set_bandwidth(int newval)
127 {
128  string rest_val;
129  int res;
131  try {
132  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
133  res = _setAttr("bandwidth", rest_val);
134  } catch (std::exception) {
136  throw;
137  }
139  return res;
140 }
141 
143 {
144  Y_AXIS_enum res;
146  try {
149  {
151  return YCompass::AXIS_INVALID;
152  }
153  }
154  }
155  res = _axis;
156  } catch (std::exception) {
158  throw;
159  }
161  return res;
162 }
163 
172 {
173  double res = 0.0;
175  try {
178  {
181  }
182  }
183  }
184  res = _magneticHeading;
185  } catch (std::exception) {
187  throw;
188  }
190  return res;
191 }
192 
221 {
222  YCompass* obj = NULL;
223  int taken = 0;
224  if (YAPI::_apiInitialized) {
226  taken = 1;
227  }try {
228  obj = (YCompass*) YFunction::_FindFromCache("Compass", func);
229  if (obj == NULL) {
230  obj = new YCompass(func);
231  YFunction::_AddToCache("Compass", func, obj);
232  }
233  } catch (std::exception) {
235  throw;
236  }
238  return obj;
239 }
240 
253 {
254  string val;
255  if (callback != NULL) {
257  } else {
259  }
260  _valueCallbackCompass = callback;
261  // Immediately invoke value callback with current value
262  if (callback != NULL && this->isOnline()) {
263  val = _advertisedValue;
264  if (!(val == "")) {
265  this->_invokeValueCallback(val);
266  }
267  }
268  return 0;
269 }
270 
272 {
273  if (_valueCallbackCompass != NULL) {
274  _valueCallbackCompass(this, value);
275  } else {
277  }
278  return 0;
279 }
280 
293 {
294  YSensor* sensor = NULL;
295  sensor = this;
296  if (callback != NULL) {
298  } else {
300  }
301  _timedReportCallbackCompass = callback;
302  return 0;
303 }
304 
306 {
307  if (_timedReportCallbackCompass != NULL) {
308  _timedReportCallbackCompass(this, value);
309  } else {
311  }
312  return 0;
313 }
314 
316 {
317  string hwid;
318 
319  if(YISERR(_nextFunction(hwid)) || hwid=="") {
320  return NULL;
321  }
322  return YCompass::FindCompass(hwid);
323 }
324 
326 {
327  vector<YFUN_DESCR> v_fundescr;
328  YDEV_DESCR ydevice;
329  string serial, funcId, funcName, funcVal, errmsg;
330 
331  if(YISERR(YapiWrapper::getFunctionsByClass("Compass", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
332  v_fundescr.size() == 0 ||
333  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
334  return NULL;
335  }
336  return YCompass::FindCompass(serial+"."+funcId);
337 }
338 
339 //--- (end of YCompass implementation)
340 
341 //--- (YCompass functions)
342 //--- (end of YCompass functions)
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
static const Y_AXIS_enum AXIS_INVALID
YCompass * nextCompass(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static YCompass * FirstCompass(void)
YCompassTimedReportCallback _timedReportCallbackCompass
Definition: yocto_compass.h:96
void(* YCompassValueCallback)(YCompass *func, const string &functionValue)
Definition: yocto_compass.h:54
int _bandwidth
Definition: yocto_compass.h:92
double get_magneticHeading(void)
static YCompass * FindCompass(string func)
YCompassValueCallback _valueCallbackCompass
Definition: yocto_compass.h:95
Y_AXIS_enum _axis
Definition: yocto_compass.h:93
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
static const double MAGNETICHEADING_INVALID
virtual int _invokeValueCallback(string value)
virtual int registerValueCallback(YCompassValueCallback callback)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
YCompass(const string &func)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
int set_bandwidth(int newval)
Y_AXIS_enum get_axis(void)
virtual int registerTimedReportCallback(YCompassTimedReportCallback callback)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
double _magneticHeading
Definition: yocto_compass.h:94
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
#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
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
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
int get_bandwidth(void)
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
virtual int _invokeTimedReportCallback(YMeasure value)
string _className
Definition: yocto_api.h:1558
static const int BANDWIDTH_INVALID
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int _parseAttr(YJSONObject *json_val)
void(* YCompassTimedReportCallback)(YCompass *func, YMeasure measure)
Definition: yocto_compass.h:56
bool has(const string &key)
Definition: yocto_api.cpp:735
Y_AXIS_enum
Definition: yocto_compass.h:59
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968


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