yocto_led.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_led.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindLed(), the high-level API for Led 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_led.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__ "led"
50 
51 YLed::YLed(const string& func): YFunction(func)
52 //--- (YLed initialization)
53  ,_power(POWER_INVALID)
54  ,_luminosity(LUMINOSITY_INVALID)
55  ,_blinking(BLINKING_INVALID)
56  ,_valueCallbackLed(NULL)
57 //--- (end of YLed initialization)
58 {
59  _className="Led";
60 }
61 
63 {
64 //--- (YLed cleanup)
65 //--- (end of YLed cleanup)
66 }
67 //--- (YLed implementation)
68 // static attributes
69 
71 {
72  if(json_val->has("power")) {
73  _power = (Y_POWER_enum)json_val->getInt("power");
74  }
75  if(json_val->has("luminosity")) {
76  _luminosity = json_val->getInt("luminosity");
77  }
78  if(json_val->has("blinking")) {
79  _blinking = (Y_BLINKING_enum)json_val->getInt("blinking");
80  }
81  return YFunction::_parseAttr(json_val);
82 }
83 
84 
93 {
94  Y_POWER_enum res;
96  try {
99  {
101  return YLed::POWER_INVALID;
102  }
103  }
104  }
105  res = _power;
106  } catch (std::exception) {
108  throw;
109  }
111  return res;
112 }
113 
124 {
125  string rest_val;
126  int res;
128  try {
129  rest_val = (newval>0 ? "1" : "0");
130  res = _setAttr("power", rest_val);
131  } catch (std::exception) {
133  throw;
134  }
136  return res;
137 }
138 
147 {
148  int res = 0;
150  try {
153  {
156  }
157  }
158  }
159  res = _luminosity;
160  } catch (std::exception) {
162  throw;
163  }
165  return res;
166 }
167 
177 int YLed::set_luminosity(int newval)
178 {
179  string rest_val;
180  int res;
182  try {
183  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
184  res = _setAttr("luminosity", rest_val);
185  } catch (std::exception) {
187  throw;
188  }
190  return res;
191 }
192 
202 {
203  Y_BLINKING_enum res;
205  try {
208  {
210  return YLed::BLINKING_INVALID;
211  }
212  }
213  }
214  res = _blinking;
215  } catch (std::exception) {
217  throw;
218  }
220  return res;
221 }
222 
234 {
235  string rest_val;
236  int res;
238  try {
239  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
240  res = _setAttr("blinking", rest_val);
241  } catch (std::exception) {
243  throw;
244  }
246  return res;
247 }
248 
276 YLed* YLed::FindLed(string func)
277 {
278  YLed* obj = NULL;
279  int taken = 0;
280  if (YAPI::_apiInitialized) {
282  taken = 1;
283  }try {
284  obj = (YLed*) YFunction::_FindFromCache("Led", func);
285  if (obj == NULL) {
286  obj = new YLed(func);
287  YFunction::_AddToCache("Led", func, obj);
288  }
289  } catch (std::exception) {
291  throw;
292  }
294  return obj;
295 }
296 
309 {
310  string val;
311  if (callback != NULL) {
313  } else {
315  }
316  _valueCallbackLed = callback;
317  // Immediately invoke value callback with current value
318  if (callback != NULL && this->isOnline()) {
319  val = _advertisedValue;
320  if (!(val == "")) {
321  this->_invokeValueCallback(val);
322  }
323  }
324  return 0;
325 }
326 
327 int YLed::_invokeValueCallback(string value)
328 {
329  if (_valueCallbackLed != NULL) {
330  _valueCallbackLed(this, value);
331  } else {
333  }
334  return 0;
335 }
336 
338 {
339  string hwid;
340 
341  if(YISERR(_nextFunction(hwid)) || hwid=="") {
342  return NULL;
343  }
344  return YLed::FindLed(hwid);
345 }
346 
348 {
349  vector<YFUN_DESCR> v_fundescr;
350  YDEV_DESCR ydevice;
351  string serial, funcId, funcName, funcVal, errmsg;
352 
353  if(YISERR(YapiWrapper::getFunctionsByClass("Led", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
354  v_fundescr.size() == 0 ||
355  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
356  return NULL;
357  }
358  return YLed::FindLed(serial+"."+funcId);
359 }
360 
361 //--- (end of YLed implementation)
362 
363 //--- (YLed functions)
364 //--- (end of YLed functions)
string _advertisedValue
Definition: yocto_api.h:1569
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_led.cpp:70
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
void(* YLedValueCallback)(YLed *func, const string &functionValue)
Definition: yocto_led.h:54
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
YLed * nextLed(void)
Definition: yocto_led.cpp:337
YLed(const string &func)
Definition: yocto_led.cpp:51
static const Y_BLINKING_enum BLINKING_INVALID
Definition: yocto_led.h:123
Y_POWER_enum
Definition: yocto_led.h:57
static int DefaultCacheValidity
Definition: yocto_api.h:468
int get_luminosity(void)
Definition: yocto_led.cpp:146
u64 _cacheExpiration
Definition: yocto_api.h:1571
static const Y_POWER_enum POWER_INVALID
Definition: yocto_led.h:115
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const int LUMINOSITY_INVALID
Definition: yocto_led.h:116
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 YLed * FindLed(string func)
Definition: yocto_led.cpp:276
~YLed()
Definition: yocto_led.cpp:62
Y_POWER_enum get_power(void)
Definition: yocto_led.cpp:92
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
static YLed * FirstLed(void)
Definition: yocto_led.cpp:347
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
Y_BLINKING_enum get_blinking(void)
Definition: yocto_led.cpp:201
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_luminosity(int newval)
Definition: yocto_led.cpp:177
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_blinking(Y_BLINKING_enum newval)
Definition: yocto_led.cpp:233
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 int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
Y_BLINKING_enum
Definition: yocto_led.h:65
YLedValueCallback _valueCallbackLed
Definition: yocto_led.h:97
string _className
Definition: yocto_api.h:1558
int _luminosity
Definition: yocto_led.h:95
Definition: yocto_led.h:86
int set_power(Y_POWER_enum newval)
Definition: yocto_led.cpp:123
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int _invokeValueCallback(string value)
Definition: yocto_led.cpp:327
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int registerValueCallback(YLedValueCallback callback)
Definition: yocto_led.cpp:308
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
Y_POWER_enum _power
Definition: yocto_led.h:94
Y_BLINKING_enum _blinking
Definition: yocto_led.h:96
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