From 9f7ae923f7bec5ea7dc4f32d4c12fe73f8d30650 Mon Sep 17 00:00:00 2001 From: "Devin A. Conley" Date: Sun, 28 Jun 2020 13:55:12 -0500 Subject: [PATCH 1/5] clean up style and formatting --- .clang-format | 25 ++ README.md | 8 +- .../control_points_displayed.ino | 23 +- examples/different_types/different_types.ino | 21 +- .../dynamically_modify_graph_layout.ino | 45 +- .../multivariable_plotting.ino | 26 +- examples/quick_start/quick_start.ino | 13 +- examples/set_colors/set_colors.ino | 24 +- src/Plotter.cpp | 328 +++++++------- src/Plotter.h | 402 +++++++++++------- 10 files changed, 538 insertions(+), 377 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b82802b --- /dev/null +++ b/.clang-format @@ -0,0 +1,25 @@ +Language: Cpp + +IndentWidth: 4 +UseTab: Never +SpacesInParentheses: true +ColumnLimit: 100 + +BreakBeforeBraces: Allman +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AlignAfterOpenBracket: AlwaysBreak +BinPackArguments: false +BinPackParameters: false + +DerivePointerAlignment: false +PointerAlignment: Left + +Cpp11BracedListStyle: false + +AllowAllConstructorInitializersOnNextLine: true +BreakConstructorInitializers: BeforeColon +ConstructorInitializerAllOnOneLineOrOnePerLine: true + +AllowShortFunctionsOnASingleLine: Empty diff --git a/README.md b/README.md index 0950c5e..c814994 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,16 @@ Plotter p; // create plotter void setup() { - p.Begin(); // start plotter + p.begin(); // start plotter - p.AddTimeGraph( "Some title of a graph", 1500, "label for x", x ); // add any graphs you want + p.addTimeGraph( "Some title of a graph", 1500, "label for x", x ); // add any graphs you want } void loop() { - x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); // update your variables like usual + x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); // update your variables like usual - p.Plot(); // plot all current data -- usually called within loop() + p.plot(); // plot all current data -- usually called within loop() } ``` diff --git a/examples/control_points_displayed/control_points_displayed.ino b/examples/control_points_displayed/control_points_displayed.ino index e46416b..4beb4da 100644 --- a/examples/control_points_displayed/control_points_displayed.ino +++ b/examples/control_points_displayed/control_points_displayed.ino @@ -11,7 +11,7 @@ #include "Plotter.h" -// Plotted variables must be declared as globals +// Plotted variables must be declared as globals double x; double y; @@ -21,24 +21,23 @@ Plotter p; void setup() { // Start plotter - p.Begin(); - - // Add X-Y graphs - p.AddXYGraph( "X-Y graph w/ 500 points", 500, "x axis", x, "y axis", y ); - p.AddXYGraph( "X-Y graph w/ 200 points", 200, "x axis", x, "y axis", y ); + p.begin(); - // Add time graphs. Notice the effect of points displayed on the time scale - p.AddTimeGraph( "Time graph w/ 500 points", 500, "x label", x ); - p.AddTimeGraph( "Time graph w/ 200 points", 200, "x label", x ); + // Add X-Y graphs + p.addXYGraph( "X-Y graph w/ 500 points", 500, "x axis", x, "y axis", y ); + p.addXYGraph( "X-Y graph w/ 200 points", 200, "x axis", x, "y axis", y ); + // Add time graphs. Notice the effect of points displayed on the time scale + p.addTimeGraph( "Time graph w/ 500 points", 500, "x label", x ); + p.addTimeGraph( "Time graph w/ 200 points", 200, "x label", x ); } void loop() { // Update variables with arbitrary sine/cosine data - x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); - y = 10*cos( 2.0*PI*( millis() / 5000.0 ) ); + x = 10 * sin( 2.0 * PI * ( millis() / 5000.0 ) ); + y = 10 * cos( 2.0 * PI * ( millis() / 5000.0 ) ); // Plot - p.Plot(); + p.plot(); } diff --git a/examples/different_types/different_types.ino b/examples/different_types/different_types.ino index 8bce765..00292f2 100644 --- a/examples/different_types/different_types.ino +++ b/examples/different_types/different_types.ino @@ -11,7 +11,7 @@ #include "Plotter.h" -// Plotted variables must be declared as globals +// Plotted variables must be declared as globals float f; float f2; int i; @@ -23,23 +23,22 @@ Plotter p; void setup() { // Start plotter - p.Begin(); - - // Add time graphs. - p.AddTimeGraph( "float vs int", 500, "float", f, "int", i ); - p.AddTimeGraph( "float vs char", 500, "float", f2, "unsigned char", ch ); + p.begin(); + // Add time graphs. + p.addTimeGraph( "float vs int", 500, "float", f, "int", i ); + p.addTimeGraph( "float vs char", 500, "float", f2, "unsigned char", ch ); } void loop() { // Update different variable types with arbitrary sine/cosine data unsigned long time = millis(); - f = 5*sin( 2.0*PI*( time / 5000.0 ) ); - i = 5*sin( 2.0*PI*( time / 5000.0 ) ); - f2 = 300*sin( 2.0*PI*( time / 5000.0 ) ); - ch = 300*sin( 2.0*PI*( time / 5000.0 ) ); + f = 5 * sin( 2.0 * PI * ( time / 5000.0 ) ); + i = 5 * sin( 2.0 * PI * ( time / 5000.0 ) ); + f2 = 300 * sin( 2.0 * PI * ( time / 5000.0 ) ); + ch = 300 * sin( 2.0 * PI * ( time / 5000.0 ) ); // Plot - p.Plot(); + p.plot(); } diff --git a/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino b/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino index cda06e1..df3debf 100644 --- a/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino +++ b/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino @@ -11,7 +11,7 @@ #include "Plotter.h" -// Plotted variables must be declared as globals +// Plotted variables must be declared as globals double x; double y; double z; @@ -27,37 +27,40 @@ boolean remove_graph = true; void setup() { // Plotter constructor - p.Begin(); - + p.begin(); + // Add a graph during setup - p.AddTimeGraph( "x and y against time", 1000, "x label", x, "y label", y ); + p.addTimeGraph( "x and y against time", 1000, "x label", x, "y label", y ); } void loop() { // After 3 seconds add a 1-variable graph - if ( millis() > 3000 && add_first ) { - p.AddTimeGraph( "First dynamic addition", 1000, "z label", z ); - add_first = false; + if ( millis() > 3000 && add_first ) + { + p.addTimeGraph( "First dynamic addition", 1000, "z label", z ); + add_first = false; } // After 5 seconds, add a 3-variable graph - if ( millis() > 5000 && add_second ) { - p.AddTimeGraph( "Second dynamic addition", 1000, "x label", x, "y label", y, "z label", z ); - add_second = false; + if ( millis() > 5000 && add_second ) + { + p.addTimeGraph( "Second dynamic addition", 1000, "x label", x, "y label", y, "z label", z ); + add_second = false; } // After 10 seconds, remove the 1-variable graph - if ( millis() > 10000 && remove_graph ) { - p.Remove( 1 ); // (an index of 1 will remove the second graph added) - remove_graph = false; + if ( millis() > 10000 && remove_graph ) + { + p.remove( 1 ); // (an index of 1 will remove the second graph added) + remove_graph = false; } - + // Update x, y and z variables with arbitrary sine/cosine data - x = 3*sin( 2.0*PI*( millis() / 2000.0 ) ); - y = 2*cos( 2.0*PI*( millis() / 2000.0 ) ); - z = 1*sin( 2.0*PI*( millis() / 2000.0 ) ); - - // Plot - p.Plot(); -} \ No newline at end of file + x = 3 * sin( 2.0 * PI * ( millis() / 2000.0 ) ); + y = 2 * cos( 2.0 * PI * ( millis() / 2000.0 ) ); + z = 1 * sin( 2.0 * PI * ( millis() / 2000.0 ) ); + + // Plot + p.plot(); +} diff --git a/examples/multivariable_plotting/multivariable_plotting.ino b/examples/multivariable_plotting/multivariable_plotting.ino index 8d6f7e9..3a24e60 100644 --- a/examples/multivariable_plotting/multivariable_plotting.ino +++ b/examples/multivariable_plotting/multivariable_plotting.ino @@ -11,7 +11,7 @@ #include "Plotter.h" -// Plotted variables must be declared as globals +// Plotted variables must be declared as globals double v; double w; double x; @@ -24,22 +24,28 @@ Plotter p; void setup() { // Start plotter - p.Begin(); - + p.begin(); + // Add 5 variable time graph - p.AddTimeGraph( "5 variable time graph", 1000, "v label", v, "w label", w, "x label", x, - "y label", y, "z label", z ); + p.addTimeGraph( + "5 variable time graph", 1000, + "v label", v, + "w label", w, + "x label", x, + "y label", y, + "z label", z + ); } void loop() { // Update variables with arbitrary sine/cosine data - v = 3*cos( 2.0*PI*( millis()/2500.0 ) ); + v = 3 * cos( 2.0 * PI * ( millis() / 2500.0 ) ); w = 4.0; - x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); - y = 7*cos( 2.0*PI*( millis() / 5000.0 ) ); - z = 5*sin( 2.0*PI*( millis() / 5000.0 ) ); + x = 10 * sin( 2.0 * PI * ( millis() / 5000.0 ) ); + y = 7 * cos( 2.0 * PI * ( millis() / 5000.0 ) ); + z = 5 * sin( 2.0 * PI * ( millis() / 5000.0 ) ); // Plot - p.Plot(); + p.plot(); } diff --git a/examples/quick_start/quick_start.ino b/examples/quick_start/quick_start.ino index 548b894..28e1baf 100644 --- a/examples/quick_start/quick_start.ino +++ b/examples/quick_start/quick_start.ino @@ -17,13 +17,14 @@ Plotter p; void setup() { - p.Begin(); + p.begin(); - p.AddTimeGraph( "Some title of a graph", 1500, "label for x", x ); + p.addTimeGraph( "Some title of a graph", 1500, "label for x", x ); } -void loop() { - x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); +void loop() +{ + x = 10 * sin( 2.0 * PI * ( millis() / 5000.0 ) ); - p.Plot(); // usually called within loop() -} \ No newline at end of file + p.plot(); // usually called within loop() +} diff --git a/examples/set_colors/set_colors.ino b/examples/set_colors/set_colors.ino index 018a886..7463dbb 100644 --- a/examples/set_colors/set_colors.ino +++ b/examples/set_colors/set_colors.ino @@ -1,6 +1,6 @@ /* =========================================================================================== - Example to demonstrate configuring specific colors for each variable + Example to demonstrate configuring specific colors for each variable ------------------------------------------------------------------------------------------- Plotter v2.4.0 @@ -19,22 +19,22 @@ Plotter p; void setup() { // Start plotter - p.Begin(); + p.begin(); // Add plots - p.AddTimeGraph( "Some title of a graph", 500, "label for x", x, "label for y", y ); - p.AddXYGraph( "Title of X vs Y graph", 1000, "x axis", x, "y axis", y ); + p.addTimeGraph( "Some title of a graph", 500, "label for x", x, "label for y", y ); + p.addXYGraph( "Title of X vs Y graph", 1000, "x axis", x, "y axis", y ); // Set variable colors of graph with index 0 to pink and orange - p.SetColor( 0, "pink", "orange" ); + p.setColor( 0, "pink", "orange" ); // Set color of x vs y graph at index 1 to cyan - p.SetColor( 1, "cyan" ); - + p.setColor( 1, "cyan" ); } -void loop() { - x = 0.0009*sin( 2.0*PI*( millis() / 5000.0 ) ); - y = 90000*cos( 2.0*PI*( millis() / 5000.0 ) ); +void loop() +{ + x = 0.0009 * sin( 2.0 * PI * ( millis() / 5000.0 ) ); + y = 90000 * cos( 2.0 * PI * ( millis() / 5000.0 ) ); - p.Plot(); // usually called within loop() -} \ No newline at end of file + p.plot(); // usually called within loop() +} diff --git a/src/Plotter.cpp b/src/Plotter.cpp index 2755a74..0ff9dc1 100644 --- a/src/Plotter.cpp +++ b/src/Plotter.cpp @@ -1,18 +1,18 @@ /* =========================================================================================== Plotter is an Arduino library that allows easy multi-variable and multi-graph plotting. The - library supports plots against time as well as 2-variable "X vs Y" graphing. + library supports plots against time as well as 2-variable "X vs Y" graphing. ------------------------------------------------------------------------------------------- - The library stores and handles all relevant graph information and variable references, + The library stores and handles all relevant graph information and variable references, and transfers information via the serial port to a listener program written with the software provided by Processing. No modification is needed to this program; graph placement, - axis-scaling, etc. are handled automatically. - Multiple options for this listener are available including stand-alone applications as well + axis-scaling, etc. are handled automatically. + Multiple options for this listener are available including stand-alone applications as well as the source Processing script. - The library, these listeners, a quick-start guide, documentation, and usage examples are + The library, these listeners, a quick-start guide, documentation, and usage examples are available at: - + https://github.com/devinaconley/arduino-plotter ------------------------------------------------------------------------------------------- @@ -36,276 +36,308 @@ Plotter::Plotter() lastUpdated = millis(); } -void Plotter::Begin() +void Plotter::begin() { Serial.begin( 115200 ); lastUpdated = millis(); } Plotter::~Plotter() -{ - Graph * temp = head; - Graph * tempNext; +{ + Graph* temp = head; + Graph* tempNext; while ( temp->next ) { - tempNext = temp->next; - delete temp; - temp = tempNext; + tempNext = temp->next; + delete temp; + temp = tempNext; } delete temp; } -void Plotter::AddGraphHelper( const char * title, VariableWrapper * wrappers, int sz, bool xvy, int pointsDisplayed ) -{ - Graph * temp = new Graph( title, wrappers, sz, xvy, pointsDisplayed ); +void Plotter::addGraphHelper( + const char* title, + VariableWrapper* wrappers, + int sz, + bool xvy, + int pointsDisplayed ) +{ + Graph* temp = new Graph( title, wrappers, sz, xvy, pointsDisplayed ); if ( head ) { - tail->next = temp; - tail = temp; + tail->next = temp; + tail = temp; } else { - head = temp; - tail = temp; + head = temp; + tail = temp; } - - numGraphs++; + + numGraphs++; lastUpdated = millis(); } -bool Plotter::Remove( int index ) +bool Plotter::remove( int index ) { if ( numGraphs == 0 || index < 0 || numGraphs <= index ) { - return false; + return false; } else { - Graph * temp = head; - if ( index == 0 ) - { - head = head->next; - delete temp; - } - else - { - Graph * last = temp; - for ( int i = 0; i < index; i++ ) - { - last = temp; - temp = temp->next; - } - last->next = temp->next; - numGraphs--; - delete temp; - } - lastUpdated = millis(); - return true; + Graph* temp = head; + if ( index == 0 ) + { + head = head->next; + delete temp; + } + else + { + Graph* last = temp; + for ( int i = 0; i < index; i++ ) + { + last = temp; + temp = temp->next; + } + last->next = temp->next; + numGraphs--; + delete temp; + } + lastUpdated = millis(); + return true; } } -bool Plotter::SetColor( int index, const char * colorA ) +bool Plotter::setColor( int index, const char* colorA ) { - const char * colors[] = { colorA }; - return SetColorHelper( index, 1, colors ); + const char* colors[] = { colorA }; + return setColorHelper( index, 1, colors ); } -bool Plotter::SetColor( int index, const char * colorA, const char * colorB ) +bool Plotter::setColor( int index, const char* colorA, const char* colorB ) { - const char * colors[] = { colorA, colorB }; - return SetColorHelper( index, 2, colors ); + const char* colors[] = { colorA, colorB }; + return setColorHelper( index, 2, colors ); } -bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC ) +bool Plotter::setColor( int index, const char* colorA, const char* colorB, const char* colorC ) { - const char * colors[] = { colorA, colorB, colorC }; - return SetColorHelper( index, 3, colors ); + const char* colors[] = { colorA, colorB, colorC }; + return setColorHelper( index, 3, colors ); } -bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC, - const char * colorD ) +bool Plotter::setColor( + int index, + const char* colorA, + const char* colorB, + const char* colorC, + const char* colorD ) { - const char * colors[] = { colorA, colorB, colorC, colorD }; - return SetColorHelper( index, 4, colors ); + const char* colors[] = { colorA, colorB, colorC, colorD }; + return setColorHelper( index, 4, colors ); } -bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC, - const char * colorD, const char * colorE ) +bool Plotter::setColor( + int index, + const char* colorA, + const char* colorB, + const char* colorC, + const char* colorD, + const char* colorE ) { - const char * colors[] = { colorA, colorB, colorC, colorD, colorE }; - return SetColorHelper( index, 5, colors ); + const char* colors[] = { colorA, colorB, colorC, colorD, colorE }; + return setColorHelper( index, 5, colors ); } -bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC, - const char * colorD, const char * colorE, const char * colorF ) +bool Plotter::setColor( + int index, + const char* colorA, + const char* colorB, + const char* colorC, + const char* colorD, + const char* colorE, + const char* colorF ) { - const char * colors[] = { colorA, colorB, colorC, colorD, colorE, colorF }; - return SetColorHelper( index, 6, colors ); + const char* colors[] = { colorA, colorB, colorC, colorD, colorE, colorF }; + return setColorHelper( index, 6, colors ); } -bool Plotter::SetColorHelper( int index, int sz, const char * * colors ) +bool Plotter::setColorHelper( int index, int sz, const char** colors ) { if ( numGraphs == 0 || index < 0 || numGraphs <= index ) { - return false; + return false; } - Graph * temp = head; + Graph* temp = head; for ( int i = 0; i < index; i++ ) { - temp = temp->next; + temp = temp->next; } - bool res = temp->SetColor( sz, colors ); + bool res = temp->setColor( sz, colors ); if ( res ) { - lastUpdated = millis(); + lastUpdated = millis(); } return res; } -void Plotter::Plot() +void Plotter::plot() { bool config = counter == 0; - Serial.print( "{\"" ); Serial.print( TIME_KEY ); Serial.print( "\":" ); Serial.print( millis() ); - + Serial.print( "{\"" ), Serial.print( TIME_KEY ); + Serial.print( "\":" ), Serial.print( millis() ); + if ( config ) { - Serial.print( ",\"" ); Serial.print( NUM_GRAPH_KEY ); Serial.print( "\":" ); Serial.print( numGraphs ); - Serial.print( ",\"" ); Serial.print( LAST_UPDATED_KEY ); Serial.print( "\":" ); Serial.print( lastUpdated ); + Serial.print( ",\"" ), Serial.print( NUM_GRAPH_KEY ); + Serial.print( "\":" ), Serial.print( numGraphs ); + Serial.print( ",\"" ), Serial.print( LAST_UPDATED_KEY ); + Serial.print( "\":" ), Serial.print( lastUpdated ); } - - Serial.print( ",\"" ); Serial.print( GRAPHS_KEY ); Serial.print( "\":[" ); - Graph * temp = head; + Serial.print( ",\"" ), Serial.print( GRAPHS_KEY ), Serial.print( "\":[" ); + + Graph* temp = head; while ( temp ) { - temp->Plot( config ); - temp = temp->next; - if ( temp ) - { - Serial.print( "," ); - } + temp->plot( config ); + temp = temp->next; + if ( temp ) + { + Serial.print( "," ); + } } - Serial.print( "]}" ); Serial.println( OUTER_KEY ); + Serial.print( "]}" ), Serial.println( OUTER_KEY ); counter++; if ( counter >= CONFIG_INTERVAL ) { - counter = 0; + counter = 0; } } // Graph -Plotter::Graph::Graph( const char * title, VariableWrapper * wrappers, int size, bool xvy, int pointsDisplayed ) : - next( NULL ), - xvy( xvy ), - size( size ), - pointsDisplayed( pointsDisplayed ), - title( title ), - wrappers( wrappers ) -{} +Plotter::Graph::Graph( + const char* title, + VariableWrapper* wrappers, + int size, + bool xvy, + int pointsDisplayed ) + : next( NULL ), + xvy( xvy ), + size( size ), + pointsDisplayed( pointsDisplayed ), + title( title ), + wrappers( wrappers ) +{ +} Plotter::Graph::~Graph() { delete[] wrappers; } -void Plotter::Graph::Plot( bool config ) +void Plotter::Graph::plot( bool config ) { Serial.print( "{" ); if ( config ) { - Serial.print( "\"" ); Serial.print( TITLE_KEY ); Serial.print( "\":" ); Serial.print( "\"" ); Serial.print( title ); Serial.print( "\"" ); - Serial.print( ",\"" ); Serial.print( XVY_KEY ); Serial.print( "\":" ); Serial.print( xvy ); - Serial.print( ",\"" ); Serial.print( POINTS_DISPLAYED_KEY ); Serial.print( "\":" ); Serial.print( pointsDisplayed ); - Serial.print( ",\"" ); Serial.print( SIZE_KEY ); Serial.print( "\":" ); Serial.print( size ); - Serial.print( ",\"" ); Serial.print( LABELS_KEY ); Serial.print( "\":[" ); - for ( int i = 0; i < size; i++ ) - { - Serial.print( "\"" ); Serial.print( wrappers[i].GetLabel() ); Serial.print( "\"" ); - if ( i + 1 < size ) - { - Serial.print( "," ); - } - } - Serial.print( "],\"" ); Serial.print( COLORS_KEY ); Serial.print( "\":[" ); - for ( int i = 0; i < size; i++ ) - { - Serial.print( "\"" ); Serial.print( wrappers[i].GetColor() ); Serial.print( "\"" ); - if ( i + 1 < size ) - { - Serial.print( "," ); - } - } - Serial.print( "]," ); + Serial.print( "\"" ), Serial.print( TITLE_KEY ), Serial.print( "\":" ); + Serial.print( "\"" ), Serial.print( title ), Serial.print( "\"" ); + Serial.print( ",\"" ), Serial.print( XVY_KEY ); + Serial.print( "\":" ), Serial.print( xvy ); + Serial.print( ",\"" ), Serial.print( POINTS_DISPLAYED_KEY ); + Serial.print( "\":" ), Serial.print( pointsDisplayed ); + Serial.print( ",\"" ), Serial.print( SIZE_KEY ); + Serial.print( "\":" ), Serial.print( size ); + Serial.print( ",\"" ), Serial.print( LABELS_KEY ), Serial.print( "\":[" ); + for ( int i = 0; i < size; i++ ) + { + Serial.print( "\"" ), Serial.print( wrappers[i].getLabel() ), Serial.print( "\"" ); + if ( i + 1 < size ) + { + Serial.print( "," ); + } + } + Serial.print( "],\"" ), Serial.print( COLORS_KEY ), Serial.print( "\":[" ); + for ( int i = 0; i < size; i++ ) + { + Serial.print( "\"" ), Serial.print( wrappers[i].getColor() ), Serial.print( "\"" ); + if ( i + 1 < size ) + { + Serial.print( "," ); + } + } + Serial.print( "]," ); } - - Serial.print( "\"" ); Serial.print( DATA_KEY ); Serial.print( "\":[" ); + + Serial.print( "\"" ), Serial.print( DATA_KEY ), Serial.print( "\":[" ); for ( int i = 0; i < size; i++ ) { - Serial.print( wrappers[i].GetValue(), 8 ); - if ( i + 1 < size ) - { - Serial.print( "," ); - } + Serial.print( wrappers[i].getValue(), 8 ); + if ( i + 1 < size ) + { + Serial.print( "," ); + } } - + Serial.print( "]}" ); } -bool Plotter::Graph::SetColor( int sz, const char * * colors ) +bool Plotter::Graph::setColor( int sz, const char** colors ) { if ( sz != size && !xvy ) { - return false; + return false; } if ( xvy ) { - wrappers[0].SetColor( colors[0] ); + wrappers[0].setColor( colors[0] ); } else { - for ( int i = 0; i < size; i++ ) - { - wrappers[i].SetColor( colors[i] ); - } + for ( int i = 0; i < size; i++ ) + { + wrappers[i].setColor( colors[i] ); + } } return true; } // VariableWrapper -Plotter::VariableWrapper::VariableWrapper() : - ref( NULL ), - deref( NULL ) -{} +Plotter::VariableWrapper::VariableWrapper() : ref( NULL ), deref( NULL ) {} -Plotter::VariableWrapper::VariableWrapper( const char * label, void * ref, double ( * deref )( void * ), const char * color ) : - label( label ), - color( color ), - ref( ref ), - deref( deref ) -{} +Plotter::VariableWrapper::VariableWrapper( + const char* label, + void* ref, + double ( *deref )( void* ), + const char* color ) + : label( label ), color( color ), ref( ref ), deref( deref ) +{ +} -const char * Plotter::VariableWrapper::GetLabel() +const char* Plotter::VariableWrapper::getLabel() { return label; } -double Plotter::VariableWrapper::GetValue() +double Plotter::VariableWrapper::getValue() { return deref( ref ); } -const char * Plotter::VariableWrapper::GetColor() +const char* Plotter::VariableWrapper::getColor() { return color; } -void Plotter::VariableWrapper::SetColor( const char * col ) +void Plotter::VariableWrapper::setColor( const char* col ) { color = col; } diff --git a/src/Plotter.h b/src/Plotter.h index c47e230..2566371 100644 --- a/src/Plotter.h +++ b/src/Plotter.h @@ -1,18 +1,18 @@ /* =========================================================================================== Plotter is an Arduino library that allows easy multi-variable and multi-graph plotting. The - library supports plots against time as well as 2-variable "X vs Y" graphing. + library supports plots against time as well as 2-variable "X vs Y" graphing. ------------------------------------------------------------------------------------------- - The library stores and handles all relevant graph information and variable references, + The library stores and handles all relevant graph information and variable references, and transfers information via the serial port to a listener program written with the software provided by Processing. No modification is needed to this program; graph placement, - axis-scaling, etc. are handled automatically. - Multiple options for this listener are available including stand-alone applications as well + axis-scaling, etc. are handled automatically. + Multiple options for this listener are available including stand-alone applications as well as the source Processing script. - The library, these listeners, a quick-start guide, documentation, and usage examples are + The library, these listeners, a quick-start guide, documentation, and usage examples are available at: - + https://github.com/devinaconley/arduino-plotter ------------------------------------------------------------------------------------------- @@ -30,64 +30,73 @@ class Plotter { -public: + public: // The constructor for Plotter requires no arguments Plotter(); // Initialize Plotter - void Begin(); - - /* + void begin(); + + /* Add a 1-variable graph vs. time - + Args: - title: const char * with title of graph - pointsDisplayed: number of points to be shown at a given time. Used to control time-scaling - labelA: const char * with label of the plotted variable - refA: reference to global variable that will be updated throughout program - - Similar methods for multi-variable graphing vs. time are declared below and follow the same format + + Similar methods for multi-variable graphing vs. time are declared below and follow the same + format */ template - void AddTimeGraph( const char * title, int pointsDisplayed, - const char * labelA, A & refA ) + void addTimeGraph( const char* title, int pointsDisplayed, const char* labelA, A& refA ) { - VariableWrapper * wrappers = new VariableWrapper[1]; - wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); - AddGraphHelper( title, wrappers, 1, false, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[1]; + wrappers[0] = + VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); + addGraphHelper( title, wrappers, 1, false, pointsDisplayed ); } - + /* - Add an X vs. Y graph - + Add an X vs. Y graph + Args: - title: const char * with title of graph - - pointsDisplayed: number of points to be shown at a given time. Determines duration of data persistance + - pointsDisplayed: number of points to be shown at a given time. Determines duration of data + persistence - labelX: const char * with label of variable to be plotted along X-axis - refX: reference to global X-variable that will be updated throughout program - labelY: const char * with label of variable to be plotted along Y-axis - refY: reference to global Y-variable that will be updated throughout program */ template - void AddXYGraph( const char * title, int pointsDisplayed, - const char * labelX, X & refX, const char * labelY, Y & refY ) + void addXYGraph( + const char* title, + int pointsDisplayed, + const char* labelX, + X& refX, + const char* labelY, + Y& refY ) { - VariableWrapper * wrappers = new VariableWrapper[2]; - wrappers[0] = VariableWrapper( labelX, static_cast( &refX ), &Dereference, "green" ); - wrappers[1] = VariableWrapper( labelY, static_cast( &refY ), &Dereference, "green" ); - AddGraphHelper( title, wrappers, 2, true, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[2]; + wrappers[0] = + VariableWrapper( labelX, static_cast( &refX ), &dereference, "green" ); + wrappers[1] = + VariableWrapper( labelY, static_cast( &refY ), &dereference, "green" ); + addGraphHelper( title, wrappers, 2, true, pointsDisplayed ); } - - /* + + /* Plot data - - Function to be called in order to send current values of all global variables to listener application. This - function will update all plots that have been added. - It is recommended to call plot() at the end of your loop function. + Function to be called in order to send current values of all global variables to listener + application. This function will update all plots that have been added. + + It is recommended to call plot() at the end of your loop function. */ - void Plot(); - + void plot(); + /* Remove Graph @@ -96,7 +105,7 @@ class Plotter Returns: - true, if successful */ - bool Remove( int index ); + bool remove( int index ); /* Set Variable Colors @@ -107,171 +116,258 @@ class Plotter Returns: - true, if successful */ - bool SetColor( int index, const char * colorA ); - + bool setColor( int index, const char* colorA ); + // Add a 2-variable graph vs. time template - void AddTimeGraph( const char * title, int pointsDisplayed, - const char * labelA, A & refA, const char * labelB, B & refB ) + void addTimeGraph( + const char* title, + int pointsDisplayed, + const char* labelA, + A& refA, + const char* labelB, + B& refB ) { - VariableWrapper * wrappers = new VariableWrapper[2]; - wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); - wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); - AddGraphHelper( title, wrappers, 2, false, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[2]; + wrappers[0] = + VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); + wrappers[1] = + VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); + addGraphHelper( title, wrappers, 2, false, pointsDisplayed ); } // Add a 3-variable graph vs. time template - void AddTimeGraph( const char * title, int pointsDisplayed, - const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC ) + void addTimeGraph( + const char* title, + int pointsDisplayed, + const char* labelA, + A& refA, + const char* labelB, + B& refB, + const char* labelC, + C& refC ) { - VariableWrapper * wrappers = new VariableWrapper[3]; - wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); - wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); - wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); - AddGraphHelper( title, wrappers, 3, false, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[3]; + wrappers[0] = + VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); + wrappers[1] = + VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); + wrappers[2] = + VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); + addGraphHelper( title, wrappers, 3, false, pointsDisplayed ); } - + // Add a 4-variable graph vs. time template - void AddTimeGraph( const char * title, int pointsDisplayed, - const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC, - const char * labelD, D & refD ) + void addTimeGraph( + const char* title, + int pointsDisplayed, + const char* labelA, + A& refA, + const char* labelB, + B& refB, + const char* labelC, + C& refC, + const char* labelD, + D& refD ) { - VariableWrapper * wrappers = new VariableWrapper[4]; - wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); - wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); - wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); - wrappers[3] = VariableWrapper( labelD, static_cast( &refD ), &Dereference, "yellow" ); - AddGraphHelper( title, wrappers, 4, false, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[4]; + wrappers[0] = + VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); + wrappers[1] = + VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); + wrappers[2] = + VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); + wrappers[3] = + VariableWrapper( labelD, static_cast( &refD ), &dereference, "yellow" ); + addGraphHelper( title, wrappers, 4, false, pointsDisplayed ); } // Add a 5-variable graph vs. time template - void AddTimeGraph( const char * title, int pointsDisplayed, - const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC, - const char * labelD, D & refD, const char * labelE, E & refE ) + void addTimeGraph( + const char* title, + int pointsDisplayed, + const char* labelA, + A& refA, + const char* labelB, + B& refB, + const char* labelC, + C& refC, + const char* labelD, + D& refD, + const char* labelE, + E& refE ) { - VariableWrapper * wrappers = new VariableWrapper[5]; - wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); - wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); - wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); - wrappers[3] = VariableWrapper( labelD, static_cast( &refD ), &Dereference, "yellow" ); - wrappers[4] = VariableWrapper( labelE, static_cast( &refE ), &Dereference, "pink" ); - AddGraphHelper( title, wrappers, 5, false, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[5]; + wrappers[0] = + VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); + wrappers[1] = + VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); + wrappers[2] = + VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); + wrappers[3] = + VariableWrapper( labelD, static_cast( &refD ), &dereference, "yellow" ); + wrappers[4] = + VariableWrapper( labelE, static_cast( &refE ), &dereference, "pink" ); + addGraphHelper( title, wrappers, 5, false, pointsDisplayed ); } // Add a 6-variable graph vs. time - template - void AddTimeGraph( const char * title, int pointsDisplayed, - const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC, - const char * labelD, D & refD, const char * labelE, E & refE, const char * labelF, F & refF ) + template + void addTimeGraph( + const char* title, + int pointsDisplayed, + const char* labelA, + A& refA, + const char* labelB, + B& refB, + const char* labelC, + C& refC, + const char* labelD, + D& refD, + const char* labelE, + E& refE, + const char* labelF, + F& refF ) { - VariableWrapper * wrappers = new VariableWrapper[6]; - wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); - wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); - wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); - wrappers[3] = VariableWrapper( labelD, static_cast( &refD ), &Dereference, "yellow" ); - wrappers[4] = VariableWrapper( labelE, static_cast( &refE ), &Dereference, "pink" ); - wrappers[5] = VariableWrapper( labelF, static_cast( &refF ), &Dereference, "blue" ); - AddGraphHelper( title, wrappers, 6, false, pointsDisplayed ); + VariableWrapper* wrappers = new VariableWrapper[6]; + wrappers[0] = + VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); + wrappers[1] = + VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); + wrappers[2] = + VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); + wrappers[3] = + VariableWrapper( labelD, static_cast( &refD ), &dereference, "yellow" ); + wrappers[4] = + VariableWrapper( labelE, static_cast( &refE ), &dereference, "pink" ); + wrappers[5] = + VariableWrapper( labelF, static_cast( &refF ), &dereference, "blue" ); + addGraphHelper( title, wrappers, 6, false, pointsDisplayed ); } // Set Colors for multivariable graphs - bool SetColor( int index, const char * colorA, const char * colorB ); - bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC ); - bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC, - const char * colorD ); - bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC, - const char * colorD, const char * colorE ); - bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC, - const char * colorD, const char * colorE, const char * colorF ); - + bool setColor( int index, const char* colorA, const char* colorB ); + bool setColor( int index, const char* colorA, const char* colorB, const char* colorC ); + bool setColor( + int index, + const char* colorA, + const char* colorB, + const char* colorC, + const char* colorD ); + bool setColor( + int index, + const char* colorA, + const char* colorB, + const char* colorC, + const char* colorD, + const char* colorE ); + bool setColor( + int index, + const char* colorA, + const char* colorB, + const char* colorC, + const char* colorD, + const char* colorE, + const char* colorF ); + // Destructor for Plotter class ~Plotter(); -public: - + public: // Nested VariableWrapper class class VariableWrapper { - public: + public: VariableWrapper(); - VariableWrapper( const char * label, void * ref, double ( * deref )( void * ), const char * color ); - - const char * GetLabel(); - double GetValue(); - const char * GetColor(); - void SetColor( const char * col ); - - private: - // Data - const char * label; - const char * color; - void * ref; - double ( * deref )( void * ); - + VariableWrapper( + const char* label, + void* ref, + double ( *deref )( void* ), + const char* color ); + + const char* getLabel(); + double getValue(); + const char* getColor(); + void setColor( const char* col ); + + private: + // Data + const char* label; + const char* color; + void* ref; + double ( *deref )( void* ); + }; //-- VariableWrapper - -public: + public: // Nested Graph node class class Graph { - public: - Graph( const char * title, VariableWrapper * wrappers, int size, bool xvy, int pointsDisplayed ); - ~Graph(); - void Plot( bool config ); - bool SetColor( int sz, const char * * colors ); - - // Data - Graph * next; - - private: - bool xvy; - int size; - int pointsDisplayed; - const char * title; - VariableWrapper * wrappers; - + public: + Graph( + const char* title, + VariableWrapper* wrappers, + int size, + bool xvy, + int pointsDisplayed ); + ~Graph(); + void plot( bool config ); + bool setColor( int sz, const char** colors ); + + // Data + Graph* next; + + private: + bool xvy; + int size; + int pointsDisplayed; + const char* title; + VariableWrapper* wrappers; + }; //-- Graph - -private: + + private: // Helpers - void AddGraphHelper( const char * title, VariableWrapper * wrappers, int sz, bool xvy, int pointsDisplayed ); - bool SetColorHelper( int index, int sz, const char * * colors ); - - template - static double Dereference( void * ref ) + void addGraphHelper( + const char* title, + VariableWrapper* wrappers, + int sz, + bool xvy, + int pointsDisplayed ); + bool setColorHelper( int index, int sz, const char** colors ); + + template static double dereference( void* ref ) { - return static_cast( ( * static_cast( ref ) ) ); + return static_cast( ( *static_cast( ref ) ) ); } - - + // Data int numGraphs; unsigned long lastUpdated; int counter; - Graph * head; - Graph * tail; - + Graph* head; + Graph* tail; + }; //-- Plotter // Constants static const int CONFIG_INTERVAL = 50; // Transmission Keys -static const char * OUTER_KEY = "#"; -static const char * TIME_KEY = "t"; -static const char * NUM_GRAPH_KEY = "ng"; -static const char * LAST_UPDATED_KEY = "lu"; -static const char * GRAPHS_KEY = "g"; -static const char * TITLE_KEY = "t"; -static const char * XVY_KEY = "xvy"; -static const char * POINTS_DISPLAYED_KEY = "pd"; -static const char * SIZE_KEY = "sz"; -static const char * LABELS_KEY = "l"; -static const char * COLORS_KEY = "c"; -static const char * DATA_KEY = "d"; +static const char* OUTER_KEY = "#"; +static const char* TIME_KEY = "t"; +static const char* NUM_GRAPH_KEY = "ng"; +static const char* LAST_UPDATED_KEY = "lu"; +static const char* GRAPHS_KEY = "g"; +static const char* TITLE_KEY = "t"; +static const char* XVY_KEY = "xvy"; +static const char* POINTS_DISPLAYED_KEY = "pd"; +static const char* SIZE_KEY = "sz"; +static const char* LABELS_KEY = "l"; +static const char* COLORS_KEY = "c"; +static const char* DATA_KEY = "d"; #endif From daf445a761bdc2a2d40237ebe6f40d9f430f0d49 Mon Sep 17 00:00:00 2001 From: "Devin A. Conley" Date: Sun, 12 Jul 2020 13:43:47 -0500 Subject: [PATCH 2/5] refactor for polymorphic graph types, expose as public class, support higher number of variables --- .gitignore | 3 +- .../multivariable_plotting.ino | 14 +- src/Graph.cpp | 148 +++++++++ src/Graph.h | 111 +++++++ src/Plotter.cpp | 217 +------------ src/Plotter.h | 285 +++--------------- src/Util.h | 33 ++ 7 files changed, 348 insertions(+), 463 deletions(-) create mode 100644 src/Graph.cpp create mode 100644 src/Graph.h create mode 100644 src/Util.h diff --git a/.gitignore b/.gitignore index cfbf241..722d5e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -plotter/LICENSE -plotter/README.md \ No newline at end of file +.vscode diff --git a/examples/multivariable_plotting/multivariable_plotting.ino b/examples/multivariable_plotting/multivariable_plotting.ino index bc3dece..0e12f1e 100644 --- a/examples/multivariable_plotting/multivariable_plotting.ino +++ b/examples/multivariable_plotting/multivariable_plotting.ino @@ -27,14 +27,12 @@ void setup() p.begin(); // Add 5 variable time graph - p.addTimeGraph( - "5 variable time graph", 1000, - "v label", v, - "w label", w, - "x label", x, - "y label", y, - "z label", z - ); + LineGraph& g = p.addLineGraph( "5 variable time graph" ); + g.line( "v label", v ); + g.line( "w label", w ); + g.line( "x label", x ); + g.line( "y label", y ); + g.line( "z label", z ); } void loop() diff --git a/src/Graph.cpp b/src/Graph.cpp new file mode 100644 index 0000000..465ca23 --- /dev/null +++ b/src/Graph.cpp @@ -0,0 +1,148 @@ +/* + =========================================================================================== + Graph classes + ------------------------------------------------------------------------------------------- + Plotter + v2.4.1 + https://github.com/devinaconley/arduino-plotter + by Devin Conley + =========================================================================================== +*/ + +#include "Graph.h" +#include "Arduino.h" + +// Graph + +Graph::Graph( const char* title, int maxPoints ) + : next( nullptr ), + title( title ), + maxPoints( maxPoints ), + size( 0 ), + head( nullptr ), + tail( nullptr ) +{ +} + +Graph::~Graph() +{ + Variable* temp = head; + Variable* tempNext; + while ( temp->next ) + { + tempNext = temp->next; + delete temp; + temp = tempNext; + } + delete temp; +} + +void Graph::addVariable( Variable* var ) +{ + if ( head ) + { + tail->next = var; + tail = var; + } + else + { + head = var; + tail = var; + } + size++; +} + +void Graph::plot( bool config ) +{ + Serial.print( "{" ); + + if ( config ) + { + Serial.print( "\"" ), Serial.print( TITLE_KEY ), Serial.print( "\":" ); + Serial.print( "\"" ), Serial.print( title ), Serial.print( "\"" ); + Serial.print( ",\"" ), Serial.print( XVY_KEY ); + Serial.print( "\":" ), Serial.print( xvy() ); + Serial.print( ",\"" ), Serial.print( POINTS_DISPLAYED_KEY ); + Serial.print( "\":" ), Serial.print( maxPoints ); + Serial.print( ",\"" ), Serial.print( SIZE_KEY ); + Serial.print( "\":" ), Serial.print( size ); + Serial.print( ",\"" ), Serial.print( LABELS_KEY ), Serial.print( "\":[" ); + Variable* temp = head; + while ( temp ) + { + Serial.print( "\"" ), Serial.print( temp->getLabel() ), Serial.print( "\"" ); + temp = temp->next; + if ( temp ) + { + Serial.print( "," ); + } + } + Serial.print( "],\"" ), Serial.print( COLORS_KEY ), Serial.print( "\":[" ); + temp = head; + while ( temp ) + { + Serial.print( "\"" ), Serial.print( temp->getColor() ), Serial.print( "\"" ); + temp = temp->next; + if ( temp ) + { + Serial.print( "," ); + } + } + Serial.print( "]," ); + } + + Serial.print( "\"" ), Serial.print( DATA_KEY ), Serial.print( "\":[" ); + Variable* temp = head; + while ( temp ) + { + Serial.print( temp->getValue() ); + temp = temp->next; + if ( temp ) + { + Serial.print( "," ); + } + } + Serial.print( "]}" ); +} + +// Line graph + +LineGraph::LineGraph( const char* title = "", int range = 5000, int maxPoints = 1000 ) + : Graph( title, maxPoints ), range( range ) +{ +} + +// Scatter graph + +ScatterGraph::ScatterGraph( const char* title = "", int maxPoints = 1000 ) + : Graph( title, maxPoints ) +{ +} + +// Variable + +Graph::Variable::Variable() : ref( nullptr ), deref( nullptr ), next( nullptr ) {} + +Graph::Variable::Variable( + const char* label, + void* ref, + double ( *deref )( void* ), + const char* color ) + : label( label ), color( color ), ref( ref ), deref( deref ), next( nullptr ) +{ +} + +const char* Graph::Variable::getLabel() +{ + return label; +} + +double Graph::Variable::getValue() +{ + return deref( ref ); +} + +const char* Graph::Variable::getColor() +{ + return color; +} diff --git a/src/Graph.h b/src/Graph.h new file mode 100644 index 0000000..8f81914 --- /dev/null +++ b/src/Graph.h @@ -0,0 +1,111 @@ +/* + =========================================================================================== + Graph classes + ------------------------------------------------------------------------------------------- + Plotter + v2.4.1 + https://github.com/devinaconley/arduino-plotter + by Devin Conley + =========================================================================================== +*/ + +#ifndef GRAPH_H +#define GRAPH_H + +#include "Util.h" + +// Abstract graph class / node +class Graph +{ + public: + Graph( const char* title, int maxPoints ); + ~Graph(); + virtual void plot( bool config ); + + // Data + Graph* next; + + protected: + // Nested Variable wrapper class + class Variable + { + public: + Variable(); + Variable( const char* label, void* ref, double ( *deref )( void* ), const char* color ); + + const char* getLabel(); + double getValue(); + const char* getColor(); + + Variable* next; + + private: + // Data + const char* label; + const char* color; + void* ref; + double ( *deref )( void* ); + + }; //-- Variable + + template static double dereference( void* ref ) + { + return static_cast( ( *static_cast( ref ) ) ); + } + + protected: + void addVariable( Variable* variable ); + virtual bool xvy() = 0; + int size; + int maxPoints; + const char* title; + Variable* head; + Variable* tail; +}; + +// Line graph vs time +class LineGraph : public Graph +{ + public: + LineGraph( const char* title = "", int range = 5000, int maxPoints = 1000 ); + + template void line( const char* label, T& ref ) + { + addVariable( + new Variable( label, static_cast( &ref ), &dereference, COLORS[size % 6] ) ); + } + + virtual bool xvy() + { + return false; + } + + private: + // Data + int range; +}; + +// X vs. Y graph +class ScatterGraph : public Graph +{ + public: + ScatterGraph( const char* title = "", int maxPoints = 1000 ); + + template + void scatter( const char* labelX, X& refX, const char* labelY, Y& refY ) + { + addVariable( new Variable( + labelX, + static_cast( &refX ), + &dereference, + COLORS[( size / 2 ) % 6] ) ); + addVariable( new Variable( labelY, static_cast( &refY ), &dereference, "" ) ); + } + + virtual bool xvy() + { + return true; + } +}; + +#endif diff --git a/src/Plotter.cpp b/src/Plotter.cpp index 40385b2..66d7831 100644 --- a/src/Plotter.cpp +++ b/src/Plotter.cpp @@ -24,13 +24,14 @@ */ #include "Plotter.h" +#include "Arduino.h" // Plotter Plotter::Plotter() { - head = NULL; - tail = NULL; + head = nullptr; + tail = nullptr; numGraphs = 0; counter = 0; lastUpdated = millis(); @@ -55,23 +56,17 @@ Plotter::~Plotter() delete temp; } -void Plotter::addGraphHelper( - const char* title, - VariableWrapper* wrappers, - int sz, - bool xvy, - int pointsDisplayed ) +void Plotter::addGraph( Graph* graph ) { - Graph* temp = new Graph( title, wrappers, sz, xvy, pointsDisplayed ); if ( head ) { - tail->next = temp; - tail = temp; + tail->next = graph; + tail = graph; } else { - head = temp; - tail = temp; + head = graph; + tail = graph; } numGraphs++; @@ -109,79 +104,6 @@ bool Plotter::remove( int index ) } } -bool Plotter::setColor( int index, const char* colorA ) -{ - const char* colors[] = { colorA }; - return setColorHelper( index, 1, colors ); -} - -bool Plotter::setColor( int index, const char* colorA, const char* colorB ) -{ - const char* colors[] = { colorA, colorB }; - return setColorHelper( index, 2, colors ); -} - -bool Plotter::setColor( int index, const char* colorA, const char* colorB, const char* colorC ) -{ - const char* colors[] = { colorA, colorB, colorC }; - return setColorHelper( index, 3, colors ); -} - -bool Plotter::setColor( - int index, - const char* colorA, - const char* colorB, - const char* colorC, - const char* colorD ) -{ - const char* colors[] = { colorA, colorB, colorC, colorD }; - return setColorHelper( index, 4, colors ); -} - -bool Plotter::setColor( - int index, - const char* colorA, - const char* colorB, - const char* colorC, - const char* colorD, - const char* colorE ) -{ - const char* colors[] = { colorA, colorB, colorC, colorD, colorE }; - return setColorHelper( index, 5, colors ); -} - -bool Plotter::setColor( - int index, - const char* colorA, - const char* colorB, - const char* colorC, - const char* colorD, - const char* colorE, - const char* colorF ) -{ - const char* colors[] = { colorA, colorB, colorC, colorD, colorE, colorF }; - return setColorHelper( index, 6, colors ); -} - -bool Plotter::setColorHelper( int index, int sz, const char** colors ) -{ - if ( numGraphs == 0 || index < 0 || numGraphs <= index ) - { - return false; - } - Graph* temp = head; - for ( int i = 0; i < index; i++ ) - { - temp = temp->next; - } - bool res = temp->setColor( sz, colors ); - if ( res ) - { - lastUpdated = millis(); - } - return res; -} - void Plotter::plot() { bool config = counter == 0; @@ -218,126 +140,3 @@ void Plotter::plot() } } -// Graph - -Plotter::Graph::Graph( - const char* title, - VariableWrapper* wrappers, - int size, - bool xvy, - int pointsDisplayed ) - : next( NULL ), - xvy( xvy ), - size( size ), - pointsDisplayed( pointsDisplayed ), - title( title ), - wrappers( wrappers ) -{ -} - -Plotter::Graph::~Graph() -{ - delete[] wrappers; -} - -void Plotter::Graph::plot( bool config ) -{ - Serial.print( "{" ); - - if ( config ) - { - Serial.print( "\"" ), Serial.print( TITLE_KEY ), Serial.print( "\":" ); - Serial.print( "\"" ), Serial.print( title ), Serial.print( "\"" ); - Serial.print( ",\"" ), Serial.print( XVY_KEY ); - Serial.print( "\":" ), Serial.print( xvy ); - Serial.print( ",\"" ), Serial.print( POINTS_DISPLAYED_KEY ); - Serial.print( "\":" ), Serial.print( pointsDisplayed ); - Serial.print( ",\"" ), Serial.print( SIZE_KEY ); - Serial.print( "\":" ), Serial.print( size ); - Serial.print( ",\"" ), Serial.print( LABELS_KEY ), Serial.print( "\":[" ); - for ( int i = 0; i < size; i++ ) - { - Serial.print( "\"" ), Serial.print( wrappers[i].getLabel() ), Serial.print( "\"" ); - if ( i + 1 < size ) - { - Serial.print( "," ); - } - } - Serial.print( "],\"" ), Serial.print( COLORS_KEY ), Serial.print( "\":[" ); - for ( int i = 0; i < size; i++ ) - { - Serial.print( "\"" ), Serial.print( wrappers[i].getColor() ), Serial.print( "\"" ); - if ( i + 1 < size ) - { - Serial.print( "," ); - } - } - Serial.print( "]," ); - } - - Serial.print( "\"" ), Serial.print( DATA_KEY ), Serial.print( "\":[" ); - for ( int i = 0; i < size; i++ ) - { - Serial.print( wrappers[i].getValue(), 8 ); - if ( i + 1 < size ) - { - Serial.print( "," ); - } - } - - Serial.print( "]}" ); -} - -bool Plotter::Graph::setColor( int sz, const char** colors ) -{ - if ( sz != size && !xvy ) - { - return false; - } - - if ( xvy ) - { - wrappers[0].setColor( colors[0] ); - } - else - { - for ( int i = 0; i < size; i++ ) - { - wrappers[i].setColor( colors[i] ); - } - } - return true; -} - -// VariableWrapper - -Plotter::VariableWrapper::VariableWrapper() : ref( NULL ), deref( NULL ) {} - -Plotter::VariableWrapper::VariableWrapper( - const char* label, - void* ref, - double ( *deref )( void* ), - const char* color ) - : label( label ), color( color ), ref( ref ), deref( deref ) -{ -} - -const char* Plotter::VariableWrapper::getLabel() -{ - return label; -} - -double Plotter::VariableWrapper::getValue() -{ - return deref( ref ); -} - -const char* Plotter::VariableWrapper::getColor() -{ - return color; -} - -void Plotter::VariableWrapper::setColor( const char* col ) -{ - color = col; -} diff --git a/src/Plotter.h b/src/Plotter.h index d5591a2..41916e7 100644 --- a/src/Plotter.h +++ b/src/Plotter.h @@ -26,7 +26,8 @@ #ifndef PLOTTER_H #define PLOTTER_H -#include "Arduino.h" +#include "Graph.h" +#include "Util.h" class Plotter { @@ -37,12 +38,26 @@ class Plotter // Initialize Plotter void begin(); + /* + Add an empty line graph vs. time + + Args: + - title: const char * with title of graph + - maxPoints: number of points to be shown at a given time. Used to control time-scaling + */ + LineGraph& addLineGraph( const char* title, int maxPoints = 1000 ) + { + LineGraph* graph = new LineGraph( title, 5000, maxPoints ); + addGraph( graph ); + return *graph; + } + /* Add a 1-variable graph vs. time Args: - title: const char * with title of graph - - pointsDisplayed: number of points to be shown at a given time. Used to control time-scaling + - maxPoints: number of points to be shown at a given time. Used to control time-scaling - labelA: const char * with label of the plotted variable - refA: reference to global variable that will be updated throughout program @@ -50,12 +65,11 @@ class Plotter format */ template - void addTimeGraph( const char* title, int pointsDisplayed, const char* labelA, A& refA ) + void addLineGraph( const char* title, int maxPoints, const char* labelA, A& refA ) { - VariableWrapper* wrappers = new VariableWrapper[1]; - wrappers[0] = - VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); - addGraphHelper( title, wrappers, 1, false, pointsDisplayed ); + LineGraph* graph = new LineGraph( title, 5000, maxPoints ); + graph->line( labelA, refA ); + addGraph( graph ); } /* @@ -63,28 +77,24 @@ class Plotter Args: - title: const char * with title of graph - - pointsDisplayed: number of points to be shown at a given time. Determines duration of data - persistence + - maxPoints: max number of points to be shown at a given time. Controls data persistence - labelX: const char * with label of variable to be plotted along X-axis - refX: reference to global X-variable that will be updated throughout program - labelY: const char * with label of variable to be plotted along Y-axis - refY: reference to global Y-variable that will be updated throughout program */ template - void addXYGraph( + void addScatterGraph( const char* title, - int pointsDisplayed, + int maxPoints, const char* labelX, X& refX, const char* labelY, Y& refY ) { - VariableWrapper* wrappers = new VariableWrapper[2]; - wrappers[0] = - VariableWrapper( labelX, static_cast( &refX ), &dereference, "green" ); - wrappers[1] = - VariableWrapper( labelY, static_cast( &refY ), &dereference, "green" ); - addGraphHelper( title, wrappers, 2, true, pointsDisplayed ); + ScatterGraph* graph = new ScatterGraph( title, maxPoints ); + graph->scatter( labelX, refX, labelY, refY ); + addGraph( graph ); } /* @@ -107,40 +117,27 @@ class Plotter */ bool remove( int index ); - /* - Set Variable Colors - - Args: - - index: position of graph to set colors for - - colorA: new color to set - Returns: - - true, if successful - */ - bool setColor( int index, const char* colorA ); - // Add a 2-variable graph vs. time template - void addTimeGraph( + void addLineGraph( const char* title, - int pointsDisplayed, + int maxPoints, const char* labelA, A& refA, const char* labelB, B& refB ) { - VariableWrapper* wrappers = new VariableWrapper[2]; - wrappers[0] = - VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); - wrappers[1] = - VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); - addGraphHelper( title, wrappers, 2, false, pointsDisplayed ); + LineGraph* graph = new LineGraph( title, 5000, maxPoints ); + graph->line( labelA, refA ); + graph->line( labelB, refB ); + addGraph( graph ); } // Add a 3-variable graph vs. time template - void addTimeGraph( + void addLineGraph( const char* title, - int pointsDisplayed, + int maxPoints, const char* labelA, A& refA, const char* labelB, @@ -148,201 +145,19 @@ class Plotter const char* labelC, C& refC ) { - VariableWrapper* wrappers = new VariableWrapper[3]; - wrappers[0] = - VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); - wrappers[1] = - VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); - wrappers[2] = - VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); - addGraphHelper( title, wrappers, 3, false, pointsDisplayed ); - } - - // Add a 4-variable graph vs. time - template - void addTimeGraph( - const char* title, - int pointsDisplayed, - const char* labelA, - A& refA, - const char* labelB, - B& refB, - const char* labelC, - C& refC, - const char* labelD, - D& refD ) - { - VariableWrapper* wrappers = new VariableWrapper[4]; - wrappers[0] = - VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); - wrappers[1] = - VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); - wrappers[2] = - VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); - wrappers[3] = - VariableWrapper( labelD, static_cast( &refD ), &dereference, "yellow" ); - addGraphHelper( title, wrappers, 4, false, pointsDisplayed ); - } - - // Add a 5-variable graph vs. time - template - void addTimeGraph( - const char* title, - int pointsDisplayed, - const char* labelA, - A& refA, - const char* labelB, - B& refB, - const char* labelC, - C& refC, - const char* labelD, - D& refD, - const char* labelE, - E& refE ) - { - VariableWrapper* wrappers = new VariableWrapper[5]; - wrappers[0] = - VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); - wrappers[1] = - VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); - wrappers[2] = - VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); - wrappers[3] = - VariableWrapper( labelD, static_cast( &refD ), &dereference, "yellow" ); - wrappers[4] = - VariableWrapper( labelE, static_cast( &refE ), &dereference, "pink" ); - addGraphHelper( title, wrappers, 5, false, pointsDisplayed ); - } - - // Add a 6-variable graph vs. time - template - void addTimeGraph( - const char* title, - int pointsDisplayed, - const char* labelA, - A& refA, - const char* labelB, - B& refB, - const char* labelC, - C& refC, - const char* labelD, - D& refD, - const char* labelE, - E& refE, - const char* labelF, - F& refF ) - { - VariableWrapper* wrappers = new VariableWrapper[6]; - wrappers[0] = - VariableWrapper( labelA, static_cast( &refA ), &dereference, "green" ); - wrappers[1] = - VariableWrapper( labelB, static_cast( &refB ), &dereference, "orange" ); - wrappers[2] = - VariableWrapper( labelC, static_cast( &refC ), &dereference, "cyan" ); - wrappers[3] = - VariableWrapper( labelD, static_cast( &refD ), &dereference, "yellow" ); - wrappers[4] = - VariableWrapper( labelE, static_cast( &refE ), &dereference, "pink" ); - wrappers[5] = - VariableWrapper( labelF, static_cast( &refF ), &dereference, "blue" ); - addGraphHelper( title, wrappers, 6, false, pointsDisplayed ); + LineGraph* graph = new LineGraph( title, 5000, maxPoints ); + graph->line( labelA, refA ); + graph->line( labelB, refB ); + graph->line( labelC, refC ); + addGraph( graph ); } - // Set Colors for multivariable graphs - bool setColor( int index, const char* colorA, const char* colorB ); - bool setColor( int index, const char* colorA, const char* colorB, const char* colorC ); - bool setColor( - int index, - const char* colorA, - const char* colorB, - const char* colorC, - const char* colorD ); - bool setColor( - int index, - const char* colorA, - const char* colorB, - const char* colorC, - const char* colorD, - const char* colorE ); - bool setColor( - int index, - const char* colorA, - const char* colorB, - const char* colorC, - const char* colorD, - const char* colorE, - const char* colorF ); - // Destructor for Plotter class ~Plotter(); - public: - // Nested VariableWrapper class - class VariableWrapper - { - public: - VariableWrapper(); - VariableWrapper( - const char* label, - void* ref, - double ( *deref )( void* ), - const char* color ); - - const char* getLabel(); - double getValue(); - const char* getColor(); - void setColor( const char* col ); - - private: - // Data - const char* label; - const char* color; - void* ref; - double ( *deref )( void* ); - - }; //-- VariableWrapper - - public: - // Nested Graph node class - class Graph - { - public: - Graph( - const char* title, - VariableWrapper* wrappers, - int size, - bool xvy, - int pointsDisplayed ); - ~Graph(); - void plot( bool config ); - bool setColor( int sz, const char** colors ); - - // Data - Graph* next; - - private: - bool xvy; - int size; - int pointsDisplayed; - const char* title; - VariableWrapper* wrappers; - - }; //-- Graph - private: // Helpers - void addGraphHelper( - const char* title, - VariableWrapper* wrappers, - int sz, - bool xvy, - int pointsDisplayed ); - bool setColorHelper( int index, int sz, const char** colors ); - - template static double dereference( void* ref ) - { - return static_cast( ( *static_cast( ref ) ) ); - } + void addGraph( Graph* graph ); // Data int numGraphs; @@ -350,24 +165,6 @@ class Plotter int counter; Graph* head; Graph* tail; - }; //-- Plotter -// Constants -static const int CONFIG_INTERVAL = 50; - -// Transmission Keys -static const char* OUTER_KEY = "#"; -static const char* TIME_KEY = "t"; -static const char* NUM_GRAPH_KEY = "ng"; -static const char* LAST_UPDATED_KEY = "lu"; -static const char* GRAPHS_KEY = "g"; -static const char* TITLE_KEY = "t"; -static const char* XVY_KEY = "xvy"; -static const char* POINTS_DISPLAYED_KEY = "pd"; -static const char* SIZE_KEY = "sz"; -static const char* LABELS_KEY = "l"; -static const char* COLORS_KEY = "c"; -static const char* DATA_KEY = "d"; - #endif diff --git a/src/Util.h b/src/Util.h new file mode 100644 index 0000000..0a133e5 --- /dev/null +++ b/src/Util.h @@ -0,0 +1,33 @@ +/* + =========================================================================================== + Utilities and constants + ------------------------------------------------------------------------------------------- + Plotter + v2.4.1 + https://github.com/devinaconley/arduino-plotter + by Devin Conley + =========================================================================================== +*/ + +#ifndef UTIL_H +#define UTIL_H + +// Constants +static const int CONFIG_INTERVAL = 50; +static const char* COLORS[] = { "green", "orange", "cyan", "yellow", "pink", "blue" }; + +// Transmission Keys +static const char* OUTER_KEY = "#"; +static const char* TIME_KEY = "t"; +static const char* NUM_GRAPH_KEY = "ng"; +static const char* LAST_UPDATED_KEY = "lu"; +static const char* GRAPHS_KEY = "g"; +static const char* TITLE_KEY = "t"; +static const char* XVY_KEY = "xvy"; +static const char* POINTS_DISPLAYED_KEY = "pd"; +static const char* SIZE_KEY = "sz"; +static const char* LABELS_KEY = "l"; +static const char* COLORS_KEY = "c"; +static const char* DATA_KEY = "d"; + +#endif \ No newline at end of file From 71a0697480063d91b7c40f2a3704177189de1063 Mon Sep 17 00:00:00 2001 From: "Devin A. Conley" Date: Sun, 12 Jul 2020 14:05:23 -0500 Subject: [PATCH 3/5] allow setting color through line/scatter function --- examples/set_colors/set_colors.ino | 14 ++++++++------ src/Graph.cpp | 2 +- src/Graph.h | 23 +++++++++++++---------- src/Plotter.h | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/examples/set_colors/set_colors.ino b/examples/set_colors/set_colors.ino index 1c1802b..31e4293 100644 --- a/examples/set_colors/set_colors.ino +++ b/examples/set_colors/set_colors.ino @@ -22,13 +22,15 @@ void setup() p.begin(); // Add plots - p.addTimeGraph( "Some title of a graph", 500, "label for x", x, "label for y", y ); - p.addXYGraph( "Title of X vs Y graph", 1000, "x axis", x, "y axis", y ); + LineGraph& lg = p.addLineGraph( "Some title of a graph", 500 ); + ScatterGraph& sg = p.addScatterGraph( "Title of X vs Y graph", 1000 ); - // Set variable colors of graph with index 0 to pink and orange - p.setColor( 0, "pink", "orange" ); - // Set color of x vs y graph at index 1 to cyan - p.setColor( 1, "cyan" ); + // Add line variables and specify color + lg.line( "label for x", x, "pink" ); + lg.line( "label for y", y, "orange" ); + + // Add scatter channel and specify color + sg.scatter( "x axis", x, "y axis", y, "cyan" ); } void loop() diff --git a/src/Graph.cpp b/src/Graph.cpp index 465ca23..9092875 100644 --- a/src/Graph.cpp +++ b/src/Graph.cpp @@ -95,7 +95,7 @@ void Graph::plot( bool config ) Variable* temp = head; while ( temp ) { - Serial.print( temp->getValue() ); + Serial.print( temp->getValue(), 8 ); temp = temp->next; if ( temp ) { diff --git a/src/Graph.h b/src/Graph.h index 8f81914..8264c0f 100644 --- a/src/Graph.h +++ b/src/Graph.h @@ -69,10 +69,13 @@ class LineGraph : public Graph public: LineGraph( const char* title = "", int range = 5000, int maxPoints = 1000 ); - template void line( const char* label, T& ref ) + template void line( const char* label, T& ref, const char* color = "" ) { - addVariable( - new Variable( label, static_cast( &ref ), &dereference, COLORS[size % 6] ) ); + if ( color[0] == '\0' ) + { + color = COLORS[size % 6]; + } + addVariable( new Variable( label, static_cast( &ref ), &dereference, color ) ); } virtual bool xvy() @@ -92,14 +95,14 @@ class ScatterGraph : public Graph ScatterGraph( const char* title = "", int maxPoints = 1000 ); template - void scatter( const char* labelX, X& refX, const char* labelY, Y& refY ) + void scatter( const char* labelX, X& refX, const char* labelY, Y& refY, const char* color = "" ) { - addVariable( new Variable( - labelX, - static_cast( &refX ), - &dereference, - COLORS[( size / 2 ) % 6] ) ); - addVariable( new Variable( labelY, static_cast( &refY ), &dereference, "" ) ); + if ( color[0] == '\0' ) + { + color = COLORS[( size / 2 ) % 6]; + } + addVariable( new Variable( labelX, static_cast( &refX ), &dereference, color ) ); + addVariable( new Variable( labelY, static_cast( &refY ), &dereference, color ) ); } virtual bool xvy() diff --git a/src/Plotter.h b/src/Plotter.h index 41916e7..8b0f2c1 100644 --- a/src/Plotter.h +++ b/src/Plotter.h @@ -44,6 +44,9 @@ class Plotter Args: - title: const char * with title of graph - maxPoints: number of points to be shown at a given time. Used to control time-scaling + + Returns: + - reference to created line graph */ LineGraph& addLineGraph( const char* title, int maxPoints = 1000 ) { @@ -52,6 +55,23 @@ class Plotter return *graph; } + /* + Add an empty scatter graph + + Args: + - title: const char * with title of graph + - maxPoints: max number of points to be shown at a given time. Controls data persistence + + Returns: + - reference to created scatter graph + */ + ScatterGraph& addScatterGraph( const char* title, int maxPoints ) + { + ScatterGraph* graph = new ScatterGraph( title, maxPoints ); + addGraph( graph ); + return *graph; + } + /* Add a 1-variable graph vs. time From 378aad7a698a32ccdd8245084db824cd78fd3a0f Mon Sep 17 00:00:00 2001 From: "Devin A. Conley" Date: Sun, 12 Jul 2020 14:49:26 -0500 Subject: [PATCH 4/5] cleanup method names and examples --- README.md | 2 +- .../control_points_displayed.ino | 8 +++---- examples/different_types/different_types.ino | 4 ++-- .../dynamically_modify_graph_layout.ino | 6 ++--- .../multivariable_plotting.ino | 2 +- examples/quick_start/quick_start.ino | 2 +- examples/set_colors/set_colors.ino | 4 ++-- src/Plotter.h | 24 +++++++++---------- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c814994..9782070 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ void setup() { p.begin(); // start plotter - p.addTimeGraph( "Some title of a graph", 1500, "label for x", x ); // add any graphs you want + p.lineGraph( "Some title of a graph", 1500, "label for x", x ); // add any graphs you want } void loop() diff --git a/examples/control_points_displayed/control_points_displayed.ino b/examples/control_points_displayed/control_points_displayed.ino index 0962d14..3cbc4cf 100644 --- a/examples/control_points_displayed/control_points_displayed.ino +++ b/examples/control_points_displayed/control_points_displayed.ino @@ -24,12 +24,12 @@ void setup() p.begin(); // Add X-Y graphs - p.addXYGraph( "X-Y graph w/ 500 points", 500, "x axis", x, "y axis", y ); - p.addXYGraph( "X-Y graph w/ 200 points", 200, "x axis", x, "y axis", y ); + p.scatterGraph( "X-Y graph w/ 500 points", 500, "x axis", x, "y axis", y ); + p.scatterGraph( "X-Y graph w/ 200 points", 200, "x axis", x, "y axis", y ); // Add time graphs. Notice the effect of points displayed on the time scale - p.addTimeGraph( "Time graph w/ 500 points", 500, "x label", x ); - p.addTimeGraph( "Time graph w/ 200 points", 200, "x label", x ); + p.lineGraph( "Time graph w/ 500 points", 500, "x label", x ); + p.lineGraph( "Time graph w/ 200 points", 200, "x label", x ); } void loop() diff --git a/examples/different_types/different_types.ino b/examples/different_types/different_types.ino index 6ba9b83..8c00461 100644 --- a/examples/different_types/different_types.ino +++ b/examples/different_types/different_types.ino @@ -26,8 +26,8 @@ void setup() p.begin(); // Add time graphs. - p.addTimeGraph( "float vs int", 500, "float", f, "int", i ); - p.addTimeGraph( "float vs char", 500, "float", f2, "unsigned char", ch ); + p.lineGraph( "float vs int", 500, "float", f, "int", i ); + p.lineGraph( "float vs char", 500, "float", f2, "unsigned char", ch ); } void loop() diff --git a/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino b/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino index 3476771..95bd041 100644 --- a/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino +++ b/examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino @@ -30,7 +30,7 @@ void setup() p.begin(); // Add a graph during setup - p.addTimeGraph( "x and y against time", 1000, "x label", x, "y label", y ); + p.lineGraph( "x and y against time", 1000, "x label", x, "y label", y ); } void loop() @@ -38,14 +38,14 @@ void loop() // After 3 seconds add a 1-variable graph if ( millis() > 3000 && add_first ) { - p.addTimeGraph( "First dynamic addition", 1000, "z label", z ); + p.lineGraph( "First dynamic addition", 1000, "z label", z ); add_first = false; } // After 5 seconds, add a 3-variable graph if ( millis() > 5000 && add_second ) { - p.addTimeGraph( "Second dynamic addition", 1000, "x label", x, "y label", y, "z label", z ); + p.lineGraph( "Second dynamic addition", 1000, "x label", x, "y label", y, "z label", z ); add_second = false; } diff --git a/examples/multivariable_plotting/multivariable_plotting.ino b/examples/multivariable_plotting/multivariable_plotting.ino index 0e12f1e..4326952 100644 --- a/examples/multivariable_plotting/multivariable_plotting.ino +++ b/examples/multivariable_plotting/multivariable_plotting.ino @@ -27,7 +27,7 @@ void setup() p.begin(); // Add 5 variable time graph - LineGraph& g = p.addLineGraph( "5 variable time graph" ); + LineGraph& g = p.lineGraph( "5 variable time graph" ); g.line( "v label", v ); g.line( "w label", w ); g.line( "x label", x ); diff --git a/examples/quick_start/quick_start.ino b/examples/quick_start/quick_start.ino index 4733e3a..de3d550 100644 --- a/examples/quick_start/quick_start.ino +++ b/examples/quick_start/quick_start.ino @@ -19,7 +19,7 @@ void setup() { p.begin(); - p.addTimeGraph( "Some title of a graph", 1500, "label for x", x ); + p.lineGraph( "Some title of a graph", 1500, "label for x", x ); } void loop() diff --git a/examples/set_colors/set_colors.ino b/examples/set_colors/set_colors.ino index 31e4293..370b32c 100644 --- a/examples/set_colors/set_colors.ino +++ b/examples/set_colors/set_colors.ino @@ -22,8 +22,8 @@ void setup() p.begin(); // Add plots - LineGraph& lg = p.addLineGraph( "Some title of a graph", 500 ); - ScatterGraph& sg = p.addScatterGraph( "Title of X vs Y graph", 1000 ); + LineGraph& lg = p.lineGraph( "Some title of a graph", 500 ); + ScatterGraph& sg = p.scatterGraph( "Title of X vs Y graph", 1000 ); // Add line variables and specify color lg.line( "label for x", x, "pink" ); diff --git a/src/Plotter.h b/src/Plotter.h index 8b0f2c1..a0cb9a9 100644 --- a/src/Plotter.h +++ b/src/Plotter.h @@ -39,7 +39,7 @@ class Plotter void begin(); /* - Add an empty line graph vs. time + Create and add an empty line graph vs. time Args: - title: const char * with title of graph @@ -48,7 +48,7 @@ class Plotter Returns: - reference to created line graph */ - LineGraph& addLineGraph( const char* title, int maxPoints = 1000 ) + LineGraph& lineGraph( const char* title, int maxPoints = 1000 ) { LineGraph* graph = new LineGraph( title, 5000, maxPoints ); addGraph( graph ); @@ -56,7 +56,7 @@ class Plotter } /* - Add an empty scatter graph + Create and add an empty scatter graph Args: - title: const char * with title of graph @@ -65,7 +65,7 @@ class Plotter Returns: - reference to created scatter graph */ - ScatterGraph& addScatterGraph( const char* title, int maxPoints ) + ScatterGraph& scatterGraph( const char* title, int maxPoints ) { ScatterGraph* graph = new ScatterGraph( title, maxPoints ); addGraph( graph ); @@ -73,7 +73,7 @@ class Plotter } /* - Add a 1-variable graph vs. time + Create and add a 1-variable graph vs. time Args: - title: const char * with title of graph @@ -85,7 +85,7 @@ class Plotter format */ template - void addLineGraph( const char* title, int maxPoints, const char* labelA, A& refA ) + void lineGraph( const char* title, int maxPoints, const char* labelA, A& refA ) { LineGraph* graph = new LineGraph( title, 5000, maxPoints ); graph->line( labelA, refA ); @@ -93,7 +93,7 @@ class Plotter } /* - Add an X vs. Y graph + Create and add an X vs. Y scatter graph Args: - title: const char * with title of graph @@ -104,7 +104,7 @@ class Plotter - refY: reference to global Y-variable that will be updated throughout program */ template - void addScatterGraph( + void scatterGraph( const char* title, int maxPoints, const char* labelX, @@ -137,9 +137,9 @@ class Plotter */ bool remove( int index ); - // Add a 2-variable graph vs. time + // Create and add a 2-variable graph vs. time template - void addLineGraph( + void lineGraph( const char* title, int maxPoints, const char* labelA, @@ -153,9 +153,9 @@ class Plotter addGraph( graph ); } - // Add a 3-variable graph vs. time + // Create and add a 3-variable graph vs. time template - void addLineGraph( + void lineGraph( const char* title, int maxPoints, const char* labelA, From 50e1b46bdfe4344ed294ae0de896ff11e1a005c7 Mon Sep 17 00:00:00 2001 From: "Devin A. Conley" Date: Tue, 28 Jul 2020 02:02:54 -0500 Subject: [PATCH 5/5] clean up java formatting --- .clang-format | 9 +- .../set_optionals.ino} | 0 listener/Graph.java | 560 +++++++++--------- src/Plotter.cpp | 1 - 4 files changed, 287 insertions(+), 283 deletions(-) rename examples/{set_colors/set_colors.ino => set_optionals/set_optionals.ino} (100%) diff --git a/.clang-format b/.clang-format index b82802b..ad035d6 100644 --- a/.clang-format +++ b/.clang-format @@ -1,9 +1,10 @@ -Language: Cpp - +--- IndentWidth: 4 UseTab: Never SpacesInParentheses: true ColumnLimit: 100 +--- +Language: Cpp BreakBeforeBraces: Allman AllowAllArgumentsOnNextLine: false @@ -23,3 +24,7 @@ BreakConstructorInitializers: BeforeColon ConstructorInitializerAllOnOneLineOrOnePerLine: true AllowShortFunctionsOnASingleLine: Empty +--- +Language: Java +# TODO +--- diff --git a/examples/set_colors/set_colors.ino b/examples/set_optionals/set_optionals.ino similarity index 100% rename from examples/set_colors/set_colors.ino rename to examples/set_optionals/set_optionals.ino diff --git a/listener/Graph.java b/listener/Graph.java index 0b0c143..0714ad0 100644 --- a/listener/Graph.java +++ b/listener/Graph.java @@ -53,155 +53,155 @@ class Graph // Contructor public Graph( PApplet parent, float posY, float posX, float height, float width, - boolean xvy, int numVars, int maxPoints, - String title, String[] labels, int[] colors ) + boolean xvy, int numVars, int maxPoints, + String title, String[] labels, int[] colors ) { - this.parent = parent; - this.posY = posY; - this.posX = posX; - this.height = height; - this.width = width; - this.xvy = xvy; - this.numVars = numVars; - this.maxPoints = maxPoints; - this.title = title; - this.labels = labels; - this.colors = colors; + this.parent = parent; + this.posY = posY; + this.posX = posX; + this.height = height; + this.width = width; + this.xvy = xvy; + this.numVars = numVars; + this.maxPoints = maxPoints; + this.title = title; + this.labels = labels; + this.colors = colors; - // this.parent.println( "Constructed new graph: ", this.title, " at ", this.posY, " ", this.posX ); + // this.parent.println( "Constructed new graph: ", this.title, " at ", this.posY, " ", this.posX ); - // Initialize - this.index = 0; - this.data = new double[maxPoints][numVars][2]; - this.extremesCounter = new int[4]; - this.extremes = new double[4]; - this.currPoints = 0; + // Initialize + this.index = 0; + this.data = new double[maxPoints][numVars][2]; + this.extremesCounter = new int[4]; + this.extremes = new double[4]; + this.currPoints = 0; } // Modifiers public void Reconfigure( float posY, float posX, float height, float width, - boolean xvy, int numVars, int maxPoints, - String title, String[] labels ) + boolean xvy, int numVars, int maxPoints, + String title, String[] labels ) { - this.posY = posY; - this.posX = posX; - this.height = height; - this.width = width; - this.xvy = xvy; - this.numVars = numVars; - this.maxPoints = maxPoints; - this.title = title; - this.labels = labels; + this.posY = posY; + this.posX = posX; + this.height = height; + this.width = width; + this.xvy = xvy; + this.numVars = numVars; + this.maxPoints = maxPoints; + this.title = title; + this.labels = labels; } public void Reconfigure( float posY, float posX, float height, float width ) { - this.posY = posY; - this.posX = posX; - this.height = height; - this.width = width; + this.posY = posY; + this.posX = posX; + this.height = height; + this.width = width; } public void Update( double[] newData, int time ) { - // Store data - if ( this.xvy ) - { - // Validate - if ( newData.length != 2 ) - { - //this.parent.println( "Invalid data passed to X v. Y graph." ); - return; - } + // Store data + if ( this.xvy ) + { + // Validate + if ( newData.length != 2 ) + { + //this.parent.println( "Invalid data passed to X v. Y graph." ); + return; + } - this.data[this.index][0][0] = newData[0]; - this.data[this.index][0][1] = newData[1]; - } - else - { - // Validate - if ( newData.length != this.numVars ) - { - //this.parent.println( "Invalid data passed to time graph." ); - return; - } - - for ( int i = 0; i < this.numVars; i++ ) - { - this.data[this.index][i][0] = time; - this.data[this.index][i][1] = newData[i]; - } - } - - // Counter for num points defined - if ( this.currPoints < this.maxPoints ) - { - this.currPoints++; - } + this.data[this.index][0][0] = newData[0]; + this.data[this.index][0][1] = newData[1]; + } + else + { + // Validate + if ( newData.length != this.numVars ) + { + //this.parent.println( "Invalid data passed to time graph." ); + return; + } + + for ( int i = 0; i < this.numVars; i++ ) + { + this.data[this.index][i][0] = time; + this.data[this.index][i][1] = newData[i]; + } + } + + // Counter for num points defined + if ( this.currPoints < this.maxPoints ) + { + this.currPoints++; + } - // Check extremes - this.CheckExtremes(); - - // Advance index position and rollback if needed - this.index++; - if ( this.index >= this.maxPoints ) - { - this.index = 0; - } + // Check extremes + this.CheckExtremes(); + + // Advance index position and rollback if needed + this.index++; + if ( this.index >= this.maxPoints ) + { + this.index = 0; + } } public void Plot() { - // Plot Background - this.parent.fill( PLOT_COL ); - this.parent.stroke( PLOT_COL ); - this.parent.strokeWeight( (this.width + this.height) / 2.0f * PT_SZ ); - this.parent.rect( this.posX, this.posY, this.width, this.height ); - - // Title - this.parent.textSize( (int)( (this.width + this.height) / 2.0f * TITLE_SZ ) ); - this.parent.fill( 255 ); - this.parent.textAlign( this.parent.CENTER, this.parent.TOP ); - this.parent.text( this.title, this.posX + this.width / 2, this.posY + 10 ); + // Plot Background + this.parent.fill( PLOT_COL ); + this.parent.stroke( PLOT_COL ); + this.parent.strokeWeight( (this.width + this.height) / 2.0f * PT_SZ ); + this.parent.rect( this.posX, this.posY, this.width, this.height ); + + // Title + this.parent.textSize( (int)( (this.width + this.height) / 2.0f * TITLE_SZ ) ); + this.parent.fill( 255 ); + this.parent.textAlign( this.parent.CENTER, this.parent.TOP ); + this.parent.text( this.title, this.posX + this.width / 2, this.posY + 10 ); - // Calculations for offset and scaling of graph ( vs. time ) - double xScale = this.width / ( this.extremes[1] - this.extremes[0] ); - double xOffset = xScale * this.extremes[0]; - double yScale = AXIS_COV * this.height / ( this.extremes[3] - this.extremes[2] ); - double yOffset = yScale * this.extremes[3] + 0.5 * ( 1.0 - AXIS_COV ) * this.height; - - // Modify scaling and offset - if ( this.xvy ) - { - xScale *= AXIS_COV; - xOffset = xScale * this.extremes[0] - 0.5 * ( 1.0 - AXIS_COV ) * this.width; - } + // Calculations for offset and scaling of graph ( vs. time ) + double xScale = this.width / ( this.extremes[1] - this.extremes[0] ); + double xOffset = xScale * this.extremes[0]; + double yScale = AXIS_COV * this.height / ( this.extremes[3] - this.extremes[2] ); + double yOffset = yScale * this.extremes[3] + 0.5 * ( 1.0 - AXIS_COV ) * this.height; + + // Modify scaling and offset + if ( this.xvy ) + { + xScale *= AXIS_COV; + xOffset = xScale * this.extremes[0] - 0.5 * ( 1.0 - AXIS_COV ) * this.width; + } // Do actual data plotting - for ( int i = 0; i < this.numVars; i++ ) - { - this.parent.stroke( this.colors[i] ); - for ( int j = 0; j < this.currPoints; j++ ) - { - this.parent.point( (float)(this.posX + (this.data[j][i][0]*xScale - xOffset)), - (float)(this.posY + yOffset - data[j][i][1]*yScale) ); - } - } + for ( int i = 0; i < this.numVars; i++ ) + { + this.parent.stroke( this.colors[i] ); + for ( int j = 0; j < this.currPoints; j++ ) + { + this.parent.point( (float)(this.posX + (this.data[j][i][0]*xScale - xOffset)), + (float)(this.posY + yOffset - data[j][i][1]*yScale) ); + } + } - // X vs Y and vs Time specific stuff - if ( this.xvy ) - { - this.DrawXYStuff(); - } - else - { - this.DrawTimeStuff(); - } - - // Draw Ticks - this.DrawTicks( xScale, xOffset, yScale, yOffset ); + // X vs Y and vs Time specific stuff + if ( this.xvy ) + { + this.DrawXYStuff(); + } + else + { + this.DrawTimeStuff(); + } + + // Draw Ticks + this.DrawTicks( xScale, xOffset, yScale, yOffset ); } @@ -209,198 +209,198 @@ public void Plot() private void DrawTimeStuff() { - int labelSz = (int) ( (this.width + this.height) / 2.0f * LABEL_SZ ); + int labelSz = (int) ( (this.width + this.height) / 2.0f * LABEL_SZ ); - // Setup legend start - float textPos = this.posY + labelSz; - this.parent.textAlign( this.parent.RIGHT, this.parent.TOP ); - this.parent.textSize( labelSz ); - - // Draw each legend entry - for ( int i = 0; i < this.numVars; i++ ) - { - this.parent.fill( this.colors[i] ); - this.parent.text( this.labels[i], this.posX + this.width - 10, textPos); - textPos += ( labelSz + labelSz/4 ); - this.parent.stroke( this.colors[i] ); - } + // Setup legend start + float textPos = this.posY + labelSz; + this.parent.textAlign( this.parent.RIGHT, this.parent.TOP ); + this.parent.textSize( labelSz ); + + // Draw each legend entry + for ( int i = 0; i < this.numVars; i++ ) + { + this.parent.fill( this.colors[i] ); + this.parent.text( this.labels[i], this.posX + this.width - 10, textPos); + textPos += ( labelSz + labelSz/4 ); + this.parent.stroke( this.colors[i] ); + } } private void DrawXYStuff() { - // X and Y labels - this.parent.textSize( (int)( (this.width + this.height) / 2.0f * LABEL_SZ) ); - this.parent.textAlign( this.parent.LEFT, this.parent.TOP ); - this.parent.text( this.labels[1], this.posX + 10, this.posY + 10); + // X and Y labels + this.parent.textSize( (int)( (this.width + this.height) / 2.0f * LABEL_SZ) ); + this.parent.textAlign( this.parent.LEFT, this.parent.TOP ); + this.parent.text( this.labels[1], this.posX + 10, this.posY + 10); - this.parent.textAlign( this.parent.RIGHT, this.parent.BOTTOM ); - this.parent.text( this.labels[0], this.posX + this.width - 10, this.posY + this.height - 3.5f*TICK_LEN); + this.parent.textAlign( this.parent.RIGHT, this.parent.BOTTOM ); + this.parent.text( this.labels[0], this.posX + this.width - 10, this.posY + this.height - 3.5f*TICK_LEN); } private void DrawTicks( double xScale, double xOffset, double yScale, double yOffset ) { - // Label graph with numbered tick marks - this.parent.stroke( 255 ); - this.parent.fill( 255 ); - this.parent.textSize( (int)( ( this.width + this.height ) / 2.0f * NUM_SZ ) ); - this.parent.textAlign( this.parent.LEFT, this.parent.CENTER ); - - // Draw ticks along y-axis - float tempX = this.posX - TICK_LEN / 2; - float tickOffset = 0.5f * ( 1.0f - AXIS_COV ) * this.height; - float tickInterval = AXIS_COV * this.height / (NUM_TICKS - 1); - for ( float tempY = this.posY + tickOffset; tempY <= this.posY + this.height - tickOffset; - tempY += tickInterval ) - { - float val = (float) ( ( ( yOffset + this.posY ) - (double)tempY ) / yScale ); - String fmt = GetNumberFormat( val ); - this.parent.line( tempX, tempY, tempX + TICK_LEN, tempY ); - this.parent.text( String.format( fmt, val ), tempX + TICK_LEN + 5, tempY ); - } - - // x-axis - this.parent.textAlign( this.parent.CENTER, this.parent.BOTTOM ); - float tempY = this.posY + this.height - TICK_LEN / 2; - tickOffset = 0.5f * ( 1.0f - AXIS_COV ) * this.width; - tickInterval = AXIS_COV * this.width / (NUM_TICKS - 1); - for ( tempX = this.posX + tickOffset; tempX <= this.posX + this.width - tickOffset; - tempX += tickInterval ) - { - float val = (float) ( ( (double)tempX + xOffset - this.posX ) / xScale ); - this.parent.line( tempX, tempY, tempX, tempY + TICK_LEN ); - if ( this.xvy ) - { - String fmt = GetNumberFormat( val ); - this.parent.text( String.format( fmt, val ), tempX, tempY - 5 ); - } - else - { - this.parent.text( String.format( "%d", (int)val ), tempX, tempY - 5 ); - } - } + // Label graph with numbered tick marks + this.parent.stroke( 255 ); + this.parent.fill( 255 ); + this.parent.textSize( (int)( ( this.width + this.height ) / 2.0f * NUM_SZ ) ); + this.parent.textAlign( this.parent.LEFT, this.parent.CENTER ); + + // Draw ticks along y-axis + float tempX = this.posX - TICK_LEN / 2; + float tickOffset = 0.5f * ( 1.0f - AXIS_COV ) * this.height; + float tickInterval = AXIS_COV * this.height / (NUM_TICKS - 1); + for ( float tempY = this.posY + tickOffset; tempY <= this.posY + this.height - tickOffset; + tempY += tickInterval ) + { + float val = (float) ( ( ( yOffset + this.posY ) - (double)tempY ) / yScale ); + String fmt = GetNumberFormat( val ); + this.parent.line( tempX, tempY, tempX + TICK_LEN, tempY ); + this.parent.text( String.format( fmt, val ), tempX + TICK_LEN + 5, tempY ); + } + + // x-axis + this.parent.textAlign( this.parent.CENTER, this.parent.BOTTOM ); + float tempY = this.posY + this.height - TICK_LEN / 2; + tickOffset = 0.5f * ( 1.0f - AXIS_COV ) * this.width; + tickInterval = AXIS_COV * this.width / (NUM_TICKS - 1); + for ( tempX = this.posX + tickOffset; tempX <= this.posX + this.width - tickOffset; + tempX += tickInterval ) + { + float val = (float) ( ( (double)tempX + xOffset - this.posX ) / xScale ); + this.parent.line( tempX, tempY, tempX, tempY + TICK_LEN ); + if ( this.xvy ) + { + String fmt = GetNumberFormat( val ); + this.parent.text( String.format( fmt, val ), tempX, tempY - 5 ); + } + else + { + this.parent.text( String.format( "%d", (int)val ), tempX, tempY - 5 ); + } + } } private String GetNumberFormat( float value ) { - int n = SIG_DIGITS; - int d = 1; - while ( n > 0 && Math.round( Math.abs( value / d ) ) > 0 ) - { - n--; - d *= 10; - } - - String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n ); - if ( ( Math.abs( value ) > 1000 || Math.abs( value ) < 0.001 ) && value != 0 ) - { - fmt += "e"; - } - else - { - fmt += "f"; - } - return fmt; + int n = SIG_DIGITS; + int d = 1; + while ( n > 0 && Math.round( Math.abs( value / d ) ) > 0 ) + { + n--; + d *= 10; + } + + String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n ); + if ( ( Math.abs( value ) > 1000 || Math.abs( value ) < 0.001 ) && value != 0 ) + { + fmt += "e"; + } + else + { + fmt += "f"; + } + return fmt; } private void CheckExtremes() { - // Check new values - this.CompareToExtremes( this.index ); + // Check new values + this.CompareToExtremes( this.index ); - // Time extremes - if ( ! this.xvy ) - { - // Get index of oldest data point - int oldest = this.index + 1; - if ( oldest >= this.currPoints ) - { - oldest = 0; - } + // Time extremes + if ( ! this.xvy ) + { + // Get index of oldest data point + int oldest = this.index + 1; + if ( oldest >= this.currPoints ) + { + oldest = 0; + } - if ( this.currPoints < this.maxPoints ) - { - // Estimate lower extreme - this.extremes[0] = this.data[this.index][0][0] - - ( this.data[this.index][0][0] - this.data[oldest][0][0] ) - * ( (double)this.maxPoints / (double)this.currPoints ); - } - else - { - // Normally just take oldest - this.extremes[0] = this.data[oldest][0][0]; - } - this.extremesCounter[0] = 0; - this.extremes[1] = this.data[this.index][0][0]; - this.extremesCounter[1] = 0; - } + if ( this.currPoints < this.maxPoints ) + { + // Estimate lower extreme + this.extremes[0] = this.data[this.index][0][0] + - ( this.data[this.index][0][0] - this.data[oldest][0][0] ) + * ( (double)this.maxPoints / (double)this.currPoints ); + } + else + { + // Normally just take oldest + this.extremes[0] = this.data[oldest][0][0]; + } + this.extremesCounter[0] = 0; + this.extremes[1] = this.data[this.index][0][0]; + this.extremesCounter[1] = 0; + } // Check for extremes going out of scope - boolean recalc = false; - for ( int i = 0; i < 4; i++ ) - { - this.extremesCounter[i]++; - recalc |= this.extremesCounter[i] > this.maxPoints; - } + boolean recalc = false; + for ( int i = 0; i < 4; i++ ) + { + this.extremesCounter[i]++; + recalc |= this.extremesCounter[i] > this.maxPoints; + } - if ( ! recalc ) - { - return; - } + if ( ! recalc ) + { + return; + } - // this.parent.println("Recalculating extremes..."); + // this.parent.println("Recalculating extremes..."); - // Full re-calculation for new extremes - if ( this.xvy ) - { - this.extremes[0] = this.data[0][0][0]; // (x-min) - this.extremesCounter[0] = 0; - this.extremes[1] = this.data[0][0][0]; // (x-max) - this.extremesCounter[1] = 0; - } - this.extremes[2] = this.data[0][0][1]; // (y-min) - this.extremesCounter[2] = 0; - this.extremes[3] = this.data[0][0][1]; // (y-max) - this.extremesCounter[3] = 0; + // Full re-calculation for new extremes + if ( this.xvy ) + { + this.extremes[0] = this.data[0][0][0]; // (x-min) + this.extremesCounter[0] = 0; + this.extremes[1] = this.data[0][0][0]; // (x-max) + this.extremesCounter[1] = 0; + } + this.extremes[2] = this.data[0][0][1]; // (y-min) + this.extremesCounter[2] = 0; + this.extremes[3] = this.data[0][0][1]; // (y-max) + this.extremesCounter[3] = 0; - for ( int i = 0; i < this.currPoints; i++ ) - { - this.CompareToExtremes( i ); - } + for ( int i = 0; i < this.currPoints; i++ ) + { + this.CompareToExtremes( i ); + } } private void CompareToExtremes( int i ) // i : dataIndex { - for ( int j = 0; j < this.numVars; j++ ) - { - // Y max/min - if ( this.data[i][j][1] < this.extremes[2] ) // (min) - { - this.extremes[2] = this.data[i][j][1]; - this.extremesCounter[2] = 0; - } - else if ( this.data[i][j][1] > this.extremes[3] ) // (max) - { - this.extremes[3] = this.data[i][j][1]; - this.extremesCounter[3] = 0; - } - // X max/min - if ( this.xvy ) - { - if ( this.data[i][j][0] < this.extremes[0] ) // (min) - { - this.extremes[0] = this.data[i][j][0]; - this.extremesCounter[0] = 0; - } - else if ( this.data[i][j][0] > this.extremes[1] ) // (max) - { - this.extremes[1] = this.data[i][j][0]; - this.extremesCounter[1] = 0; - } - } - } + for ( int j = 0; j < this.numVars; j++ ) + { + // Y max/min + if ( this.data[i][j][1] < this.extremes[2] ) // (min) + { + this.extremes[2] = this.data[i][j][1]; + this.extremesCounter[2] = 0; + } + else if ( this.data[i][j][1] > this.extremes[3] ) // (max) + { + this.extremes[3] = this.data[i][j][1]; + this.extremesCounter[3] = 0; + } + // X max/min + if ( this.xvy ) + { + if ( this.data[i][j][0] < this.extremes[0] ) // (min) + { + this.extremes[0] = this.data[i][j][0]; + this.extremesCounter[0] = 0; + } + else if ( this.data[i][j][0] > this.extremes[1] ) // (max) + { + this.extremes[1] = this.data[i][j][0]; + this.extremesCounter[1] = 0; + } + } + } } // Constants diff --git a/src/Plotter.cpp b/src/Plotter.cpp index 66d7831..d2373cf 100644 --- a/src/Plotter.cpp +++ b/src/Plotter.cpp @@ -139,4 +139,3 @@ void Plotter::plot() counter = 0; } } -