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:
Tue Jul 29 13:43:31 2014 +0000
Revision:
8:d4f705ba2ea5
Parent:
7:9409a72ab6c0
Documentation tidy up and fix a stupid error in the merge between the documented and the multi-line branches.

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