yocto_lightsensor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_lightsensor.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindLightSensor(), the high-level API for LightSensor 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_lightsensor.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__ "lightsensor"
50 
51 YLightSensor::YLightSensor(const string& func): YSensor(func)
52 //--- (YLightSensor initialization)
53  ,_measureType(MEASURETYPE_INVALID)
54  ,_valueCallbackLightSensor(NULL)
55  ,_timedReportCallbackLightSensor(NULL)
56 //--- (end of YLightSensor initialization)
57 {
58  _className="LightSensor";
59 }
60 
62 {
63 //--- (YLightSensor cleanup)
64 //--- (end of YLightSensor cleanup)
65 }
66 //--- (YLightSensor implementation)
67 // static attributes
68 
70 {
71  if(json_val->has("measureType")) {
72  _measureType = (Y_MEASURETYPE_enum)json_val->getInt("measureType");
73  }
74  return YSensor::_parseAttr(json_val);
75 }
76 
77 
79 {
80  string rest_val;
81  int res;
83  try {
84  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
85  res = _setAttr("currentValue", rest_val);
86  } catch (std::exception) {
88  throw;
89  }
91  return res;
92 }
93 
107 int YLightSensor::calibrate(double calibratedVal)
108 {
109  string rest_val;
110  char buf[32]; sprintf(buf,"%d", (int)floor(calibratedVal * 65536.0 + 0.5)); rest_val = string(buf);
111  return _setAttr("currentValue", rest_val);
112 }
113 
123 {
124  Y_MEASURETYPE_enum res;
126  try {
129  {
132  }
133  }
134  }
135  res = _measureType;
136  } catch (std::exception) {
138  throw;
139  }
141  return res;
142 }
143 
160 {
161  string rest_val;
162  int res;
164  try {
165  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
166  res = _setAttr("measureType", rest_val);
167  } catch (std::exception) {
169  throw;
170  }
172  return res;
173 }
174 
203 {
204  YLightSensor* obj = NULL;
205  int taken = 0;
206  if (YAPI::_apiInitialized) {
208  taken = 1;
209  }try {
210  obj = (YLightSensor*) YFunction::_FindFromCache("LightSensor", func);
211  if (obj == NULL) {
212  obj = new YLightSensor(func);
213  YFunction::_AddToCache("LightSensor", func, obj);
214  }
215  } catch (std::exception) {
217  throw;
218  }
220  return obj;
221 }
222 
235 {
236  string val;
237  if (callback != NULL) {
239  } else {
241  }
242  _valueCallbackLightSensor = callback;
243  // Immediately invoke value callback with current value
244  if (callback != NULL && this->isOnline()) {
245  val = _advertisedValue;
246  if (!(val == "")) {
247  this->_invokeValueCallback(val);
248  }
249  }
250  return 0;
251 }
252 
254 {
255  if (_valueCallbackLightSensor != NULL) {
256  _valueCallbackLightSensor(this, value);
257  } else {
259  }
260  return 0;
261 }
262 
275 {
276  YSensor* sensor = NULL;
277  sensor = this;
278  if (callback != NULL) {
280  } else {
282  }
284  return 0;
285 }
286 
288 {
289  if (_timedReportCallbackLightSensor != NULL) {
290  _timedReportCallbackLightSensor(this, value);
291  } else {
293  }
294  return 0;
295 }
296 
298 {
299  string hwid;
300 
301  if(YISERR(_nextFunction(hwid)) || hwid=="") {
302  return NULL;
303  }
304  return YLightSensor::FindLightSensor(hwid);
305 }
306 
308 {
309  vector<YFUN_DESCR> v_fundescr;
310  YDEV_DESCR ydevice;
311  string serial, funcId, funcName, funcVal, errmsg;
312 
313  if(YISERR(YapiWrapper::getFunctionsByClass("LightSensor", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
314  v_fundescr.size() == 0 ||
315  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
316  return NULL;
317  }
318  return YLightSensor::FindLightSensor(serial+"."+funcId);
319 }
320 
321 //--- (end of YLightSensor implementation)
322 
323 //--- (YLightSensor functions)
324 //--- (end of YLightSensor functions)
string _advertisedValue
Definition: yocto_api.h:1569
int set_measureType(Y_MEASURETYPE_enum newval)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_MEASURETYPE_enum
Y_MEASURETYPE_enum _measureType
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
virtual int _invokeTimedReportCallback(YMeasure value)
Y_MEASURETYPE_enum get_measureType(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const Y_MEASURETYPE_enum MEASURETYPE_INVALID
void(* YLightSensorValueCallback)(YLightSensor *func, const string &functionValue)
YLightSensorTimedReportCallback _timedReportCallbackLightSensor
static int DefaultCacheValidity
Definition: yocto_api.h:468
u64 _cacheExpiration
Definition: yocto_api.h:1571
virtual int _parseAttr(YJSONObject *json_val)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int registerValueCallback(YLightSensorValueCallback 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
virtual int _invokeValueCallback(string value)
static YLightSensor * FindLightSensor(string func)
YLightSensorValueCallback _valueCallbackLightSensor
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
int set_currentValue(double newval)
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
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 registerTimedReportCallback(YLightSensorTimedReportCallback callback)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
string _className
Definition: yocto_api.h:1558
int calibrate(double calibratedVal)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
YLightSensor * nextLightSensor(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
static YLightSensor * FirstLightSensor(void)
#define YISERR(retcode)
Definition: ydef.h:394
void(* YLightSensorTimedReportCallback)(YLightSensor *func, YMeasure measure)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
YLightSensor(const string &func)


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