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.

plotly.h

Committer:
AndyA
Date:
2014-07-28
Revision:
5:fc8eefeb301b
Parent:
4:33006c37c633
Child:
7:9409a72ab6c0

File content as of revision 5:fc8eefeb301b:

#ifndef plotly_streaming_ethernet_h
#define plotly_streaming_ethernet_h

#include <EthernetInterface.h>
#include <TCPSocketConnection.h>

// size of the large buffer used for constructing messages.
#define k_bufferSize 400


/** 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:
    /**
    @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();

        /**
        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);

        /**
        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(float x, float y);

        /**
        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 printNetTerminator_();
 
        
        bool sendFormatedText(char* data, int size);

        char buffer[512];
//        char rxBuffer[128];
        TCPSocketConnection *socket;
        
        char *username_;
        char *api_key_;
        char *stream_token_;
        char *filename_;
        
        bool initalised;

};
#endif