yocto_multicellweighscale.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_multicellweighscale.cpp 29661 2018-01-18 13:32:13Z mvuilleu $
4  *
5  * Implements yFindMultiCellWeighScale(), the high-level API for MultiCellWeighScale 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
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__ "multicellweighscale"
50 
52 //--- (YMultiCellWeighScale initialization)
53  ,_cellCount(CELLCOUNT_INVALID)
54  ,_excitation(EXCITATION_INVALID)
55  ,_compTempAdaptRatio(COMPTEMPADAPTRATIO_INVALID)
56  ,_compTempAvg(COMPTEMPAVG_INVALID)
57  ,_compTempChg(COMPTEMPCHG_INVALID)
58  ,_compensation(COMPENSATION_INVALID)
59  ,_zeroTracking(ZEROTRACKING_INVALID)
60  ,_command(COMMAND_INVALID)
61  ,_valueCallbackMultiCellWeighScale(NULL)
62  ,_timedReportCallbackMultiCellWeighScale(NULL)
63 //--- (end of YMultiCellWeighScale initialization)
64 {
65  _className="MultiCellWeighScale";
66 }
67 
69 {
70 //--- (YMultiCellWeighScale cleanup)
71 //--- (end of YMultiCellWeighScale cleanup)
72 }
73 //--- (YMultiCellWeighScale implementation)
74 // static attributes
81 
83 {
84  if(json_val->has("cellCount")) {
85  _cellCount = json_val->getInt("cellCount");
86  }
87  if(json_val->has("excitation")) {
88  _excitation = (Y_EXCITATION_enum)json_val->getInt("excitation");
89  }
90  if(json_val->has("compTempAdaptRatio")) {
91  _compTempAdaptRatio = floor(json_val->getDouble("compTempAdaptRatio") * 1000.0 / 65536.0 + 0.5) / 1000.0;
92  }
93  if(json_val->has("compTempAvg")) {
94  _compTempAvg = floor(json_val->getDouble("compTempAvg") * 1000.0 / 65536.0 + 0.5) / 1000.0;
95  }
96  if(json_val->has("compTempChg")) {
97  _compTempChg = floor(json_val->getDouble("compTempChg") * 1000.0 / 65536.0 + 0.5) / 1000.0;
98  }
99  if(json_val->has("compensation")) {
100  _compensation = floor(json_val->getDouble("compensation") * 1000.0 / 65536.0 + 0.5) / 1000.0;
101  }
102  if(json_val->has("zeroTracking")) {
103  _zeroTracking = floor(json_val->getDouble("zeroTracking") * 1000.0 / 65536.0 + 0.5) / 1000.0;
104  }
105  if(json_val->has("command")) {
106  _command = json_val->getString("command");
107  }
108  return YSensor::_parseAttr(json_val);
109 }
110 
111 
123 int YMultiCellWeighScale::set_unit(const string& newval)
124 {
125  string rest_val;
126  int res;
128  try {
129  rest_val = newval;
130  res = _setAttr("unit", rest_val);
131  } catch (std::exception) {
133  throw;
134  }
136  return res;
137 }
138 
147 {
148  int res = 0;
150  try {
153  {
156  }
157  }
158  }
159  res = _cellCount;
160  } catch (std::exception) {
162  throw;
163  }
165  return res;
166 }
167 
178 {
179  string rest_val;
180  int res;
182  try {
183  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
184  res = _setAttr("cellCount", rest_val);
185  } catch (std::exception) {
187  throw;
188  }
190  return res;
191 }
192 
202 {
203  Y_EXCITATION_enum res;
205  try {
208  {
211  }
212  }
213  }
214  res = _excitation;
215  } catch (std::exception) {
217  throw;
218  }
220  return res;
221 }
222 
234 {
235  string rest_val;
236  int res;
238  try {
239  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
240  res = _setAttr("excitation", rest_val);
241  } catch (std::exception) {
243  throw;
244  }
246  return res;
247 }
248 
262 {
263  string rest_val;
264  int res;
266  try {
267  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
268  res = _setAttr("compTempAdaptRatio", rest_val);
269  } catch (std::exception) {
271  throw;
272  }
274  return res;
275 }
276 
288 {
289  double res = 0.0;
291  try {
294  {
297  }
298  }
299  }
300  res = _compTempAdaptRatio;
301  } catch (std::exception) {
303  throw;
304  }
306  return res;
307 }
308 
317 {
318  double res = 0.0;
320  try {
323  {
326  }
327  }
328  }
329  res = _compTempAvg;
330  } catch (std::exception) {
332  throw;
333  }
335  return res;
336 }
337 
347 {
348  double res = 0.0;
350  try {
353  {
356  }
357  }
358  }
359  res = _compTempChg;
360  } catch (std::exception) {
362  throw;
363  }
365  return res;
366 }
367 
376 {
377  double res = 0.0;
379  try {
382  {
385  }
386  }
387  }
388  res = _compensation;
389  } catch (std::exception) {
391  throw;
392  }
394  return res;
395 }
396 
407 {
408  string rest_val;
409  int res;
411  try {
412  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
413  res = _setAttr("zeroTracking", rest_val);
414  } catch (std::exception) {
416  throw;
417  }
419  return res;
420 }
421 
432 {
433  double res = 0.0;
435  try {
438  {
441  }
442  }
443  }
444  res = _zeroTracking;
445  } catch (std::exception) {
447  throw;
448  }
450  return res;
451 }
452 
454 {
455  string res;
457  try {
460  {
463  }
464  }
465  }
466  res = _command;
467  } catch (std::exception) {
469  throw;
470  }
472  return res;
473 }
474 
475 int YMultiCellWeighScale::set_command(const string& newval)
476 {
477  string rest_val;
478  int res;
480  try {
481  rest_val = newval;
482  res = _setAttr("command", rest_val);
483  } catch (std::exception) {
485  throw;
486  }
488  return res;
489 }
490 
519 {
520  YMultiCellWeighScale* obj = NULL;
521  int taken = 0;
522  if (YAPI::_apiInitialized) {
524  taken = 1;
525  }try {
526  obj = (YMultiCellWeighScale*) YFunction::_FindFromCache("MultiCellWeighScale", func);
527  if (obj == NULL) {
528  obj = new YMultiCellWeighScale(func);
529  YFunction::_AddToCache("MultiCellWeighScale", func, obj);
530  }
531  } catch (std::exception) {
533  throw;
534  }
536  return obj;
537 }
538 
551 {
552  string val;
553  if (callback != NULL) {
555  } else {
557  }
559  // Immediately invoke value callback with current value
560  if (callback != NULL && this->isOnline()) {
561  val = _advertisedValue;
562  if (!(val == "")) {
563  this->_invokeValueCallback(val);
564  }
565  }
566  return 0;
567 }
568 
570 {
571  if (_valueCallbackMultiCellWeighScale != NULL) {
573  } else {
575  }
576  return 0;
577 }
578 
591 {
592  YSensor* sensor = NULL;
593  sensor = this;
594  if (callback != NULL) {
596  } else {
598  }
600  return 0;
601 }
602 
604 {
607  } else {
609  }
610  return 0;
611 }
612 
622 {
623  return this->set_command("T");
624 }
625 
637 int YMultiCellWeighScale::setupSpan(double currWeight,double maxWeight)
638 {
639  return this->set_command(YapiWrapper::ysprintf("S%d:%d", (int) floor(1000*currWeight+0.5),(int) floor(1000*maxWeight+0.5)));
640 }
641 
643 {
644  string hwid;
645 
646  if(YISERR(_nextFunction(hwid)) || hwid=="") {
647  return NULL;
648  }
650 }
651 
653 {
654  vector<YFUN_DESCR> v_fundescr;
655  YDEV_DESCR ydevice;
656  string serial, funcId, funcName, funcVal, errmsg;
657 
658  if(YISERR(YapiWrapper::getFunctionsByClass("MultiCellWeighScale", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
659  v_fundescr.size() == 0 ||
660  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
661  return NULL;
662  }
663  return YMultiCellWeighScale::FindMultiCellWeighScale(serial+"."+funcId);
664 }
665 
666 //--- (end of YMultiCellWeighScale implementation)
667 
668 //--- (YMultiCellWeighScale functions)
669 //--- (end of YMultiCellWeighScale functions)
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const double COMPENSATION_INVALID
static const double COMPTEMPADAPTRATIO_INVALID
static YMultiCellWeighScale * FindMultiCellWeighScale(string func)
virtual int _invokeTimedReportCallback(YMeasure value)
static const double COMPTEMPAVG_INVALID
virtual int _parseAttr(YJSONObject *json_val)
void(* YMultiCellWeighScaleTimedReportCallback)(YMultiCellWeighScale *func, YMeasure measure)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
static const string COMMAND_INVALID
YMultiCellWeighScaleTimedReportCallback _timedReportCallbackMultiCellWeighScale
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const double COMPTEMPCHG_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
void(* YMultiCellWeighScaleValueCallback)(YMultiCellWeighScale *func, const string &functionValue)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
int set_command(const string &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 void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_api.cpp:7936
int set_unit(const string &newval)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
YMultiCellWeighScale(const string &func)
static const double ZEROTRACKING_INVALID
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 set_compTempAdaptRatio(double newval)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
Y_EXCITATION_enum get_excitation(void)
int set_excitation(Y_EXCITATION_enum newval)
string _className
Definition: yocto_api.h:1558
virtual int setupSpan(double currWeight, double maxWeight)
static const Y_EXCITATION_enum EXCITATION_INVALID
virtual int _invokeValueCallback(string value)
YMultiCellWeighScaleValueCallback _valueCallbackMultiCellWeighScale
virtual int registerValueCallback(YMultiCellWeighScaleValueCallback callback)
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
virtual int registerTimedReportCallback(YMultiCellWeighScaleTimedReportCallback callback)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
YMultiCellWeighScale * nextMultiCellWeighScale(void)
static YMultiCellWeighScale * FirstMultiCellWeighScale(void)


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