Skip to content

Commit

Permalink
Update, now with overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni [dacav] Simoni committed Jun 14, 2010
1 parent f136d3d commit b705ae5
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 10 deletions.
33 changes: 31 additions & 2 deletions src/gnup/gp_base.hpp
Expand Up @@ -65,7 +65,7 @@ namespace gnup {
* data sequence for the 'plot' command.
*/
virtual void writePlotting (Comm *c, DataSet::iterator begin,
DataSet::iterator end) = 0;
DataSet::iterator end) = 0;

const char * getTitle ();

Expand All @@ -86,11 +86,31 @@ namespace gnup {

void writeStyle (Comm *c);

/** Enables or disables the auto-update function
*
* The function allows to enable or disable the real-time
* plotting behaviour by enabling or disabling the trigger
* when the addVector() method is called.
*
* By default the class has a real-time behaviour.
*
* @param au Auto-update flag, true for real-time behaviour,
* false for offline behaviour.
*/
void setAutoUpdate (bool au);

/** Used by a GnuPlot instance in order to get the plotting
* format for the 'plot' command.
*/
virtual void writeFormat (Comm *c) = 0;

/** Set the maximum length of the internal data list.
*
* @param max The max length. Set it to 0 in order to keep all
* data.
*/
void setOverflow (size_t max);

protected:
Plot (const char *title);

Expand All @@ -105,12 +125,15 @@ namespace gnup {
*/
void addVector (float *vals);

DataSet data;

private:

const char *title;
Trigger *trigger;
DataSet data;
style_t style;
bool auto_update;
size_t max_size;

};

Expand All @@ -123,8 +146,14 @@ namespace gnup {
void trig ();
void addSource (Plot &src) throw (PlotError);

void setXLabel (const char *label);
void setYLabel (const char *label);
void setZLabel (const char *label);

private:

void setLabel (char which, const char *label);

std::list<Plot *> sources;
size_t dimensions;

Expand Down
55 changes: 55 additions & 0 deletions src/gnup/plots.hpp
@@ -0,0 +1,55 @@
#ifndef __defined_gnup_plots_hpp
#define __defined_gnup_plots_hpp

#include <gnup/gp_base.hpp>
#include <gnup/except.hpp>

namespace gnup {

enum axis_t { DATA, AUTO };

class Plot2D : public Plot {

public:
Plot2D (const char *title, axis_t x, axis_t y);

void writePlotting (Comm *c, DataSet::iterator begin,
DataSet::iterator end);

size_t getDimension ();

void addVector (float x, float y);

void writeFormat (Comm *c);

private:
struct { axis_t x, y; } coords;
uint8_t realsize;

};

class Plot3D : public Plot {

public:
Plot3D (const char *title, axis_t x, axis_t y, axis_t z);

void writePlotting (Comm *c, DataSet::iterator begin,
DataSet::iterator end);

size_t getDimension ();

void addVector (float x, float y, float z);

void writeFormat (Comm *c);

private:
struct { axis_t x, y, z; } coords;

uint8_t realsize;

};

}

#endif // __defined_gnup_plots_hpp

37 changes: 36 additions & 1 deletion src/gp_base.cpp
Expand Up @@ -9,6 +9,8 @@ namespace gnup {
trigger = NULL;
title = tit;
style = LINES;
auto_update = true;
max_size = -1;
}

Plot::~Plot ()
Expand All @@ -28,7 +30,10 @@ namespace gnup {
memcpy((void *)v, (void *)vals, n * sizeof(float));

data.push_back(v);
if (trigger) {
if (max_size && data.size() > max_size) {
data.pop_front();
}
if (auto_update && trigger) {
trigger->trig();
}
}
Expand Down Expand Up @@ -89,6 +94,16 @@ namespace gnup {
c->command("with %s ", sn);
}

void Plot::setAutoUpdate (bool au)
{
auto_update = au;
}

void Plot::setOverflow (size_t max)
{
max_size = max;
}

GnuPlot::GnuPlot (size_t dims, const char *prog) throw (CommError)
: Comm(prog, true)
{
Expand Down Expand Up @@ -170,5 +185,25 @@ namespace gnup {
}
}

void GnuPlot::setXLabel (const char *label)
{
setLabel('x', label);
}

void GnuPlot::setYLabel (const char *label)
{
setLabel('y', label);
}

void GnuPlot::setZLabel (const char *label)
{
setLabel('z', label);
}

void GnuPlot::setLabel (char which, const char *label)
{
command("set %clabel \"%s\"", which, label);
}

}

21 changes: 14 additions & 7 deletions src/main.cpp
Expand Up @@ -9,20 +9,27 @@ using namespace gnup;
int main (int argc, char **argv)
{
try {
GnuPlot gplot(2, argc > 1 ? argv[1] : "gnuplot");
Plot2D p("foo", DATA, DATA);
GnuPlot gp(2);

Plot2D p("foo", AUTO, DATA);
p.setStyle(Plot::LINESPOINTS);

gplot.addSource(p);
gp.addSource(p);
//p.setAutoUpdate(true);
p.setOverflow(200);

for (int i = 0; i < 100; i ++) {
p.addVector(100 * i, ((float )rand()) / RAND_MAX);
usleep(250e3);
for (int i = 0; i < 1000; i ++) {
p.addVector(0, ((float )rand()) / RAND_MAX);
usleep(5e3);
}
printf("Ok, finished!\n");

gp.trig();

sleep(100);

} catch (gnup::CommError &err) {
perror(err.what());
throw;
}
}
}
99 changes: 99 additions & 0 deletions src/plots.cpp
@@ -0,0 +1,99 @@
#include <gnup.hpp>

namespace gnup {

Plot2D::Plot2D (const char *title, axis_t x, axis_t y)
: Plot(title)
{
coords.x = x;
coords.y = y;

realsize = 0;
if (x == DATA) realsize ++;
if (y == DATA) realsize ++;
}

void Plot2D::writePlotting (Comm *c, DataSet::iterator b,
DataSet::iterator e)
{
// Alternative (incremental) value for the y axis (if not
// specified).
unsigned alt_y = 0;

while (b != e) {
const float *vals = *b;

if (coords.x == AUTO) {
c->command("%f\n", coords.y == DATA ? vals[1] : alt_y ++);
} else {
c->command("%f %f\n",
vals[0], coords.y == DATA ? vals[1] : alt_y ++);
}

b ++;
}
}

size_t Plot2D::getDimension ()
{
return 2;
}

void Plot2D::addVector (float x, float y)
{
float v[] = {x, y};
Plot::addVector(v);
}

void Plot2D::writeFormat (Comm *c)
{
}

Plot3D::Plot3D (const char *title, axis_t x, axis_t y, axis_t z)
: Plot(title)
{
coords.x = x;
coords.y = y;
coords.z = z;

realsize = 0;
if (x == DATA) realsize ++;
if (y == DATA) realsize ++;
if (z == DATA) realsize ++;
}

size_t Plot3D::getDimension ()
{
return 3;
}

void Plot3D::addVector (float x, float y, float z)
{
float v[] = {x, y, z};
Plot::addVector(v);
}

void Plot3D::writePlotting (Comm *c, DataSet::iterator b,
DataSet::iterator e)
{
// Alternative (incremental) values for the x and y axis (if not
// specified).
unsigned alt_x = 0, alt_y = 0, alt_z = 0;

while (b != e) {
const float *vals = *b;

c->command("%f %f %f\n",
coords.x == DATA ? vals[0] : alt_x ++,
coords.y == DATA ? vals[1] : alt_y ++,
coords.z == DATA ? vals[2] : alt_z ++);
b ++;
}
}

void Plot3D::writeFormat (Comm *c)
{
}

}

16 changes: 16 additions & 0 deletions src/testecho
@@ -0,0 +1,16 @@
#!/usr/bin/env python

import sys

def main (argv=None):
if not argv: args = sys.argv
try:
while 1:
print >> sys.stderr, raw_input()
except:
pass
return 0

if __name__ == '__main__':
sys.exit(main())

0 comments on commit b705ae5

Please sign in to comment.