yocto_digitalio.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_digitalio.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindDigitalIO(), the high-level API for DigitalIO 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_digitalio.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__ "digitalio"
50 
51 YDigitalIO::YDigitalIO(const string& func): YFunction(func)
52 //--- (YDigitalIO initialization)
53  ,_portState(PORTSTATE_INVALID)
54  ,_portDirection(PORTDIRECTION_INVALID)
55  ,_portOpenDrain(PORTOPENDRAIN_INVALID)
56  ,_portPolarity(PORTPOLARITY_INVALID)
57  ,_portDiags(PORTDIAGS_INVALID)
58  ,_portSize(PORTSIZE_INVALID)
59  ,_outputVoltage(OUTPUTVOLTAGE_INVALID)
60  ,_command(COMMAND_INVALID)
61  ,_valueCallbackDigitalIO(NULL)
62 //--- (end of YDigitalIO initialization)
63 {
64  _className="DigitalIO";
65 }
66 
68 {
69 //--- (YDigitalIO cleanup)
70 //--- (end of YDigitalIO cleanup)
71 }
72 //--- (YDigitalIO implementation)
73 // static attributes
75 
77 {
78  if(json_val->has("portState")) {
79  _portState = json_val->getInt("portState");
80  }
81  if(json_val->has("portDirection")) {
82  _portDirection = json_val->getInt("portDirection");
83  }
84  if(json_val->has("portOpenDrain")) {
85  _portOpenDrain = json_val->getInt("portOpenDrain");
86  }
87  if(json_val->has("portPolarity")) {
88  _portPolarity = json_val->getInt("portPolarity");
89  }
90  if(json_val->has("portDiags")) {
91  _portDiags = json_val->getInt("portDiags");
92  }
93  if(json_val->has("portSize")) {
94  _portSize = json_val->getInt("portSize");
95  }
96  if(json_val->has("outputVoltage")) {
97  _outputVoltage = (Y_OUTPUTVOLTAGE_enum)json_val->getInt("outputVoltage");
98  }
99  if(json_val->has("command")) {
100  _command = json_val->getString("command");
101  }
102  return YFunction::_parseAttr(json_val);
103 }
104 
105 
114 {
115  int res = 0;
117  try {
120  {
123  }
124  }
125  }
126  res = _portState;
127  } catch (std::exception) {
129  throw;
130  }
132  return res;
133 }
134 
146 {
147  string rest_val;
148  int res;
150  try {
151  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
152  res = _setAttr("portState", rest_val);
153  } catch (std::exception) {
155  throw;
156  }
158  return res;
159 }
160 
170 {
171  int res = 0;
173  try {
176  {
179  }
180  }
181  }
182  res = _portDirection;
183  } catch (std::exception) {
185  throw;
186  }
188  return res;
189 }
190 
203 {
204  string rest_val;
205  int res;
207  try {
208  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
209  res = _setAttr("portDirection", rest_val);
210  } catch (std::exception) {
212  throw;
213  }
215  return res;
216 }
217 
228 {
229  int res = 0;
231  try {
234  {
237  }
238  }
239  }
240  res = _portOpenDrain;
241  } catch (std::exception) {
243  throw;
244  }
246  return res;
247 }
248 
261 {
262  string rest_val;
263  int res;
265  try {
266  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
267  res = _setAttr("portOpenDrain", rest_val);
268  } catch (std::exception) {
270  throw;
271  }
273  return res;
274 }
275 
285 {
286  int res = 0;
288  try {
291  {
294  }
295  }
296  }
297  res = _portPolarity;
298  } catch (std::exception) {
300  throw;
301  }
303  return res;
304 }
305 
320 {
321  string rest_val;
322  int res;
324  try {
325  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
326  res = _setAttr("portPolarity", rest_val);
327  } catch (std::exception) {
329  throw;
330  }
332  return res;
333 }
334 
345 {
346  int res = 0;
348  try {
351  {
354  }
355  }
356  }
357  res = _portDiags;
358  } catch (std::exception) {
360  throw;
361  }
363  return res;
364 }
365 
374 {
375  int res = 0;
377  try {
380  {
383  }
384  }
385  }
386  res = _portSize;
387  } catch (std::exception) {
389  throw;
390  }
392  return res;
393 }
394 
404 {
407  try {
410  {
413  }
414  }
415  }
416  res = _outputVoltage;
417  } catch (std::exception) {
419  throw;
420  }
422  return res;
423 }
424 
437 {
438  string rest_val;
439  int res;
441  try {
442  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
443  res = _setAttr("outputVoltage", rest_val);
444  } catch (std::exception) {
446  throw;
447  }
449  return res;
450 }
451 
453 {
454  string res;
456  try {
459  {
462  }
463  }
464  }
465  res = _command;
466  } catch (std::exception) {
468  throw;
469  }
471  return res;
472 }
473 
474 int YDigitalIO::set_command(const string& newval)
475 {
476  string rest_val;
477  int res;
479  try {
480  rest_val = newval;
481  res = _setAttr("command", rest_val);
482  } catch (std::exception) {
484  throw;
485  }
487  return res;
488 }
489 
518 {
519  YDigitalIO* obj = NULL;
520  int taken = 0;
521  if (YAPI::_apiInitialized) {
523  taken = 1;
524  }try {
525  obj = (YDigitalIO*) YFunction::_FindFromCache("DigitalIO", func);
526  if (obj == NULL) {
527  obj = new YDigitalIO(func);
528  YFunction::_AddToCache("DigitalIO", func, obj);
529  }
530  } catch (std::exception) {
532  throw;
533  }
535  return obj;
536 }
537 
550 {
551  string val;
552  if (callback != NULL) {
554  } else {
556  }
557  _valueCallbackDigitalIO = callback;
558  // Immediately invoke value callback with current value
559  if (callback != NULL && this->isOnline()) {
560  val = _advertisedValue;
561  if (!(val == "")) {
562  this->_invokeValueCallback(val);
563  }
564  }
565  return 0;
566 }
567 
569 {
570  if (_valueCallbackDigitalIO != NULL) {
571  _valueCallbackDigitalIO(this, value);
572  } else {
574  }
575  return 0;
576 }
577 
588 int YDigitalIO::set_bitState(int bitno,int bitstate)
589 {
590  if (!(bitstate >= 0)) {
591  _throw(YAPI_INVALID_ARGUMENT,"invalid bitstate");
592  return YAPI_INVALID_ARGUMENT;
593  }
594  if (!(bitstate <= 1)) {
595  _throw(YAPI_INVALID_ARGUMENT,"invalid bitstate");
596  return YAPI_INVALID_ARGUMENT;
597  }
598  return this->set_command(YapiWrapper::ysprintf("%c%d",82+bitstate,bitno));
599 }
600 
611 {
612  int portVal = 0;
613  portVal = this->get_portState();
614  return ((((portVal) >> (bitno))) & (1));
615 }
616 
627 {
628  return this->set_command(YapiWrapper::ysprintf("T%d",bitno));
629 }
630 
642 int YDigitalIO::set_bitDirection(int bitno,int bitdirection)
643 {
644  if (!(bitdirection >= 0)) {
645  _throw(YAPI_INVALID_ARGUMENT,"invalid direction");
646  return YAPI_INVALID_ARGUMENT;
647  }
648  if (!(bitdirection <= 1)) {
649  _throw(YAPI_INVALID_ARGUMENT,"invalid direction");
650  return YAPI_INVALID_ARGUMENT;
651  }
652  return this->set_command(YapiWrapper::ysprintf("%c%d",73+6*bitdirection,bitno));
653 }
654 
665 {
666  int portDir = 0;
667  portDir = this->get_portDirection();
668  return ((((portDir) >> (bitno))) & (1));
669 }
670 
683 int YDigitalIO::set_bitPolarity(int bitno,int bitpolarity)
684 {
685  if (!(bitpolarity >= 0)) {
686  _throw(YAPI_INVALID_ARGUMENT,"invalid bitpolarity");
687  return YAPI_INVALID_ARGUMENT;
688  }
689  if (!(bitpolarity <= 1)) {
690  _throw(YAPI_INVALID_ARGUMENT,"invalid bitpolarity");
691  return YAPI_INVALID_ARGUMENT;
692  }
693  return this->set_command(YapiWrapper::ysprintf("%c%d",110+4*bitpolarity,bitno));
694 }
695 
707 {
708  int portPol = 0;
709  portPol = this->get_portPolarity();
710  return ((((portPol) >> (bitno))) & (1));
711 }
712 
725 int YDigitalIO::set_bitOpenDrain(int bitno,int opendrain)
726 {
727  if (!(opendrain >= 0)) {
728  _throw(YAPI_INVALID_ARGUMENT,"invalid state");
729  return YAPI_INVALID_ARGUMENT;
730  }
731  if (!(opendrain <= 1)) {
732  _throw(YAPI_INVALID_ARGUMENT,"invalid state");
733  return YAPI_INVALID_ARGUMENT;
734  }
735  return this->set_command(YapiWrapper::ysprintf("%c%d",100-32*opendrain,bitno));
736 }
737 
750 {
751  int portOpenDrain = 0;
752  portOpenDrain = this->get_portOpenDrain();
753  return ((((portOpenDrain) >> (bitno))) & (1));
754 }
755 
768 int YDigitalIO::pulse(int bitno,int ms_duration)
769 {
770  return this->set_command(YapiWrapper::ysprintf("Z%d,0,%d", bitno,ms_duration));
771 }
772 
786 int YDigitalIO::delayedPulse(int bitno,int ms_delay,int ms_duration)
787 {
788  return this->set_command(YapiWrapper::ysprintf("Z%d,%d,%d",bitno,ms_delay,ms_duration));
789 }
790 
792 {
793  string hwid;
794 
795  if(YISERR(_nextFunction(hwid)) || hwid=="") {
796  return NULL;
797  }
798  return YDigitalIO::FindDigitalIO(hwid);
799 }
800 
802 {
803  vector<YFUN_DESCR> v_fundescr;
804  YDEV_DESCR ydevice;
805  string serial, funcId, funcName, funcVal, errmsg;
806 
807  if(YISERR(YapiWrapper::getFunctionsByClass("DigitalIO", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
808  v_fundescr.size() == 0 ||
809  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
810  return NULL;
811  }
812  return YDigitalIO::FindDigitalIO(serial+"."+funcId);
813 }
814 
815 //--- (end of YDigitalIO implementation)
816 
817 //--- (YDigitalIO functions)
818 //--- (end of YDigitalIO functions)
string _advertisedValue
Definition: yocto_api.h:1569
int get_portPolarity(void)
Y_OUTPUTVOLTAGE_enum
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
int set_portState(int newval)
virtual int get_bitDirection(int bitno)
static const int PORTDIRECTION_INVALID
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _parseAttr(YJSONObject *json_val)
virtual int get_bitPolarity(int bitno)
static int DefaultCacheValidity
Definition: yocto_api.h:468
YDigitalIO(const string &func)
string getString(const string &key)
Definition: yocto_api.cpp:773
int get_portOpenDrain(void)
void(* YDigitalIOValueCallback)(YDigitalIO *func, const string &functionValue)
static const int PORTOPENDRAIN_INVALID
string get_command(void)
int get_portState(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void _throw(YRETCODE errType, string errMsg)
Definition: yocto_api.cpp:2656
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
int set_portPolarity(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
static const int PORTSTATE_INVALID
virtual int delayedPulse(int bitno, int ms_delay, int ms_duration)
virtual int get_bitState(int bitno)
Y_OUTPUTVOLTAGE_enum get_outputVoltage(void)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
string _command
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
Y_OUTPUTVOLTAGE_enum _outputVoltage
int set_portOpenDrain(int newval)
int portOpenDrain(void)
virtual int toggle_bitState(int bitno)
static const int PORTDIAGS_INVALID
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_portDiags(void)
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
virtual int set_bitOpenDrain(int bitno, int opendrain)
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
virtual int registerValueCallback(YDigitalIOValueCallback callback)
static const int PORTPOLARITY_INVALID
static const string COMMAND_INVALID
virtual int _invokeValueCallback(string value)
static const Y_OUTPUTVOLTAGE_enum OUTPUTVOLTAGE_INVALID
static const int PORTSIZE_INVALID
int set_outputVoltage(Y_OUTPUTVOLTAGE_enum newval)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
YDigitalIOValueCallback _valueCallbackDigitalIO
virtual int set_bitPolarity(int bitno, int bitpolarity)
virtual int set_bitDirection(int bitno, int bitdirection)
static YDigitalIO * FirstDigitalIO(void)
int set_command(const string &newval)
string _className
Definition: yocto_api.h:1558
YDigitalIO * nextDigitalIO(void)
int get_portSize(void)
virtual int set_bitState(int bitno, int bitstate)
static bool _apiInitialized
Definition: yocto_api.h:475
int get_portDirection(void)
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
virtual int get_bitOpenDrain(int bitno)
bool has(const string &key)
Definition: yocto_api.cpp:735
virtual int pulse(int bitno, int ms_duration)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
int set_portDirection(int newval)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
static YDigitalIO * FindDigitalIO(string func)


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