yocto_currentloopoutput.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_currentloopoutput.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindCurrentLoopOutput(), the high-level API for CurrentLoopOutput 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
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__ "currentloopoutput"
50 
52 //--- (YCurrentLoopOutput initialization)
53  ,_current(CURRENT_INVALID)
54  ,_currentTransition(CURRENTTRANSITION_INVALID)
55  ,_currentAtStartUp(CURRENTATSTARTUP_INVALID)
56  ,_loopPower(LOOPPOWER_INVALID)
57  ,_valueCallbackCurrentLoopOutput(NULL)
58 //--- (end of YCurrentLoopOutput initialization)
59 {
60  _className="CurrentLoopOutput";
61 }
62 
64 {
65 //--- (YCurrentLoopOutput cleanup)
66 //--- (end of YCurrentLoopOutput cleanup)
67 }
68 //--- (YCurrentLoopOutput implementation)
69 // static attributes
73 
75 {
76  if(json_val->has("current")) {
77  _current = floor(json_val->getDouble("current") * 1000.0 / 65536.0 + 0.5) / 1000.0;
78  }
79  if(json_val->has("currentTransition")) {
80  _currentTransition = json_val->getString("currentTransition");
81  }
82  if(json_val->has("currentAtStartUp")) {
83  _currentAtStartUp = floor(json_val->getDouble("currentAtStartUp") * 1000.0 / 65536.0 + 0.5) / 1000.0;
84  }
85  if(json_val->has("loopPower")) {
86  _loopPower = (Y_LOOPPOWER_enum)json_val->getInt("loopPower");
87  }
88  return YFunction::_parseAttr(json_val);
89 }
90 
91 
104 {
105  string rest_val;
106  int res;
108  try {
109  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
110  res = _setAttr("current", rest_val);
111  } catch (std::exception) {
113  throw;
114  }
116  return res;
117 }
118 
127 {
128  double res = 0.0;
130  try {
133  {
136  }
137  }
138  }
139  res = _current;
140  } catch (std::exception) {
142  throw;
143  }
145  return res;
146 }
147 
149 {
150  string res;
152  try {
155  {
158  }
159  }
160  }
161  res = _currentTransition;
162  } catch (std::exception) {
164  throw;
165  }
167  return res;
168 }
169 
171 {
172  string rest_val;
173  int res;
175  try {
176  rest_val = newval;
177  res = _setAttr("currentTransition", rest_val);
178  } catch (std::exception) {
180  throw;
181  }
183  return res;
184 }
185 
197 {
198  string rest_val;
199  int res;
201  try {
202  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
203  res = _setAttr("currentAtStartUp", rest_val);
204  } catch (std::exception) {
206  throw;
207  }
209  return res;
210 }
211 
220 {
221  double res = 0.0;
223  try {
226  {
229  }
230  }
231  }
232  res = _currentAtStartUp;
233  } catch (std::exception) {
235  throw;
236  }
238  return res;
239 }
240 
252 {
253  Y_LOOPPOWER_enum res;
255  try {
258  {
261  }
262  }
263  }
264  res = _loopPower;
265  } catch (std::exception) {
267  throw;
268  }
270  return res;
271 }
272 
301 {
302  YCurrentLoopOutput* obj = NULL;
303  int taken = 0;
304  if (YAPI::_apiInitialized) {
306  taken = 1;
307  }try {
308  obj = (YCurrentLoopOutput*) YFunction::_FindFromCache("CurrentLoopOutput", func);
309  if (obj == NULL) {
310  obj = new YCurrentLoopOutput(func);
311  YFunction::_AddToCache("CurrentLoopOutput", func, obj);
312  }
313  } catch (std::exception) {
315  throw;
316  }
318  return obj;
319 }
320 
333 {
334  string val;
335  if (callback != NULL) {
337  } else {
339  }
341  // Immediately invoke value callback with current value
342  if (callback != NULL && this->isOnline()) {
343  val = _advertisedValue;
344  if (!(val == "")) {
345  this->_invokeValueCallback(val);
346  }
347  }
348  return 0;
349 }
350 
352 {
353  if (_valueCallbackCurrentLoopOutput != NULL) {
354  _valueCallbackCurrentLoopOutput(this, value);
355  } else {
357  }
358  return 0;
359 }
360 
371 int YCurrentLoopOutput::currentMove(double mA_target,int ms_duration)
372 {
373  string newval;
374  if (mA_target < 3.0) {
375  mA_target = 3.0;
376  }
377  if (mA_target > 21.0) {
378  mA_target = 21.0;
379  }
380  newval = YapiWrapper::ysprintf("%d:%d", (int) floor(mA_target*65536+0.5),ms_duration);
381 
382  return this->set_currentTransition(newval);
383 }
384 
386 {
387  string hwid;
388 
389  if(YISERR(_nextFunction(hwid)) || hwid=="") {
390  return NULL;
391  }
393 }
394 
396 {
397  vector<YFUN_DESCR> v_fundescr;
398  YDEV_DESCR ydevice;
399  string serial, funcId, funcName, funcVal, errmsg;
400 
401  if(YISERR(YapiWrapper::getFunctionsByClass("CurrentLoopOutput", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
402  v_fundescr.size() == 0 ||
403  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
404  return NULL;
405  }
406  return YCurrentLoopOutput::FindCurrentLoopOutput(serial+"."+funcId);
407 }
408 
409 //--- (end of YCurrentLoopOutput implementation)
410 
411 //--- (YCurrentLoopOutput functions)
412 //--- (end of YCurrentLoopOutput functions)
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
virtual int registerValueCallback(YCurrentLoopOutputValueCallback callback)
Y_LOOPPOWER_enum get_loopPower(void)
static const double CURRENTATSTARTUP_INVALID
YCurrentLoopOutput(const string &func)
Y_LOOPPOWER_enum _loopPower
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
YCurrentLoopOutput * nextCurrentLoopOutput(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
void(* YCurrentLoopOutputValueCallback)(YCurrentLoopOutput *func, const string &functionValue)
u64 _cacheExpiration
Definition: yocto_api.h:1571
static const string CURRENTTRANSITION_INVALID
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
YCurrentLoopOutputValueCallback _valueCallbackCurrentLoopOutput
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
int set_currentAtStartUp(double newval)
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
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
virtual int currentMove(double mA_target, int ms_duration)
static const double CURRENT_INVALID
virtual int _parseAttr(YJSONObject *json_val)
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
int set_currentTransition(const string &newval)
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 _invokeValueCallback(string value)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static const Y_LOOPPOWER_enum LOOPPOWER_INVALID
string _className
Definition: yocto_api.h:1558
static YCurrentLoopOutput * FindCurrentLoopOutput(string func)
int set_current(double newval)
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
static YCurrentLoopOutput * FirstCurrentLoopOutput(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