yocto_anbutton.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_anbutton.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindAnButton(), the high-level API for AnButton 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_anbutton.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__ "anbutton"
50 
51 YAnButton::YAnButton(const string& func): YFunction(func)
52 //--- (YAnButton initialization)
53  ,_calibratedValue(CALIBRATEDVALUE_INVALID)
54  ,_rawValue(RAWVALUE_INVALID)
55  ,_analogCalibration(ANALOGCALIBRATION_INVALID)
56  ,_calibrationMax(CALIBRATIONMAX_INVALID)
57  ,_calibrationMin(CALIBRATIONMIN_INVALID)
58  ,_sensitivity(SENSITIVITY_INVALID)
59  ,_isPressed(ISPRESSED_INVALID)
60  ,_lastTimePressed(LASTTIMEPRESSED_INVALID)
61  ,_lastTimeReleased(LASTTIMERELEASED_INVALID)
62  ,_pulseCounter(PULSECOUNTER_INVALID)
63  ,_pulseTimer(PULSETIMER_INVALID)
64  ,_valueCallbackAnButton(NULL)
65 //--- (end of YAnButton initialization)
66 {
67  _className="AnButton";
68 }
69 
71 {
72 //--- (YAnButton cleanup)
73 //--- (end of YAnButton cleanup)
74 }
75 //--- (YAnButton implementation)
76 // static attributes
77 
79 {
80  if(json_val->has("calibratedValue")) {
81  _calibratedValue = json_val->getInt("calibratedValue");
82  }
83  if(json_val->has("rawValue")) {
84  _rawValue = json_val->getInt("rawValue");
85  }
86  if(json_val->has("analogCalibration")) {
87  _analogCalibration = (Y_ANALOGCALIBRATION_enum)json_val->getInt("analogCalibration");
88  }
89  if(json_val->has("calibrationMax")) {
90  _calibrationMax = json_val->getInt("calibrationMax");
91  }
92  if(json_val->has("calibrationMin")) {
93  _calibrationMin = json_val->getInt("calibrationMin");
94  }
95  if(json_val->has("sensitivity")) {
96  _sensitivity = json_val->getInt("sensitivity");
97  }
98  if(json_val->has("isPressed")) {
99  _isPressed = (Y_ISPRESSED_enum)json_val->getInt("isPressed");
100  }
101  if(json_val->has("lastTimePressed")) {
102  _lastTimePressed = json_val->getLong("lastTimePressed");
103  }
104  if(json_val->has("lastTimeReleased")) {
105  _lastTimeReleased = json_val->getLong("lastTimeReleased");
106  }
107  if(json_val->has("pulseCounter")) {
108  _pulseCounter = json_val->getLong("pulseCounter");
109  }
110  if(json_val->has("pulseTimer")) {
111  _pulseTimer = json_val->getLong("pulseTimer");
112  }
113  return YFunction::_parseAttr(json_val);
114 }
115 
116 
125 {
126  int res = 0;
128  try {
131  {
134  }
135  }
136  }
137  res = _calibratedValue;
138  } catch (std::exception) {
140  throw;
141  }
143  return res;
144 }
145 
154 {
155  int res = 0;
157  try {
160  {
163  }
164  }
165  }
166  res = _rawValue;
167  } catch (std::exception) {
169  throw;
170  }
172  return res;
173 }
174 
183 {
186  try {
189  {
192  }
193  }
194  }
195  res = _analogCalibration;
196  } catch (std::exception) {
198  throw;
199  }
201  return res;
202 }
203 
215 {
216  string rest_val;
217  int res;
219  try {
220  rest_val = (newval>0 ? "1" : "0");
221  res = _setAttr("analogCalibration", rest_val);
222  } catch (std::exception) {
224  throw;
225  }
227  return res;
228 }
229 
239 {
240  int res = 0;
242  try {
245  {
248  }
249  }
250  }
251  res = _calibrationMax;
252  } catch (std::exception) {
254  throw;
255  }
257  return res;
258 }
259 
274 {
275  string rest_val;
276  int res;
278  try {
279  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
280  res = _setAttr("calibrationMax", rest_val);
281  } catch (std::exception) {
283  throw;
284  }
286  return res;
287 }
288 
298 {
299  int res = 0;
301  try {
304  {
307  }
308  }
309  }
310  res = _calibrationMin;
311  } catch (std::exception) {
313  throw;
314  }
316  return res;
317 }
318 
333 {
334  string rest_val;
335  int res;
337  try {
338  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
339  res = _setAttr("calibrationMin", rest_val);
340  } catch (std::exception) {
342  throw;
343  }
345  return res;
346 }
347 
357 {
358  int res = 0;
360  try {
363  {
366  }
367  }
368  }
369  res = _sensitivity;
370  } catch (std::exception) {
372  throw;
373  }
375  return res;
376 }
377 
394 {
395  string rest_val;
396  int res;
398  try {
399  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
400  res = _setAttr("sensitivity", rest_val);
401  } catch (std::exception) {
403  throw;
404  }
406  return res;
407 }
408 
418 {
419  Y_ISPRESSED_enum res;
421  try {
424  {
427  }
428  }
429  }
430  res = _isPressed;
431  } catch (std::exception) {
433  throw;
434  }
436  return res;
437 }
438 
450 {
451  s64 res = 0;
453  try {
456  {
459  }
460  }
461  }
462  res = _lastTimePressed;
463  } catch (std::exception) {
465  throw;
466  }
468  return res;
469 }
470 
482 {
483  s64 res = 0;
485  try {
488  {
491  }
492  }
493  }
494  res = _lastTimeReleased;
495  } catch (std::exception) {
497  throw;
498  }
500  return res;
501 }
502 
513 {
514  s64 res = 0;
516  try {
519  {
522  }
523  }
524  }
525  res = _pulseCounter;
526  } catch (std::exception) {
528  throw;
529  }
531  return res;
532 }
533 
535 {
536  string rest_val;
537  int res;
539  try {
540  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
541  res = _setAttr("pulseCounter", rest_val);
542  } catch (std::exception) {
544  throw;
545  }
547  return res;
548 }
549 
558 {
559  s64 res = 0;
561  try {
564  {
567  }
568  }
569  }
570  res = _pulseTimer;
571  } catch (std::exception) {
573  throw;
574  }
576  return res;
577 }
578 
607 {
608  YAnButton* obj = NULL;
609  int taken = 0;
610  if (YAPI::_apiInitialized) {
612  taken = 1;
613  }try {
614  obj = (YAnButton*) YFunction::_FindFromCache("AnButton", func);
615  if (obj == NULL) {
616  obj = new YAnButton(func);
617  YFunction::_AddToCache("AnButton", func, obj);
618  }
619  } catch (std::exception) {
621  throw;
622  }
624  return obj;
625 }
626 
639 {
640  string val;
641  if (callback != NULL) {
643  } else {
645  }
646  _valueCallbackAnButton = callback;
647  // Immediately invoke value callback with current value
648  if (callback != NULL && this->isOnline()) {
649  val = _advertisedValue;
650  if (!(val == "")) {
651  this->_invokeValueCallback(val);
652  }
653  }
654  return 0;
655 }
656 
658 {
659  if (_valueCallbackAnButton != NULL) {
660  _valueCallbackAnButton(this, value);
661  } else {
663  }
664  return 0;
665 }
666 
675 {
676  return this->set_pulseCounter(0);
677 }
678 
680 {
681  string hwid;
682 
683  if(YISERR(_nextFunction(hwid)) || hwid=="") {
684  return NULL;
685  }
686  return YAnButton::FindAnButton(hwid);
687 }
688 
690 {
691  vector<YFUN_DESCR> v_fundescr;
692  YDEV_DESCR ydevice;
693  string serial, funcId, funcName, funcVal, errmsg;
694 
695  if(YISERR(YapiWrapper::getFunctionsByClass("AnButton", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
696  v_fundescr.size() == 0 ||
697  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
698  return NULL;
699  }
700  return YAnButton::FindAnButton(serial+"."+funcId);
701 }
702 
703 //--- (end of YAnButton implementation)
704 
705 //--- (YAnButton functions)
706 //--- (end of YAnButton functions)
string _advertisedValue
Definition: yocto_api.h:1569
s64 _lastTimeReleased
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_ANALOGCALIBRATION_enum
int get_calibrationMin(void)
s64 get_lastTimeReleased(void)
Y_ANALOGCALIBRATION_enum _analogCalibration
int set_calibrationMax(int newval)
Y_ISPRESSED_enum _isPressed
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
int set_pulseCounter(s64 newval)
static const Y_ISPRESSED_enum ISPRESSED_INVALID
static const int RAWVALUE_INVALID
s64 get_pulseCounter(void)
int _calibrationMax
static int DefaultCacheValidity
Definition: yocto_api.h:468
static const s64 LASTTIMEPRESSED_INVALID
int _calibratedValue
static const int SENSITIVITY_INVALID
Y_ISPRESSED_enum get_isPressed(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
Y_ANALOGCALIBRATION_enum get_analogCalibration(void)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
int get_calibrationMax(void)
virtual int registerValueCallback(YAnButtonValueCallback callback)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
static const int CALIBRATEDVALUE_INVALID
int get_sensitivity(void)
int set_calibrationMin(int newval)
s64 get_pulseTimer(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
Y_ISPRESSED_enum
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
static const s64 LASTTIMERELEASED_INVALID
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
virtual int resetCounter(void)
virtual int _parseAttr(YJSONObject *json_val)
int set_sensitivity(int newval)
static YAnButton * FirstAnButton(void)
int set_analogCalibration(Y_ANALOGCALIBRATION_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
static const int CALIBRATIONMAX_INVALID
static const s64 PULSETIMER_INVALID
static const Y_ANALOGCALIBRATION_enum ANALOGCALIBRATION_INVALID
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
static const int CALIBRATIONMIN_INVALID
bool isOnline(void)
Definition: yocto_api.cpp:3291
YAnButtonValueCallback _valueCallbackAnButton
YAnButton(const string &func)
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
void(* YAnButtonValueCallback)(YAnButton *func, const string &functionValue)
int get_rawValue(void)
string _className
Definition: yocto_api.h:1558
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
int get_calibratedValue(void)
YAnButton * nextAnButton(void)
int _calibrationMin
static YAnButton * FindAnButton(string func)
s64 get_lastTimePressed(void)
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
s64 _lastTimePressed
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
static const s64 PULSECOUNTER_INVALID


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