yocto_audioout.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_audioout.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindAudioOut(), the high-level API for AudioOut 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_audioout.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__ "audioout"
50 
51 YAudioOut::YAudioOut(const string& func): YFunction(func)
52 //--- (YAudioOut initialization)
53  ,_volume(VOLUME_INVALID)
54  ,_mute(MUTE_INVALID)
55  ,_volumeRange(VOLUMERANGE_INVALID)
56  ,_signal(SIGNAL_INVALID)
57  ,_noSignalFor(NOSIGNALFOR_INVALID)
58  ,_valueCallbackAudioOut(NULL)
59 //--- (end of YAudioOut initialization)
60 {
61  _className="AudioOut";
62 }
63 
65 {
66 //--- (YAudioOut cleanup)
67 //--- (end of YAudioOut cleanup)
68 }
69 //--- (YAudioOut implementation)
70 // static attributes
72 
74 {
75  if(json_val->has("volume")) {
76  _volume = json_val->getInt("volume");
77  }
78  if(json_val->has("mute")) {
79  _mute = (Y_MUTE_enum)json_val->getInt("mute");
80  }
81  if(json_val->has("volumeRange")) {
82  _volumeRange = json_val->getString("volumeRange");
83  }
84  if(json_val->has("signal")) {
85  _signal = json_val->getInt("signal");
86  }
87  if(json_val->has("noSignalFor")) {
88  _noSignalFor = json_val->getInt("noSignalFor");
89  }
90  return YFunction::_parseAttr(json_val);
91 }
92 
93 
102 {
103  int res = 0;
105  try {
108  {
111  }
112  }
113  }
114  res = _volume;
115  } catch (std::exception) {
117  throw;
118  }
120  return res;
121 }
122 
132 int YAudioOut::set_volume(int newval)
133 {
134  string rest_val;
135  int res;
137  try {
138  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
139  res = _setAttr("volume", rest_val);
140  } catch (std::exception) {
142  throw;
143  }
145  return res;
146 }
147 
156 {
157  Y_MUTE_enum res;
159  try {
162  {
165  }
166  }
167  }
168  res = _mute;
169  } catch (std::exception) {
171  throw;
172  }
174  return res;
175 }
176 
188 {
189  string rest_val;
190  int res;
192  try {
193  rest_val = (newval>0 ? "1" : "0");
194  res = _setAttr("mute", rest_val);
195  } catch (std::exception) {
197  throw;
198  }
200  return res;
201 }
202 
214 {
215  string res;
217  try {
220  {
223  }
224  }
225  }
226  res = _volumeRange;
227  } catch (std::exception) {
229  throw;
230  }
232  return res;
233 }
234 
243 {
244  int res = 0;
246  try {
249  {
252  }
253  }
254  }
255  res = _signal;
256  } catch (std::exception) {
258  throw;
259  }
261  return res;
262 }
263 
272 {
273  int res = 0;
275  try {
278  {
281  }
282  }
283  }
284  res = _noSignalFor;
285  } catch (std::exception) {
287  throw;
288  }
290  return res;
291 }
292 
321 {
322  YAudioOut* obj = NULL;
323  int taken = 0;
324  if (YAPI::_apiInitialized) {
326  taken = 1;
327  }try {
328  obj = (YAudioOut*) YFunction::_FindFromCache("AudioOut", func);
329  if (obj == NULL) {
330  obj = new YAudioOut(func);
331  YFunction::_AddToCache("AudioOut", func, obj);
332  }
333  } catch (std::exception) {
335  throw;
336  }
338  return obj;
339 }
340 
353 {
354  string val;
355  if (callback != NULL) {
357  } else {
359  }
360  _valueCallbackAudioOut = callback;
361  // Immediately invoke value callback with current value
362  if (callback != NULL && this->isOnline()) {
363  val = _advertisedValue;
364  if (!(val == "")) {
365  this->_invokeValueCallback(val);
366  }
367  }
368  return 0;
369 }
370 
372 {
373  if (_valueCallbackAudioOut != NULL) {
374  _valueCallbackAudioOut(this, value);
375  } else {
377  }
378  return 0;
379 }
380 
382 {
383  string hwid;
384 
385  if(YISERR(_nextFunction(hwid)) || hwid=="") {
386  return NULL;
387  }
388  return YAudioOut::FindAudioOut(hwid);
389 }
390 
392 {
393  vector<YFUN_DESCR> v_fundescr;
394  YDEV_DESCR ydevice;
395  string serial, funcId, funcName, funcVal, errmsg;
396 
397  if(YISERR(YapiWrapper::getFunctionsByClass("AudioOut", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
398  v_fundescr.size() == 0 ||
399  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
400  return NULL;
401  }
402  return YAudioOut::FindAudioOut(serial+"."+funcId);
403 }
404 
405 //--- (end of YAudioOut implementation)
406 
407 //--- (YAudioOut functions)
408 //--- (end of YAudioOut functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
YAudioOut(const string &func)
Y_MUTE_enum
Definition: yocto_audioin.h:57
int get_volume(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
int _noSignalFor
static const Y_MUTE_enum MUTE_INVALID
string get_volumeRange(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
int set_mute(Y_MUTE_enum newval)
u64 _cacheExpiration
Definition: yocto_api.h:1571
int get_signal(void)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
int set_volume(int newval)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
Y_MUTE_enum get_mute(void)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
YAudioOutValueCallback _valueCallbackAudioOut
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
Y_MUTE_enum _mute
static const int VOLUME_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
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
virtual int _invokeValueCallback(string value)
static const string VOLUMERANGE_INVALID
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
static const int NOSIGNALFOR_INVALID
static YAudioOut * FirstAudioOut(void)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
virtual int registerValueCallback(YAudioOutValueCallback callback)
string _className
Definition: yocto_api.h:1558
static YAudioOut * FindAudioOut(string func)
void(* YAudioOutValueCallback)(YAudioOut *func, const string &functionValue)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
string _volumeRange
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
int get_noSignalFor(void)
bool has(const string &key)
Definition: yocto_api.cpp:735
static const int SIGNAL_INVALID
YAudioOut * nextAudioOut(void)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _parseAttr(YJSONObject *json_val)
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