yocto_tilt.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_tilt.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindTilt(), the high-level API for Tilt 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_tilt.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__ "tilt"
50 
51 YTilt::YTilt(const string& func): YSensor(func)
52 //--- (YTilt initialization)
53  ,_bandwidth(BANDWIDTH_INVALID)
54  ,_axis(AXIS_INVALID)
55  ,_valueCallbackTilt(NULL)
56  ,_timedReportCallbackTilt(NULL)
57 //--- (end of YTilt initialization)
58 {
59  _className="Tilt";
60 }
61 
63 {
64 //--- (YTilt cleanup)
65 //--- (end of YTilt cleanup)
66 }
67 //--- (YTilt implementation)
68 // static attributes
69 
71 {
72  if(json_val->has("bandwidth")) {
73  _bandwidth = json_val->getInt("bandwidth");
74  }
75  if(json_val->has("axis")) {
76  _axis = (Y_AXIS_enum) json_val->getInt("axis");
77  }
78  return YSensor::_parseAttr(json_val);
79 }
80 
81 
90 {
91  int res = 0;
93  try {
96  {
99  }
100  }
101  }
102  res = _bandwidth;
103  } catch (std::exception) {
105  throw;
106  }
108  return res;
109 }
110 
121 int YTilt::set_bandwidth(int newval)
122 {
123  string rest_val;
124  int res;
126  try {
127  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
128  res = _setAttr("bandwidth", rest_val);
129  } catch (std::exception) {
131  throw;
132  }
134  return res;
135 }
136 
138 {
139  Y_AXIS_enum res;
141  try {
144  {
146  return YTilt::AXIS_INVALID;
147  }
148  }
149  }
150  res = _axis;
151  } catch (std::exception) {
153  throw;
154  }
156  return res;
157 }
158 
186 YTilt* YTilt::FindTilt(string func)
187 {
188  YTilt* obj = NULL;
189  int taken = 0;
190  if (YAPI::_apiInitialized) {
192  taken = 1;
193  }try {
194  obj = (YTilt*) YFunction::_FindFromCache("Tilt", func);
195  if (obj == NULL) {
196  obj = new YTilt(func);
197  YFunction::_AddToCache("Tilt", func, obj);
198  }
199  } catch (std::exception) {
201  throw;
202  }
204  return obj;
205 }
206 
219 {
220  string val;
221  if (callback != NULL) {
223  } else {
225  }
226  _valueCallbackTilt = callback;
227  // Immediately invoke value callback with current value
228  if (callback != NULL && this->isOnline()) {
229  val = _advertisedValue;
230  if (!(val == "")) {
231  this->_invokeValueCallback(val);
232  }
233  }
234  return 0;
235 }
236 
238 {
239  if (_valueCallbackTilt != NULL) {
240  _valueCallbackTilt(this, value);
241  } else {
243  }
244  return 0;
245 }
246 
259 {
260  YSensor* sensor = NULL;
261  sensor = this;
262  if (callback != NULL) {
264  } else {
266  }
267  _timedReportCallbackTilt = callback;
268  return 0;
269 }
270 
272 {
273  if (_timedReportCallbackTilt != NULL) {
274  _timedReportCallbackTilt(this, value);
275  } else {
277  }
278  return 0;
279 }
280 
282 {
283  string hwid;
284 
285  if(YISERR(_nextFunction(hwid)) || hwid=="") {
286  return NULL;
287  }
288  return YTilt::FindTilt(hwid);
289 }
290 
292 {
293  vector<YFUN_DESCR> v_fundescr;
294  YDEV_DESCR ydevice;
295  string serial, funcId, funcName, funcVal, errmsg;
296 
297  if(YISERR(YapiWrapper::getFunctionsByClass("Tilt", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
298  v_fundescr.size() == 0 ||
299  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
300  return NULL;
301  }
302  return YTilt::FindTilt(serial+"."+funcId);
303 }
304 
305 //--- (end of YTilt implementation)
306 
307 //--- (YTilt functions)
308 //--- (end of YTilt functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
virtual int registerValueCallback(YTiltValueCallback callback)
Definition: yocto_tilt.cpp:218
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_tilt.cpp:70
YTiltValueCallback _valueCallbackTilt
Definition: yocto_tilt.h:93
static int DefaultCacheValidity
Definition: yocto_api.h:468
static const Y_AXIS_enum AXIS_INVALID
Definition: yocto_tilt.h:114
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
Y_AXIS_enum _axis
Definition: yocto_tilt.h:92
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
~YTilt()
Definition: yocto_tilt.cpp:62
YTilt(const string &func)
Definition: yocto_tilt.cpp:51
void(* YTiltValueCallback)(YTilt *func, const string &functionValue)
Definition: yocto_tilt.h:54
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int set_bandwidth(int newval)
Definition: yocto_tilt.cpp:121
int getInt(const string &key)
Definition: yocto_api.cpp:779
static YTilt * FindTilt(string func)
Definition: yocto_tilt.cpp:186
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(YTiltTimedReportCallback callback)
Definition: yocto_tilt.cpp:258
YTiltTimedReportCallback _timedReportCallbackTilt
Definition: yocto_tilt.h:94
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_api.cpp:7936
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
int get_bandwidth(void)
Definition: yocto_tilt.cpp:89
Y_AXIS_enum get_axis(void)
Definition: yocto_tilt.cpp:137
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static YTilt * FirstTilt(void)
Definition: yocto_tilt.cpp:291
void(* YTiltTimedReportCallback)(YTilt *func, YMeasure measure)
Definition: yocto_tilt.h:56
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
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_tilt.cpp:271
string _className
Definition: yocto_api.h:1558
static const int BANDWIDTH_INVALID
Definition: yocto_tilt.h:110
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
int _bandwidth
Definition: yocto_tilt.h:91
YTilt * nextTilt(void)
Definition: yocto_tilt.cpp:281
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
Y_AXIS_enum
Definition: yocto_compass.h:59
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)
Definition: yocto_tilt.cpp:237


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