yocto_segmenteddisplay.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_segmenteddisplay.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindSegmentedDisplay(), the high-level API for SegmentedDisplay 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_segmenteddisplay.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__ "segmenteddisplay"
50 
52 //--- (YSegmentedDisplay initialization)
53  ,_displayedText(DISPLAYEDTEXT_INVALID)
54  ,_displayMode(DISPLAYMODE_INVALID)
55  ,_valueCallbackSegmentedDisplay(NULL)
56 //--- (end of YSegmentedDisplay initialization)
57 {
58  _className="SegmentedDisplay";
59 }
60 
62 {
63 //--- (YSegmentedDisplay cleanup)
64 //--- (end of YSegmentedDisplay cleanup)
65 }
66 //--- (YSegmentedDisplay implementation)
67 // static attributes
69 
71 {
72  if(json_val->has("displayedText")) {
73  _displayedText = json_val->getString("displayedText");
74  }
75  if(json_val->has("displayMode")) {
76  _displayMode = (Y_DISPLAYMODE_enum)json_val->getInt("displayMode");
77  }
78  return YFunction::_parseAttr(json_val);
79 }
80 
81 
90 {
91  string res;
93  try {
96  {
99  }
100  }
101  }
102  res = _displayedText;
103  } catch (std::exception) {
105  throw;
106  }
108  return res;
109 }
110 
120 int YSegmentedDisplay::set_displayedText(const string& newval)
121 {
122  string rest_val;
123  int res;
125  try {
126  rest_val = newval;
127  res = _setAttr("displayedText", rest_val);
128  } catch (std::exception) {
130  throw;
131  }
133  return res;
134 }
135 
137 {
138  Y_DISPLAYMODE_enum res;
140  try {
143  {
146  }
147  }
148  }
149  res = _displayMode;
150  } catch (std::exception) {
152  throw;
153  }
155  return res;
156 }
157 
159 {
160  string rest_val;
161  int res;
163  try {
164  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
165  res = _setAttr("displayMode", rest_val);
166  } catch (std::exception) {
168  throw;
169  }
171  return res;
172 }
173 
202 {
203  YSegmentedDisplay* obj = NULL;
204  int taken = 0;
205  if (YAPI::_apiInitialized) {
207  taken = 1;
208  }try {
209  obj = (YSegmentedDisplay*) YFunction::_FindFromCache("SegmentedDisplay", func);
210  if (obj == NULL) {
211  obj = new YSegmentedDisplay(func);
212  YFunction::_AddToCache("SegmentedDisplay", func, obj);
213  }
214  } catch (std::exception) {
216  throw;
217  }
219  return obj;
220 }
221 
234 {
235  string val;
236  if (callback != NULL) {
238  } else {
240  }
242  // Immediately invoke value callback with current value
243  if (callback != NULL && this->isOnline()) {
244  val = _advertisedValue;
245  if (!(val == "")) {
246  this->_invokeValueCallback(val);
247  }
248  }
249  return 0;
250 }
251 
253 {
254  if (_valueCallbackSegmentedDisplay != NULL) {
255  _valueCallbackSegmentedDisplay(this, value);
256  } else {
258  }
259  return 0;
260 }
261 
263 {
264  string hwid;
265 
266  if(YISERR(_nextFunction(hwid)) || hwid=="") {
267  return NULL;
268  }
270 }
271 
273 {
274  vector<YFUN_DESCR> v_fundescr;
275  YDEV_DESCR ydevice;
276  string serial, funcId, funcName, funcVal, errmsg;
277 
278  if(YISERR(YapiWrapper::getFunctionsByClass("SegmentedDisplay", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
279  v_fundescr.size() == 0 ||
280  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
281  return NULL;
282  }
283  return YSegmentedDisplay::FindSegmentedDisplay(serial+"."+funcId);
284 }
285 
286 //--- (end of YSegmentedDisplay implementation)
287 
288 //--- (YSegmentedDisplay functions)
289 //--- (end of YSegmentedDisplay functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static YSegmentedDisplay * FindSegmentedDisplay(string func)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _invokeValueCallback(string value)
Y_DISPLAYMODE_enum get_displayMode(void)
void(* YSegmentedDisplayValueCallback)(YSegmentedDisplay *func, const string &functionValue)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
virtual int _parseAttr(YJSONObject *json_val)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const string DISPLAYEDTEXT_INVALID
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
YSegmentedDisplay(const string &func)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static const Y_DISPLAYMODE_enum DISPLAYMODE_INVALID
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
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_displayedText(const string &newval)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
virtual int registerValueCallback(YSegmentedDisplayValueCallback callback)
bool isOnline(void)
Definition: yocto_api.cpp:3291
Y_DISPLAYMODE_enum _displayMode
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
YSegmentedDisplayValueCallback _valueCallbackSegmentedDisplay
static bool _apiInitialized
Definition: yocto_api.h:475
int set_displayMode(Y_DISPLAYMODE_enum newval)
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
Y_DISPLAYMODE_enum
YSegmentedDisplay * nextSegmentedDisplay(void)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
static YSegmentedDisplay * FirstSegmentedDisplay(void)


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