yocto_pwminput.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_pwminput.cpp 28807 2017-10-12 09:46:33Z seb $
4  *
5  * Implements yFindPwmInput(), the high-level API for PwmInput 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_pwminput.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__ "pwminput"
50 
51 YPwmInput::YPwmInput(const string& func): YSensor(func)
52 //--- (YPwmInput initialization)
53  ,_dutyCycle(DUTYCYCLE_INVALID)
54  ,_pulseDuration(PULSEDURATION_INVALID)
55  ,_frequency(FREQUENCY_INVALID)
56  ,_period(PERIOD_INVALID)
57  ,_pulseCounter(PULSECOUNTER_INVALID)
58  ,_pulseTimer(PULSETIMER_INVALID)
59  ,_pwmReportMode(PWMREPORTMODE_INVALID)
60  ,_debouncePeriod(DEBOUNCEPERIOD_INVALID)
61  ,_valueCallbackPwmInput(NULL)
62  ,_timedReportCallbackPwmInput(NULL)
63 //--- (end of YPwmInput initialization)
64 {
65  _className="PwmInput";
66 }
67 
69 {
70 //--- (YPwmInput cleanup)
71 //--- (end of YPwmInput cleanup)
72 }
73 //--- (YPwmInput implementation)
74 // static attributes
79 
81 {
82  if(json_val->has("dutyCycle")) {
83  _dutyCycle = floor(json_val->getDouble("dutyCycle") * 1000.0 / 65536.0 + 0.5) / 1000.0;
84  }
85  if(json_val->has("pulseDuration")) {
86  _pulseDuration = floor(json_val->getDouble("pulseDuration") * 1000.0 / 65536.0 + 0.5) / 1000.0;
87  }
88  if(json_val->has("frequency")) {
89  _frequency = floor(json_val->getDouble("frequency") * 1000.0 / 65536.0 + 0.5) / 1000.0;
90  }
91  if(json_val->has("period")) {
92  _period = floor(json_val->getDouble("period") * 1000.0 / 65536.0 + 0.5) / 1000.0;
93  }
94  if(json_val->has("pulseCounter")) {
95  _pulseCounter = json_val->getLong("pulseCounter");
96  }
97  if(json_val->has("pulseTimer")) {
98  _pulseTimer = json_val->getLong("pulseTimer");
99  }
100  if(json_val->has("pwmReportMode")) {
101  _pwmReportMode = (Y_PWMREPORTMODE_enum)json_val->getInt("pwmReportMode");
102  }
103  if(json_val->has("debouncePeriod")) {
104  _debouncePeriod = json_val->getInt("debouncePeriod");
105  }
106  return YSensor::_parseAttr(json_val);
107 }
108 
109 
118 {
119  double res = 0.0;
121  try {
124  {
127  }
128  }
129  }
130  res = _dutyCycle;
131  } catch (std::exception) {
133  throw;
134  }
136  return res;
137 }
138 
148 {
149  double res = 0.0;
151  try {
154  {
157  }
158  }
159  }
160  res = _pulseDuration;
161  } catch (std::exception) {
163  throw;
164  }
166  return res;
167 }
168 
177 {
178  double res = 0.0;
180  try {
183  {
186  }
187  }
188  }
189  res = _frequency;
190  } catch (std::exception) {
192  throw;
193  }
195  return res;
196 }
197 
206 {
207  double res = 0.0;
209  try {
212  {
215  }
216  }
217  }
218  res = _period;
219  } catch (std::exception) {
221  throw;
222  }
224  return res;
225 }
226 
237 {
238  s64 res = 0;
240  try {
243  {
246  }
247  }
248  }
249  res = _pulseCounter;
250  } catch (std::exception) {
252  throw;
253  }
255  return res;
256 }
257 
259 {
260  string rest_val;
261  int res;
263  try {
264  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
265  res = _setAttr("pulseCounter", rest_val);
266  } catch (std::exception) {
268  throw;
269  }
271  return res;
272 }
273 
282 {
283  s64 res = 0;
285  try {
288  {
291  }
292  }
293  }
294  res = _pulseTimer;
295  } catch (std::exception) {
297  throw;
298  }
300  return res;
301 }
302 
316 {
319  try {
322  {
325  }
326  }
327  }
328  res = _pwmReportMode;
329  } catch (std::exception) {
331  throw;
332  }
334  return res;
335 }
336 
354 {
355  string rest_val;
356  int res;
358  try {
359  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
360  res = _setAttr("pwmReportMode", rest_val);
361  } catch (std::exception) {
363  throw;
364  }
366  return res;
367 }
368 
377 {
378  int res = 0;
380  try {
383  {
386  }
387  }
388  }
389  res = _debouncePeriod;
390  } catch (std::exception) {
392  throw;
393  }
395  return res;
396 }
397 
408 {
409  string rest_val;
410  int res;
412  try {
413  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
414  res = _setAttr("debouncePeriod", rest_val);
415  } catch (std::exception) {
417  throw;
418  }
420  return res;
421 }
422 
451 {
452  YPwmInput* obj = NULL;
453  int taken = 0;
454  if (YAPI::_apiInitialized) {
456  taken = 1;
457  }try {
458  obj = (YPwmInput*) YFunction::_FindFromCache("PwmInput", func);
459  if (obj == NULL) {
460  obj = new YPwmInput(func);
461  YFunction::_AddToCache("PwmInput", func, obj);
462  }
463  } catch (std::exception) {
465  throw;
466  }
468  return obj;
469 }
470 
483 {
484  string val;
485  if (callback != NULL) {
487  } else {
489  }
490  _valueCallbackPwmInput = callback;
491  // Immediately invoke value callback with current value
492  if (callback != NULL && this->isOnline()) {
493  val = _advertisedValue;
494  if (!(val == "")) {
495  this->_invokeValueCallback(val);
496  }
497  }
498  return 0;
499 }
500 
502 {
503  if (_valueCallbackPwmInput != NULL) {
504  _valueCallbackPwmInput(this, value);
505  } else {
507  }
508  return 0;
509 }
510 
523 {
524  YSensor* sensor = NULL;
525  sensor = this;
526  if (callback != NULL) {
528  } else {
530  }
531  _timedReportCallbackPwmInput = callback;
532  return 0;
533 }
534 
536 {
537  if (_timedReportCallbackPwmInput != NULL) {
538  _timedReportCallbackPwmInput(this, value);
539  } else {
541  }
542  return 0;
543 }
544 
553 {
554  return this->set_pulseCounter(0);
555 }
556 
558 {
559  string hwid;
560 
561  if(YISERR(_nextFunction(hwid)) || hwid=="") {
562  return NULL;
563  }
564  return YPwmInput::FindPwmInput(hwid);
565 }
566 
568 {
569  vector<YFUN_DESCR> v_fundescr;
570  YDEV_DESCR ydevice;
571  string serial, funcId, funcName, funcVal, errmsg;
572 
573  if(YISERR(YapiWrapper::getFunctionsByClass("PwmInput", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
574  v_fundescr.size() == 0 ||
575  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
576  return NULL;
577  }
578  return YPwmInput::FindPwmInput(serial+"."+funcId);
579 }
580 
581 //--- (end of YPwmInput implementation)
582 
583 //--- (YPwmInput functions)
584 //--- (end of YPwmInput functions)
void(* YPwmInputValueCallback)(YPwmInput *func, const string &functionValue)
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
double get_dutyCycle(void)
Y_PWMREPORTMODE_enum
s64 get_pulseTimer(void)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
static const s64 PULSECOUNTER_INVALID
int get_debouncePeriod(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
double _frequency
int set_debouncePeriod(int newval)
double _period
YPwmInputTimedReportCallback _timedReportCallbackPwmInput
Y_PWMREPORTMODE_enum get_pwmReportMode(void)
void(* YPwmInputTimedReportCallback)(YPwmInput *func, YMeasure measure)
double get_period(void)
int _debouncePeriod
static int DefaultCacheValidity
Definition: yocto_api.h:468
static const double PERIOD_INVALID
static YPwmInput * FindPwmInput(string func)
static const int DEBOUNCEPERIOD_INVALID
u64 _cacheExpiration
Definition: yocto_api.h:1571
static const double FREQUENCY_INVALID
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
double get_pulseDuration(void)
static const s64 PULSETIMER_INVALID
s64 get_pulseCounter(void)
int set_pulseCounter(s64 newval)
static const double PULSEDURATION_INVALID
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
YPwmInputValueCallback _valueCallbackPwmInput
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_api.cpp:7936
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
virtual int registerTimedReportCallback(YPwmInputTimedReportCallback callback)
virtual int resetCounter(void)
double _dutyCycle
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
static YPwmInput * FirstPwmInput(void)
double _pulseDuration
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
double get_frequency(void)
static const Y_PWMREPORTMODE_enum PWMREPORTMODE_INVALID
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
YPwmInput(const string &func)
int set_pwmReportMode(Y_PWMREPORTMODE_enum newval)
string _className
Definition: yocto_api.h:1558
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
virtual int _parseAttr(YJSONObject *json_val)
Y_PWMREPORTMODE_enum _pwmReportMode
YPwmInput * nextPwmInput(void)
static const double DUTYCYCLE_INVALID
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
virtual int _invokeTimedReportCallback(YMeasure value)
bool has(const string &key)
Definition: yocto_api.cpp:735
virtual int registerValueCallback(YPwmInputValueCallback callback)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)


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