yocto_colorled.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_colorled.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindColorLed(), the high-level API for ColorLed 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_colorled.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__ "colorled"
50 
51 YColorLed::YColorLed(const string& func): YFunction(func)
52 //--- (YColorLed initialization)
53  ,_rgbColor(RGBCOLOR_INVALID)
54  ,_hslColor(HSLCOLOR_INVALID)
55  ,_rgbMove(RGBMOVE_INVALID)
56  ,_hslMove(HSLMOVE_INVALID)
57  ,_rgbColorAtPowerOn(RGBCOLORATPOWERON_INVALID)
58  ,_blinkSeqSize(BLINKSEQSIZE_INVALID)
59  ,_blinkSeqMaxSize(BLINKSEQMAXSIZE_INVALID)
60  ,_blinkSeqSignature(BLINKSEQSIGNATURE_INVALID)
61  ,_command(COMMAND_INVALID)
62  ,_valueCallbackColorLed(NULL)
63 //--- (end of YColorLed initialization)
64 {
65  _className="ColorLed";
66 }
67 
69 {
70 //--- (YColorLed cleanup)
71 //--- (end of YColorLed cleanup)
72 }
73 //--- (YColorLed implementation)
74 // static attributes
78 
80 {
81  if(json_val->has("rgbColor")) {
82  _rgbColor = json_val->getInt("rgbColor");
83  }
84  if(json_val->has("hslColor")) {
85  _hslColor = json_val->getInt("hslColor");
86  }
87  if(json_val->has("rgbMove")) {
88  YJSONObject* subjson = json_val->getYJSONObject("rgbMove");
89  if (subjson->has("moving")) {
90  _rgbMove.moving = subjson->getInt("moving");
91  }
92  if (subjson->has("target")) {
93  _rgbMove.target = subjson->getInt("target");
94  }
95  if (subjson->has("ms")) {
96  _rgbMove.ms = subjson->getInt("ms");
97  }
98  }
99  if(json_val->has("hslMove")) {
100  YJSONObject* subjson = json_val->getYJSONObject("hslMove");
101  if (subjson->has("moving")) {
102  _hslMove.moving = subjson->getInt("moving");
103  }
104  if (subjson->has("target")) {
105  _hslMove.target = subjson->getInt("target");
106  }
107  if (subjson->has("ms")) {
108  _hslMove.ms = subjson->getInt("ms");
109  }
110  }
111  if(json_val->has("rgbColorAtPowerOn")) {
112  _rgbColorAtPowerOn = json_val->getInt("rgbColorAtPowerOn");
113  }
114  if(json_val->has("blinkSeqSize")) {
115  _blinkSeqSize = json_val->getInt("blinkSeqSize");
116  }
117  if(json_val->has("blinkSeqMaxSize")) {
118  _blinkSeqMaxSize = json_val->getInt("blinkSeqMaxSize");
119  }
120  if(json_val->has("blinkSeqSignature")) {
121  _blinkSeqSignature = json_val->getInt("blinkSeqSignature");
122  }
123  if(json_val->has("command")) {
124  _command = json_val->getString("command");
125  }
126  return YFunction::_parseAttr(json_val);
127 }
128 
129 
138 {
139  int res = 0;
141  try {
144  {
147  }
148  }
149  }
150  res = _rgbColor;
151  } catch (std::exception) {
153  throw;
154  }
156  return res;
157 }
158 
168 int YColorLed::set_rgbColor(int newval)
169 {
170  string rest_val;
171  int res;
173  try {
174  char buf[32]; sprintf(buf,"0x%06x",newval); rest_val = string(buf);
175  res = _setAttr("rgbColor", rest_val);
176  } catch (std::exception) {
178  throw;
179  }
181  return res;
182 }
183 
192 {
193  int res = 0;
195  try {
198  {
201  }
202  }
203  }
204  res = _hslColor;
205  } catch (std::exception) {
207  throw;
208  }
210  return res;
211 }
212 
222 int YColorLed::set_hslColor(int newval)
223 {
224  string rest_val;
225  int res;
227  try {
228  char buf[32]; sprintf(buf,"0x%06x",newval); rest_val = string(buf);
229  res = _setAttr("hslColor", rest_val);
230  } catch (std::exception) {
232  throw;
233  }
235  return res;
236 }
237 
239 {
240  YMove res;
242  try {
245  {
248  }
249  }
250  }
251  res = _rgbMove;
252  } catch (std::exception) {
254  throw;
255  }
257  return res;
258 }
259 
261 {
262  string rest_val;
263  int res;
265  try {
266  char buff[64]; sprintf(buff,"%d:%d",newval.target,newval.ms); rest_val = string(buff);
267  res = _setAttr("rgbMove", rest_val);
268  } catch (std::exception) {
270  throw;
271  }
273  return res;
274 }
275 
286 int YColorLed::rgbMove(int rgb_target,int ms_duration)
287 {
288  string rest_val;
289  char buff[64]; sprintf(buff,"%d:%d",rgb_target,ms_duration); rest_val = string(buff);
290  return _setAttr("rgbMove", rest_val);
291 }
292 
294 {
295  YMove res;
297  try {
300  {
303  }
304  }
305  }
306  res = _hslMove;
307  } catch (std::exception) {
309  throw;
310  }
312  return res;
313 }
314 
316 {
317  string rest_val;
318  int res;
320  try {
321  char buff[64]; sprintf(buff,"%d:%d",newval.target,newval.ms); rest_val = string(buff);
322  res = _setAttr("hslMove", rest_val);
323  } catch (std::exception) {
325  throw;
326  }
328  return res;
329 }
330 
341 int YColorLed::hslMove(int hsl_target,int ms_duration)
342 {
343  string rest_val;
344  char buff[64]; sprintf(buff,"%d:%d",hsl_target,ms_duration); rest_val = string(buff);
345  return _setAttr("hslMove", rest_val);
346 }
347 
356 {
357  int res = 0;
359  try {
362  {
365  }
366  }
367  }
368  res = _rgbColorAtPowerOn;
369  } catch (std::exception) {
371  throw;
372  }
374  return res;
375 }
376 
388 {
389  string rest_val;
390  int res;
392  try {
393  char buf[32]; sprintf(buf,"0x%06x",newval); rest_val = string(buf);
394  res = _setAttr("rgbColorAtPowerOn", rest_val);
395  } catch (std::exception) {
397  throw;
398  }
400  return res;
401 }
402 
411 {
412  int res = 0;
414  try {
417  {
420  }
421  }
422  }
423  res = _blinkSeqSize;
424  } catch (std::exception) {
426  throw;
427  }
429  return res;
430 }
431 
440 {
441  int res = 0;
443  try {
444  if (_cacheExpiration == 0) {
446  {
449  }
450  }
451  }
452  res = _blinkSeqMaxSize;
453  } catch (std::exception) {
455  throw;
456  }
458  return res;
459 }
460 
472 {
473  int res = 0;
475  try {
478  {
481  }
482  }
483  }
484  res = _blinkSeqSignature;
485  } catch (std::exception) {
487  throw;
488  }
490  return res;
491 }
492 
494 {
495  string res;
497  try {
500  {
503  }
504  }
505  }
506  res = _command;
507  } catch (std::exception) {
509  throw;
510  }
512  return res;
513 }
514 
515 int YColorLed::set_command(const string& newval)
516 {
517  string rest_val;
518  int res;
520  try {
521  rest_val = newval;
522  res = _setAttr("command", rest_val);
523  } catch (std::exception) {
525  throw;
526  }
528  return res;
529 }
530 
559 {
560  YColorLed* obj = NULL;
561  int taken = 0;
562  if (YAPI::_apiInitialized) {
564  taken = 1;
565  }try {
566  obj = (YColorLed*) YFunction::_FindFromCache("ColorLed", func);
567  if (obj == NULL) {
568  obj = new YColorLed(func);
569  YFunction::_AddToCache("ColorLed", func, obj);
570  }
571  } catch (std::exception) {
573  throw;
574  }
576  return obj;
577 }
578 
591 {
592  string val;
593  if (callback != NULL) {
595  } else {
597  }
598  _valueCallbackColorLed = callback;
599  // Immediately invoke value callback with current value
600  if (callback != NULL && this->isOnline()) {
601  val = _advertisedValue;
602  if (!(val == "")) {
603  this->_invokeValueCallback(val);
604  }
605  }
606  return 0;
607 }
608 
610 {
611  if (_valueCallbackColorLed != NULL) {
612  _valueCallbackColorLed(this, value);
613  } else {
615  }
616  return 0;
617 }
618 
620 {
621  return this->set_command(command);
622 }
623 
634 int YColorLed::addHslMoveToBlinkSeq(int HSLcolor,int msDelay)
635 {
636  return this->sendCommand(YapiWrapper::ysprintf("H%d,%d",HSLcolor,msDelay));
637 }
638 
649 int YColorLed::addRgbMoveToBlinkSeq(int RGBcolor,int msDelay)
650 {
651  return this->sendCommand(YapiWrapper::ysprintf("R%d,%d",RGBcolor,msDelay));
652 }
653 
663 {
664  return this->sendCommand("S");
665 }
666 
674 {
675  return this->sendCommand("X");
676 }
677 
685 {
686  return this->sendCommand("Z");
687 }
688 
690 {
691  string hwid;
692 
693  if(YISERR(_nextFunction(hwid)) || hwid=="") {
694  return NULL;
695  }
696  return YColorLed::FindColorLed(hwid);
697 }
698 
700 {
701  vector<YFUN_DESCR> v_fundescr;
702  YDEV_DESCR ydevice;
703  string serial, funcId, funcName, funcVal, errmsg;
704 
705  if(YISERR(YapiWrapper::getFunctionsByClass("ColorLed", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
706  v_fundescr.size() == 0 ||
707  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
708  return NULL;
709  }
710  return YColorLed::FindColorLed(serial+"."+funcId);
711 }
712 
713 //--- (end of YColorLed implementation)
714 
715 //--- (YColorLed functions)
716 //--- (end of YColorLed functions)
string _advertisedValue
Definition: yocto_api.h:1569
static const int HSLCOLOR_INVALID
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
YMove _hslMove
int set_hslMove(YMove newval)
int set_rgbColor(int newval)
static const int RGBCOLOR_INVALID
static const YMove HSLMOVE_INVALID
YColorLedValueCallback _valueCallbackColorLed
static const int RGBCOLORATPOWERON_INVALID
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int startBlinkSeq(void)
int set_rgbMove(YMove newval)
virtual int addHslMoveToBlinkSeq(int HSLcolor, int msDelay)
int get_blinkSeqSize(void)
virtual int stopBlinkSeq(void)
string command(void)
virtual int _parseAttr(YJSONObject *json_val)
int get_blinkSeqSignature(void)
int set_command(const string &newval)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
YJSONObject * getYJSONObject(const string &key)
Definition: yocto_api.cpp:744
int target
u64 _cacheExpiration
Definition: yocto_api.h:1571
YMove get_rgbMove(void)
virtual int registerValueCallback(YColorLedValueCallback callback)
virtual int sendCommand(string command)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
int get_blinkSeqMaxSize(void)
int moving
virtual int resetBlinkSeq(void)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
static const YMove RGBMOVE_INVALID
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
static const int BLINKSEQSIGNATURE_INVALID
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
int set_hslColor(int newval)
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
YMove hslMove(void)
int _blinkSeqSignature
int _blinkSeqMaxSize
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
int get_rgbColorAtPowerOn(void)
static YColorLed * FirstColorLed(void)
string get_command(void)
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
YMove rgbMove(void)
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
int get_hslColor(void)
YColorLed * nextColorLed(void)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static YColorLed * FindColorLed(string func)
static const int BLINKSEQSIZE_INVALID
YMove get_hslMove(void)
string _className
Definition: yocto_api.h:1558
string _command
YMove _rgbMove
int get_rgbColor(void)
static const int BLINKSEQMAXSIZE_INVALID
static bool _apiInitialized
Definition: yocto_api.h:475
virtual int addRgbMoveToBlinkSeq(int RGBcolor, int msDelay)
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
int set_rgbColorAtPowerOn(int newval)
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
static const string COMMAND_INVALID
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)
YColorLed(const string &func)
void(* YColorLedValueCallback)(YColorLed *func, const string &functionValue)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
int _rgbColorAtPowerOn


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