yocto_weighscale.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_weighscale.cpp 29661 2018-01-18 13:32:13Z mvuilleu $
4  *
5  * Implements yFindWeighScale(), the high-level API for WeighScale 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_weighscale.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__ "weighscale"
50 
51 YWeighScale::YWeighScale(const string& func): YSensor(func)
52 //--- (YWeighScale initialization)
53  ,_excitation(EXCITATION_INVALID)
54  ,_compTempAdaptRatio(COMPTEMPADAPTRATIO_INVALID)
55  ,_compTempAvg(COMPTEMPAVG_INVALID)
56  ,_compTempChg(COMPTEMPCHG_INVALID)
57  ,_compensation(COMPENSATION_INVALID)
58  ,_zeroTracking(ZEROTRACKING_INVALID)
59  ,_command(COMMAND_INVALID)
60  ,_valueCallbackWeighScale(NULL)
61  ,_timedReportCallbackWeighScale(NULL)
62 //--- (end of YWeighScale initialization)
63 {
64  _className="WeighScale";
65 }
66 
68 {
69 //--- (YWeighScale cleanup)
70 //--- (end of YWeighScale cleanup)
71 }
72 //--- (YWeighScale implementation)
73 // static attributes
80 
82 {
83  if(json_val->has("excitation")) {
84  _excitation = (Y_EXCITATION_enum)json_val->getInt("excitation");
85  }
86  if(json_val->has("compTempAdaptRatio")) {
87  _compTempAdaptRatio = floor(json_val->getDouble("compTempAdaptRatio") * 1000.0 / 65536.0 + 0.5) / 1000.0;
88  }
89  if(json_val->has("compTempAvg")) {
90  _compTempAvg = floor(json_val->getDouble("compTempAvg") * 1000.0 / 65536.0 + 0.5) / 1000.0;
91  }
92  if(json_val->has("compTempChg")) {
93  _compTempChg = floor(json_val->getDouble("compTempChg") * 1000.0 / 65536.0 + 0.5) / 1000.0;
94  }
95  if(json_val->has("compensation")) {
96  _compensation = floor(json_val->getDouble("compensation") * 1000.0 / 65536.0 + 0.5) / 1000.0;
97  }
98  if(json_val->has("zeroTracking")) {
99  _zeroTracking = floor(json_val->getDouble("zeroTracking") * 1000.0 / 65536.0 + 0.5) / 1000.0;
100  }
101  if(json_val->has("command")) {
102  _command = json_val->getString("command");
103  }
104  return YSensor::_parseAttr(json_val);
105 }
106 
107 
119 int YWeighScale::set_unit(const string& newval)
120 {
121  string rest_val;
122  int res;
124  try {
125  rest_val = newval;
126  res = _setAttr("unit", rest_val);
127  } catch (std::exception) {
129  throw;
130  }
132  return res;
133 }
134 
144 {
145  Y_EXCITATION_enum res;
147  try {
150  {
153  }
154  }
155  }
156  res = _excitation;
157  } catch (std::exception) {
159  throw;
160  }
162  return res;
163 }
164 
176 {
177  string rest_val;
178  int res;
180  try {
181  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
182  res = _setAttr("excitation", rest_val);
183  } catch (std::exception) {
185  throw;
186  }
188  return res;
189 }
190 
204 {
205  string rest_val;
206  int res;
208  try {
209  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
210  res = _setAttr("compTempAdaptRatio", rest_val);
211  } catch (std::exception) {
213  throw;
214  }
216  return res;
217 }
218 
230 {
231  double res = 0.0;
233  try {
236  {
239  }
240  }
241  }
242  res = _compTempAdaptRatio;
243  } catch (std::exception) {
245  throw;
246  }
248  return res;
249 }
250 
259 {
260  double res = 0.0;
262  try {
265  {
268  }
269  }
270  }
271  res = _compTempAvg;
272  } catch (std::exception) {
274  throw;
275  }
277  return res;
278 }
279 
289 {
290  double res = 0.0;
292  try {
295  {
298  }
299  }
300  }
301  res = _compTempChg;
302  } catch (std::exception) {
304  throw;
305  }
307  return res;
308 }
309 
318 {
319  double res = 0.0;
321  try {
324  {
327  }
328  }
329  }
330  res = _compensation;
331  } catch (std::exception) {
333  throw;
334  }
336  return res;
337 }
338 
349 {
350  string rest_val;
351  int res;
353  try {
354  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
355  res = _setAttr("zeroTracking", rest_val);
356  } catch (std::exception) {
358  throw;
359  }
361  return res;
362 }
363 
374 {
375  double res = 0.0;
377  try {
380  {
383  }
384  }
385  }
386  res = _zeroTracking;
387  } catch (std::exception) {
389  throw;
390  }
392  return res;
393 }
394 
396 {
397  string res;
399  try {
402  {
405  }
406  }
407  }
408  res = _command;
409  } catch (std::exception) {
411  throw;
412  }
414  return res;
415 }
416 
417 int YWeighScale::set_command(const string& newval)
418 {
419  string rest_val;
420  int res;
422  try {
423  rest_val = newval;
424  res = _setAttr("command", rest_val);
425  } catch (std::exception) {
427  throw;
428  }
430  return res;
431 }
432 
461 {
462  YWeighScale* obj = NULL;
463  int taken = 0;
464  if (YAPI::_apiInitialized) {
466  taken = 1;
467  }try {
468  obj = (YWeighScale*) YFunction::_FindFromCache("WeighScale", func);
469  if (obj == NULL) {
470  obj = new YWeighScale(func);
471  YFunction::_AddToCache("WeighScale", func, obj);
472  }
473  } catch (std::exception) {
475  throw;
476  }
478  return obj;
479 }
480 
493 {
494  string val;
495  if (callback != NULL) {
497  } else {
499  }
500  _valueCallbackWeighScale = callback;
501  // Immediately invoke value callback with current value
502  if (callback != NULL && this->isOnline()) {
503  val = _advertisedValue;
504  if (!(val == "")) {
505  this->_invokeValueCallback(val);
506  }
507  }
508  return 0;
509 }
510 
512 {
513  if (_valueCallbackWeighScale != NULL) {
514  _valueCallbackWeighScale(this, value);
515  } else {
517  }
518  return 0;
519 }
520 
533 {
534  YSensor* sensor = NULL;
535  sensor = this;
536  if (callback != NULL) {
538  } else {
540  }
542  return 0;
543 }
544 
546 {
547  if (_timedReportCallbackWeighScale != NULL) {
548  _timedReportCallbackWeighScale(this, value);
549  } else {
551  }
552  return 0;
553 }
554 
564 {
565  return this->set_command("T");
566 }
567 
579 int YWeighScale::setupSpan(double currWeight,double maxWeight)
580 {
581  return this->set_command(YapiWrapper::ysprintf("S%d:%d", (int) floor(1000*currWeight+0.5),(int) floor(1000*maxWeight+0.5)));
582 }
583 
584 int YWeighScale::setCompensationTable(int tableIndex,vector<double> tempValues,vector<double> compValues)
585 {
586  int siz = 0;
587  int res = 0;
588  int idx = 0;
589  int found = 0;
590  double prev = 0.0;
591  double curr = 0.0;
592  double currComp = 0.0;
593  double idxTemp = 0.0;
594  siz = (int)tempValues.size();
595  if (!(siz != 1)) {
596  _throw(YAPI_INVALID_ARGUMENT,"thermal compensation table must have at least two points");
597  return YAPI_INVALID_ARGUMENT;
598  }
599  if (!(siz == (int)compValues.size())) {
600  _throw(YAPI_INVALID_ARGUMENT,"table sizes mismatch");
601  return YAPI_INVALID_ARGUMENT;
602  }
603 
604  res = this->set_command(YapiWrapper::ysprintf("%dZ",tableIndex));
605  if (!(res==YAPI_SUCCESS)) {
606  _throw(YAPI_IO_ERROR,"unable to reset thermal compensation table");
607  return YAPI_IO_ERROR;
608  }
609  // add records in growing temperature value
610  found = 1;
611  prev = -999999.0;
612  while (found > 0) {
613  found = 0;
614  curr = 99999999.0;
615  currComp = -999999.0;
616  idx = 0;
617  while (idx < siz) {
618  idxTemp = tempValues[idx];
619  if ((idxTemp > prev) && (idxTemp < curr)) {
620  curr = idxTemp;
621  currComp = compValues[idx];
622  found = 1;
623  }
624  idx = idx + 1;
625  }
626  if (found > 0) {
627  res = this->set_command(YapiWrapper::ysprintf("%dm%d:%d", tableIndex, (int) floor(1000*curr+0.5),(int) floor(1000*currComp+0.5)));
628  if (!(res==YAPI_SUCCESS)) {
629  _throw(YAPI_IO_ERROR,"unable to set thermal compensation table");
630  return YAPI_IO_ERROR;
631  }
632  prev = curr;
633  }
634  }
635  return YAPI_SUCCESS;
636 }
637 
638 int YWeighScale::loadCompensationTable(int tableIndex,vector<double>& tempValues,vector<double>& compValues)
639 {
640  string id;
641  string bin_json;
642  vector<string> paramlist;
643  int siz = 0;
644  int idx = 0;
645  double temp = 0.0;
646  double comp = 0.0;
647 
648  id = this->get_functionId();
649  id = (id).substr( 10, (int)(id).length() - 10);
650  bin_json = this->_download(YapiWrapper::ysprintf("extra.json?page=%d",(4*atoi((id).c_str()))+tableIndex));
651  paramlist = this->_json_get_array(bin_json);
652  // convert all values to float and append records
653  siz = (((int)paramlist.size()) >> (1));
654  tempValues.clear();
655  compValues.clear();
656  idx = 0;
657  while (idx < siz) {
658  temp = atof((paramlist[2*idx]).c_str())/1000.0;
659  comp = atof((paramlist[2*idx+1]).c_str())/1000.0;
660  tempValues.push_back(temp);
661  compValues.push_back(comp);
662  idx = idx + 1;
663  }
664  return YAPI_SUCCESS;
665 }
666 
682 int YWeighScale::set_offsetAvgCompensationTable(vector<double> tempValues,vector<double> compValues)
683 {
684  return this->setCompensationTable(0, tempValues, compValues);
685 }
686 
702 int YWeighScale::loadOffsetAvgCompensationTable(vector<double>& tempValues,vector<double>& compValues)
703 {
704  return this->loadCompensationTable(0, tempValues, compValues);
705 }
706 
722 int YWeighScale::set_offsetChgCompensationTable(vector<double> tempValues,vector<double> compValues)
723 {
724  return this->setCompensationTable(1, tempValues, compValues);
725 }
726 
742 int YWeighScale::loadOffsetChgCompensationTable(vector<double>& tempValues,vector<double>& compValues)
743 {
744  return this->loadCompensationTable(1, tempValues, compValues);
745 }
746 
762 int YWeighScale::set_spanAvgCompensationTable(vector<double> tempValues,vector<double> compValues)
763 {
764  return this->setCompensationTable(2, tempValues, compValues);
765 }
766 
782 int YWeighScale::loadSpanAvgCompensationTable(vector<double>& tempValues,vector<double>& compValues)
783 {
784  return this->loadCompensationTable(2, tempValues, compValues);
785 }
786 
802 int YWeighScale::set_spanChgCompensationTable(vector<double> tempValues,vector<double> compValues)
803 {
804  return this->setCompensationTable(3, tempValues, compValues);
805 }
806 
822 int YWeighScale::loadSpanChgCompensationTable(vector<double>& tempValues,vector<double>& compValues)
823 {
824  return this->loadCompensationTable(3, tempValues, compValues);
825 }
826 
828 {
829  string hwid;
830 
831  if(YISERR(_nextFunction(hwid)) || hwid=="") {
832  return NULL;
833  }
834  return YWeighScale::FindWeighScale(hwid);
835 }
836 
838 {
839  vector<YFUN_DESCR> v_fundescr;
840  YDEV_DESCR ydevice;
841  string serial, funcId, funcName, funcVal, errmsg;
842 
843  if(YISERR(YapiWrapper::getFunctionsByClass("WeighScale", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
844  v_fundescr.size() == 0 ||
845  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
846  return NULL;
847  }
848  return YWeighScale::FindWeighScale(serial+"."+funcId);
849 }
850 
851 //--- (end of YWeighScale implementation)
852 
853 //--- (YWeighScale functions)
854 //--- (end of YWeighScale functions)
string _advertisedValue
Definition: yocto_api.h:1569
virtual int _parseAttr(YJSONObject *json_val)
YWeighScaleValueCallback _valueCallbackWeighScale
int set_unit(const string &newval)
double getDouble(const string &key)
Definition: yocto_api.cpp:796
double get_zeroTracking(void)
void(* YWeighScaleTimedReportCallback)(YWeighScale *func, YMeasure measure)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
virtual int set_offsetChgCompensationTable(vector< double > tempValues, vector< double > compValues)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
virtual int _invokeTimedReportCallback(YMeasure value)
static const double COMPTEMPADAPTRATIO_INVALID
YWeighScaleTimedReportCallback _timedReportCallbackWeighScale
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
YWeighScale(const string &func)
static const string COMMAND_INVALID
virtual int loadOffsetChgCompensationTable(vector< double > &tempValues, vector< double > &compValues)
YWeighScale * nextWeighScale(void)
int set_command(const string &newval)
double get_compTempChg(void)
static const double ZEROTRACKING_INVALID
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
int set_compTempAdaptRatio(double newval)
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
static const Y_EXCITATION_enum EXCITATION_INVALID
virtual int registerValueCallback(YWeighScaleValueCallback callback)
string _download(const string &url)
Definition: yocto_api.cpp:3053
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 double COMPTEMPCHG_INVALID
double _compTempAvg
virtual int loadCompensationTable(int tableIndex, vector< double > &tempValues, vector< double > &compValues)
virtual int loadOffsetAvgCompensationTable(vector< double > &tempValues, vector< double > &compValues)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
string get_functionId(void)
Definition: yocto_api.cpp:3229
int set_excitation(Y_EXCITATION_enum newval)
static YWeighScale * FindWeighScale(string func)
double get_compensation(void)
double _compensation
static YWeighScale * FirstWeighScale(void)
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
virtual int loadSpanAvgCompensationTable(vector< double > &tempValues, vector< double > &compValues)
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
virtual int set_spanAvgCompensationTable(vector< double > tempValues, vector< double > compValues)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
int set_zeroTracking(double newval)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
vector< string > _json_get_array(const string &json)
Definition: yocto_api.cpp:2811
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
virtual int loadSpanChgCompensationTable(vector< double > &tempValues, vector< double > &compValues)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
static const double COMPENSATION_INVALID
bool isOnline(void)
Definition: yocto_api.cpp:3291
double get_compTempAvg(void)
string get_command(void)
virtual int set_offsetAvgCompensationTable(vector< double > tempValues, vector< double > compValues)
double _compTempAdaptRatio
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
void(* YWeighScaleValueCallback)(YWeighScale *func, const string &functionValue)
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 setCompensationTable(int tableIndex, vector< double > tempValues, vector< double > compValues)
virtual int set_spanChgCompensationTable(vector< double > tempValues, vector< double > compValues)
string _className
Definition: yocto_api.h:1558
double _zeroTracking
virtual int registerTimedReportCallback(YWeighScaleTimedReportCallback callback)
virtual int _invokeValueCallback(string value)
virtual int tare(void)
static const double COMPTEMPAVG_INVALID
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
Y_EXCITATION_enum _excitation
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
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int setupSpan(double currWeight, double maxWeight)
double _compTempChg
Y_EXCITATION_enum get_excitation(void)
double get_compTempAdaptRatio(void)


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