yocto_powersupply.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_powersupply.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindPowerSupply(), the high-level API for PowerSupply 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_powersupply.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__ "powersupply"
50 
51 YPowerSupply::YPowerSupply(const string& func): YFunction(func)
52 //--- (YPowerSupply initialization)
53  ,_voltageSetPoint(VOLTAGESETPOINT_INVALID)
54  ,_currentLimit(CURRENTLIMIT_INVALID)
55  ,_powerOutput(POWEROUTPUT_INVALID)
56  ,_voltageSense(VOLTAGESENSE_INVALID)
57  ,_measuredVoltage(MEASUREDVOLTAGE_INVALID)
58  ,_measuredCurrent(MEASUREDCURRENT_INVALID)
59  ,_inputVoltage(INPUTVOLTAGE_INVALID)
60  ,_vInt(VINT_INVALID)
61  ,_ldoTemperature(LDOTEMPERATURE_INVALID)
62  ,_voltageTransition(VOLTAGETRANSITION_INVALID)
63  ,_voltageAtStartUp(VOLTAGEATSTARTUP_INVALID)
64  ,_currentAtStartUp(CURRENTATSTARTUP_INVALID)
65  ,_command(COMMAND_INVALID)
66  ,_valueCallbackPowerSupply(NULL)
67 //--- (end of YPowerSupply initialization)
68 {
69  _className="PowerSupply";
70 }
71 
73 {
74 //--- (YPowerSupply cleanup)
75 //--- (end of YPowerSupply cleanup)
76 }
77 //--- (YPowerSupply implementation)
78 // static attributes
90 
92 {
93  if(json_val->has("voltageSetPoint")) {
94  _voltageSetPoint = floor(json_val->getDouble("voltageSetPoint") * 1000.0 / 65536.0 + 0.5) / 1000.0;
95  }
96  if(json_val->has("currentLimit")) {
97  _currentLimit = floor(json_val->getDouble("currentLimit") * 1000.0 / 65536.0 + 0.5) / 1000.0;
98  }
99  if(json_val->has("powerOutput")) {
100  _powerOutput = (Y_POWEROUTPUT_enum)json_val->getInt("powerOutput");
101  }
102  if(json_val->has("voltageSense")) {
103  _voltageSense = (Y_VOLTAGESENSE_enum)json_val->getInt("voltageSense");
104  }
105  if(json_val->has("measuredVoltage")) {
106  _measuredVoltage = floor(json_val->getDouble("measuredVoltage") * 1000.0 / 65536.0 + 0.5) / 1000.0;
107  }
108  if(json_val->has("measuredCurrent")) {
109  _measuredCurrent = floor(json_val->getDouble("measuredCurrent") * 1000.0 / 65536.0 + 0.5) / 1000.0;
110  }
111  if(json_val->has("inputVoltage")) {
112  _inputVoltage = floor(json_val->getDouble("inputVoltage") * 1000.0 / 65536.0 + 0.5) / 1000.0;
113  }
114  if(json_val->has("vInt")) {
115  _vInt = floor(json_val->getDouble("vInt") * 1000.0 / 65536.0 + 0.5) / 1000.0;
116  }
117  if(json_val->has("ldoTemperature")) {
118  _ldoTemperature = floor(json_val->getDouble("ldoTemperature") * 1000.0 / 65536.0 + 0.5) / 1000.0;
119  }
120  if(json_val->has("voltageTransition")) {
121  _voltageTransition = json_val->getString("voltageTransition");
122  }
123  if(json_val->has("voltageAtStartUp")) {
124  _voltageAtStartUp = floor(json_val->getDouble("voltageAtStartUp") * 1000.0 / 65536.0 + 0.5) / 1000.0;
125  }
126  if(json_val->has("currentAtStartUp")) {
127  _currentAtStartUp = floor(json_val->getDouble("currentAtStartUp") * 1000.0 / 65536.0 + 0.5) / 1000.0;
128  }
129  if(json_val->has("command")) {
130  _command = json_val->getString("command");
131  }
132  return YFunction::_parseAttr(json_val);
133 }
134 
135 
146 {
147  string rest_val;
148  int res;
150  try {
151  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
152  res = _setAttr("voltageSetPoint", rest_val);
153  } catch (std::exception) {
155  throw;
156  }
158  return res;
159 }
160 
169 {
170  double res = 0.0;
172  try {
175  {
178  }
179  }
180  }
181  res = _voltageSetPoint;
182  } catch (std::exception) {
184  throw;
185  }
187  return res;
188 }
189 
200 {
201  string rest_val;
202  int res;
204  try {
205  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
206  res = _setAttr("currentLimit", rest_val);
207  } catch (std::exception) {
209  throw;
210  }
212  return res;
213 }
214 
223 {
224  double res = 0.0;
226  try {
229  {
232  }
233  }
234  }
235  res = _currentLimit;
236  } catch (std::exception) {
238  throw;
239  }
241  return res;
242 }
243 
252 {
253  Y_POWEROUTPUT_enum res;
255  try {
258  {
261  }
262  }
263  }
264  res = _powerOutput;
265  } catch (std::exception) {
267  throw;
268  }
270  return res;
271 }
272 
283 {
284  string rest_val;
285  int res;
287  try {
288  rest_val = (newval>0 ? "1" : "0");
289  res = _setAttr("powerOutput", rest_val);
290  } catch (std::exception) {
292  throw;
293  }
295  return res;
296 }
297 
306 {
309  try {
312  {
315  }
316  }
317  }
318  res = _voltageSense;
319  } catch (std::exception) {
321  throw;
322  }
324  return res;
325 }
326 
337 {
338  string rest_val;
339  int res;
341  try {
342  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
343  res = _setAttr("voltageSense", rest_val);
344  } catch (std::exception) {
346  throw;
347  }
349  return res;
350 }
351 
360 {
361  double res = 0.0;
363  try {
366  {
369  }
370  }
371  }
372  res = _measuredVoltage;
373  } catch (std::exception) {
375  throw;
376  }
378  return res;
379 }
380 
389 {
390  double res = 0.0;
392  try {
395  {
398  }
399  }
400  }
401  res = _measuredCurrent;
402  } catch (std::exception) {
404  throw;
405  }
407  return res;
408 }
409 
418 {
419  double res = 0.0;
421  try {
424  {
427  }
428  }
429  }
430  res = _inputVoltage;
431  } catch (std::exception) {
433  throw;
434  }
436  return res;
437 }
438 
447 {
448  double res = 0.0;
450  try {
453  {
456  }
457  }
458  }
459  res = _vInt;
460  } catch (std::exception) {
462  throw;
463  }
465  return res;
466 }
467 
476 {
477  double res = 0.0;
479  try {
482  {
485  }
486  }
487  }
488  res = _ldoTemperature;
489  } catch (std::exception) {
491  throw;
492  }
494  return res;
495 }
496 
498 {
499  string res;
501  try {
504  {
507  }
508  }
509  }
510  res = _voltageTransition;
511  } catch (std::exception) {
513  throw;
514  }
516  return res;
517 }
518 
519 int YPowerSupply::set_voltageTransition(const string& newval)
520 {
521  string rest_val;
522  int res;
524  try {
525  rest_val = newval;
526  res = _setAttr("voltageTransition", rest_val);
527  } catch (std::exception) {
529  throw;
530  }
532  return res;
533 }
534 
546 {
547  string rest_val;
548  int res;
550  try {
551  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
552  res = _setAttr("voltageAtStartUp", rest_val);
553  } catch (std::exception) {
555  throw;
556  }
558  return res;
559 }
560 
569 {
570  double res = 0.0;
572  try {
575  {
578  }
579  }
580  }
581  res = _voltageAtStartUp;
582  } catch (std::exception) {
584  throw;
585  }
587  return res;
588 }
589 
601 {
602  string rest_val;
603  int res;
605  try {
606  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
607  res = _setAttr("currentAtStartUp", rest_val);
608  } catch (std::exception) {
610  throw;
611  }
613  return res;
614 }
615 
624 {
625  double res = 0.0;
627  try {
630  {
633  }
634  }
635  }
636  res = _currentAtStartUp;
637  } catch (std::exception) {
639  throw;
640  }
642  return res;
643 }
644 
646 {
647  string res;
649  try {
652  {
655  }
656  }
657  }
658  res = _command;
659  } catch (std::exception) {
661  throw;
662  }
664  return res;
665 }
666 
667 int YPowerSupply::set_command(const string& newval)
668 {
669  string rest_val;
670  int res;
672  try {
673  rest_val = newval;
674  res = _setAttr("command", rest_val);
675  } catch (std::exception) {
677  throw;
678  }
680  return res;
681 }
682 
711 {
712  YPowerSupply* obj = NULL;
713  int taken = 0;
714  if (YAPI::_apiInitialized) {
716  taken = 1;
717  }try {
718  obj = (YPowerSupply*) YFunction::_FindFromCache("PowerSupply", func);
719  if (obj == NULL) {
720  obj = new YPowerSupply(func);
721  YFunction::_AddToCache("PowerSupply", func, obj);
722  }
723  } catch (std::exception) {
725  throw;
726  }
728  return obj;
729 }
730 
743 {
744  string val;
745  if (callback != NULL) {
747  } else {
749  }
750  _valueCallbackPowerSupply = callback;
751  // Immediately invoke value callback with current value
752  if (callback != NULL && this->isOnline()) {
753  val = _advertisedValue;
754  if (!(val == "")) {
755  this->_invokeValueCallback(val);
756  }
757  }
758  return 0;
759 }
760 
762 {
763  if (_valueCallbackPowerSupply != NULL) {
764  _valueCallbackPowerSupply(this, value);
765  } else {
767  }
768  return 0;
769 }
770 
781 int YPowerSupply::voltageMove(double V_target,int ms_duration)
782 {
783  string newval;
784  if (V_target < 0.0) {
785  V_target = 0.0;
786  }
787  newval = YapiWrapper::ysprintf("%d:%d", (int) floor(V_target*65536+0.5),ms_duration);
788 
789  return this->set_voltageTransition(newval);
790 }
791 
793 {
794  string hwid;
795 
796  if(YISERR(_nextFunction(hwid)) || hwid=="") {
797  return NULL;
798  }
799  return YPowerSupply::FindPowerSupply(hwid);
800 }
801 
803 {
804  vector<YFUN_DESCR> v_fundescr;
805  YDEV_DESCR ydevice;
806  string serial, funcId, funcName, funcVal, errmsg;
807 
808  if(YISERR(YapiWrapper::getFunctionsByClass("PowerSupply", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
809  v_fundescr.size() == 0 ||
810  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
811  return NULL;
812  }
813  return YPowerSupply::FindPowerSupply(serial+"."+funcId);
814 }
815 
816 //--- (end of YPowerSupply implementation)
817 
818 //--- (YPowerSupply functions)
819 //--- (end of YPowerSupply functions)
YPowerSupplyValueCallback _valueCallbackPowerSupply
string _advertisedValue
Definition: yocto_api.h:1569
static const double CURRENTLIMIT_INVALID
double _currentAtStartUp
double getDouble(const string &key)
Definition: yocto_api.cpp:796
static const double CURRENTATSTARTUP_INVALID
void(* YPowerSupplyValueCallback)(YPowerSupply *func, const string &functionValue)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
int set_voltageAtStartUp(double newval)
static const Y_POWEROUTPUT_enum POWEROUTPUT_INVALID
Y_VOLTAGESENSE_enum
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const double INPUTVOLTAGE_INVALID
static const string VOLTAGETRANSITION_INVALID
static const double LDOTEMPERATURE_INVALID
virtual int registerValueCallback(YPowerSupplyValueCallback callback)
static const double VINT_INVALID
static YPowerSupply * FirstPowerSupply(void)
int set_currentAtStartUp(double newval)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
double get_vInt(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
double _voltageSetPoint
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const double MEASUREDVOLTAGE_INVALID
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
double get_measuredVoltage(void)
int set_voltageTransition(const string &newval)
double _voltageAtStartUp
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
double get_currentAtStartUp(void)
double get_currentLimit(void)
int set_voltageSetPoint(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 const double VOLTAGESETPOINT_INVALID
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
double get_ldoTemperature(void)
virtual int _parseAttr(YJSONObject *json_val)
int set_command(const string &newval)
int set_powerOutput(Y_POWEROUTPUT_enum newval)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
string get_voltageTransition(void)
string _voltageTransition
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
int set_currentLimit(double 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
Y_POWEROUTPUT_enum _powerOutput
static const string COMMAND_INVALID
static YPowerSupply * FindPowerSupply(string func)
static const double MEASUREDCURRENT_INVALID
double get_voltageSetPoint(void)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
int set_voltageSense(Y_VOLTAGESENSE_enum newval)
string get_command(void)
Y_VOLTAGESENSE_enum get_voltageSense(void)
string _className
Definition: yocto_api.h:1558
double get_inputVoltage(void)
Y_VOLTAGESENSE_enum _voltageSense
double get_voltageAtStartUp(void)
YPowerSupply(const string &func)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int _invokeValueCallback(string value)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int voltageMove(double V_target, int ms_duration)
Y_POWEROUTPUT_enum
bool has(const string &key)
Definition: yocto_api.cpp:735
Y_POWEROUTPUT_enum get_powerOutput(void)
YPowerSupply * nextPowerSupply(void)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static const Y_VOLTAGESENSE_enum VOLTAGESENSE_INVALID
double get_measuredCurrent(void)
static const double VOLTAGEATSTARTUP_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