yocto_poweroutput.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_poweroutput.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindPowerOutput(), the high-level API for PowerOutput 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_poweroutput.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__ "poweroutput"
50 
51 YPowerOutput::YPowerOutput(const string& func): YFunction(func)
52 //--- (YPowerOutput initialization)
53  ,_voltage(VOLTAGE_INVALID)
54  ,_valueCallbackPowerOutput(NULL)
55 //--- (end of YPowerOutput initialization)
56 {
57  _className="PowerOutput";
58 }
59 
61 {
62 //--- (YPowerOutput cleanup)
63 //--- (end of YPowerOutput cleanup)
64 }
65 //--- (YPowerOutput implementation)
66 // static attributes
67 
69 {
70  if(json_val->has("voltage")) {
71  _voltage = (Y_VOLTAGE_enum)json_val->getInt("voltage");
72  }
73  return YFunction::_parseAttr(json_val);
74 }
75 
76 
86 {
87  Y_VOLTAGE_enum res;
89  try {
92  {
95  }
96  }
97  }
98  res = _voltage;
99  } catch (std::exception) {
101  throw;
102  }
104  return res;
105 }
106 
121 {
122  string rest_val;
123  int res;
125  try {
126  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
127  res = _setAttr("voltage", rest_val);
128  } catch (std::exception) {
130  throw;
131  }
133  return res;
134 }
135 
164 {
165  YPowerOutput* obj = NULL;
166  int taken = 0;
167  if (YAPI::_apiInitialized) {
169  taken = 1;
170  }try {
171  obj = (YPowerOutput*) YFunction::_FindFromCache("PowerOutput", func);
172  if (obj == NULL) {
173  obj = new YPowerOutput(func);
174  YFunction::_AddToCache("PowerOutput", func, obj);
175  }
176  } catch (std::exception) {
178  throw;
179  }
181  return obj;
182 }
183 
196 {
197  string val;
198  if (callback != NULL) {
200  } else {
202  }
203  _valueCallbackPowerOutput = callback;
204  // Immediately invoke value callback with current value
205  if (callback != NULL && this->isOnline()) {
206  val = _advertisedValue;
207  if (!(val == "")) {
208  this->_invokeValueCallback(val);
209  }
210  }
211  return 0;
212 }
213 
215 {
216  if (_valueCallbackPowerOutput != NULL) {
217  _valueCallbackPowerOutput(this, value);
218  } else {
220  }
221  return 0;
222 }
223 
225 {
226  string hwid;
227 
228  if(YISERR(_nextFunction(hwid)) || hwid=="") {
229  return NULL;
230  }
231  return YPowerOutput::FindPowerOutput(hwid);
232 }
233 
235 {
236  vector<YFUN_DESCR> v_fundescr;
237  YDEV_DESCR ydevice;
238  string serial, funcId, funcName, funcVal, errmsg;
239 
240  if(YISERR(YapiWrapper::getFunctionsByClass("PowerOutput", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
241  v_fundescr.size() == 0 ||
242  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
243  return NULL;
244  }
245  return YPowerOutput::FindPowerOutput(serial+"."+funcId);
246 }
247 
248 //--- (end of YPowerOutput implementation)
249 
250 //--- (YPowerOutput functions)
251 //--- (end of YPowerOutput functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
void(* YPowerOutputValueCallback)(YPowerOutput *func, const string &functionValue)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
Y_VOLTAGE_enum get_voltage(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
Y_VOLTAGE_enum _voltage
int set_voltage(Y_VOLTAGE_enum newval)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int registerValueCallback(YPowerOutputValueCallback 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
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 YPowerOutput * FindPowerOutput(string func)
static const Y_VOLTAGE_enum VOLTAGE_INVALID
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
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
Y_VOLTAGE_enum
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
virtual int _invokeValueCallback(string value)
YPowerOutputValueCallback _valueCallbackPowerOutput
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
YPowerOutput(const string &func)
virtual int _parseAttr(YJSONObject *json_val)
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
static YPowerOutput * FirstPowerOutput(void)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
YPowerOutput * nextPowerOutput(void)


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