yocto_voltageoutput.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_voltageoutput.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindVoltageOutput(), the high-level API for VoltageOutput 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_voltageoutput.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__ "voltageoutput"
50 
51 YVoltageOutput::YVoltageOutput(const string& func): YFunction(func)
52 //--- (YVoltageOutput initialization)
53  ,_currentVoltage(CURRENTVOLTAGE_INVALID)
54  ,_voltageTransition(VOLTAGETRANSITION_INVALID)
55  ,_voltageAtStartUp(VOLTAGEATSTARTUP_INVALID)
56  ,_valueCallbackVoltageOutput(NULL)
57 //--- (end of YVoltageOutput initialization)
58 {
59  _className="VoltageOutput";
60 }
61 
63 {
64 //--- (YVoltageOutput cleanup)
65 //--- (end of YVoltageOutput cleanup)
66 }
67 //--- (YVoltageOutput implementation)
68 // static attributes
72 
74 {
75  if(json_val->has("currentVoltage")) {
76  _currentVoltage = floor(json_val->getDouble("currentVoltage") * 1000.0 / 65536.0 + 0.5) / 1000.0;
77  }
78  if(json_val->has("voltageTransition")) {
79  _voltageTransition = json_val->getString("voltageTransition");
80  }
81  if(json_val->has("voltageAtStartUp")) {
82  _voltageAtStartUp = floor(json_val->getDouble("voltageAtStartUp") * 1000.0 / 65536.0 + 0.5) / 1000.0;
83  }
84  return YFunction::_parseAttr(json_val);
85 }
86 
87 
98 {
99  string rest_val;
100  int res;
102  try {
103  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
104  res = _setAttr("currentVoltage", rest_val);
105  } catch (std::exception) {
107  throw;
108  }
110  return res;
111 }
112 
121 {
122  double res = 0.0;
124  try {
127  {
130  }
131  }
132  }
133  res = _currentVoltage;
134  } catch (std::exception) {
136  throw;
137  }
139  return res;
140 }
141 
143 {
144  string res;
146  try {
149  {
152  }
153  }
154  }
155  res = _voltageTransition;
156  } catch (std::exception) {
158  throw;
159  }
161  return res;
162 }
163 
164 int YVoltageOutput::set_voltageTransition(const string& newval)
165 {
166  string rest_val;
167  int res;
169  try {
170  rest_val = newval;
171  res = _setAttr("voltageTransition", rest_val);
172  } catch (std::exception) {
174  throw;
175  }
177  return res;
178 }
179 
191 {
192  string rest_val;
193  int res;
195  try {
196  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
197  res = _setAttr("voltageAtStartUp", rest_val);
198  } catch (std::exception) {
200  throw;
201  }
203  return res;
204 }
205 
214 {
215  double res = 0.0;
217  try {
220  {
223  }
224  }
225  }
226  res = _voltageAtStartUp;
227  } catch (std::exception) {
229  throw;
230  }
232  return res;
233 }
234 
263 {
264  YVoltageOutput* obj = NULL;
265  int taken = 0;
266  if (YAPI::_apiInitialized) {
268  taken = 1;
269  }try {
270  obj = (YVoltageOutput*) YFunction::_FindFromCache("VoltageOutput", func);
271  if (obj == NULL) {
272  obj = new YVoltageOutput(func);
273  YFunction::_AddToCache("VoltageOutput", func, obj);
274  }
275  } catch (std::exception) {
277  throw;
278  }
280  return obj;
281 }
282 
295 {
296  string val;
297  if (callback != NULL) {
299  } else {
301  }
302  _valueCallbackVoltageOutput = callback;
303  // Immediately invoke value callback with current value
304  if (callback != NULL && this->isOnline()) {
305  val = _advertisedValue;
306  if (!(val == "")) {
307  this->_invokeValueCallback(val);
308  }
309  }
310  return 0;
311 }
312 
314 {
315  if (_valueCallbackVoltageOutput != NULL) {
316  _valueCallbackVoltageOutput(this, value);
317  } else {
319  }
320  return 0;
321 }
322 
333 int YVoltageOutput::voltageMove(double V_target,int ms_duration)
334 {
335  string newval;
336  if (V_target < 0.0) {
337  V_target = 0.0;
338  }
339  if (V_target > 10.0) {
340  V_target = 10.0;
341  }
342  newval = YapiWrapper::ysprintf("%d:%d", (int) floor(V_target*65536+0.5),ms_duration);
343 
344  return this->set_voltageTransition(newval);
345 }
346 
348 {
349  string hwid;
350 
351  if(YISERR(_nextFunction(hwid)) || hwid=="") {
352  return NULL;
353  }
355 }
356 
358 {
359  vector<YFUN_DESCR> v_fundescr;
360  YDEV_DESCR ydevice;
361  string serial, funcId, funcName, funcVal, errmsg;
362 
363  if(YISERR(YapiWrapper::getFunctionsByClass("VoltageOutput", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
364  v_fundescr.size() == 0 ||
365  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
366  return NULL;
367  }
368  return YVoltageOutput::FindVoltageOutput(serial+"."+funcId);
369 }
370 
371 //--- (end of YVoltageOutput implementation)
372 
373 //--- (YVoltageOutput functions)
374 //--- (end of YVoltageOutput functions)
string _advertisedValue
Definition: yocto_api.h:1569
double get_voltageAtStartUp(void)
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
virtual int voltageMove(double V_target, int ms_duration)
double get_currentVoltage(void)
static YVoltageOutput * FindVoltageOutput(string func)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
string get_voltageTransition(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
static const double VOLTAGEATSTARTUP_INVALID
static YVoltageOutput * FirstVoltageOutput(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
virtual int _invokeValueCallback(string value)
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
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
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
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
static const double CURRENTVOLTAGE_INVALID
bool isOnline(void)
Definition: yocto_api.cpp:3291
int set_currentVoltage(double newval)
virtual int _parseAttr(YJSONObject *json_val)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
YVoltageOutput(const string &func)
int set_voltageAtStartUp(double newval)
string _className
Definition: yocto_api.h:1558
virtual int registerValueCallback(YVoltageOutputValueCallback callback)
YVoltageOutput * nextVoltageOutput(void)
int set_voltageTransition(const string &newval)
YVoltageOutputValueCallback _valueCallbackVoltageOutput
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
void(* YVoltageOutputValueCallback)(YVoltageOutput *func, const string &functionValue)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static const string VOLTAGETRANSITION_INVALID
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