yocto_quadraturedecoder.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_quadraturedecoder.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindQuadratureDecoder(), the high-level API for QuadratureDecoder 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
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__ "quadraturedecoder"
50 
52 //--- (YQuadratureDecoder initialization)
53  ,_speed(SPEED_INVALID)
54  ,_decoding(DECODING_INVALID)
55  ,_valueCallbackQuadratureDecoder(NULL)
56  ,_timedReportCallbackQuadratureDecoder(NULL)
57 //--- (end of YQuadratureDecoder initialization)
58 {
59  _className="QuadratureDecoder";
60 }
61 
63 {
64 //--- (YQuadratureDecoder cleanup)
65 //--- (end of YQuadratureDecoder cleanup)
66 }
67 //--- (YQuadratureDecoder implementation)
68 // static attributes
70 
72 {
73  if(json_val->has("speed")) {
74  _speed = floor(json_val->getDouble("speed") * 1000.0 / 65536.0 + 0.5) / 1000.0;
75  }
76  if(json_val->has("decoding")) {
77  _decoding = (Y_DECODING_enum)json_val->getInt("decoding");
78  }
79  return YSensor::_parseAttr(json_val);
80 }
81 
82 
94 {
95  string rest_val;
96  int res;
98  try {
99  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
100  res = _setAttr("currentValue", rest_val);
101  } catch (std::exception) {
103  throw;
104  }
106  return res;
107 }
108 
117 {
118  double res = 0.0;
120  try {
123  {
126  }
127  }
128  }
129  res = _speed;
130  } catch (std::exception) {
132  throw;
133  }
135  return res;
136 }
137 
147 {
148  Y_DECODING_enum res;
150  try {
153  {
156  }
157  }
158  }
159  res = _decoding;
160  } catch (std::exception) {
162  throw;
163  }
165  return res;
166 }
167 
179 {
180  string rest_val;
181  int res;
183  try {
184  rest_val = (newval>0 ? "1" : "0");
185  res = _setAttr("decoding", rest_val);
186  } catch (std::exception) {
188  throw;
189  }
191  return res;
192 }
193 
222 {
223  YQuadratureDecoder* obj = NULL;
224  int taken = 0;
225  if (YAPI::_apiInitialized) {
227  taken = 1;
228  }try {
229  obj = (YQuadratureDecoder*) YFunction::_FindFromCache("QuadratureDecoder", func);
230  if (obj == NULL) {
231  obj = new YQuadratureDecoder(func);
232  YFunction::_AddToCache("QuadratureDecoder", func, obj);
233  }
234  } catch (std::exception) {
236  throw;
237  }
239  return obj;
240 }
241 
254 {
255  string val;
256  if (callback != NULL) {
258  } else {
260  }
262  // Immediately invoke value callback with current value
263  if (callback != NULL && this->isOnline()) {
264  val = _advertisedValue;
265  if (!(val == "")) {
266  this->_invokeValueCallback(val);
267  }
268  }
269  return 0;
270 }
271 
273 {
274  if (_valueCallbackQuadratureDecoder != NULL) {
275  _valueCallbackQuadratureDecoder(this, value);
276  } else {
278  }
279  return 0;
280 }
281 
294 {
295  YSensor* sensor = NULL;
296  sensor = this;
297  if (callback != NULL) {
299  } else {
301  }
303  return 0;
304 }
305 
307 {
310  } else {
312  }
313  return 0;
314 }
315 
317 {
318  string hwid;
319 
320  if(YISERR(_nextFunction(hwid)) || hwid=="") {
321  return NULL;
322  }
324 }
325 
327 {
328  vector<YFUN_DESCR> v_fundescr;
329  YDEV_DESCR ydevice;
330  string serial, funcId, funcName, funcVal, errmsg;
331 
332  if(YISERR(YapiWrapper::getFunctionsByClass("QuadratureDecoder", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
333  v_fundescr.size() == 0 ||
334  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
335  return NULL;
336  }
337  return YQuadratureDecoder::FindQuadratureDecoder(serial+"."+funcId);
338 }
339 
340 //--- (end of YQuadratureDecoder implementation)
341 
342 //--- (YQuadratureDecoder functions)
343 //--- (end of YQuadratureDecoder 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
virtual int registerTimedReportCallback(YQuadratureDecoderTimedReportCallback callback)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static YQuadratureDecoder * FirstQuadratureDecoder(void)
virtual int _parseAttr(YJSONObject *json_val)
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
int set_decoding(Y_DECODING_enum newval)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
YQuadratureDecoder(const string &func)
virtual int _invokeValueCallback(string value)
int set_currentValue(double newval)
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
#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
void(* YQuadratureDecoderValueCallback)(YQuadratureDecoder *func, const string &functionValue)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static const Y_DECODING_enum DECODING_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
YQuadratureDecoderValueCallback _valueCallbackQuadratureDecoder
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
virtual int registerValueCallback(YQuadratureDecoderValueCallback callback)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
void(* YQuadratureDecoderTimedReportCallback)(YQuadratureDecoder *func, YMeasure measure)
string _className
Definition: yocto_api.h:1558
static const double SPEED_INVALID
static YQuadratureDecoder * FindQuadratureDecoder(string func)
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
Y_DECODING_enum get_decoding(void)
virtual int _invokeTimedReportCallback(YMeasure value)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
YQuadratureDecoder * nextQuadratureDecoder(void)
YQuadratureDecoderTimedReportCallback _timedReportCallbackQuadratureDecoder


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