yocto_magnetometer.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_magnetometer.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindMagnetometer(), the high-level API for Magnetometer 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_magnetometer.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__ "magnetometer"
50 
51 YMagnetometer::YMagnetometer(const string& func): YSensor(func)
52 //--- (YMagnetometer initialization)
53  ,_bandwidth(BANDWIDTH_INVALID)
54  ,_xValue(XVALUE_INVALID)
55  ,_yValue(YVALUE_INVALID)
56  ,_zValue(ZVALUE_INVALID)
57  ,_valueCallbackMagnetometer(NULL)
58  ,_timedReportCallbackMagnetometer(NULL)
59 //--- (end of YMagnetometer initialization)
60 {
61  _className="Magnetometer";
62 }
63 
65 {
66 //--- (YMagnetometer cleanup)
67 //--- (end of YMagnetometer cleanup)
68 }
69 //--- (YMagnetometer implementation)
70 // static attributes
74 
76 {
77  if(json_val->has("bandwidth")) {
78  _bandwidth = json_val->getInt("bandwidth");
79  }
80  if(json_val->has("xValue")) {
81  _xValue = floor(json_val->getDouble("xValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
82  }
83  if(json_val->has("yValue")) {
84  _yValue = floor(json_val->getDouble("yValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
85  }
86  if(json_val->has("zValue")) {
87  _zValue = floor(json_val->getDouble("zValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
88  }
89  return YSensor::_parseAttr(json_val);
90 }
91 
92 
101 {
102  int res = 0;
104  try {
107  {
110  }
111  }
112  }
113  res = _bandwidth;
114  } catch (std::exception) {
116  throw;
117  }
119  return res;
120 }
121 
133 {
134  string rest_val;
135  int res;
137  try {
138  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
139  res = _setAttr("bandwidth", rest_val);
140  } catch (std::exception) {
142  throw;
143  }
145  return res;
146 }
147 
157 {
158  double res = 0.0;
160  try {
163  {
166  }
167  }
168  }
169  res = _xValue;
170  } catch (std::exception) {
172  throw;
173  }
175  return res;
176 }
177 
187 {
188  double res = 0.0;
190  try {
193  {
196  }
197  }
198  }
199  res = _yValue;
200  } catch (std::exception) {
202  throw;
203  }
205  return res;
206 }
207 
217 {
218  double res = 0.0;
220  try {
223  {
226  }
227  }
228  }
229  res = _zValue;
230  } catch (std::exception) {
232  throw;
233  }
235  return res;
236 }
237 
266 {
267  YMagnetometer* obj = NULL;
268  int taken = 0;
269  if (YAPI::_apiInitialized) {
271  taken = 1;
272  }try {
273  obj = (YMagnetometer*) YFunction::_FindFromCache("Magnetometer", func);
274  if (obj == NULL) {
275  obj = new YMagnetometer(func);
276  YFunction::_AddToCache("Magnetometer", func, obj);
277  }
278  } catch (std::exception) {
280  throw;
281  }
283  return obj;
284 }
285 
298 {
299  string val;
300  if (callback != NULL) {
302  } else {
304  }
305  _valueCallbackMagnetometer = callback;
306  // Immediately invoke value callback with current value
307  if (callback != NULL && this->isOnline()) {
308  val = _advertisedValue;
309  if (!(val == "")) {
310  this->_invokeValueCallback(val);
311  }
312  }
313  return 0;
314 }
315 
317 {
318  if (_valueCallbackMagnetometer != NULL) {
319  _valueCallbackMagnetometer(this, value);
320  } else {
322  }
323  return 0;
324 }
325 
338 {
339  YSensor* sensor = NULL;
340  sensor = this;
341  if (callback != NULL) {
343  } else {
345  }
347  return 0;
348 }
349 
351 {
352  if (_timedReportCallbackMagnetometer != NULL) {
354  } else {
356  }
357  return 0;
358 }
359 
361 {
362  string hwid;
363 
364  if(YISERR(_nextFunction(hwid)) || hwid=="") {
365  return NULL;
366  }
367  return YMagnetometer::FindMagnetometer(hwid);
368 }
369 
371 {
372  vector<YFUN_DESCR> v_fundescr;
373  YDEV_DESCR ydevice;
374  string serial, funcId, funcName, funcVal, errmsg;
375 
376  if(YISERR(YapiWrapper::getFunctionsByClass("Magnetometer", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
377  v_fundescr.size() == 0 ||
378  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
379  return NULL;
380  }
381  return YMagnetometer::FindMagnetometer(serial+"."+funcId);
382 }
383 
384 //--- (end of YMagnetometer implementation)
385 
386 //--- (YMagnetometer functions)
387 //--- (end of YMagnetometer 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
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const double XVALUE_INVALID
virtual int _invokeTimedReportCallback(YMeasure value)
virtual int registerTimedReportCallback(YMagnetometerTimedReportCallback callback)
static int DefaultCacheValidity
Definition: yocto_api.h:468
virtual int registerValueCallback(YMagnetometerValueCallback callback)
u64 _cacheExpiration
Definition: yocto_api.h:1571
virtual int _invokeValueCallback(string value)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const double YVALUE_INVALID
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
double get_xValue(void)
YMagnetometer(const string &func)
void(* YMagnetometerTimedReportCallback)(YMagnetometer *func, YMeasure measure)
static const double ZVALUE_INVALID
void(* YMagnetometerValueCallback)(YMagnetometer *func, const string &functionValue)
YMagnetometer * nextMagnetometer(void)
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
YMagnetometerTimedReportCallback _timedReportCallbackMagnetometer
#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 _parseAttr(YJSONObject *json_val)
static YMagnetometer * FindMagnetometer(string func)
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
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
string _className
Definition: yocto_api.h:1558
YMagnetometerValueCallback _valueCallbackMagnetometer
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static YMagnetometer * FirstMagnetometer(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
double get_zValue(void)
int set_bandwidth(int newval)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static const int BANDWIDTH_INVALID
double get_yValue(void)


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