plotly interface based on ardunio sample code

Dependents:   Plotly_HelloWorld

Library for plotting a simple x/y scatter chart on the plot.ly website.

See plotly_HelloWorld for sample usage.

Committer:
AndyA
Date:
Wed Jul 02 10:13:24 2014 +0000
Revision:
1:d532e96fca12
Parent:
0:96532c59670f
Child:
2:d53d74ed68ac
Creates plots but fails to send data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndyA 0:96532c59670f 1 #include "plotly.h"
AndyA 0:96532c59670f 2 #include "mbed.h"
AndyA 0:96532c59670f 3
AndyA 0:96532c59670f 4 #define plotlyURL "plot.ly"
AndyA 0:96532c59670f 5
AndyA 0:96532c59670f 6 #define dataURL "arduino.plot.ly"
AndyA 0:96532c59670f 7
AndyA 0:96532c59670f 8
AndyA 0:96532c59670f 9 //#define plotlyURL "192.168.1.87"
AndyA 0:96532c59670f 10 //#define dataURL "192.168.1.87"
AndyA 0:96532c59670f 11
AndyA 0:96532c59670f 12 plotly::plotly(char *username, char *api_key, char* stream_tokens[], char *filename, int nTraces)
AndyA 0:96532c59670f 13 {
AndyA 0:96532c59670f 14 log_level = 0; // 0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors, 4 = Quiet (// Serial Off)
AndyA 0:96532c59670f 15 dry_run = false;
AndyA 0:96532c59670f 16 username_ = username;
AndyA 0:96532c59670f 17 api_key_ = api_key;
AndyA 0:96532c59670f 18 stream_tokens_ = stream_tokens;
AndyA 0:96532c59670f 19 filename_ = filename;
AndyA 0:96532c59670f 20 nTraces_ = nTraces;
AndyA 0:96532c59670f 21 maxpoints = 30;
AndyA 0:96532c59670f 22 fibonacci_ = 1;
AndyA 0:96532c59670f 23 world_readable = true;
AndyA 0:96532c59670f 24 convertTimestamp = true;
AndyA 0:96532c59670f 25 timezone = "America/Montreal";
AndyA 0:96532c59670f 26 fileopt = "overwrite";
AndyA 1:d532e96fca12 27
AndyA 1:d532e96fca12 28 initalised = false;
AndyA 0:96532c59670f 29 }
AndyA 0:96532c59670f 30
AndyA 0:96532c59670f 31
AndyA 1:d532e96fca12 32 plotly::~plotly()
AndyA 1:d532e96fca12 33 {
AndyA 1:d532e96fca12 34 closeStream();
AndyA 1:d532e96fca12 35 }
AndyA 0:96532c59670f 36
AndyA 0:96532c59670f 37 bool plotly::init()
AndyA 0:96532c59670f 38 {
AndyA 0:96532c59670f 39
AndyA 0:96532c59670f 40 //
AndyA 0:96532c59670f 41 // Validate a stream with a REST post to plotly
AndyA 0:96532c59670f 42 //
AndyA 0:96532c59670f 43 if(dry_run && log_level < 3) {
AndyA 0:96532c59670f 44 fprintf(stderr,"... This is a dry run, we are not connecting to plotly's servers...\n");
AndyA 0:96532c59670f 45 } else if(log_level < 3) {
AndyA 0:96532c59670f 46 fprintf(stderr,"... Attempting to connect to plotly's REST servers\n");
AndyA 0:96532c59670f 47 }
AndyA 0:96532c59670f 48
AndyA 1:d532e96fca12 49 socket.set_blocking(false);
AndyA 1:d532e96fca12 50
AndyA 1:d532e96fca12 51 do {
AndyA 1:d532e96fca12 52
AndyA 1:d532e96fca12 53 socket.connect(plotlyURL, 80);
AndyA 1:d532e96fca12 54 wait(10);
AndyA 1:d532e96fca12 55 if(!(socket.is_connected()) && (log_level < 4)) {
AndyA 0:96532c59670f 56 fprintf(stderr,"... Couldn\'t connect to plotly's REST servers... trying again!\n");
AndyA 0:96532c59670f 57 }
AndyA 1:d532e96fca12 58 } while (!(socket.is_connected()));
AndyA 1:d532e96fca12 59
AndyA 1:d532e96fca12 60 if(log_level < 3) fprintf(stderr,"... Connected to plotly's REST servers\n");
AndyA 1:d532e96fca12 61
AndyA 1:d532e96fca12 62 if(log_level < 3) fprintf(stderr,"... Sending HTTP Post to plotly\n");
AndyA 1:d532e96fca12 63
AndyA 0:96532c59670f 64 print_("POST /clientresp HTTP/1.1\r\n");
AndyA 0:96532c59670f 65 print_("Host: 107.21.214.199\r\n");
AndyA 0:96532c59670f 66 print_("User-Agent: Arduino/0.5.1\r\n");
AndyA 0:96532c59670f 67
AndyA 0:96532c59670f 68 print_("Content-Length: ");
AndyA 0:96532c59670f 69 int contentLength = 126 + len_(username_) + len_(fileopt) + nTraces_*(87+len_(maxpoints)) + (nTraces_-1)*2 + len_(filename_);
AndyA 0:96532c59670f 70 if(world_readable) {
AndyA 0:96532c59670f 71 contentLength += 4;
AndyA 0:96532c59670f 72 } else {
AndyA 0:96532c59670f 73 contentLength += 5;
AndyA 0:96532c59670f 74 }
AndyA 0:96532c59670f 75 print_(contentLength);
AndyA 0:96532c59670f 76 // contentLength =
AndyA 0:96532c59670f 77 // 44 // first part of querystring below
AndyA 0:96532c59670f 78 // + len_(username) // upper bound on username length
AndyA 0:96532c59670f 79 // + 5 // &key=
AndyA 0:96532c59670f 80 // + 10 // api_key length
AndyA 0:96532c59670f 81 // + 7 // &args=[...
AndyA 0:96532c59670f 82 // + nTraces*(87+len(maxpoints)) // len({\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \") + 10 + len(\", "maxpoints": )+len(maxpoints)+len(}})
AndyA 0:96532c59670f 83 // + (nTraces-1)*2 // ", " in between trace objects
AndyA 0:96532c59670f 84 // + 22 // ]&kwargs={\"fileopt\": \"
AndyA 0:96532c59670f 85 // + len_(fileopt)
AndyA 0:96532c59670f 86 // + 16 // \", \"filename\": \"
AndyA 0:96532c59670f 87 // + len_(filename)
AndyA 0:96532c59670f 88 // + 21 // ", "world_readable":
AndyA 0:96532c59670f 89 // + 4 if world_readable, 5 otherwise
AndyA 0:96532c59670f 90 // + 1 // closing }
AndyA 0:96532c59670f 91 //------
AndyA 0:96532c59670f 92 // 126 + len_(username) + len_(fileopt) + nTraces*(86+len(maxpoints)) + (nTraces-1)*2 + len_(filename)
AndyA 0:96532c59670f 93 //
AndyA 0:96532c59670f 94 // Terminate headers with new lines
AndyA 0:96532c59670f 95 print_("\r\n\r\n");
AndyA 0:96532c59670f 96
AndyA 0:96532c59670f 97 // Start printing querystring body
AndyA 0:96532c59670f 98 print_("version=2.2&origin=plot&platform=arduino&un=");
AndyA 0:96532c59670f 99 print_(username_);
AndyA 0:96532c59670f 100 print_("&key=");
AndyA 0:96532c59670f 101 print_(api_key_);
AndyA 0:96532c59670f 102 print_("&args=[");
AndyA 0:96532c59670f 103 // print a trace for each token supplied
AndyA 0:96532c59670f 104 for(int i=0; i<nTraces_; i++) {
AndyA 0:96532c59670f 105 print_("{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \"");
AndyA 0:96532c59670f 106 print_(stream_tokens_[i]);
AndyA 0:96532c59670f 107 print_("\", \"maxpoints\": ");
AndyA 0:96532c59670f 108 print_(maxpoints);
AndyA 0:96532c59670f 109 print_("}}");
AndyA 0:96532c59670f 110 if(nTraces_ > 1 && i != nTraces_-1) {
AndyA 0:96532c59670f 111 print_(", ");
AndyA 0:96532c59670f 112 }
AndyA 0:96532c59670f 113 }
AndyA 0:96532c59670f 114 print_("]&kwargs={\"fileopt\": \"");
AndyA 0:96532c59670f 115 print_(fileopt);
AndyA 0:96532c59670f 116 print_("\", \"filename\": \"");
AndyA 0:96532c59670f 117 print_(filename_);
AndyA 0:96532c59670f 118 print_("\", \"world_readable\": ");
AndyA 0:96532c59670f 119 if(world_readable) {
AndyA 0:96532c59670f 120 print_("true");
AndyA 0:96532c59670f 121 } else {
AndyA 0:96532c59670f 122 print_("false");
AndyA 0:96532c59670f 123 }
AndyA 0:96532c59670f 124 print_("}");
AndyA 0:96532c59670f 125 // final newline to terminate the POST
AndyA 0:96532c59670f 126 print_("\r\n");
AndyA 0:96532c59670f 127
AndyA 0:96532c59670f 128 //
AndyA 0:96532c59670f 129 // Wait for a response
AndyA 0:96532c59670f 130 // Parse the response for the "All Streams Go!" and proceed to streaming
AndyA 0:96532c59670f 131 // if we find it
AndyA 0:96532c59670f 132 //
AndyA 0:96532c59670f 133 char allStreamsGo[] = "All Streams Go!";
AndyA 0:96532c59670f 134 int asgCnt = 0; // asg stands for All Streams Go
AndyA 0:96532c59670f 135 char url[] = "\"url\": \"http://107.21.214.199/~";
AndyA 0:96532c59670f 136 char fid[4];
AndyA 0:96532c59670f 137 int fidCnt = 0;
AndyA 0:96532c59670f 138 int urlCnt = 0;
AndyA 0:96532c59670f 139 int usernameCnt = 0;
AndyA 0:96532c59670f 140 bool proceed = false;
AndyA 0:96532c59670f 141 bool fidMatched = false;
AndyA 0:96532c59670f 142 char c;
AndyA 0:96532c59670f 143
AndyA 0:96532c59670f 144 if(log_level < 2) {
AndyA 0:96532c59670f 145 fprintf(stderr,"... Sent message, waiting for plotly's response...\n");
AndyA 0:96532c59670f 146 }
AndyA 0:96532c59670f 147
AndyA 0:96532c59670f 148 if(!dry_run) {
AndyA 0:96532c59670f 149 while(!proceed) {
AndyA 1:d532e96fca12 150 int32_t dataIn = socket.receive(rxBuffer,127);
AndyA 1:d532e96fca12 151 if (dataIn < 0) {
AndyA 1:d532e96fca12 152 if(log_level < 3) fprintf(stderr,"error reading network socket\n");
AndyA 1:d532e96fca12 153 break;
AndyA 1:d532e96fca12 154 }
AndyA 0:96532c59670f 155 if(dataIn > 0) {
AndyA 0:96532c59670f 156 rxBuffer[dataIn]=0;
AndyA 0:96532c59670f 157
AndyA 0:96532c59670f 158 if(log_level < 2) fprintf(stderr,rxBuffer);
AndyA 0:96532c59670f 159
AndyA 0:96532c59670f 160 for (int i = 0; i<dataIn; i++) {
AndyA 0:96532c59670f 161 c = rxBuffer[i];
AndyA 0:96532c59670f 162 //
AndyA 0:96532c59670f 163 // Attempt to read the "All streams go" msg if it exists
AndyA 0:96532c59670f 164 // by comparing characters as they roll in
AndyA 0:96532c59670f 165 //
AndyA 0:96532c59670f 166
AndyA 0:96532c59670f 167 if(asgCnt == len_(allStreamsGo) && !proceed) {
AndyA 0:96532c59670f 168 proceed = true;
AndyA 0:96532c59670f 169 } else if(allStreamsGo[asgCnt]==c) {
AndyA 0:96532c59670f 170 asgCnt += 1;
AndyA 0:96532c59670f 171 } else if(asgCnt > 0) {
AndyA 0:96532c59670f 172 // reset counter
AndyA 0:96532c59670f 173 asgCnt = 0;
AndyA 0:96532c59670f 174 }
AndyA 0:96532c59670f 175
AndyA 0:96532c59670f 176 //
AndyA 0:96532c59670f 177 // Extract the last bit of the URL from the response
AndyA 0:96532c59670f 178 // The url is in the form http://107.21.214.199/~USERNAME/FID
AndyA 0:96532c59670f 179 // We'll character-count up through char url[] and through username_, then start
AndyA 0:96532c59670f 180 // filling in characters into fid
AndyA 0:96532c59670f 181 //
AndyA 0:96532c59670f 182
AndyA 0:96532c59670f 183 if(log_level < 3) {
AndyA 0:96532c59670f 184 if(url[urlCnt]==c && urlCnt < len_(url)) {
AndyA 0:96532c59670f 185 urlCnt += 1;
AndyA 0:96532c59670f 186 } else if(urlCnt > 0 && urlCnt < len_(url)) {
AndyA 0:96532c59670f 187 // Reset counter
AndyA 0:96532c59670f 188 urlCnt = 0;
AndyA 0:96532c59670f 189 }
AndyA 0:96532c59670f 190 if(urlCnt == len_(url) && fidCnt < 4 && !fidMatched) {
AndyA 0:96532c59670f 191 // We've counted through the url, start counting through the username
AndyA 0:96532c59670f 192 if(usernameCnt < len_(username_)+2) {
AndyA 0:96532c59670f 193 usernameCnt += 1;
AndyA 0:96532c59670f 194 } else {
AndyA 0:96532c59670f 195 // the url ends with "
AndyA 0:96532c59670f 196 if(c != '"') {
AndyA 0:96532c59670f 197 fid[fidCnt] = c;
AndyA 0:96532c59670f 198 fidCnt += 1;
AndyA 0:96532c59670f 199 } else if(fidCnt>0) {
AndyA 0:96532c59670f 200 fidMatched = true;
AndyA 0:96532c59670f 201 }
AndyA 0:96532c59670f 202
AndyA 0:96532c59670f 203 }
AndyA 0:96532c59670f 204 }
AndyA 0:96532c59670f 205 }
AndyA 0:96532c59670f 206 }
AndyA 0:96532c59670f 207 }
AndyA 0:96532c59670f 208 wait(0.1);
AndyA 0:96532c59670f 209 }
AndyA 0:96532c59670f 210 }
AndyA 0:96532c59670f 211
AndyA 0:96532c59670f 212 if(!dry_run && !proceed && log_level < 4) {
AndyA 0:96532c59670f 213 fprintf(stderr,"... Error initializing stream, aborting. Try again or get in touch with Chris at chris@plot.ly\n");
AndyA 0:96532c59670f 214 }
AndyA 0:96532c59670f 215
AndyA 0:96532c59670f 216 if(!dry_run && proceed && log_level < 3) {
AndyA 0:96532c59670f 217 fprintf(stderr,"... A-ok from plotly, All Streams Go!\n");
AndyA 0:96532c59670f 218 if(fidMatched) {
AndyA 0:96532c59670f 219 fprintf(stderr,"... View your streaming plot here: https://plot.ly/~");
AndyA 0:96532c59670f 220 fprintf(stderr,username_);
AndyA 0:96532c59670f 221 fprintf(stderr,"/");
AndyA 0:96532c59670f 222 for(int i=0; i<fidCnt; i++) {
AndyA 0:96532c59670f 223 fprintf(stderr,"%d ",fid[i]);
AndyA 0:96532c59670f 224 }
AndyA 0:96532c59670f 225 fprintf(stderr,"\n");
AndyA 0:96532c59670f 226 }
AndyA 0:96532c59670f 227 }
AndyA 1:d532e96fca12 228 initalised = proceed || dry_run;
AndyA 0:96532c59670f 229 return proceed;
AndyA 0:96532c59670f 230 }
AndyA 0:96532c59670f 231
AndyA 0:96532c59670f 232 void plotly::openStream()
AndyA 0:96532c59670f 233 {
AndyA 1:d532e96fca12 234
AndyA 1:d532e96fca12 235 if (!initalised)
AndyA 1:d532e96fca12 236 return;
AndyA 0:96532c59670f 237 //
AndyA 0:96532c59670f 238 // Start request to stream servers
AndyA 0:96532c59670f 239 //
AndyA 0:96532c59670f 240 if(log_level < 3) {} fprintf(stderr,"... Connecting to plotly's streaming servers...\n");
AndyA 0:96532c59670f 241 char server[] = dataURL;
AndyA 0:96532c59670f 242 int port = 80;
AndyA 0:96532c59670f 243 while ( !socket.connect(server, port) ) {
AndyA 0:96532c59670f 244 if(log_level < 4) fprintf(stderr,"... Couldn\'t connect to servers... trying again!\n");
AndyA 0:96532c59670f 245 fibonacci_ += fibonacci_;
AndyA 0:96532c59670f 246 wait(10);
AndyA 0:96532c59670f 247 }
AndyA 0:96532c59670f 248 fibonacci_ = 1;
AndyA 0:96532c59670f 249 if(log_level < 3) {} fprintf(stderr,"... Connected to plotly's streaming servers\n... Initializing stream\n");
AndyA 0:96532c59670f 250
AndyA 0:96532c59670f 251 print_("POST / HTTP/1.1\r\n");
AndyA 0:96532c59670f 252 print_("Host: 127.0.0.1\r\n");
AndyA 0:96532c59670f 253 print_("User-Agent: Python\r\n");
AndyA 0:96532c59670f 254 print_("Transfer-Encoding: chunked\r\n");
AndyA 0:96532c59670f 255 print_("Connection: close\r\n");
AndyA 0:96532c59670f 256 if(convertTimestamp) {
AndyA 0:96532c59670f 257 print_("plotly-convertTimestamp: \"");
AndyA 0:96532c59670f 258 print_(timezone);
AndyA 0:96532c59670f 259 print_("\"");
AndyA 0:96532c59670f 260 }
AndyA 0:96532c59670f 261 print_("\r\n\r\n");
AndyA 0:96532c59670f 262
AndyA 1:d532e96fca12 263 if(log_level < 3) fprintf(stderr,"... Done initializing, ready to stream!\n");
AndyA 0:96532c59670f 264 }
AndyA 0:96532c59670f 265
AndyA 0:96532c59670f 266 void plotly::closeStream()
AndyA 0:96532c59670f 267 {
AndyA 0:96532c59670f 268 print_("0\r\n\r\n");
AndyA 0:96532c59670f 269 socket.close();
AndyA 0:96532c59670f 270 }
AndyA 1:d532e96fca12 271
AndyA 0:96532c59670f 272 void plotly::reconnectStream()
AndyA 0:96532c59670f 273 {
AndyA 1:d532e96fca12 274 if (!initalised)
AndyA 1:d532e96fca12 275 return;
AndyA 1:d532e96fca12 276
AndyA 0:96532c59670f 277 while(!socket.is_connected()) {
AndyA 0:96532c59670f 278 if(log_level<4) fprintf(stderr,"... Disconnected from streaming servers\n");
AndyA 0:96532c59670f 279 closeStream();
AndyA 0:96532c59670f 280 openStream();
AndyA 0:96532c59670f 281 }
AndyA 0:96532c59670f 282 }
AndyA 0:96532c59670f 283 void plotly::jsonStart(int i)
AndyA 0:96532c59670f 284 {
AndyA 0:96532c59670f 285 // Print the length of the message in hex:
AndyA 0:96532c59670f 286 // 15 char for the json that wraps the data: {"x": , "y": }\n
AndyA 0:96532c59670f 287 // + 23 char for the token: , "token": "abcdefghij"
AndyA 0:96532c59670f 288 // = 38
AndyA 0:96532c59670f 289 printHex_(i+44);
AndyA 0:96532c59670f 290 print_("\r\n{\"x\": ");
AndyA 0:96532c59670f 291 }
AndyA 0:96532c59670f 292 void plotly::jsonMiddle()
AndyA 0:96532c59670f 293 {
AndyA 0:96532c59670f 294 print_(", \"y\": ");
AndyA 0:96532c59670f 295 }
AndyA 0:96532c59670f 296 void plotly::jsonEnd(char *token)
AndyA 0:96532c59670f 297 {
AndyA 0:96532c59670f 298 print_(", \"streamtoken\": \"");
AndyA 0:96532c59670f 299 print_(token);
AndyA 0:96532c59670f 300 print_("\"}\n\r\n");
AndyA 0:96532c59670f 301 }
AndyA 0:96532c59670f 302
AndyA 0:96532c59670f 303 int plotly::len_(int i)
AndyA 0:96532c59670f 304 {
AndyA 0:96532c59670f 305 // int range: -32,768 to 32,767
AndyA 0:96532c59670f 306 if(i > 9999) return 5;
AndyA 0:96532c59670f 307 else if(i > 999) return 4;
AndyA 0:96532c59670f 308 else if(i > 99) return 3;
AndyA 0:96532c59670f 309 else if(i > 9) return 2;
AndyA 0:96532c59670f 310 else if(i > -1) return 1;
AndyA 0:96532c59670f 311 else if(i > -10) return 2;
AndyA 0:96532c59670f 312 else if(i > -100) return 3;
AndyA 0:96532c59670f 313 else if(i > -1000) return 4;
AndyA 0:96532c59670f 314 else if(i > -10000) return 5;
AndyA 0:96532c59670f 315 else return 6;
AndyA 0:96532c59670f 316 }
AndyA 0:96532c59670f 317 int plotly::len_(unsigned long i)
AndyA 0:96532c59670f 318 {
AndyA 0:96532c59670f 319 // max length of unsigned long: 4294967295
AndyA 0:96532c59670f 320 if(i > 999999999) return 10;
AndyA 0:96532c59670f 321 else if(i > 99999999) return 9;
AndyA 0:96532c59670f 322 else if(i > 9999999) return 8;
AndyA 0:96532c59670f 323 else if(i > 999999) return 7;
AndyA 0:96532c59670f 324 else if(i > 99999) return 6;
AndyA 0:96532c59670f 325 else if(i > 9999) return 5;
AndyA 0:96532c59670f 326 else if(i > 999) return 4;
AndyA 0:96532c59670f 327 else if(i > 99) return 3;
AndyA 0:96532c59670f 328 else if(i > 9) return 2;
AndyA 0:96532c59670f 329 else return 1;
AndyA 0:96532c59670f 330 }
AndyA 0:96532c59670f 331 int plotly::len_(char *i)
AndyA 0:96532c59670f 332 {
AndyA 0:96532c59670f 333 return strlen(i);
AndyA 0:96532c59670f 334 }
AndyA 0:96532c59670f 335 void plotly::plot(unsigned long x, int y, char *token)
AndyA 0:96532c59670f 336 {
AndyA 0:96532c59670f 337 reconnectStream();
AndyA 0:96532c59670f 338 jsonStart(len_(x)+len_(y));
AndyA 0:96532c59670f 339 print_(x);
AndyA 0:96532c59670f 340 jsonMiddle();
AndyA 0:96532c59670f 341 print_(y);
AndyA 0:96532c59670f 342 jsonEnd(token);
AndyA 0:96532c59670f 343 }
AndyA 0:96532c59670f 344
AndyA 0:96532c59670f 345 void plotly::plot(unsigned long x, float y, char *token)
AndyA 0:96532c59670f 346 {
AndyA 0:96532c59670f 347 reconnectStream();
AndyA 0:96532c59670f 348
AndyA 0:96532c59670f 349 char s_[15];
AndyA 0:96532c59670f 350 snprintf(s_,15,"%2.3lf",y);
AndyA 0:96532c59670f 351
AndyA 0:96532c59670f 352 jsonStart(len_(x)+len_(s_)-1);
AndyA 0:96532c59670f 353 print_(x);
AndyA 0:96532c59670f 354 jsonMiddle();
AndyA 0:96532c59670f 355 print_(y);
AndyA 0:96532c59670f 356 jsonEnd(token);
AndyA 0:96532c59670f 357 }
AndyA 0:96532c59670f 358
AndyA 1:d532e96fca12 359 bool plotly::print_(int d)
AndyA 0:96532c59670f 360 {
AndyA 1:d532e96fca12 361 int32_t len = snprintf(txBuffer,128,"%d",d);
AndyA 1:d532e96fca12 362 if(log_level < 2)
AndyA 1:d532e96fca12 363 fprintf(stderr,"%s",txBuffer);
AndyA 1:d532e96fca12 364 if(!dry_run) {
AndyA 1:d532e96fca12 365 int32_t sent = socket.send(txBuffer,len);
AndyA 1:d532e96fca12 366 if (sent == len)
AndyA 1:d532e96fca12 367 return true;
AndyA 1:d532e96fca12 368 else {
AndyA 1:d532e96fca12 369 fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len);
AndyA 1:d532e96fca12 370 return false;
AndyA 1:d532e96fca12 371 }
AndyA 1:d532e96fca12 372 } else
AndyA 1:d532e96fca12 373 return true;
AndyA 1:d532e96fca12 374
AndyA 0:96532c59670f 375 }
AndyA 1:d532e96fca12 376 bool plotly::print_(unsigned long d)
AndyA 0:96532c59670f 377 {
AndyA 1:d532e96fca12 378 int32_t len = snprintf(txBuffer,128,"%lu",d);
AndyA 1:d532e96fca12 379 if(log_level < 2)
AndyA 1:d532e96fca12 380 fprintf(stderr,"%s",txBuffer);
AndyA 1:d532e96fca12 381 if(!dry_run) {
AndyA 1:d532e96fca12 382 int32_t sent = socket.send(txBuffer,len);
AndyA 1:d532e96fca12 383 if (sent == len)
AndyA 1:d532e96fca12 384 return true;
AndyA 1:d532e96fca12 385 else {
AndyA 1:d532e96fca12 386 fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len);
AndyA 1:d532e96fca12 387 return false;
AndyA 1:d532e96fca12 388 }
AndyA 1:d532e96fca12 389 } else
AndyA 1:d532e96fca12 390 return true;
AndyA 0:96532c59670f 391 }
AndyA 1:d532e96fca12 392 bool plotly::print_(float d)
AndyA 0:96532c59670f 393 {
AndyA 1:d532e96fca12 394 int32_t len = snprintf(txBuffer,128,"%f",d);
AndyA 1:d532e96fca12 395 if(log_level < 2)
AndyA 1:d532e96fca12 396 fprintf(stderr,"%s",txBuffer);
AndyA 1:d532e96fca12 397 if(!dry_run) {
AndyA 1:d532e96fca12 398 int32_t sent = socket.send(txBuffer,len);
AndyA 1:d532e96fca12 399 if (sent == len)
AndyA 1:d532e96fca12 400 return true;
AndyA 1:d532e96fca12 401 else {
AndyA 1:d532e96fca12 402 fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len);
AndyA 1:d532e96fca12 403 return false;
AndyA 1:d532e96fca12 404 }
AndyA 1:d532e96fca12 405 } else
AndyA 1:d532e96fca12 406 return true;
AndyA 1:d532e96fca12 407 }
AndyA 1:d532e96fca12 408 bool plotly::print_(char *d)
AndyA 1:d532e96fca12 409 {
AndyA 1:d532e96fca12 410 int32_t len = snprintf(txBuffer,128,"%s",d);
AndyA 1:d532e96fca12 411 if(log_level < 2)
AndyA 1:d532e96fca12 412 fprintf(stderr,"%s",txBuffer);
AndyA 1:d532e96fca12 413 if(!dry_run) {
AndyA 1:d532e96fca12 414 int32_t sent = socket.send(txBuffer,len);
AndyA 1:d532e96fca12 415 if (sent == len)
AndyA 1:d532e96fca12 416 return true;
AndyA 1:d532e96fca12 417 else {
AndyA 1:d532e96fca12 418 fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len);
AndyA 1:d532e96fca12 419 return false;
AndyA 1:d532e96fca12 420 }
AndyA 1:d532e96fca12 421 } else
AndyA 1:d532e96fca12 422 return true;
AndyA 0:96532c59670f 423 }
AndyA 0:96532c59670f 424
AndyA 1:d532e96fca12 425 bool plotly::printHex_(uint16_t d)
AndyA 0:96532c59670f 426 {
AndyA 1:d532e96fca12 427 int32_t len = snprintf(txBuffer,128,"%X",d);
AndyA 1:d532e96fca12 428 if(log_level < 2)
AndyA 1:d532e96fca12 429 fprintf(stderr,"%s",txBuffer);
AndyA 1:d532e96fca12 430 if(!dry_run) {
AndyA 1:d532e96fca12 431 int32_t sent = socket.send(txBuffer,len);
AndyA 1:d532e96fca12 432 if (sent == len)
AndyA 1:d532e96fca12 433 return true;
AndyA 1:d532e96fca12 434 else {
AndyA 1:d532e96fca12 435 fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len);
AndyA 1:d532e96fca12 436 return false;
AndyA 1:d532e96fca12 437 }
AndyA 1:d532e96fca12 438 } else
AndyA 1:d532e96fca12 439 return true;
AndyA 0:96532c59670f 440 }