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 11:45:09 2014 +0000
Revision:
6:e57d6e9313f4
Parent:
3:967be3d46701
Child:
7:9409a72ab6c0
Added support for multiple traces;

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