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.

Revision:
4:33006c37c633
Parent:
2:d53d74ed68ac
Child:
5:fc8eefeb301b
--- a/plotly.h	Fri Jul 11 08:08:06 2014 +0000
+++ b/plotly.h	Fri Jul 11 10:10:16 2014 +0000
@@ -4,89 +4,138 @@
 #include <EthernetInterface.h>
 #include <TCPSocketConnection.h>
 
-
-/*********************************
-in main.cpp
-EthernetInterface eth;
-#define num_traces 1
-// Sign up to plotly here: https://plot.ly
-// View your API key and stream tokens in your settings: https://plot.ly/settings
-char *streaming_tokens[num_traces] = {"leofdu7cun"};
-plotly graph = plotly("AndyA", "acchq52p7j", streaming_tokens, "test1", num_traces);
-
-void plotSetup() {
-
-//    eth.init();
-//    eth.connect();
-
-  // Initialize a streaming graph in your plotly account
-    pc.printf("plot init..\n");
-  graph.init();
-    pc.printf("Open stream..\n");
-  // Initialize plotly's streaming service
-  graph.openStream(); 
-}
-
-void plotGenerateData() {
-  static int counter = 0;
-  graph.plot(counter, counter*counter, streaming_tokens[0]);
-  counter++;
-}
+// size of the large buffer used for constructing messages.
+#define k_bufferSize 400
 
 
-********************************************/
-
-#define k_bufferSize 512
+/** Create a plot on plot.ly
+*
+* Based on the Ardunio code supplied by plot.ly
+*
+* Creates a streaming X/Y scatter plot with line on the plot.ly site.
+* Update periods can be between 50ms and 60s due to limitations imposed by plot.ly.
+* 
+* Requires an mbed with network support.
+* 
+* Provided as is, it works for me but your mileage may vary. Sorry, I don't have time to offer much support on this.
+* 
+* You will need to create a plot.ly account and then go to https://plot.ly/settings to get your API key and a streaming token.
+* 
+* See Plotly_HelloWorld for a sample implimentation.
+* 
+*/
 
 class plotly
 {
     public:
-        plotly(char *username, char *api_key, char* stream_tokens[], char *filename, int nTraces);
+    /**
+    @param username Your plot.ly username
+    @param api_key Your plot.ly API key
+    @param stream_token A plot.ly streaming token for your plot.ly account
+    @param filename The name of the file to save the chart as
+    */
+        plotly(char *username, char *api_key, char* stream_token, char *filename);
+        /**
+        */
         ~plotly();
+        
+        /** Initalises the chart
+        
+        This fucntion creates a blank chart on the plot.ly system and configures it to recieve streamed data using the specified token
+        
+        Time taken for this function can vary depending on network delays.
+        
+        If you wish to change any of the options line max points or world readability then make sure you change them BEFORE calling this function.       
+        */
         bool init();
-        void openStream();
-        void closeStream();
-        void reconnectStream();
-        void jsonStart(int i);
-        void jsonMiddle();
-        void jsonEnd(char *token);
+
+        /**
+        Adds a point to the chart. The chart MUST be initalised before calling this.
+        Note, if the streaming network port is closed then this will attempt to open the port and re-establish the stream connection, this could block for a while.
+        
+        @param x The X value.
+        @param y The y value.
+        */
+        void plot(unsigned long x, int y);
+        /**
+        Adds a point to the chart. The chart MUST be initalised before calling this.
+        Note, if the streaming network port is closed then this will attempt to open the port and re-establish the stream connection, this could block for a while.
+        
+        @param x The X value.
+        @param y The y value.
+        */
+        void plot(unsigned long x, float y);
 
-        void plot(unsigned long x, int y, char *token);
-        void plot(unsigned long x, float y, char *token);
+        /**
+        Opens the streaming connection.
+        
+        Normally you'd do this after a sucessful call to init() and before starting plotting in order to avoid a connection delays when you first call plot()
+        */
+        bool openStream();
+        /**
+        closes the streaming connection
+        */
+        void closeStream();
 
+        /**
+         output message level
+         Messages are sent to stderr (which defaults to the mBed USB programing/debug port).
+          0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors (default), 4 = Quiet
+          */
         int log_level;
+
+        /**
+        set true to not actually connect to the network..
+        */
         bool dry_run;
+
+        /**
+        Maximum points to display on the streaming chart, once you go over this old points will no longer be displayed.
+        Defaults to 30
+        */
         int maxpoints;
+        
+        /**
+        Sets whether the chart will be public or not.
+        Defaults to true
+        */
         bool world_readable;
+        
+        /**
+        Converts timestamps to the local time zone
+        */
         bool convertTimestamp;
+        /**
+        Timezone string to use
+        */
         char *timezone;
+        /**
+        Sets what to do if the file already exists, valid options are:
+        "new","overwrite" (default),"append","extend"
+        */
         char *fileopt;
 
     private:
+
+        void reconnectStream();
+
         bool print_(int d);
         bool print_(unsigned long d);
         bool print_(float d);
         bool print_(char *d);
-        bool printHex_(uint16_t d);
+        bool printNetTerminator_();
+ 
         
         bool sendFormatedText(char* data, int size);
-        void echoRxData();
-
-
-        int len_(int i);
-        int len_(unsigned long i);
-        int len_(char *i);
 
         char buffer[512];
 //        char rxBuffer[128];
         TCPSocketConnection *socket;
         
-        unsigned long fibonacci_;
         char *username_;
         char *api_key_;
-        char** stream_tokens_;
+        char *stream_token_;
         char *filename_;
-        int nTraces_;
         
         bool initalised;