yocto_servo.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_servo.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindServo(), the high-level API for Servo 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_servo.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__ "servo"
50 
51 YServo::YServo(const string& func): YFunction(func)
52 //--- (YServo initialization)
53  ,_position(POSITION_INVALID)
54  ,_enabled(ENABLED_INVALID)
55  ,_range(RANGE_INVALID)
56  ,_neutral(NEUTRAL_INVALID)
57  ,_move(MOVE_INVALID)
58  ,_positionAtPowerOn(POSITIONATPOWERON_INVALID)
59  ,_enabledAtPowerOn(ENABLEDATPOWERON_INVALID)
60  ,_valueCallbackServo(NULL)
61 //--- (end of YServo initialization)
62 {
63  _className="Servo";
64 }
65 
67 {
68 //--- (YServo cleanup)
69 //--- (end of YServo cleanup)
70 }
71 //--- (YServo implementation)
72 // static attributes
74 
76 {
77  if(json_val->has("position")) {
78  _position = json_val->getInt("position");
79  }
80  if(json_val->has("enabled")) {
81  _enabled = (Y_ENABLED_enum)json_val->getInt("enabled");
82  }
83  if(json_val->has("range")) {
84  _range = json_val->getInt("range");
85  }
86  if(json_val->has("neutral")) {
87  _neutral = json_val->getInt("neutral");
88  }
89  if(json_val->has("move")) {
90  YJSONObject* subjson = json_val->getYJSONObject("move");
91  if (subjson->has("moving")) {
92  _move.moving = subjson->getInt("moving");
93  }
94  if (subjson->has("target")) {
95  _move.target = subjson->getInt("target");
96  }
97  if (subjson->has("ms")) {
98  _move.ms = subjson->getInt("ms");
99  }
100  }
101  if(json_val->has("positionAtPowerOn")) {
102  _positionAtPowerOn = json_val->getInt("positionAtPowerOn");
103  }
104  if(json_val->has("enabledAtPowerOn")) {
105  _enabledAtPowerOn = (Y_ENABLEDATPOWERON_enum)json_val->getInt("enabledAtPowerOn");
106  }
107  return YFunction::_parseAttr(json_val);
108 }
109 
110 
119 {
120  int res = 0;
122  try {
125  {
128  }
129  }
130  }
131  res = _position;
132  } catch (std::exception) {
134  throw;
135  }
137  return res;
138 }
139 
149 int YServo::set_position(int newval)
150 {
151  string rest_val;
152  int res;
154  try {
155  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
156  res = _setAttr("position", rest_val);
157  } catch (std::exception) {
159  throw;
160  }
162  return res;
163 }
164 
173 {
174  Y_ENABLED_enum res;
176  try {
179  {
182  }
183  }
184  }
185  res = _enabled;
186  } catch (std::exception) {
188  throw;
189  }
191  return res;
192 }
193 
204 {
205  string rest_val;
206  int res;
208  try {
209  rest_val = (newval>0 ? "1" : "0");
210  res = _setAttr("enabled", rest_val);
211  } catch (std::exception) {
213  throw;
214  }
216  return res;
217 }
218 
227 {
228  int res = 0;
230  try {
233  {
235  return YServo::RANGE_INVALID;
236  }
237  }
238  }
239  res = _range;
240  } catch (std::exception) {
242  throw;
243  }
245  return res;
246 }
247 
263 int YServo::set_range(int newval)
264 {
265  string rest_val;
266  int res;
268  try {
269  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
270  res = _setAttr("range", rest_val);
271  } catch (std::exception) {
273  throw;
274  }
276  return res;
277 }
278 
287 {
288  int res = 0;
290  try {
293  {
296  }
297  }
298  }
299  res = _neutral;
300  } catch (std::exception) {
302  throw;
303  }
305  return res;
306 }
307 
323 int YServo::set_neutral(int newval)
324 {
325  string rest_val;
326  int res;
328  try {
329  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
330  res = _setAttr("neutral", rest_val);
331  } catch (std::exception) {
333  throw;
334  }
336  return res;
337 }
338 
340 {
341  YMove res;
343  try {
346  {
348  return YServo::MOVE_INVALID;
349  }
350  }
351  }
352  res = _move;
353  } catch (std::exception) {
355  throw;
356  }
358  return res;
359 }
360 
362 {
363  string rest_val;
364  int res;
366  try {
367  char buff[64]; sprintf(buff,"%d:%d",newval.target,newval.ms); rest_val = string(buff);
368  res = _setAttr("move", rest_val);
369  } catch (std::exception) {
371  throw;
372  }
374  return res;
375 }
376 
387 int YServo::move(int target,int ms_duration)
388 {
389  string rest_val;
390  char buff[64]; sprintf(buff,"%d:%d",target,ms_duration); rest_val = string(buff);
391  return _setAttr("move", rest_val);
392 }
393 
402 {
403  int res = 0;
405  try {
408  {
411  }
412  }
413  }
414  res = _positionAtPowerOn;
415  } catch (std::exception) {
417  throw;
418  }
420  return res;
421 }
422 
434 {
435  string rest_val;
436  int res;
438  try {
439  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
440  res = _setAttr("positionAtPowerOn", rest_val);
441  } catch (std::exception) {
443  throw;
444  }
446  return res;
447 }
448 
458 {
461  try {
464  {
467  }
468  }
469  }
470  res = _enabledAtPowerOn;
471  } catch (std::exception) {
473  throw;
474  }
476  return res;
477 }
478 
490 {
491  string rest_val;
492  int res;
494  try {
495  rest_val = (newval>0 ? "1" : "0");
496  res = _setAttr("enabledAtPowerOn", rest_val);
497  } catch (std::exception) {
499  throw;
500  }
502  return res;
503 }
504 
533 {
534  YServo* obj = NULL;
535  int taken = 0;
536  if (YAPI::_apiInitialized) {
538  taken = 1;
539  }try {
540  obj = (YServo*) YFunction::_FindFromCache("Servo", func);
541  if (obj == NULL) {
542  obj = new YServo(func);
543  YFunction::_AddToCache("Servo", func, obj);
544  }
545  } catch (std::exception) {
547  throw;
548  }
550  return obj;
551 }
552 
565 {
566  string val;
567  if (callback != NULL) {
569  } else {
571  }
572  _valueCallbackServo = callback;
573  // Immediately invoke value callback with current value
574  if (callback != NULL && this->isOnline()) {
575  val = _advertisedValue;
576  if (!(val == "")) {
577  this->_invokeValueCallback(val);
578  }
579  }
580  return 0;
581 }
582 
584 {
585  if (_valueCallbackServo != NULL) {
586  _valueCallbackServo(this, value);
587  } else {
589  }
590  return 0;
591 }
592 
594 {
595  string hwid;
596 
597  if(YISERR(_nextFunction(hwid)) || hwid=="") {
598  return NULL;
599  }
600  return YServo::FindServo(hwid);
601 }
602 
604 {
605  vector<YFUN_DESCR> v_fundescr;
606  YDEV_DESCR ydevice;
607  string serial, funcId, funcName, funcVal, errmsg;
608 
609  if(YISERR(YapiWrapper::getFunctionsByClass("Servo", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
610  v_fundescr.size() == 0 ||
611  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
612  return NULL;
613  }
614  return YServo::FindServo(serial+"."+funcId);
615 }
616 
617 //--- (end of YServo implementation)
618 
619 //--- (YServo functions)
620 //--- (end of YServo functions)
string _advertisedValue
Definition: yocto_api.h:1569
Y_ENABLEDATPOWERON_enum
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_servo.cpp:75
Y_ENABLED_enum
Definition: yocto_current.h:59
int set_position(int newval)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const YMove MOVE_INVALID
Definition: yocto_servo.h:140
YMove get_move(void)
int set_neutral(int newval)
int _neutral
Definition: yocto_servo.h:114
int set_enabled(Y_ENABLED_enum newval)
static int DefaultCacheValidity
Definition: yocto_api.h:468
int _positionAtPowerOn
Definition: yocto_servo.h:116
YJSONObject * getYJSONObject(const string &key)
Definition: yocto_api.cpp:744
virtual int _invokeValueCallback(string value)
YServo * nextServo(void)
YMove _move
Definition: yocto_servo.h:115
static YServo * FindServo(string func)
Y_ENABLED_enum _enabled
Definition: yocto_servo.h:112
int target
u64 _cacheExpiration
Definition: yocto_api.h:1571
static const Y_ENABLED_enum ENABLED_INVALID
Definition: yocto_servo.h:137
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
int set_range(int newval)
int set_positionAtPowerOn(int newval)
YServo(const string &func)
Definition: yocto_servo.cpp:51
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 registerValueCallback(YServoValueCallback callback)
int set_move(YMove newval)
Y_ENABLEDATPOWERON_enum _enabledAtPowerOn
Definition: yocto_servo.h:117
int moving
int get_range(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
static const int POSITION_INVALID
Definition: yocto_servo.h:134
int set_enabledAtPowerOn(Y_ENABLEDATPOWERON_enum newval)
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 YServo * FirstServo(void)
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
YMove move(void)
Definition: yocto_servo.h:260
Y_ENABLEDATPOWERON_enum get_enabledAtPowerOn(void)
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_position(void)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
static const int NEUTRAL_INVALID
Definition: yocto_servo.h:139
bool isOnline(void)
Definition: yocto_api.cpp:3291
void(* YServoValueCallback)(YServo *func, const string &functionValue)
Definition: yocto_servo.h:54
static const Y_ENABLEDATPOWERON_enum ENABLEDATPOWERON_INVALID
Definition: yocto_servo.h:144
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static const int POSITIONATPOWERON_INVALID
Definition: yocto_servo.h:141
string _className
Definition: yocto_api.h:1558
int get_neutral(void)
Y_ENABLED_enum get_enabled(void)
int _position
Definition: yocto_servo.h:111
static bool _apiInitialized
Definition: yocto_api.h:475
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
YServoValueCallback _valueCallbackServo
Definition: yocto_servo.h:118
static const int RANGE_INVALID
Definition: yocto_servo.h:138
int get_positionAtPowerOn(void)
bool has(const string &key)
Definition: yocto_api.cpp:735
int _range
Definition: yocto_servo.h:113
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
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