yocto_proximity.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_proximity.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindProximity(), the high-level API for Proximity 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_proximity.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__ "proximity"
50 
51 YProximity::YProximity(const string& func): YSensor(func)
52 //--- (YProximity initialization)
53  ,_signalValue(SIGNALVALUE_INVALID)
54  ,_detectionThreshold(DETECTIONTHRESHOLD_INVALID)
55  ,_isPresent(ISPRESENT_INVALID)
56  ,_lastTimeApproached(LASTTIMEAPPROACHED_INVALID)
57  ,_lastTimeRemoved(LASTTIMEREMOVED_INVALID)
58  ,_pulseCounter(PULSECOUNTER_INVALID)
59  ,_pulseTimer(PULSETIMER_INVALID)
60  ,_proximityReportMode(PROXIMITYREPORTMODE_INVALID)
61  ,_valueCallbackProximity(NULL)
62  ,_timedReportCallbackProximity(NULL)
63 //--- (end of YProximity initialization)
64 {
65  _className="Proximity";
66 }
67 
69 {
70 //--- (YProximity cleanup)
71 //--- (end of YProximity cleanup)
72 }
73 //--- (YProximity implementation)
74 // static attributes
76 
78 {
79  if(json_val->has("signalValue")) {
80  _signalValue = floor(json_val->getDouble("signalValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
81  }
82  if(json_val->has("detectionThreshold")) {
83  _detectionThreshold = json_val->getInt("detectionThreshold");
84  }
85  if(json_val->has("isPresent")) {
86  _isPresent = (Y_ISPRESENT_enum)json_val->getInt("isPresent");
87  }
88  if(json_val->has("lastTimeApproached")) {
89  _lastTimeApproached = json_val->getLong("lastTimeApproached");
90  }
91  if(json_val->has("lastTimeRemoved")) {
92  _lastTimeRemoved = json_val->getLong("lastTimeRemoved");
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("proximityReportMode")) {
101  _proximityReportMode = (Y_PROXIMITYREPORTMODE_enum)json_val->getInt("proximityReportMode");
102  }
103  return YSensor::_parseAttr(json_val);
104 }
105 
106 
115 {
116  double res = 0.0;
118  try {
121  {
124  }
125  }
126  }
127  res = floor(_signalValue * 1000+0.5) / 1000;
128  } catch (std::exception) {
130  throw;
131  }
133  return res;
134 }
135 
147 {
148  int res = 0;
150  try {
153  {
156  }
157  }
158  }
159  res = _detectionThreshold;
160  } catch (std::exception) {
162  throw;
163  }
165  return res;
166 }
167 
181 {
182  string rest_val;
183  int res;
185  try {
186  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
187  res = _setAttr("detectionThreshold", rest_val);
188  } catch (std::exception) {
190  throw;
191  }
193  return res;
194 }
195 
206 {
207  Y_ISPRESENT_enum res;
209  try {
212  {
215  }
216  }
217  }
218  res = _isPresent;
219  } catch (std::exception) {
221  throw;
222  }
224  return res;
225 }
226 
238 {
239  s64 res = 0;
241  try {
244  {
247  }
248  }
249  }
250  res = _lastTimeApproached;
251  } catch (std::exception) {
253  throw;
254  }
256  return res;
257 }
258 
270 {
271  s64 res = 0;
273  try {
276  {
279  }
280  }
281  }
282  res = _lastTimeRemoved;
283  } catch (std::exception) {
285  throw;
286  }
288  return res;
289 }
290 
301 {
302  s64 res = 0;
304  try {
307  {
310  }
311  }
312  }
313  res = _pulseCounter;
314  } catch (std::exception) {
316  throw;
317  }
319  return res;
320 }
321 
323 {
324  string rest_val;
325  int res;
327  try {
328  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
329  res = _setAttr("pulseCounter", rest_val);
330  } catch (std::exception) {
332  throw;
333  }
335  return res;
336 }
337 
346 {
347  s64 res = 0;
349  try {
352  {
355  }
356  }
357  }
358  res = _pulseTimer;
359  } catch (std::exception) {
361  throw;
362  }
364  return res;
365 }
366 
378 {
381  try {
384  {
387  }
388  }
389  }
390  res = _proximityReportMode;
391  } catch (std::exception) {
393  throw;
394  }
396  return res;
397 }
398 
414 {
415  string rest_val;
416  int res;
418  try {
419  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
420  res = _setAttr("proximityReportMode", rest_val);
421  } catch (std::exception) {
423  throw;
424  }
426  return res;
427 }
428 
457 {
458  YProximity* obj = NULL;
459  int taken = 0;
460  if (YAPI::_apiInitialized) {
462  taken = 1;
463  }try {
464  obj = (YProximity*) YFunction::_FindFromCache("Proximity", func);
465  if (obj == NULL) {
466  obj = new YProximity(func);
467  YFunction::_AddToCache("Proximity", func, obj);
468  }
469  } catch (std::exception) {
471  throw;
472  }
474  return obj;
475 }
476 
489 {
490  string val;
491  if (callback != NULL) {
493  } else {
495  }
496  _valueCallbackProximity = callback;
497  // Immediately invoke value callback with current value
498  if (callback != NULL && this->isOnline()) {
499  val = _advertisedValue;
500  if (!(val == "")) {
501  this->_invokeValueCallback(val);
502  }
503  }
504  return 0;
505 }
506 
508 {
509  if (_valueCallbackProximity != NULL) {
510  _valueCallbackProximity(this, value);
511  } else {
513  }
514  return 0;
515 }
516 
529 {
530  YSensor* sensor = NULL;
531  sensor = this;
532  if (callback != NULL) {
534  } else {
536  }
538  return 0;
539 }
540 
542 {
543  if (_timedReportCallbackProximity != NULL) {
544  _timedReportCallbackProximity(this, value);
545  } else {
547  }
548  return 0;
549 }
550 
559 {
560  return this->set_pulseCounter(0);
561 }
562 
564 {
565  string hwid;
566 
567  if(YISERR(_nextFunction(hwid)) || hwid=="") {
568  return NULL;
569  }
570  return YProximity::FindProximity(hwid);
571 }
572 
574 {
575  vector<YFUN_DESCR> v_fundescr;
576  YDEV_DESCR ydevice;
577  string serial, funcId, funcName, funcVal, errmsg;
578 
579  if(YISERR(YapiWrapper::getFunctionsByClass("Proximity", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
580  v_fundescr.size() == 0 ||
581  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
582  return NULL;
583  }
584  return YProximity::FindProximity(serial+"."+funcId);
585 }
586 
587 //--- (end of YProximity implementation)
588 
589 //--- (YProximity functions)
590 //--- (end of YProximity 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
Y_ISPRESENT_enum
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
YProximity * nextProximity(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
YProximityValueCallback _valueCallbackProximity
static const Y_ISPRESENT_enum ISPRESENT_INVALID
YProximityTimedReportCallback _timedReportCallbackProximity
int get_detectionThreshold(void)
double _signalValue
Y_PROXIMITYREPORTMODE_enum get_proximityReportMode(void)
s64 get_lastTimeApproached(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
static YProximity * FirstProximity(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
virtual int _parseAttr(YJSONObject *json_val)
static const s64 LASTTIMEAPPROACHED_INVALID
Y_ISPRESENT_enum _isPresent
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
double get_signalValue(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
virtual int registerTimedReportCallback(YProximityTimedReportCallback callback)
static const double SIGNALVALUE_INVALID
Y_PROXIMITYREPORTMODE_enum
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
static const s64 PULSECOUNTER_INVALID
#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
YProximity(const string &func)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
int _detectionThreshold
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 _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
s64 get_pulseTimer(void)
int set_detectionThreshold(int newval)
s64 get_pulseCounter(void)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
s64 _lastTimeApproached
virtual int resetCounter(void)
string _className
Definition: yocto_api.h:1558
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
int set_pulseCounter(s64 newval)
static const int DETECTIONTHRESHOLD_INVALID
static YProximity * FindProximity(string func)
virtual int _invokeTimedReportCallback(YMeasure value)
static const s64 LASTTIMEREMOVED_INVALID
static const s64 PULSETIMER_INVALID
virtual int registerValueCallback(YProximityValueCallback callback)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
Y_PROXIMITYREPORTMODE_enum _proximityReportMode
s64 get_lastTimeRemoved(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
static const Y_PROXIMITYREPORTMODE_enum PROXIMITYREPORTMODE_INVALID
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)
void(* YProximityTimedReportCallback)(YProximity *func, YMeasure measure)
int set_proximityReportMode(Y_PROXIMITYREPORTMODE_enum newval)
void(* YProximityValueCallback)(YProximity *func, const string &functionValue)
Y_ISPRESENT_enum get_isPresent(void)


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