yocto_accelerometer.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_accelerometer.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindAccelerometer(), the high-level API for Accelerometer 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_accelerometer.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__ "accelerometer"
50 
51 YAccelerometer::YAccelerometer(const string& func): YSensor(func)
52 //--- (YAccelerometer initialization)
53  ,_bandwidth(BANDWIDTH_INVALID)
54  ,_xValue(XVALUE_INVALID)
55  ,_yValue(YVALUE_INVALID)
56  ,_zValue(ZVALUE_INVALID)
57  ,_gravityCancellation(GRAVITYCANCELLATION_INVALID)
58  ,_valueCallbackAccelerometer(NULL)
59  ,_timedReportCallbackAccelerometer(NULL)
60 //--- (end of YAccelerometer initialization)
61 {
62  _className="Accelerometer";
63 }
64 
66 {
67 //--- (YAccelerometer cleanup)
68 //--- (end of YAccelerometer cleanup)
69 }
70 //--- (YAccelerometer implementation)
71 // static attributes
75 
77 {
78  if(json_val->has("bandwidth")) {
79  _bandwidth = json_val->getInt("bandwidth");
80  }
81  if(json_val->has("xValue")) {
82  _xValue = floor(json_val->getDouble("xValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
83  }
84  if(json_val->has("yValue")) {
85  _yValue = floor(json_val->getDouble("yValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
86  }
87  if(json_val->has("zValue")) {
88  _zValue = floor(json_val->getDouble("zValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
89  }
90  if(json_val->has("gravityCancellation")) {
91  _gravityCancellation = (Y_GRAVITYCANCELLATION_enum)json_val->getInt("gravityCancellation");
92  }
93  return YSensor::_parseAttr(json_val);
94 }
95 
96 
105 {
106  int res = 0;
108  try {
111  {
114  }
115  }
116  }
117  res = _bandwidth;
118  } catch (std::exception) {
120  throw;
121  }
123  return res;
124 }
125 
137 {
138  string rest_val;
139  int res;
141  try {
142  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
143  res = _setAttr("bandwidth", rest_val);
144  } catch (std::exception) {
146  throw;
147  }
149  return res;
150 }
151 
160 {
161  double res = 0.0;
163  try {
166  {
169  }
170  }
171  }
172  res = _xValue;
173  } catch (std::exception) {
175  throw;
176  }
178  return res;
179 }
180 
189 {
190  double res = 0.0;
192  try {
195  {
198  }
199  }
200  }
201  res = _yValue;
202  } catch (std::exception) {
204  throw;
205  }
207  return res;
208 }
209 
218 {
219  double res = 0.0;
221  try {
224  {
227  }
228  }
229  }
230  res = _zValue;
231  } catch (std::exception) {
233  throw;
234  }
236  return res;
237 }
238 
240 {
243  try {
246  {
249  }
250  }
251  }
252  res = _gravityCancellation;
253  } catch (std::exception) {
255  throw;
256  }
258  return res;
259 }
260 
262 {
263  string rest_val;
264  int res;
266  try {
267  rest_val = (newval>0 ? "1" : "0");
268  res = _setAttr("gravityCancellation", rest_val);
269  } catch (std::exception) {
271  throw;
272  }
274  return res;
275 }
276 
305 {
306  YAccelerometer* obj = NULL;
307  int taken = 0;
308  if (YAPI::_apiInitialized) {
310  taken = 1;
311  }try {
312  obj = (YAccelerometer*) YFunction::_FindFromCache("Accelerometer", func);
313  if (obj == NULL) {
314  obj = new YAccelerometer(func);
315  YFunction::_AddToCache("Accelerometer", func, obj);
316  }
317  } catch (std::exception) {
319  throw;
320  }
322  return obj;
323 }
324 
337 {
338  string val;
339  if (callback != NULL) {
341  } else {
343  }
344  _valueCallbackAccelerometer = callback;
345  // Immediately invoke value callback with current value
346  if (callback != NULL && this->isOnline()) {
347  val = _advertisedValue;
348  if (!(val == "")) {
349  this->_invokeValueCallback(val);
350  }
351  }
352  return 0;
353 }
354 
356 {
357  if (_valueCallbackAccelerometer != NULL) {
358  _valueCallbackAccelerometer(this, value);
359  } else {
361  }
362  return 0;
363 }
364 
377 {
378  YSensor* sensor = NULL;
379  sensor = this;
380  if (callback != NULL) {
382  } else {
384  }
386  return 0;
387 }
388 
390 {
391  if (_timedReportCallbackAccelerometer != NULL) {
393  } else {
395  }
396  return 0;
397 }
398 
400 {
401  string hwid;
402 
403  if(YISERR(_nextFunction(hwid)) || hwid=="") {
404  return NULL;
405  }
407 }
408 
410 {
411  vector<YFUN_DESCR> v_fundescr;
412  YDEV_DESCR ydevice;
413  string serial, funcId, funcName, funcVal, errmsg;
414 
415  if(YISERR(YapiWrapper::getFunctionsByClass("Accelerometer", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
416  v_fundescr.size() == 0 ||
417  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
418  return NULL;
419  }
420  return YAccelerometer::FindAccelerometer(serial+"."+funcId);
421 }
422 
423 //--- (end of YAccelerometer implementation)
424 
425 //--- (YAccelerometer functions)
426 //--- (end of YAccelerometer 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
YAccelerometerValueCallback _valueCallbackAccelerometer
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
void(* YAccelerometerValueCallback)(YAccelerometer *func, const string &functionValue)
static const double ZVALUE_INVALID
YAccelerometer * nextAccelerometer(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
YAccelerometerTimedReportCallback _timedReportCallbackAccelerometer
Y_GRAVITYCANCELLATION_enum _gravityCancellation
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const double XVALUE_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
int set_gravityCancellation(Y_GRAVITYCANCELLATION_enum newval)
int set_bandwidth(int newval)
void(* YAccelerometerTimedReportCallback)(YAccelerometer *func, YMeasure measure)
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
virtual int registerTimedReportCallback(YAccelerometerTimedReportCallback callback)
#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
virtual int registerValueCallback(YAccelerometerValueCallback callback)
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 const int BANDWIDTH_INVALID
Y_GRAVITYCANCELLATION_enum
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 _invokeValueCallback(string value)
string _className
Definition: yocto_api.h:1558
static YAccelerometer * FirstAccelerometer(void)
Y_GRAVITYCANCELLATION_enum get_gravityCancellation(void)
static const double YVALUE_INVALID
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static const Y_GRAVITYCANCELLATION_enum GRAVITYCANCELLATION_INVALID
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
bool has(const string &key)
Definition: yocto_api.cpp:735
static YAccelerometer * FindAccelerometer(string func)
YAccelerometer(const string &func)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeTimedReportCallback(YMeasure value)


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