yocto_spiport.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_spiport.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindSpiPort(), the high-level API for SpiPort 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_spiport.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__ "spiport"
50 
51 YSpiPort::YSpiPort(const string& func): YFunction(func)
52 //--- (YSpiPort initialization)
53  ,_rxCount(RXCOUNT_INVALID)
54  ,_txCount(TXCOUNT_INVALID)
55  ,_errCount(ERRCOUNT_INVALID)
56  ,_rxMsgCount(RXMSGCOUNT_INVALID)
57  ,_txMsgCount(TXMSGCOUNT_INVALID)
58  ,_lastMsg(LASTMSG_INVALID)
59  ,_currentJob(CURRENTJOB_INVALID)
60  ,_startupJob(STARTUPJOB_INVALID)
61  ,_command(COMMAND_INVALID)
62  ,_voltageLevel(VOLTAGELEVEL_INVALID)
63  ,_protocol(PROTOCOL_INVALID)
64  ,_spiMode(SPIMODE_INVALID)
65  ,_ssPolarity(SSPOLARITY_INVALID)
66  ,_shitftSampling(SHITFTSAMPLING_INVALID)
67  ,_valueCallbackSpiPort(NULL)
68  ,_rxptr(0)
69  ,_rxbuffptr(0)
70 //--- (end of YSpiPort initialization)
71 {
72  _className="SpiPort";
73 }
74 
76 {
77 //--- (YSpiPort cleanup)
78 //--- (end of YSpiPort cleanup)
79 }
80 //--- (YSpiPort implementation)
81 // static attributes
88 
90 {
91  if(json_val->has("rxCount")) {
92  _rxCount = json_val->getInt("rxCount");
93  }
94  if(json_val->has("txCount")) {
95  _txCount = json_val->getInt("txCount");
96  }
97  if(json_val->has("errCount")) {
98  _errCount = json_val->getInt("errCount");
99  }
100  if(json_val->has("rxMsgCount")) {
101  _rxMsgCount = json_val->getInt("rxMsgCount");
102  }
103  if(json_val->has("txMsgCount")) {
104  _txMsgCount = json_val->getInt("txMsgCount");
105  }
106  if(json_val->has("lastMsg")) {
107  _lastMsg = json_val->getString("lastMsg");
108  }
109  if(json_val->has("currentJob")) {
110  _currentJob = json_val->getString("currentJob");
111  }
112  if(json_val->has("startupJob")) {
113  _startupJob = json_val->getString("startupJob");
114  }
115  if(json_val->has("command")) {
116  _command = json_val->getString("command");
117  }
118  if(json_val->has("voltageLevel")) {
119  _voltageLevel = (Y_VOLTAGELEVEL_enum)json_val->getInt("voltageLevel");
120  }
121  if(json_val->has("protocol")) {
122  _protocol = json_val->getString("protocol");
123  }
124  if(json_val->has("spiMode")) {
125  _spiMode = json_val->getString("spiMode");
126  }
127  if(json_val->has("ssPolarity")) {
128  _ssPolarity = (Y_SSPOLARITY_enum)json_val->getInt("ssPolarity");
129  }
130  if(json_val->has("shitftSampling")) {
131  _shitftSampling = (Y_SHITFTSAMPLING_enum)json_val->getInt("shitftSampling");
132  }
133  return YFunction::_parseAttr(json_val);
134 }
135 
136 
145 {
146  int res = 0;
148  try {
151  {
154  }
155  }
156  }
157  res = _rxCount;
158  } catch (std::exception) {
160  throw;
161  }
163  return res;
164 }
165 
174 {
175  int res = 0;
177  try {
180  {
183  }
184  }
185  }
186  res = _txCount;
187  } catch (std::exception) {
189  throw;
190  }
192  return res;
193 }
194 
203 {
204  int res = 0;
206  try {
209  {
212  }
213  }
214  }
215  res = _errCount;
216  } catch (std::exception) {
218  throw;
219  }
221  return res;
222 }
223 
232 {
233  int res = 0;
235  try {
238  {
241  }
242  }
243  }
244  res = _rxMsgCount;
245  } catch (std::exception) {
247  throw;
248  }
250  return res;
251 }
252 
261 {
262  int res = 0;
264  try {
267  {
270  }
271  }
272  }
273  res = _txMsgCount;
274  } catch (std::exception) {
276  throw;
277  }
279  return res;
280 }
281 
290 {
291  string res;
293  try {
296  {
299  }
300  }
301  }
302  res = _lastMsg;
303  } catch (std::exception) {
305  throw;
306  }
308  return res;
309 }
310 
319 {
320  string res;
322  try {
325  {
328  }
329  }
330  }
331  res = _currentJob;
332  } catch (std::exception) {
334  throw;
335  }
337  return res;
338 }
339 
351 int YSpiPort::set_currentJob(const string& newval)
352 {
353  string rest_val;
354  int res;
356  try {
357  rest_val = newval;
358  res = _setAttr("currentJob", rest_val);
359  } catch (std::exception) {
361  throw;
362  }
364  return res;
365 }
366 
375 {
376  string res;
378  try {
381  {
384  }
385  }
386  }
387  res = _startupJob;
388  } catch (std::exception) {
390  throw;
391  }
393  return res;
394 }
395 
407 int YSpiPort::set_startupJob(const string& newval)
408 {
409  string rest_val;
410  int res;
412  try {
413  rest_val = newval;
414  res = _setAttr("startupJob", rest_val);
415  } catch (std::exception) {
417  throw;
418  }
420  return res;
421 }
422 
424 {
425  string res;
427  try {
430  {
433  }
434  }
435  }
436  res = _command;
437  } catch (std::exception) {
439  throw;
440  }
442  return res;
443 }
444 
445 int YSpiPort::set_command(const string& newval)
446 {
447  string rest_val;
448  int res;
450  try {
451  rest_val = newval;
452  res = _setAttr("command", rest_val);
453  } catch (std::exception) {
455  throw;
456  }
458  return res;
459 }
460 
471 {
474  try {
477  {
480  }
481  }
482  }
483  res = _voltageLevel;
484  } catch (std::exception) {
486  throw;
487  }
489  return res;
490 }
491 
508 {
509  string rest_val;
510  int res;
512  try {
513  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
514  res = _setAttr("voltageLevel", rest_val);
515  } catch (std::exception) {
517  throw;
518  }
520  return res;
521 }
522 
535 {
536  string res;
538  try {
541  {
544  }
545  }
546  }
547  res = _protocol;
548  } catch (std::exception) {
550  throw;
551  }
553  return res;
554 }
555 
571 int YSpiPort::set_protocol(const string& newval)
572 {
573  string rest_val;
574  int res;
576  try {
577  rest_val = newval;
578  res = _setAttr("protocol", rest_val);
579  } catch (std::exception) {
581  throw;
582  }
584  return res;
585 }
586 
598 {
599  string res;
601  try {
604  {
607  }
608  }
609  }
610  res = _spiMode;
611  } catch (std::exception) {
613  throw;
614  }
616  return res;
617 }
618 
631 int YSpiPort::set_spiMode(const string& newval)
632 {
633  string rest_val;
634  int res;
636  try {
637  rest_val = newval;
638  res = _setAttr("spiMode", rest_val);
639  } catch (std::exception) {
641  throw;
642  }
644  return res;
645 }
646 
655 {
656  Y_SSPOLARITY_enum res;
658  try {
661  {
664  }
665  }
666  }
667  res = _ssPolarity;
668  } catch (std::exception) {
670  throw;
671  }
673  return res;
674 }
675 
686 {
687  string rest_val;
688  int res;
690  try {
691  rest_val = (newval>0 ? "1" : "0");
692  res = _setAttr("ssPolarity", rest_val);
693  } catch (std::exception) {
695  throw;
696  }
698  return res;
699 }
700 
710 {
713  try {
716  {
719  }
720  }
721  }
722  res = _shitftSampling;
723  } catch (std::exception) {
725  throw;
726  }
728  return res;
729 }
730 
743 {
744  string rest_val;
745  int res;
747  try {
748  rest_val = (newval>0 ? "1" : "0");
749  res = _setAttr("shitftSampling", rest_val);
750  } catch (std::exception) {
752  throw;
753  }
755  return res;
756 }
757 
786 {
787  YSpiPort* obj = NULL;
788  int taken = 0;
789  if (YAPI::_apiInitialized) {
791  taken = 1;
792  }try {
793  obj = (YSpiPort*) YFunction::_FindFromCache("SpiPort", func);
794  if (obj == NULL) {
795  obj = new YSpiPort(func);
796  YFunction::_AddToCache("SpiPort", func, obj);
797  }
798  } catch (std::exception) {
800  throw;
801  }
803  return obj;
804 }
805 
818 {
819  string val;
820  if (callback != NULL) {
822  } else {
824  }
825  _valueCallbackSpiPort = callback;
826  // Immediately invoke value callback with current value
827  if (callback != NULL && this->isOnline()) {
828  val = _advertisedValue;
829  if (!(val == "")) {
830  this->_invokeValueCallback(val);
831  }
832  }
833  return 0;
834 }
835 
837 {
838  if (_valueCallbackSpiPort != NULL) {
839  _valueCallbackSpiPort(this, value);
840  } else {
842  }
843  return 0;
844 }
845 
846 int YSpiPort::sendCommand(string text)
847 {
848  return this->set_command(text);
849 }
850 
859 {
860  _rxptr = 0;
861  _rxbuffptr = 0;
862  _rxbuff = string(0, (char)0);
863 
864  return this->sendCommand("Z");
865 }
866 
876 int YSpiPort::writeByte(int code)
877 {
878  return this->sendCommand(YapiWrapper::ysprintf("$%02x",code));
879 }
880 
890 int YSpiPort::writeStr(string text)
891 {
892  string buff;
893  int bufflen = 0;
894  int idx = 0;
895  int ch = 0;
896  buff = text;
897  bufflen = (int)(buff).size();
898  if (bufflen < 100) {
899  // if string is pure text, we can send it as a simple command (faster)
900  ch = 0x20;
901  idx = 0;
902  while ((idx < bufflen) && (ch != 0)) {
903  ch = ((u8)buff[idx]);
904  if ((ch >= 0x20) && (ch < 0x7f)) {
905  idx = idx + 1;
906  } else {
907  ch = 0;
908  }
909  }
910  if (idx >= bufflen) {
911  return this->sendCommand(YapiWrapper::ysprintf("+%s",text.c_str()));
912  }
913  }
914  // send string using file upload
915  return this->_upload("txdata", buff);
916 }
917 
927 int YSpiPort::writeBin(string buff)
928 {
929  return this->_upload("txdata", buff);
930 }
931 
941 int YSpiPort::writeArray(vector<int> byteList)
942 {
943  string buff;
944  int bufflen = 0;
945  int idx = 0;
946  int hexb = 0;
947  int res = 0;
948  bufflen = (int)byteList.size();
949  buff = string(bufflen, (char)0);
950  idx = 0;
951  while (idx < bufflen) {
952  hexb = byteList[idx];
953  buff[idx] = (char)(hexb);
954  idx = idx + 1;
955  }
956 
957  res = this->_upload("txdata", buff);
958  return res;
959 }
960 
970 int YSpiPort::writeHex(string hexString)
971 {
972  string buff;
973  int bufflen = 0;
974  int idx = 0;
975  int hexb = 0;
976  int res = 0;
977  bufflen = (int)(hexString).length();
978  if (bufflen < 100) {
979  return this->sendCommand(YapiWrapper::ysprintf("$%s",hexString.c_str()));
980  }
981  bufflen = ((bufflen) >> (1));
982  buff = string(bufflen, (char)0);
983  idx = 0;
984  while (idx < bufflen) {
985  hexb = (int)strtoul((hexString).substr( 2 * idx, 2).c_str(), NULL, 16);
986  buff[idx] = (char)(hexb);
987  idx = idx + 1;
988  }
989 
990  res = this->_upload("txdata", buff);
991  return res;
992 }
993 
1003 int YSpiPort::writeLine(string text)
1004 {
1005  string buff;
1006  int bufflen = 0;
1007  int idx = 0;
1008  int ch = 0;
1009  buff = YapiWrapper::ysprintf("%s\r\n",text.c_str());
1010  bufflen = (int)(buff).size()-2;
1011  if (bufflen < 100) {
1012  // if string is pure text, we can send it as a simple command (faster)
1013  ch = 0x20;
1014  idx = 0;
1015  while ((idx < bufflen) && (ch != 0)) {
1016  ch = ((u8)buff[idx]);
1017  if ((ch >= 0x20) && (ch < 0x7f)) {
1018  idx = idx + 1;
1019  } else {
1020  ch = 0;
1021  }
1022  }
1023  if (idx >= bufflen) {
1024  return this->sendCommand(YapiWrapper::ysprintf("!%s",text.c_str()));
1025  }
1026  }
1027  // send string using file upload
1028  return this->_upload("txdata", buff);
1029 }
1030 
1041 {
1042  int currpos = 0;
1043  int reqlen = 0;
1044  string buff;
1045  int bufflen = 0;
1046  int mult = 0;
1047  int endpos = 0;
1048  int res = 0;
1049  // first check if we have the requested character in the look-ahead buffer
1050  bufflen = (int)(_rxbuff).size();
1051  if ((_rxptr >= _rxbuffptr) && (_rxptr < _rxbuffptr+bufflen)) {
1052  res = ((u8)_rxbuff[_rxptr-_rxbuffptr]);
1053  _rxptr = _rxptr + 1;
1054  return res;
1055  }
1056  // try to preload more than one byte to speed-up byte-per-byte access
1057  currpos = _rxptr;
1058  reqlen = 1024;
1059  buff = this->readBin(reqlen);
1060  bufflen = (int)(buff).size();
1061  if (_rxptr == currpos+bufflen) {
1062  res = ((u8)buff[0]);
1063  _rxptr = currpos+1;
1064  _rxbuffptr = currpos;
1065  _rxbuff = buff;
1066  return res;
1067  }
1068  // mixed bidirectional data, retry with a smaller block
1069  _rxptr = currpos;
1070  reqlen = 16;
1071  buff = this->readBin(reqlen);
1072  bufflen = (int)(buff).size();
1073  if (_rxptr == currpos+bufflen) {
1074  res = ((u8)buff[0]);
1075  _rxptr = currpos+1;
1076  _rxbuffptr = currpos;
1077  _rxbuff = buff;
1078  return res;
1079  }
1080  // still mixed, need to process character by character
1081  _rxptr = currpos;
1082 
1083  buff = this->_download(YapiWrapper::ysprintf("rxdata.bin?pos=%d&len=1",_rxptr));
1084  bufflen = (int)(buff).size() - 1;
1085  endpos = 0;
1086  mult = 1;
1087  while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1088  endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1089  mult = mult * 10;
1090  bufflen = bufflen - 1;
1091  }
1092  _rxptr = endpos;
1093  if (bufflen == 0) {
1094  return YAPI_NO_MORE_DATA;
1095  }
1096  res = ((u8)buff[0]);
1097  return res;
1098 }
1099 
1111 string YSpiPort::readStr(int nChars)
1112 {
1113  string buff;
1114  int bufflen = 0;
1115  int mult = 0;
1116  int endpos = 0;
1117  string res;
1118  if (nChars > 65535) {
1119  nChars = 65535;
1120  }
1121 
1122  buff = this->_download(YapiWrapper::ysprintf("rxdata.bin?pos=%d&len=%d", _rxptr,nChars));
1123  bufflen = (int)(buff).size() - 1;
1124  endpos = 0;
1125  mult = 1;
1126  while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1127  endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1128  mult = mult * 10;
1129  bufflen = bufflen - 1;
1130  }
1131  _rxptr = endpos;
1132  res = (buff).substr( 0, bufflen);
1133  return res;
1134 }
1135 
1147 string YSpiPort::readBin(int nChars)
1148 {
1149  string buff;
1150  int bufflen = 0;
1151  int mult = 0;
1152  int endpos = 0;
1153  int idx = 0;
1154  string res;
1155  if (nChars > 65535) {
1156  nChars = 65535;
1157  }
1158 
1159  buff = this->_download(YapiWrapper::ysprintf("rxdata.bin?pos=%d&len=%d", _rxptr,nChars));
1160  bufflen = (int)(buff).size() - 1;
1161  endpos = 0;
1162  mult = 1;
1163  while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1164  endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1165  mult = mult * 10;
1166  bufflen = bufflen - 1;
1167  }
1168  _rxptr = endpos;
1169  res = string(bufflen, (char)0);
1170  idx = 0;
1171  while (idx < bufflen) {
1172  res[idx] = (char)(((u8)buff[idx]));
1173  idx = idx + 1;
1174  }
1175  return res;
1176 }
1177 
1189 vector<int> YSpiPort::readArray(int nChars)
1190 {
1191  string buff;
1192  int bufflen = 0;
1193  int mult = 0;
1194  int endpos = 0;
1195  int idx = 0;
1196  int b = 0;
1197  vector<int> res;
1198  if (nChars > 65535) {
1199  nChars = 65535;
1200  }
1201 
1202  buff = this->_download(YapiWrapper::ysprintf("rxdata.bin?pos=%d&len=%d", _rxptr,nChars));
1203  bufflen = (int)(buff).size() - 1;
1204  endpos = 0;
1205  mult = 1;
1206  while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1207  endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1208  mult = mult * 10;
1209  bufflen = bufflen - 1;
1210  }
1211  _rxptr = endpos;
1212  res.clear();
1213  idx = 0;
1214  while (idx < bufflen) {
1215  b = ((u8)buff[idx]);
1216  res.push_back(b);
1217  idx = idx + 1;
1218  }
1219  return res;
1220 }
1221 
1233 string YSpiPort::readHex(int nBytes)
1234 {
1235  string buff;
1236  int bufflen = 0;
1237  int mult = 0;
1238  int endpos = 0;
1239  int ofs = 0;
1240  string res;
1241  if (nBytes > 65535) {
1242  nBytes = 65535;
1243  }
1244 
1245  buff = this->_download(YapiWrapper::ysprintf("rxdata.bin?pos=%d&len=%d", _rxptr,nBytes));
1246  bufflen = (int)(buff).size() - 1;
1247  endpos = 0;
1248  mult = 1;
1249  while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1250  endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1251  mult = mult * 10;
1252  bufflen = bufflen - 1;
1253  }
1254  _rxptr = endpos;
1255  res = "";
1256  ofs = 0;
1257  while (ofs + 3 < bufflen) {
1258  res = YapiWrapper::ysprintf("%s%02x%02x%02x%02x", res.c_str(), ((u8)buff[ofs]), ((u8)buff[ofs + 1]), ((u8)buff[ofs + 2]),((u8)buff[ofs + 3]));
1259  ofs = ofs + 4;
1260  }
1261  while (ofs < bufflen) {
1262  res = YapiWrapper::ysprintf("%s%02x", res.c_str(),((u8)buff[ofs]));
1263  ofs = ofs + 1;
1264  }
1265  return res;
1266 }
1267 
1282 {
1283  string url;
1284  string msgbin;
1285  vector<string> msgarr;
1286  int msglen = 0;
1287  string res;
1288 
1289  url = YapiWrapper::ysprintf("rxmsg.json?pos=%d&len=1&maxw=1",_rxptr);
1290  msgbin = this->_download(url);
1291  msgarr = this->_json_get_array(msgbin);
1292  msglen = (int)msgarr.size();
1293  if (msglen == 0) {
1294  return "";
1295  }
1296  // last element of array is the new position
1297  msglen = msglen - 1;
1298  _rxptr = atoi((msgarr[msglen]).c_str());
1299  if (msglen == 0) {
1300  return "";
1301  }
1302  res = this->_json_get_string(msgarr[0]);
1303  return res;
1304 }
1305 
1327 vector<string> YSpiPort::readMessages(string pattern,int maxWait)
1328 {
1329  string url;
1330  string msgbin;
1331  vector<string> msgarr;
1332  int msglen = 0;
1333  vector<string> res;
1334  int idx = 0;
1335 
1336  url = YapiWrapper::ysprintf("rxmsg.json?pos=%d&maxw=%d&pat=%s", _rxptr, maxWait,pattern.c_str());
1337  msgbin = this->_download(url);
1338  msgarr = this->_json_get_array(msgbin);
1339  msglen = (int)msgarr.size();
1340  if (msglen == 0) {
1341  return res;
1342  }
1343  // last element of array is the new position
1344  msglen = msglen - 1;
1345  _rxptr = atoi((msgarr[msglen]).c_str());
1346  idx = 0;
1347  while (idx < msglen) {
1348  res.push_back(this->_json_get_string(msgarr[idx]));
1349  idx = idx + 1;
1350  }
1351  return res;
1352 }
1353 
1363 int YSpiPort::read_seek(int absPos)
1364 {
1365  _rxptr = absPos;
1366  return YAPI_SUCCESS;
1367 }
1368 
1375 {
1376  return _rxptr;
1377 }
1378 
1386 {
1387  string buff;
1388  int bufflen = 0;
1389  int res = 0;
1390 
1391  buff = this->_download(YapiWrapper::ysprintf("rxcnt.bin?pos=%d",_rxptr));
1392  bufflen = (int)(buff).size() - 1;
1393  while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1394  bufflen = bufflen - 1;
1395  }
1396  res = atoi(((buff).substr( 0, bufflen)).c_str());
1397  return res;
1398 }
1399 
1412 string YSpiPort::queryLine(string query,int maxWait)
1413 {
1414  string url;
1415  string msgbin;
1416  vector<string> msgarr;
1417  int msglen = 0;
1418  string res;
1419 
1420  url = YapiWrapper::ysprintf("rxmsg.json?len=1&maxw=%d&cmd=!%s", maxWait,query.c_str());
1421  msgbin = this->_download(url);
1422  msgarr = this->_json_get_array(msgbin);
1423  msglen = (int)msgarr.size();
1424  if (msglen == 0) {
1425  return "";
1426  }
1427  // last element of array is the new position
1428  msglen = msglen - 1;
1429  _rxptr = atoi((msgarr[msglen]).c_str());
1430  if (msglen == 0) {
1431  return "";
1432  }
1433  res = this->_json_get_string(msgarr[0]);
1434  return res;
1435 }
1436 
1448 int YSpiPort::uploadJob(string jobfile,string jsonDef)
1449 {
1450  this->_upload(jobfile, jsonDef);
1451  return YAPI_SUCCESS;
1452 }
1453 
1465 int YSpiPort::selectJob(string jobfile)
1466 {
1467  return this->set_currentJob(jobfile);
1468 }
1469 
1480 int YSpiPort::set_SS(int val)
1481 {
1482  return this->sendCommand(YapiWrapper::ysprintf("S%d",val));
1483 }
1484 
1486 {
1487  string hwid;
1488 
1489  if(YISERR(_nextFunction(hwid)) || hwid=="") {
1490  return NULL;
1491  }
1492  return YSpiPort::FindSpiPort(hwid);
1493 }
1494 
1496 {
1497  vector<YFUN_DESCR> v_fundescr;
1498  YDEV_DESCR ydevice;
1499  string serial, funcId, funcName, funcVal, errmsg;
1500 
1501  if(YISERR(YapiWrapper::getFunctionsByClass("SpiPort", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
1502  v_fundescr.size() == 0 ||
1503  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
1504  return NULL;
1505  }
1506  return YSpiPort::FindSpiPort(serial+"."+funcId);
1507 }
1508 
1509 //--- (end of YSpiPort implementation)
1510 
1511 //--- (YSpiPort functions)
1512 //--- (end of YSpiPort functions)
int set_command(const string &newval)
Y_SHITFTSAMPLING_enum get_shitftSampling(void)
string _advertisedValue
Definition: yocto_api.h:1569
Y_SHITFTSAMPLING_enum
Definition: yocto_spiport.h:78
int get_txCount(void)
Y_VOLTAGELEVEL_enum get_voltageLevel(void)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_SHITFTSAMPLING_enum _shitftSampling
virtual int sendCommand(string text)
YSpiPort(const string &func)
string _json_get_string(const string &json)
Definition: yocto_api.cpp:2798
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int read_seek(int absPos)
virtual string readHex(int nBytes)
int set_ssPolarity(Y_SSPOLARITY_enum newval)
virtual int _invokeValueCallback(string value)
string _command
virtual int read_tell(void)
virtual int uploadJob(string jobfile, string jsonDef)
int set_shitftSampling(Y_SHITFTSAMPLING_enum newval)
virtual string readStr(int nChars)
string _protocol
static const Y_SHITFTSAMPLING_enum SHITFTSAMPLING_INVALID
YSpiPortValueCallback _valueCallbackSpiPort
virtual string readLine(void)
Y_VOLTAGELEVEL_enum
string _currentJob
YRETCODE _upload(const string &path, const string &content)
Definition: yocto_api.cpp:3098
static int DefaultCacheValidity
Definition: yocto_api.h:468
void(* YSpiPortValueCallback)(YSpiPort *func, const string &functionValue)
Definition: yocto_spiport.h:54
string getString(const string &key)
Definition: yocto_api.cpp:773
int get_errCount(void)
string _rxbuff
static const string LASTMSG_INVALID
u64 _cacheExpiration
Definition: yocto_api.h:1571
int get_rxMsgCount(void)
static const int RXMSGCOUNT_INVALID
string get_spiMode(void)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const int TXCOUNT_INVALID
static const string CURRENTJOB_INVALID
YSpiPort * nextSpiPort(void)
string _download(const string &url)
Definition: yocto_api.cpp:3053
string _spiMode
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 string SPIMODE_INVALID
virtual int writeByte(int code)
int _rxMsgCount
string _startupJob
static const Y_VOLTAGELEVEL_enum VOLTAGELEVEL_INVALID
static YSpiPort * FindSpiPort(string func)
int set_voltageLevel(Y_VOLTAGELEVEL_enum newval)
virtual int writeArray(vector< int > byteList)
Y_SSPOLARITY_enum
Definition: yocto_spiport.h:70
static const int RXCOUNT_INVALID
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
static const int ERRCOUNT_INVALID
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
int _txMsgCount
int get_txMsgCount(void)
virtual int registerValueCallback(YSpiPortValueCallback callback)
virtual int selectJob(string jobfile)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
string get_currentJob(void)
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_SSPOLARITY_enum _ssPolarity
virtual int _parseAttr(YJSONObject *json_val)
virtual int read_avail(void)
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
virtual string readBin(int nChars)
int _rxbuffptr
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
virtual int reset(void)
int set_currentJob(const string &newval)
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 YSpiPort * FirstSpiPort(void)
int set_protocol(const string &newval)
string _lastMsg
virtual int writeLine(string text)
static const Y_SSPOLARITY_enum SSPOLARITY_INVALID
string get_command(void)
virtual vector< int > readArray(int nChars)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
int get_rxCount(void)
string _className
Definition: yocto_api.h:1558
Y_VOLTAGELEVEL_enum _voltageLevel
int set_spiMode(const string &newval)
virtual int set_SS(int val)
virtual int writeBin(string buff)
virtual string queryLine(string query, int maxWait)
static const string PROTOCOL_INVALID
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
virtual int readByte(void)
#define YISERR(retcode)
Definition: ydef.h:394
Y_SSPOLARITY_enum get_ssPolarity(void)
static const string STARTUPJOB_INVALID
static const string COMMAND_INVALID
static const int TXMSGCOUNT_INVALID
bool has(const string &key)
Definition: yocto_api.cpp:735
string get_lastMsg(void)
string get_startupJob(void)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int writeHex(string hexString)
virtual int writeStr(string text)
int set_startupJob(const string &newval)
virtual vector< string > readMessages(string pattern, int maxWait)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
string get_protocol(void)


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