yocto_pwmpowersource.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_pwmpowersource.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindPwmPowerSource(), the high-level API for PwmPowerSource 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_pwmpowersource.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__ "pwmpowersource"
50 
51 YPwmPowerSource::YPwmPowerSource(const string& func): YFunction(func)
52 //--- (YPwmPowerSource initialization)
53  ,_powerMode(POWERMODE_INVALID)
54  ,_valueCallbackPwmPowerSource(NULL)
55 //--- (end of YPwmPowerSource initialization)
56 {
57  _className="PwmPowerSource";
58 }
59 
61 {
62 //--- (YPwmPowerSource cleanup)
63 //--- (end of YPwmPowerSource cleanup)
64 }
65 //--- (YPwmPowerSource implementation)
66 // static attributes
67 
69 {
70  if(json_val->has("powerMode")) {
71  _powerMode = (Y_POWERMODE_enum)json_val->getInt("powerMode");
72  }
73  return YFunction::_parseAttr(json_val);
74 }
75 
76 
86 {
87  Y_POWERMODE_enum res;
89  try {
92  {
95  }
96  }
97  }
98  res = _powerMode;
99  } catch (std::exception) {
101  throw;
102  }
104  return res;
105 }
106 
124 {
125  string rest_val;
126  int res;
128  try {
129  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
130  res = _setAttr("powerMode", rest_val);
131  } catch (std::exception) {
133  throw;
134  }
136  return res;
137 }
138 
167 {
168  YPwmPowerSource* obj = NULL;
169  int taken = 0;
170  if (YAPI::_apiInitialized) {
172  taken = 1;
173  }try {
174  obj = (YPwmPowerSource*) YFunction::_FindFromCache("PwmPowerSource", func);
175  if (obj == NULL) {
176  obj = new YPwmPowerSource(func);
177  YFunction::_AddToCache("PwmPowerSource", func, obj);
178  }
179  } catch (std::exception) {
181  throw;
182  }
184  return obj;
185 }
186 
199 {
200  string val;
201  if (callback != NULL) {
203  } else {
205  }
206  _valueCallbackPwmPowerSource = callback;
207  // Immediately invoke value callback with current value
208  if (callback != NULL && this->isOnline()) {
209  val = _advertisedValue;
210  if (!(val == "")) {
211  this->_invokeValueCallback(val);
212  }
213  }
214  return 0;
215 }
216 
218 {
219  if (_valueCallbackPwmPowerSource != NULL) {
220  _valueCallbackPwmPowerSource(this, value);
221  } else {
223  }
224  return 0;
225 }
226 
228 {
229  string hwid;
230 
231  if(YISERR(_nextFunction(hwid)) || hwid=="") {
232  return NULL;
233  }
235 }
236 
238 {
239  vector<YFUN_DESCR> v_fundescr;
240  YDEV_DESCR ydevice;
241  string serial, funcId, funcName, funcVal, errmsg;
242 
243  if(YISERR(YapiWrapper::getFunctionsByClass("PwmPowerSource", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
244  v_fundescr.size() == 0 ||
245  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
246  return NULL;
247  }
248  return YPwmPowerSource::FindPwmPowerSource(serial+"."+funcId);
249 }
250 
251 //--- (end of YPwmPowerSource implementation)
252 
253 //--- (YPwmPowerSource functions)
254 //--- (end of YPwmPowerSource functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_POWERMODE_enum
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _invokeValueCallback(string value)
virtual int _parseAttr(YJSONObject *json_val)
static YPwmPowerSource * FindPwmPowerSource(string func)
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
YPwmPowerSource(const string &func)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
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
static const Y_POWERMODE_enum POWERMODE_INVALID
Y_POWERMODE_enum _powerMode
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(YPwmPowerSourceValueCallback callback)
YPwmPowerSource * nextPwmPowerSource(void)
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 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
YPwmPowerSourceValueCallback _valueCallbackPwmPowerSource
void(* YPwmPowerSourceValueCallback)(YPwmPowerSource *func, const string &functionValue)
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
int set_powerMode(Y_POWERMODE_enum newval)
bool has(const string &key)
Definition: yocto_api.cpp:735
Y_POWERMODE_enum get_powerMode(void)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static YPwmPowerSource * FirstPwmPowerSource(void)
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