yocto_pwmoutput.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_pwmoutput.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindPwmOutput(), the high-level API for PwmOutput 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_pwmoutput.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__ "pwmoutput"
50 
51 YPwmOutput::YPwmOutput(const string& func): YFunction(func)
52 //--- (YPwmOutput initialization)
53  ,_enabled(ENABLED_INVALID)
54  ,_frequency(FREQUENCY_INVALID)
55  ,_period(PERIOD_INVALID)
56  ,_dutyCycle(DUTYCYCLE_INVALID)
57  ,_pulseDuration(PULSEDURATION_INVALID)
58  ,_pwmTransition(PWMTRANSITION_INVALID)
59  ,_enabledAtPowerOn(ENABLEDATPOWERON_INVALID)
60  ,_dutyCycleAtPowerOn(DUTYCYCLEATPOWERON_INVALID)
61  ,_valueCallbackPwmOutput(NULL)
62 //--- (end of YPwmOutput initialization)
63 {
64  _className="PwmOutput";
65 }
66 
68 {
69 //--- (YPwmOutput cleanup)
70 //--- (end of YPwmOutput cleanup)
71 }
72 //--- (YPwmOutput implementation)
73 // static attributes
80 
82 {
83  if(json_val->has("enabled")) {
84  _enabled = (Y_ENABLED_enum)json_val->getInt("enabled");
85  }
86  if(json_val->has("frequency")) {
87  _frequency = floor(json_val->getDouble("frequency") * 1000.0 / 65536.0 + 0.5) / 1000.0;
88  }
89  if(json_val->has("period")) {
90  _period = floor(json_val->getDouble("period") * 1000.0 / 65536.0 + 0.5) / 1000.0;
91  }
92  if(json_val->has("dutyCycle")) {
93  _dutyCycle = floor(json_val->getDouble("dutyCycle") * 1000.0 / 65536.0 + 0.5) / 1000.0;
94  }
95  if(json_val->has("pulseDuration")) {
96  _pulseDuration = floor(json_val->getDouble("pulseDuration") * 1000.0 / 65536.0 + 0.5) / 1000.0;
97  }
98  if(json_val->has("pwmTransition")) {
99  _pwmTransition = json_val->getString("pwmTransition");
100  }
101  if(json_val->has("enabledAtPowerOn")) {
102  _enabledAtPowerOn = (Y_ENABLEDATPOWERON_enum)json_val->getInt("enabledAtPowerOn");
103  }
104  if(json_val->has("dutyCycleAtPowerOn")) {
105  _dutyCycleAtPowerOn = floor(json_val->getDouble("dutyCycleAtPowerOn") * 1000.0 / 65536.0 + 0.5) / 1000.0;
106  }
107  return YFunction::_parseAttr(json_val);
108 }
109 
110 
119 {
120  Y_ENABLED_enum res;
122  try {
125  {
128  }
129  }
130  }
131  res = _enabled;
132  } catch (std::exception) {
134  throw;
135  }
137  return res;
138 }
139 
150 {
151  string rest_val;
152  int res;
154  try {
155  rest_val = (newval>0 ? "1" : "0");
156  res = _setAttr("enabled", rest_val);
157  } catch (std::exception) {
159  throw;
160  }
162  return res;
163 }
164 
175 int YPwmOutput::set_frequency(double newval)
176 {
177  string rest_val;
178  int res;
180  try {
181  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
182  res = _setAttr("frequency", rest_val);
183  } catch (std::exception) {
185  throw;
186  }
188  return res;
189 }
190 
199 {
200  double res = 0.0;
202  try {
205  {
208  }
209  }
210  }
211  res = _frequency;
212  } catch (std::exception) {
214  throw;
215  }
217  return res;
218 }
219 
229 int YPwmOutput::set_period(double newval)
230 {
231  string rest_val;
232  int res;
234  try {
235  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
236  res = _setAttr("period", rest_val);
237  } catch (std::exception) {
239  throw;
240  }
242  return res;
243 }
244 
253 {
254  double res = 0.0;
256  try {
259  {
262  }
263  }
264  }
265  res = _period;
266  } catch (std::exception) {
268  throw;
269  }
271  return res;
272 }
273 
283 int YPwmOutput::set_dutyCycle(double newval)
284 {
285  string rest_val;
286  int res;
288  try {
289  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
290  res = _setAttr("dutyCycle", rest_val);
291  } catch (std::exception) {
293  throw;
294  }
296  return res;
297 }
298 
307 {
308  double res = 0.0;
310  try {
313  {
316  }
317  }
318  }
319  res = _dutyCycle;
320  } catch (std::exception) {
322  throw;
323  }
325  return res;
326 }
327 
339 {
340  string rest_val;
341  int res;
343  try {
344  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
345  res = _setAttr("pulseDuration", rest_val);
346  } catch (std::exception) {
348  throw;
349  }
351  return res;
352 }
353 
363 {
364  double res = 0.0;
366  try {
369  {
372  }
373  }
374  }
375  res = _pulseDuration;
376  } catch (std::exception) {
378  throw;
379  }
381  return res;
382 }
383 
385 {
386  string res;
388  try {
391  {
394  }
395  }
396  }
397  res = _pwmTransition;
398  } catch (std::exception) {
400  throw;
401  }
403  return res;
404 }
405 
406 int YPwmOutput::set_pwmTransition(const string& newval)
407 {
408  string rest_val;
409  int res;
411  try {
412  rest_val = newval;
413  res = _setAttr("pwmTransition", rest_val);
414  } catch (std::exception) {
416  throw;
417  }
419  return res;
420 }
421 
431 {
434  try {
437  {
440  }
441  }
442  }
443  res = _enabledAtPowerOn;
444  } catch (std::exception) {
446  throw;
447  }
449  return res;
450 }
451 
464 {
465  string rest_val;
466  int res;
468  try {
469  rest_val = (newval>0 ? "1" : "0");
470  res = _setAttr("enabledAtPowerOn", rest_val);
471  } catch (std::exception) {
473  throw;
474  }
476  return res;
477 }
478 
490 {
491  string rest_val;
492  int res;
494  try {
495  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
496  res = _setAttr("dutyCycleAtPowerOn", rest_val);
497  } catch (std::exception) {
499  throw;
500  }
502  return res;
503 }
504 
514 {
515  double res = 0.0;
517  try {
520  {
523  }
524  }
525  }
526  res = _dutyCycleAtPowerOn;
527  } catch (std::exception) {
529  throw;
530  }
532  return res;
533 }
534 
563 {
564  YPwmOutput* obj = NULL;
565  int taken = 0;
566  if (YAPI::_apiInitialized) {
568  taken = 1;
569  }try {
570  obj = (YPwmOutput*) YFunction::_FindFromCache("PwmOutput", func);
571  if (obj == NULL) {
572  obj = new YPwmOutput(func);
573  YFunction::_AddToCache("PwmOutput", func, obj);
574  }
575  } catch (std::exception) {
577  throw;
578  }
580  return obj;
581 }
582 
595 {
596  string val;
597  if (callback != NULL) {
599  } else {
601  }
602  _valueCallbackPwmOutput = callback;
603  // Immediately invoke value callback with current value
604  if (callback != NULL && this->isOnline()) {
605  val = _advertisedValue;
606  if (!(val == "")) {
607  this->_invokeValueCallback(val);
608  }
609  }
610  return 0;
611 }
612 
614 {
615  if (_valueCallbackPwmOutput != NULL) {
616  _valueCallbackPwmOutput(this, value);
617  } else {
619  }
620  return 0;
621 }
622 
635 int YPwmOutput::pulseDurationMove(double ms_target,int ms_duration)
636 {
637  string newval;
638  if (ms_target < 0.0) {
639  ms_target = 0.0;
640  }
641  newval = YapiWrapper::ysprintf("%dms:%d", (int) floor(ms_target*65536+0.5),ms_duration);
642  return this->set_pwmTransition(newval);
643 }
644 
656 int YPwmOutput::dutyCycleMove(double target,int ms_duration)
657 {
658  string newval;
659  if (target < 0.0) {
660  target = 0.0;
661  }
662  if (target > 100.0) {
663  target = 100.0;
664  }
665  newval = YapiWrapper::ysprintf("%d:%d", (int) floor(target*65536+0.5),ms_duration);
666  return this->set_pwmTransition(newval);
667 }
668 
670 {
671  string hwid;
672 
673  if(YISERR(_nextFunction(hwid)) || hwid=="") {
674  return NULL;
675  }
676  return YPwmOutput::FindPwmOutput(hwid);
677 }
678 
680 {
681  vector<YFUN_DESCR> v_fundescr;
682  YDEV_DESCR ydevice;
683  string serial, funcId, funcName, funcVal, errmsg;
684 
685  if(YISERR(YapiWrapper::getFunctionsByClass("PwmOutput", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
686  v_fundescr.size() == 0 ||
687  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
688  return NULL;
689  }
690  return YPwmOutput::FindPwmOutput(serial+"."+funcId);
691 }
692 
693 //--- (end of YPwmOutput implementation)
694 
695 //--- (YPwmOutput functions)
696 //--- (end of YPwmOutput functions)
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
Y_ENABLEDATPOWERON_enum
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
int set_enabled(Y_ENABLED_enum newval)
Y_ENABLED_enum
Definition: yocto_current.h:59
virtual int registerValueCallback(YPwmOutputValueCallback callback)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
Y_ENABLED_enum get_enabled(void)
double get_period(void)
double get_pulseDuration(void)
static const double PULSEDURATION_INVALID
virtual int _invokeValueCallback(string value)
double get_frequency(void)
int set_dutyCycleAtPowerOn(double newval)
double _dutyCycle
int set_frequency(double newval)
static const string PWMTRANSITION_INVALID
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
virtual int dutyCycleMove(double target, int ms_duration)
static const double PERIOD_INVALID
u64 _cacheExpiration
Definition: yocto_api.h:1571
Y_ENABLED_enum _enabled
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
double get_dutyCycleAtPowerOn(void)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
int set_pulseDuration(double newval)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
static YPwmOutput * FirstPwmOutput(void)
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
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
void(* YPwmOutputValueCallback)(YPwmOutput *func, const string &functionValue)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
int set_pwmTransition(const string &newval)
string _pwmTransition
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
static const Y_ENABLED_enum ENABLED_INVALID
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
double _period
static const double DUTYCYCLEATPOWERON_INVALID
bool isOnline(void)
Definition: yocto_api.cpp:3291
YPwmOutputValueCallback _valueCallbackPwmOutput
int set_enabledAtPowerOn(Y_ENABLEDATPOWERON_enum newval)
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_period(double newval)
string _className
Definition: yocto_api.h:1558
double _dutyCycleAtPowerOn
virtual int _parseAttr(YJSONObject *json_val)
int set_dutyCycle(double newval)
string get_pwmTransition(void)
static const double DUTYCYCLE_INVALID
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
double _pulseDuration
double _frequency
static const double FREQUENCY_INVALID
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
double get_dutyCycle(void)
virtual int pulseDurationMove(double ms_target, int ms_duration)
YPwmOutput * nextPwmOutput(void)
static YPwmOutput * FindPwmOutput(string func)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static const Y_ENABLEDATPOWERON_enum ENABLEDATPOWERON_INVALID
Y_ENABLEDATPOWERON_enum get_enabledAtPowerOn(void)
Y_ENABLEDATPOWERON_enum _enabledAtPowerOn
YPwmOutput(const string &func)
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