yocto_dualpower.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_dualpower.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindDualPower(), the high-level API for DualPower 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_dualpower.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__ "dualpower"
50 
51 YDualPower::YDualPower(const string& func): YFunction(func)
52 //--- (YDualPower initialization)
53  ,_powerState(POWERSTATE_INVALID)
54  ,_powerControl(POWERCONTROL_INVALID)
55  ,_extVoltage(EXTVOLTAGE_INVALID)
56  ,_valueCallbackDualPower(NULL)
57 //--- (end of YDualPower initialization)
58 {
59  _className="DualPower";
60 }
61 
63 {
64 //--- (YDualPower cleanup)
65 //--- (end of YDualPower cleanup)
66 }
67 //--- (YDualPower implementation)
68 // static attributes
69 
71 {
72  if(json_val->has("powerState")) {
73  _powerState = (Y_POWERSTATE_enum)json_val->getInt("powerState");
74  }
75  if(json_val->has("powerControl")) {
76  _powerControl = (Y_POWERCONTROL_enum)json_val->getInt("powerControl");
77  }
78  if(json_val->has("extVoltage")) {
79  _extVoltage = json_val->getInt("extVoltage");
80  }
81  return YFunction::_parseAttr(json_val);
82 }
83 
84 
94 {
97  try {
100  {
103  }
104  }
105  }
106  res = _powerState;
107  } catch (std::exception) {
109  throw;
110  }
112  return res;
113 }
114 
124 {
127  try {
130  {
133  }
134  }
135  }
136  res = _powerControl;
137  } catch (std::exception) {
139  throw;
140  }
142  return res;
143 }
144 
157 {
158  string rest_val;
159  int res;
161  try {
162  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
163  res = _setAttr("powerControl", rest_val);
164  } catch (std::exception) {
166  throw;
167  }
169  return res;
170 }
171 
180 {
181  int res = 0;
183  try {
186  {
189  }
190  }
191  }
192  res = _extVoltage;
193  } catch (std::exception) {
195  throw;
196  }
198  return res;
199 }
200 
229 {
230  YDualPower* obj = NULL;
231  int taken = 0;
232  if (YAPI::_apiInitialized) {
234  taken = 1;
235  }try {
236  obj = (YDualPower*) YFunction::_FindFromCache("DualPower", func);
237  if (obj == NULL) {
238  obj = new YDualPower(func);
239  YFunction::_AddToCache("DualPower", func, obj);
240  }
241  } catch (std::exception) {
243  throw;
244  }
246  return obj;
247 }
248 
261 {
262  string val;
263  if (callback != NULL) {
265  } else {
267  }
268  _valueCallbackDualPower = callback;
269  // Immediately invoke value callback with current value
270  if (callback != NULL && this->isOnline()) {
271  val = _advertisedValue;
272  if (!(val == "")) {
273  this->_invokeValueCallback(val);
274  }
275  }
276  return 0;
277 }
278 
280 {
281  if (_valueCallbackDualPower != NULL) {
282  _valueCallbackDualPower(this, value);
283  } else {
285  }
286  return 0;
287 }
288 
290 {
291  string hwid;
292 
293  if(YISERR(_nextFunction(hwid)) || hwid=="") {
294  return NULL;
295  }
296  return YDualPower::FindDualPower(hwid);
297 }
298 
300 {
301  vector<YFUN_DESCR> v_fundescr;
302  YDEV_DESCR ydevice;
303  string serial, funcId, funcName, funcVal, errmsg;
304 
305  if(YISERR(YapiWrapper::getFunctionsByClass("DualPower", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
306  v_fundescr.size() == 0 ||
307  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
308  return NULL;
309  }
310  return YDualPower::FindDualPower(serial+"."+funcId);
311 }
312 
313 //--- (end of YDualPower implementation)
314 
315 //--- (YDualPower functions)
316 //--- (end of YDualPower functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static const Y_POWERSTATE_enum POWERSTATE_INVALID
Y_POWERCONTROL_enum
Y_POWERSTATE_enum _powerState
Y_POWERSTATE_enum
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
int set_powerControl(Y_POWERCONTROL_enum newval)
int get_extVoltage(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
Y_POWERCONTROL_enum _powerControl
virtual int registerValueCallback(YDualPowerValueCallback callback)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
YDualPower * nextDualPower(void)
void(* YDualPowerValueCallback)(YDualPower *func, const string &functionValue)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
virtual int _invokeValueCallback(string value)
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
Y_POWERCONTROL_enum get_powerControl(void)
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_POWERCONTROL_enum POWERCONTROL_INVALID
static const int EXTVOLTAGE_INVALID
YDualPowerValueCallback _valueCallbackDualPower
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
static YDualPower * FindDualPower(string func)
Y_POWERSTATE_enum get_powerState(void)
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 _parseAttr(YJSONObject *json_val)
string _className
Definition: yocto_api.h:1558
YDualPower(const string &func)
static YDualPower * FirstDualPower(void)
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
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563


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