├── .gitignore ├── LICENSE ├── README.md ├── examples ├── control_points_displayed │ └── control_points_displayed.ino ├── different_types │ └── different_types.ino ├── dynamically_modify_graph_layout │ └── dynamically_modify_graph_layout.ino ├── multivariable_plotting │ └── multivariable_plotting.ino ├── quick_start │ └── quick_start.ino └── set_colors │ └── set_colors.ino ├── keywords.txt ├── library.properties ├── listener ├── Graph.java └── listener.pde └── src ├── Plotter.cpp └── Plotter.h /.gitignore: -------------------------------------------------------------------------------- 1 | plotter/LICENSE 2 | plotter/README.md -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Devin Conley 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arduino-plotter 2 | 3 | [![arduino-library-badge](https://www.ardu-badge.com/badge/Plotter.svg?)](https://www.ardu-badge.com/Plotter) 4 | 5 | **Plotter** is an Arduino library for easy graphing on host computer via serial communication 6 | 7 | --- 8 | 9 | ### Features: 10 | - Continuous multi-variable plots against time 11 | - 2-variable "x" vs "y" plots 12 | - Display multiple graphs within single resizable window 13 | - Support for any data type that can be cast to a double 14 | - Simply pass a reference to your variables when the graph is added, no need to update each value explicitly 15 | - Control number of data points displayed on each graph 16 | - Auto-scaling to fit all data on graph 17 | - Configurable line color per variable 18 | - Stand-alone listener application, written with Processing, is provided 19 | 20 | ![Plotter Preview](https://www.dropbox.com/s/2mtg5ig7lyrrffi/plotter_preview.gif?raw=1) 21 | 22 | --- 23 | 24 | ### Extremely easy usage: 25 | ```c++ 26 | #include "Plotter.h" 27 | 28 | double x; // global variables 29 | Plotter p; // create plotter 30 | 31 | void setup() 32 | { 33 | p.Begin(); // start plotter 34 | 35 | p.AddTimeGraph( "Some title of a graph", 1500, "label for x", x ); // add any graphs you want 36 | } 37 | 38 | void loop() 39 | { 40 | x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); // update your variables like usual 41 | 42 | p.Plot(); // plot all current data -- usually called within loop() 43 | } 44 | ``` 45 | 46 | --- 47 | 48 | ### See the Wiki for more information: 49 | [Home](https://github.com/devinaconley/arduino-plotter/wiki) 50 | [Quickstart](https://github.com/devinaconley/arduino-plotter/wiki/Installation-and-Quickstart) 51 | [Documentation](https://github.com/devinaconley/arduino-plotter/wiki/Documentation) 52 | -------------------------------------------------------------------------------- /examples/control_points_displayed/control_points_displayed.ino: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Example to demonstrate effect of "Points Displayed" in Time Graph and in XY Graph 4 | ------------------------------------------------------------------------------------------- 5 | Plotter 6 | v2.4.1 7 | https://github.com/devinaconley/arduino-plotter 8 | by Devin Conley 9 | =========================================================================================== 10 | */ 11 | 12 | #include "Plotter.h" 13 | 14 | // Plotted variables must be declared as globals 15 | double x; 16 | double y; 17 | 18 | // Also declare plotter as global 19 | Plotter p; 20 | 21 | void setup() 22 | { 23 | // Start plotter 24 | p.Begin(); 25 | 26 | // Add X-Y graphs 27 | p.AddXYGraph( "X-Y graph w/ 500 points", 500, "x axis", x, "y axis", y ); 28 | p.AddXYGraph( "X-Y graph w/ 200 points", 200, "x axis", x, "y axis", y ); 29 | 30 | // Add time graphs. Notice the effect of points displayed on the time scale 31 | p.AddTimeGraph( "Time graph w/ 500 points", 500, "x label", x ); 32 | p.AddTimeGraph( "Time graph w/ 200 points", 200, "x label", x ); 33 | 34 | } 35 | 36 | void loop() 37 | { 38 | // Update variables with arbitrary sine/cosine data 39 | x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); 40 | y = 10*cos( 2.0*PI*( millis() / 5000.0 ) ); 41 | 42 | // Plot 43 | p.Plot(); 44 | } 45 | -------------------------------------------------------------------------------- /examples/different_types/different_types.ino: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Example to demonstrate effect of plotting different variable types 4 | ------------------------------------------------------------------------------------------- 5 | Plotter 6 | v2.4.1 7 | https://github.com/devinaconley/arduino-plotter 8 | by Devin Conley 9 | =========================================================================================== 10 | */ 11 | 12 | #include "Plotter.h" 13 | 14 | // Plotted variables must be declared as globals 15 | float f; 16 | float f2; 17 | int i; 18 | char ch; 19 | 20 | // Also declare plotter as global 21 | Plotter p; 22 | 23 | void setup() 24 | { 25 | // Start plotter 26 | p.Begin(); 27 | 28 | // Add time graphs. 29 | p.AddTimeGraph( "float vs int", 500, "float", f, "int", i ); 30 | p.AddTimeGraph( "float vs char", 500, "float", f2, "unsigned char", ch ); 31 | 32 | } 33 | 34 | void loop() 35 | { 36 | // Update different variable types with arbitrary sine/cosine data 37 | unsigned long time = millis(); 38 | f = 5*sin( 2.0*PI*( time / 5000.0 ) ); 39 | i = 5*sin( 2.0*PI*( time / 5000.0 ) ); 40 | f2 = 300*sin( 2.0*PI*( time / 5000.0 ) ); 41 | ch = 300*sin( 2.0*PI*( time / 5000.0 ) ); 42 | 43 | // Plot 44 | p.Plot(); 45 | } 46 | -------------------------------------------------------------------------------- /examples/dynamically_modify_graph_layout/dynamically_modify_graph_layout.ino: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Example to demonstrate dynamic addition and removal of graphs 4 | ------------------------------------------------------------------------------------------- 5 | Plotter 6 | v2.4.1 7 | https://github.com/devinaconley/arduino-plotter 8 | by Devin Conley 9 | =========================================================================================== 10 | */ 11 | 12 | #include "Plotter.h" 13 | 14 | // Plotted variables must be declared as globals 15 | double x; 16 | double y; 17 | double z; 18 | 19 | // Also declare plotter as global 20 | Plotter p; 21 | 22 | // Booleans so each modification only happens once 23 | boolean add_first = true; 24 | boolean add_second = true; 25 | boolean remove_graph = true; 26 | 27 | void setup() 28 | { 29 | // Plotter constructor 30 | p.Begin(); 31 | 32 | // Add a graph during setup 33 | p.AddTimeGraph( "x and y against time", 1000, "x label", x, "y label", y ); 34 | } 35 | 36 | void loop() 37 | { 38 | // After 3 seconds add a 1-variable graph 39 | if ( millis() > 3000 && add_first ) { 40 | p.AddTimeGraph( "First dynamic addition", 1000, "z label", z ); 41 | add_first = false; 42 | } 43 | 44 | // After 5 seconds, add a 3-variable graph 45 | if ( millis() > 5000 && add_second ) { 46 | p.AddTimeGraph( "Second dynamic addition", 1000, "x label", x, "y label", y, "z label", z ); 47 | add_second = false; 48 | } 49 | 50 | // After 10 seconds, remove the 1-variable graph 51 | if ( millis() > 10000 && remove_graph ) { 52 | p.Remove( 1 ); // (an index of 1 will remove the second graph added) 53 | remove_graph = false; 54 | } 55 | 56 | // Update x, y and z variables with arbitrary sine/cosine data 57 | x = 3*sin( 2.0*PI*( millis() / 2000.0 ) ); 58 | y = 2*cos( 2.0*PI*( millis() / 2000.0 ) ); 59 | z = 1*sin( 2.0*PI*( millis() / 2000.0 ) ); 60 | 61 | // Plot 62 | p.Plot(); 63 | } -------------------------------------------------------------------------------- /examples/multivariable_plotting/multivariable_plotting.ino: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Example to demonstrate multi-variable plotting against time 4 | ------------------------------------------------------------------------------------------- 5 | Plotter 6 | v2.4.1 7 | https://github.com/devinaconley/arduino-plotter 8 | by Devin Conley 9 | =========================================================================================== 10 | */ 11 | 12 | #include "Plotter.h" 13 | 14 | // Plotted variables must be declared as globals 15 | double v; 16 | double w; 17 | double x; 18 | double y; 19 | double z; 20 | 21 | // Also declare plotter as global 22 | Plotter p; 23 | 24 | void setup() 25 | { 26 | // Start plotter 27 | p.Begin(); 28 | 29 | // Add 5 variable time graph 30 | p.AddTimeGraph( "5 variable time graph", 1000, "v label", v, "w label", w, "x label", x, 31 | "y label", y, "z label", z ); 32 | } 33 | 34 | void loop() 35 | { 36 | // Update variables with arbitrary sine/cosine data 37 | v = 3*cos( 2.0*PI*( millis()/2500.0 ) ); 38 | w = 4.0; 39 | x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); 40 | y = 7*cos( 2.0*PI*( millis() / 5000.0 ) ); 41 | z = 5*sin( 2.0*PI*( millis() / 5000.0 ) ); 42 | 43 | // Plot 44 | p.Plot(); 45 | } 46 | -------------------------------------------------------------------------------- /examples/quick_start/quick_start.ino: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Example used in Quick-Start 4 | ------------------------------------------------------------------------------------------- 5 | Plotter 6 | v2.4.1 7 | https://github.com/devinaconley/arduino-plotter 8 | by Devin Conley 9 | =========================================================================================== 10 | */ 11 | 12 | #include "Plotter.h" 13 | 14 | double x; 15 | 16 | Plotter p; 17 | 18 | void setup() 19 | { 20 | p.Begin(); 21 | 22 | p.AddTimeGraph( "Some title of a graph", 1500, "label for x", x ); 23 | } 24 | 25 | void loop() { 26 | x = 10*sin( 2.0*PI*( millis() / 5000.0 ) ); 27 | 28 | p.Plot(); // usually called within loop() 29 | } -------------------------------------------------------------------------------- /examples/set_colors/set_colors.ino: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Example to demonstrate configuring specific colors for each variable 4 | ------------------------------------------------------------------------------------------- 5 | Plotter 6 | v2.4.1 7 | https://github.com/deviaconley/arduino-plotter 8 | by Devin Conley 9 | =========================================================================================== 10 | */ 11 | 12 | #include "Plotter.h" 13 | 14 | double x; 15 | double y; 16 | 17 | Plotter p; 18 | 19 | void setup() 20 | { 21 | // Start plotter 22 | p.Begin(); 23 | 24 | // Add plots 25 | p.AddTimeGraph( "Some title of a graph", 500, "label for x", x, "label for y", y ); 26 | p.AddXYGraph( "Title of X vs Y graph", 1000, "x axis", x, "y axis", y ); 27 | 28 | // Set variable colors of graph with index 0 to pink and orange 29 | p.SetColor( 0, "pink", "orange" ); 30 | // Set color of x vs y graph at index 1 to cyan 31 | p.SetColor( 1, "cyan" ); 32 | 33 | } 34 | 35 | void loop() { 36 | x = 0.0009*sin( 2.0*PI*( millis() / 5000.0 ) ); 37 | y = 90000*cos( 2.0*PI*( millis() / 5000.0 ) ); 38 | 39 | p.Plot(); // usually called within loop() 40 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | Plotter KEYWORD1 2 | Begin KEYWORD2 3 | AddTimeGraph KEYWORD2 4 | AddXYGraph KEYWORD2 5 | Plot KEYWORD2 6 | Remove KEYWORD2 7 | SetColor KEYWORD2 8 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Plotter 2 | version=2.4.1 3 | author=Devin Conley 4 | maintainer=Devin Conley 5 | sentence=An Arduino library for easy plotting on host computer via serial communication. 6 | paragraph=Supports multi-variable plots against time as well as 2D plotting of an X vs Y variable. Multiple graphs can be displayed at once, with all formatting and scaling handled automatically. A stand-alone listener application, written with Processing, is provided. 7 | category=Data Processing 8 | url=https://github.com/devinaconley/arduino-plotter 9 | architectures=* -------------------------------------------------------------------------------- /listener/Graph.java: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | This Graph class handles an individual graph being displayed on the Arduino Plotter 4 | listener. 5 | ------------------------------------------------------------------------------------------- 6 | The library stores and handles all relevant graph information and variable references, 7 | and transfers information via the serial port to a listener program written with the 8 | software provided by Processing. No modification is needed to this program; graph placement, 9 | axis-scaling, etc. are handled automatically. 10 | Multiple options for this listener are available including stand-alone applications as well 11 | as the source Processing script. 12 | 13 | The library, these listeners, a quick-start guide, documentation, and usage examples are 14 | available at: 15 | 16 | https://github.com/devinaconley/arduino-plotter 17 | 18 | ------------------------------------------------------------------------------------------- 19 | Arduino Plotter Listener 20 | v2.4.1 21 | https://github.com/devinaconley/arduino-plotter 22 | by Devin Conley 23 | =========================================================================================== 24 | */ 25 | 26 | 27 | import processing.core.PApplet; 28 | 29 | class Graph 30 | { 31 | // Reference to PApplet drawing canvas 32 | PApplet parent; 33 | 34 | // Config 35 | float posY; 36 | float posX; 37 | float height; 38 | float width; 39 | 40 | boolean xvy; // ( alternative is vs time ) 41 | int numVars; 42 | int maxPoints; 43 | String title; 44 | String[] labels; 45 | int[] colors; // TODO: Configurable colors 46 | 47 | // Data 48 | int index; 49 | double[][][] data; 50 | int[] extremesCounter; 51 | double[] extremes; // {min_x, max_x, min_y, max_y} 52 | int currPoints; 53 | 54 | // Contructor 55 | public Graph( PApplet parent, float posY, float posX, float height, float width, 56 | boolean xvy, int numVars, int maxPoints, 57 | String title, String[] labels, int[] colors ) 58 | { 59 | this.parent = parent; 60 | this.posY = posY; 61 | this.posX = posX; 62 | this.height = height; 63 | this.width = width; 64 | this.xvy = xvy; 65 | this.numVars = numVars; 66 | this.maxPoints = maxPoints; 67 | this.title = title; 68 | this.labels = labels; 69 | this.colors = colors; 70 | 71 | // this.parent.println( "Constructed new graph: ", this.title, " at ", this.posY, " ", this.posX ); 72 | 73 | // Initialize 74 | this.index = 0; 75 | this.data = new double[maxPoints][numVars][2]; 76 | this.extremesCounter = new int[4]; 77 | this.extremes = new double[4]; 78 | this.currPoints = 0; 79 | } 80 | 81 | // Modifiers 82 | public void Reconfigure( float posY, float posX, float height, float width, 83 | boolean xvy, int numVars, int maxPoints, 84 | String title, String[] labels ) 85 | { 86 | this.posY = posY; 87 | this.posX = posX; 88 | this.height = height; 89 | this.width = width; 90 | this.xvy = xvy; 91 | this.numVars = numVars; 92 | this.maxPoints = maxPoints; 93 | this.title = title; 94 | this.labels = labels; 95 | } 96 | 97 | public void Reconfigure( float posY, float posX, float height, float width ) 98 | { 99 | this.posY = posY; 100 | this.posX = posX; 101 | this.height = height; 102 | this.width = width; 103 | } 104 | 105 | public void Update( double[] newData, int time ) 106 | { 107 | // Store data 108 | if ( this.xvy ) 109 | { 110 | // Validate 111 | if ( newData.length != 2 ) 112 | { 113 | //this.parent.println( "Invalid data passed to X v. Y graph." ); 114 | return; 115 | } 116 | 117 | this.data[this.index][0][0] = newData[0]; 118 | this.data[this.index][0][1] = newData[1]; 119 | } 120 | else 121 | { 122 | // Validate 123 | if ( newData.length != this.numVars ) 124 | { 125 | //this.parent.println( "Invalid data passed to time graph." ); 126 | return; 127 | } 128 | 129 | for ( int i = 0; i < this.numVars; i++ ) 130 | { 131 | this.data[this.index][i][0] = time; 132 | this.data[this.index][i][1] = newData[i]; 133 | } 134 | } 135 | 136 | // Counter for num points defined 137 | if ( this.currPoints < this.maxPoints ) 138 | { 139 | this.currPoints++; 140 | } 141 | 142 | // Check extremes 143 | this.CheckExtremes(); 144 | 145 | // Advance index position and rollback if needed 146 | this.index++; 147 | if ( this.index >= this.maxPoints ) 148 | { 149 | this.index = 0; 150 | } 151 | } 152 | 153 | public void Plot() 154 | { 155 | // Plot Background 156 | this.parent.fill( PLOT_COL ); 157 | this.parent.stroke( PLOT_COL ); 158 | this.parent.strokeWeight( (this.width + this.height) / 2.0f * PT_SZ ); 159 | this.parent.rect( this.posX, this.posY, this.width, this.height ); 160 | 161 | // Title 162 | this.parent.textSize( (int)( (this.width + this.height) / 2.0f * TITLE_SZ ) ); 163 | this.parent.fill( 255 ); 164 | this.parent.textAlign( this.parent.CENTER, this.parent.TOP ); 165 | this.parent.text( this.title, this.posX + this.width / 2, this.posY + 10 ); 166 | 167 | 168 | // Calculations for offset and scaling of graph ( vs. time ) 169 | double xScale = this.width / ( this.extremes[1] - this.extremes[0] ); 170 | double xOffset = xScale * this.extremes[0]; 171 | double yScale = AXIS_COV * this.height / ( this.extremes[3] - this.extremes[2] ); 172 | double yOffset = yScale * this.extremes[3] + 0.5 * ( 1.0 - AXIS_COV ) * this.height; 173 | 174 | // Modify scaling and offset 175 | if ( this.xvy ) 176 | { 177 | xScale *= AXIS_COV; 178 | xOffset = xScale * this.extremes[0] - 0.5 * ( 1.0 - AXIS_COV ) * this.width; 179 | } 180 | 181 | 182 | // Do actual data plotting 183 | for ( int i = 0; i < this.numVars; i++ ) 184 | { 185 | this.parent.stroke( this.colors[i] ); 186 | for ( int j = 0; j < this.currPoints; j++ ) 187 | { 188 | this.parent.point( (float)(this.posX + (this.data[j][i][0]*xScale - xOffset)), 189 | (float)(this.posY + yOffset - data[j][i][1]*yScale) ); 190 | } 191 | } 192 | 193 | // X vs Y and vs Time specific stuff 194 | if ( this.xvy ) 195 | { 196 | this.DrawXYStuff(); 197 | } 198 | else 199 | { 200 | this.DrawTimeStuff(); 201 | } 202 | 203 | // Draw Ticks 204 | this.DrawTicks( xScale, xOffset, yScale, yOffset ); 205 | 206 | } 207 | 208 | // Private Helpers 209 | 210 | private void DrawTimeStuff() 211 | { 212 | int labelSz = (int) ( (this.width + this.height) / 2.0f * LABEL_SZ ); 213 | 214 | // Setup legend start 215 | float textPos = this.posY + labelSz; 216 | this.parent.textAlign( this.parent.RIGHT, this.parent.TOP ); 217 | this.parent.textSize( labelSz ); 218 | 219 | // Draw each legend entry 220 | for ( int i = 0; i < this.numVars; i++ ) 221 | { 222 | this.parent.fill( this.colors[i] ); 223 | this.parent.text( this.labels[i], this.posX + this.width - 10, textPos); 224 | textPos += ( labelSz + labelSz/4 ); 225 | this.parent.stroke( this.colors[i] ); 226 | } 227 | 228 | } 229 | 230 | private void DrawXYStuff() 231 | { 232 | // X and Y labels 233 | this.parent.textSize( (int)( (this.width + this.height) / 2.0f * LABEL_SZ) ); 234 | this.parent.textAlign( this.parent.LEFT, this.parent.TOP ); 235 | this.parent.text( this.labels[1], this.posX + 10, this.posY + 10); 236 | 237 | this.parent.textAlign( this.parent.RIGHT, this.parent.BOTTOM ); 238 | this.parent.text( this.labels[0], this.posX + this.width - 10, this.posY + this.height - 3.5f*TICK_LEN); 239 | } 240 | 241 | 242 | private void DrawTicks( double xScale, double xOffset, double yScale, double yOffset ) 243 | { 244 | // Label graph with numbered tick marks 245 | this.parent.stroke( 255 ); 246 | this.parent.fill( 255 ); 247 | this.parent.textSize( (int)( ( this.width + this.height ) / 2.0f * NUM_SZ ) ); 248 | this.parent.textAlign( this.parent.LEFT, this.parent.CENTER ); 249 | 250 | // Draw ticks along y-axis 251 | float tempX = this.posX - TICK_LEN / 2; 252 | float tickOffset = 0.5f * ( 1.0f - AXIS_COV ) * this.height; 253 | float tickInterval = AXIS_COV * this.height / (NUM_TICKS - 1); 254 | for ( float tempY = this.posY + tickOffset; tempY <= this.posY + this.height - tickOffset; 255 | tempY += tickInterval ) 256 | { 257 | float val = (float) ( ( ( yOffset + this.posY ) - (double)tempY ) / yScale ); 258 | String fmt = GetNumberFormat( val ); 259 | this.parent.line( tempX, tempY, tempX + TICK_LEN, tempY ); 260 | this.parent.text( String.format( fmt, val ), tempX + TICK_LEN + 5, tempY ); 261 | } 262 | 263 | // x-axis 264 | this.parent.textAlign( this.parent.CENTER, this.parent.BOTTOM ); 265 | float tempY = this.posY + this.height - TICK_LEN / 2; 266 | tickOffset = 0.5f * ( 1.0f - AXIS_COV ) * this.width; 267 | tickInterval = AXIS_COV * this.width / (NUM_TICKS - 1); 268 | for ( tempX = this.posX + tickOffset; tempX <= this.posX + this.width - tickOffset; 269 | tempX += tickInterval ) 270 | { 271 | float val = (float) ( ( (double)tempX + xOffset - this.posX ) / xScale ); 272 | this.parent.line( tempX, tempY, tempX, tempY + TICK_LEN ); 273 | if ( this.xvy ) 274 | { 275 | String fmt = GetNumberFormat( val ); 276 | this.parent.text( String.format( fmt, val ), tempX, tempY - 5 ); 277 | } 278 | else 279 | { 280 | this.parent.text( String.format( "%d", (int)val ), tempX, tempY - 5 ); 281 | } 282 | } 283 | 284 | } 285 | 286 | private String GetNumberFormat( float value ) 287 | { 288 | int n = SIG_DIGITS; 289 | int d = 1; 290 | while ( n > 0 && Math.round( Math.abs( value / d ) ) > 0 ) 291 | { 292 | n--; 293 | d *= 10; 294 | } 295 | 296 | String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n ); 297 | if ( ( Math.abs( value ) > 1000 || Math.abs( value ) < 0.001 ) && value != 0 ) 298 | { 299 | fmt += "e"; 300 | } 301 | else 302 | { 303 | fmt += "f"; 304 | } 305 | return fmt; 306 | } 307 | 308 | private void CheckExtremes() 309 | { 310 | // Check new values 311 | this.CompareToExtremes( this.index ); 312 | 313 | // Time extremes 314 | if ( ! this.xvy ) 315 | { 316 | // Get index of oldest data point 317 | int oldest = this.index + 1; 318 | if ( oldest >= this.currPoints ) 319 | { 320 | oldest = 0; 321 | } 322 | 323 | if ( this.currPoints < this.maxPoints ) 324 | { 325 | // Estimate lower extreme 326 | this.extremes[0] = this.data[this.index][0][0] 327 | - ( this.data[this.index][0][0] - this.data[oldest][0][0] ) 328 | * ( (double)this.maxPoints / (double)this.currPoints ); 329 | } 330 | else 331 | { 332 | // Normally just take oldest 333 | this.extremes[0] = this.data[oldest][0][0]; 334 | } 335 | this.extremesCounter[0] = 0; 336 | this.extremes[1] = this.data[this.index][0][0]; 337 | this.extremesCounter[1] = 0; 338 | } 339 | 340 | // Check for extremes going out of scope 341 | boolean recalc = false; 342 | for ( int i = 0; i < 4; i++ ) 343 | { 344 | this.extremesCounter[i]++; 345 | recalc |= this.extremesCounter[i] > this.maxPoints; 346 | } 347 | 348 | if ( ! recalc ) 349 | { 350 | return; 351 | } 352 | 353 | // this.parent.println("Recalculating extremes..."); 354 | 355 | // Full re-calculation for new extremes 356 | if ( this.xvy ) 357 | { 358 | this.extremes[0] = this.data[0][0][0]; // (x-min) 359 | this.extremesCounter[0] = 0; 360 | this.extremes[1] = this.data[0][0][0]; // (x-max) 361 | this.extremesCounter[1] = 0; 362 | } 363 | this.extremes[2] = this.data[0][0][1]; // (y-min) 364 | this.extremesCounter[2] = 0; 365 | this.extremes[3] = this.data[0][0][1]; // (y-max) 366 | this.extremesCounter[3] = 0; 367 | 368 | for ( int i = 0; i < this.currPoints; i++ ) 369 | { 370 | this.CompareToExtremes( i ); 371 | } 372 | } 373 | 374 | private void CompareToExtremes( int i ) // i : dataIndex 375 | { 376 | for ( int j = 0; j < this.numVars; j++ ) 377 | { 378 | // Y max/min 379 | if ( this.data[i][j][1] < this.extremes[2] ) // (min) 380 | { 381 | this.extremes[2] = this.data[i][j][1]; 382 | this.extremesCounter[2] = 0; 383 | } 384 | else if ( this.data[i][j][1] > this.extremes[3] ) // (max) 385 | { 386 | this.extremes[3] = this.data[i][j][1]; 387 | this.extremesCounter[3] = 0; 388 | } 389 | // X max/min 390 | if ( this.xvy ) 391 | { 392 | if ( this.data[i][j][0] < this.extremes[0] ) // (min) 393 | { 394 | this.extremes[0] = this.data[i][j][0]; 395 | this.extremesCounter[0] = 0; 396 | } 397 | else if ( this.data[i][j][0] > this.extremes[1] ) // (max) 398 | { 399 | this.extremes[1] = this.data[i][j][0]; 400 | this.extremesCounter[1] = 0; 401 | } 402 | } 403 | } 404 | } 405 | 406 | // Constants 407 | private static final float AXIS_COV = 0.75f; 408 | private static final int PLOT_COL = 115; 409 | private static final float LABEL_SZ = 0.025f; 410 | private static final float TITLE_SZ = 0.03f; 411 | private static final float NUM_SZ = 0.02f; 412 | private static final int TICK_LEN = 6; 413 | private static final int NUM_TICKS = 5; 414 | private static final float PT_SZ = 0.0025f; 415 | private static final int SIG_DIGITS = 3; 416 | } 417 | -------------------------------------------------------------------------------- /listener/listener.pde: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | This listener is the main processing script that corresponds to the Arduino Plotter 4 | library for Arduino. This driver script handles serial port information and manages a 5 | set of Graph objects to do the actual plotting. 6 | ------------------------------------------------------------------------------------------- 7 | The library stores and handles all relevant graph information and variable references, 8 | and transfers information via the serial port to a listener program written with the 9 | software provided by Processing. No modification is needed to this program; graph placement, 10 | axis-scaling, etc. are handled automatically. 11 | Multiple options for this listener are available including stand-alone applications as well 12 | as the source Processing script. 13 | 14 | The library, these listeners, a quick-start guide, documentation, and usage examples are 15 | available at: 16 | 17 | https://github.com/devinaconley/arduino-plotter 18 | 19 | ------------------------------------------------------------------------------------------- 20 | Arduino Plotter Listener 21 | v2.4.1 22 | https://github.com/devinaconley/arduino-plotter 23 | by Devin Conley 24 | =========================================================================================== 25 | */ 26 | 27 | import processing.serial.*; 28 | import java.util.Map; 29 | 30 | import java.util.concurrent.Callable; 31 | import java.util.concurrent.ExecutorService; 32 | import java.util.concurrent.Executors; 33 | import java.util.concurrent.Future; 34 | import java.util.concurrent.TimeUnit; 35 | import java.util.concurrent.TimeoutException; 36 | 37 | // FLAG FOR DEBUG MODE 38 | final boolean DEBUG = false; 39 | 40 | //CONSTANTS 41 | final char OUTER_KEY = '#'; 42 | final int MARGIN_SZ = 20; // between plots 43 | final int BG_COL = 75; // background 44 | final int PORT_INTERVAL = 5000; // time to sit on each port 45 | final int CONNECT_TIMEOUT = 2000; // force timeout on connecting to serial port 46 | final int BAUD_RATE = 115200; 47 | final HashMap COLORMAP = new HashMap() 48 | { 49 | { 50 | put( "red", color( 255, 0, 0 ) ); 51 | put( "green", color( 0, 255, 0 ) ); 52 | put( "blue", color( 0, 0, 255 ) ); 53 | put( "orange", color( 255, 153, 51 ) ); 54 | put( "yellow", color( 255, 255, 0 ) ); 55 | put( "pink", color( 255, 51, 204 ) ); 56 | put( "purple", color( 172, 0, 230 ) ); 57 | put( "cyan", color( 0, 255, 255 ) ); 58 | } 59 | }; 60 | 61 | // Setup and config Globals 62 | int h; 63 | int w; 64 | int numGraphs; 65 | String configCode = "This will not be matched!"; 66 | String lastLabels = "Also will not be matched"; 67 | boolean configured = false; 68 | int lastConfig; 69 | int lastPortSwitch; 70 | int portIndex; 71 | Serial port; 72 | ArrayList graphs; 73 | 74 | void setup() 75 | { 76 | // Canvas 77 | size( 800, 800 ); 78 | surface.setResizable( true ); 79 | h = height; 80 | w = width; 81 | frameRate( 100 ); 82 | 83 | // Serial comms 84 | while ( Serial.list().length < 1 ) 85 | { 86 | text( "No serial ports available. Waiting...", 20, 20 ); 87 | delay( 100 ); 88 | } 89 | portIndex = 0; 90 | lastPortSwitch = millis(); 91 | attemptConnect( portIndex ); 92 | } 93 | 94 | void draw() 95 | { 96 | //PLOT ALL 97 | try 98 | { 99 | background( BG_COL ); 100 | 101 | if ( configured ) 102 | { 103 | for( int i = 0; i < graphs.size(); i++ ) 104 | { 105 | graphs.get(i).Plot(); 106 | } 107 | } 108 | else 109 | { 110 | // Continue to scan ports if not configuring 111 | text( "Scanning serial ports... (" + Serial.list()[portIndex] + ")", 20, 20 ); 112 | 113 | if ( millis() - lastPortSwitch > PORT_INTERVAL ) 114 | { // Go to next port 115 | portIndex++; 116 | if ( portIndex >= Serial.list().length ) 117 | { 118 | portIndex = 0; 119 | } 120 | 121 | logMessage( "Trying next port... index: " + portIndex + ", name: " + Serial.list()[portIndex], 122 | true ); 123 | 124 | attemptConnect( portIndex ); 125 | } 126 | } 127 | // Resize if needed 128 | if ( h != height || w != width) 129 | { 130 | h = height; 131 | w = width; 132 | float[][] posGraphs = setupGraphPosition( numGraphs ); 133 | for ( int i = 0; i < numGraphs; i++ ) 134 | { 135 | graphs.get(i).Reconfigure( posGraphs[i][0], posGraphs[i][1], posGraphs[i][2], posGraphs[i][3] ); 136 | } 137 | } 138 | } 139 | catch ( Exception e ) 140 | {} 141 | } 142 | 143 | void serialEvent( Serial ser ) 144 | { 145 | // Listen for serial data until #, the end of transmission key 146 | try 147 | { 148 | String message = ser.readStringUntil( OUTER_KEY ); 149 | if ( message == null || message.isEmpty() || message.equals( OUTER_KEY ) ) 150 | { 151 | return; 152 | } 153 | 154 | JSONObject json = parseJSONObject( message ); 155 | 156 | if ( json == null ) 157 | { 158 | return; 159 | } 160 | 161 | // ********************************************************* // 162 | // ************* PLOT SETUP FROM CONFIG CODE *************** // 163 | // ********************************************************* // 164 | 165 | String tempCode = ""; 166 | boolean config = false; 167 | if ( json.hasKey( "ng" ) && json.hasKey( "lu" ) ) 168 | { 169 | tempCode = Integer.toString( json.getInt( "ng" ) ) + Integer.toString( json.getInt( "lu" ) ); 170 | config = true; 171 | } 172 | 173 | // If config code has changed, need to go through setup again 174 | if ( config && !configCode.equals( tempCode ) ) 175 | { 176 | lastPortSwitch = millis(); // (likely on the right port, just need to reconfigure graph layout) 177 | 178 | // Check for size of full transmission against expected to flag bad transmission 179 | numGraphs = json.getInt( "ng" ); 180 | 181 | JSONArray jsonGraphs = json.getJSONArray( "g" ); 182 | 183 | if ( jsonGraphs.size() != numGraphs ) 184 | { 185 | return; 186 | } 187 | 188 | configured = false; 189 | String concatLabels = ""; 190 | 191 | // Setup new layout 192 | float[][] posGraphs = setupGraphPosition( numGraphs ); 193 | 194 | graphs = new ArrayList(); 195 | 196 | // Iterate through the individual graph data blocks to get graph specific info 197 | for ( int i = 0; i < numGraphs; i++ ) 198 | { 199 | JSONObject g = jsonGraphs.getJSONObject( i ); 200 | 201 | String title = g.getString( "t" ); 202 | boolean xvyTemp = g.getInt( "xvy" ) == 1; 203 | int maxPoints = g.getInt( "pd" ); 204 | int numVars = g.getInt( "sz" ); 205 | String[] labelsTemp = new String[numVars]; 206 | int[] colorsTemp = new int[numVars]; 207 | 208 | concatLabels += title; 209 | 210 | JSONArray l = g.getJSONArray( "l" ); 211 | JSONArray c = g.getJSONArray( "c" ); 212 | for ( int j = 0; j < numVars; j++ ) 213 | { 214 | labelsTemp[j] = l.getString( j ); 215 | colorsTemp[j] = COLORMAP.get( c.getString( j ) ); 216 | if ( colorsTemp[j] == 0 ) 217 | { 218 | logMessage( "Invalid color: " + c.getString( j ) + ", defaulting to green.", true ); 219 | colorsTemp[j] = COLORMAP.get( "green" ); 220 | } 221 | concatLabels += labelsTemp[j]; 222 | } 223 | 224 | if ( xvyTemp ) 225 | { 226 | numVars = 1; 227 | } 228 | 229 | // Create new Graph 230 | Graph temp = new Graph( this, posGraphs[i][0], posGraphs[i][1], posGraphs[i][2], posGraphs[i][3], 231 | xvyTemp, numVars, maxPoints, title, labelsTemp, colorsTemp ); 232 | graphs.add( temp ); 233 | } 234 | 235 | // Set new config code 236 | if ( concatLabels.equals( lastLabels ) ) // Only when we're sure on labels 237 | { 238 | configCode = tempCode; 239 | lastConfig = millis(); 240 | logMessage( "Configured " + graphs.size() + " graphs", false ); 241 | } 242 | lastLabels = concatLabels; 243 | 244 | logMessage( "Config code: " + configCode + ", Label config: " + concatLabels, true ); 245 | } 246 | else 247 | { 248 | // Matching a code means we have configured correctly 249 | configured = true; 250 | 251 | // *********************************************************** // 252 | // ************ NORMAL PLOTTING FUNCTIONALITY **************** // 253 | // *********************************************************** // 254 | int tempTime = json.getInt( "t" ); 255 | 256 | JSONArray jsonGraphs = json.getJSONArray( "g" ); 257 | 258 | for ( int i = 0; i < numGraphs; i++ ) 259 | { 260 | JSONArray data = jsonGraphs.getJSONObject( i ).getJSONArray( "d" ); 261 | 262 | double[] tempData = new double[ data.size() ]; 263 | 264 | // Update graph objects with new data 265 | for ( int j = 0; j < data.size(); j++ ) 266 | { 267 | tempData[j] = data.getDouble( j ); 268 | } 269 | graphs.get( i ).Update( tempData, tempTime ); 270 | } 271 | } 272 | } 273 | catch ( Exception e ) 274 | { 275 | logMessage( "Exception in serialEvent: " + e.toString(), true ); 276 | } 277 | } 278 | 279 | // Helper method to calculate bounds of graphs 280 | float[][] setupGraphPosition( int numGraphs ) 281 | { 282 | // Determine orientation of each graph 283 | int numHigh = 1; 284 | int numWide = 1; 285 | // Increase num subsections in each direction until all graphs can fit 286 | while ( numHigh * numWide < numGraphs ) 287 | { 288 | if ( numWide > numHigh ) 289 | { 290 | numHigh++; 291 | } 292 | else if ( numHigh > numWide ) 293 | { 294 | numWide++; 295 | } 296 | else if ( height >= width ) 297 | { 298 | numHigh++; 299 | } 300 | else 301 | { 302 | // Want to increase in width first 303 | numWide++; 304 | } 305 | } 306 | 307 | float[][] posGraphs = new float[numGraphs][4]; 308 | 309 | float subHeight = round( h / numHigh ); 310 | float subWidth = round( w / numWide ); 311 | 312 | // Set bounding box for each subsection 313 | for(int i = 0; i < numHigh; i++) 314 | { 315 | for (int j = 0; j < numWide; j++) 316 | { 317 | int k = i * numWide + j; 318 | if ( k < numGraphs ) 319 | { 320 | posGraphs[k][0] = i*subHeight + MARGIN_SZ / 2; 321 | posGraphs[k][1] = j*subWidth + MARGIN_SZ / 2; 322 | posGraphs[k][2] = subHeight - MARGIN_SZ; 323 | posGraphs[k][3] = subWidth - MARGIN_SZ; 324 | } 325 | } 326 | } 327 | 328 | return posGraphs; 329 | } 330 | 331 | void attemptConnect( int index ) 332 | { 333 | // Attempt connect on specified serial port 334 | if ( index >= Serial.list().length ) 335 | { 336 | return; 337 | } 338 | String portName = Serial.list()[portIndex]; 339 | logMessage( "Attempting connect on port: " + portName, false ); 340 | 341 | // Wrap Serial port connect in future to force timeout 342 | ExecutorService exec = Executors.newSingleThreadExecutor(); 343 | Future future = exec.submit( new ConnectWithTimeout( this, portName, BAUD_RATE ) ); 344 | 345 | try 346 | { 347 | // Close port if another is open 348 | if ( port != null && port.active() ) 349 | { 350 | port.stop(); 351 | } 352 | 353 | // Do connect with timeout 354 | port = future.get( CONNECT_TIMEOUT, TimeUnit.MILLISECONDS ); 355 | 356 | lastPortSwitch = millis(); // at end so that we try again immediately on invalid port 357 | logMessage( "Connected on " + portName + ". Listening for configuration...", false ); 358 | } 359 | catch ( TimeoutException e ) 360 | { 361 | future.cancel( true ); 362 | logMessage( "Timed out.", true ); 363 | } 364 | catch ( Exception e ) 365 | { 366 | logMessage( "Exception on connect: " + e.toString(), true ); 367 | } 368 | 369 | exec.shutdownNow(); 370 | } 371 | 372 | // Callable class to wrap Serial connect 373 | class ConnectWithTimeout implements Callable 374 | { 375 | private final PApplet parent; 376 | private final String portName; 377 | private final int baudRate; 378 | 379 | public ConnectWithTimeout( PApplet parent, String portName, int baud ) 380 | { 381 | this.parent = parent; 382 | this.portName = portName; 383 | this.baudRate = baud; 384 | } 385 | 386 | @Override 387 | public Serial call() throws Exception 388 | { 389 | return new Serial( this.parent, this.portName, baudRate ); 390 | } 391 | } 392 | 393 | // Logger helper 394 | void logMessage( String message, boolean debugOnly ) 395 | { 396 | if ( DEBUG || !debugOnly ) 397 | { 398 | String level = debugOnly ? "DEBUG" : "STATUS"; 399 | println( "[Time: " + millis() + " ms]" + "[" + level + "] " + message ); 400 | } 401 | } 402 | 403 | 404 | -------------------------------------------------------------------------------- /src/Plotter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Plotter is an Arduino library that allows easy multi-variable and multi-graph plotting. The 4 | library supports plots against time as well as 2-variable "X vs Y" graphing. 5 | ------------------------------------------------------------------------------------------- 6 | The library stores and handles all relevant graph information and variable references, 7 | and transfers information via the serial port to a listener program written with the 8 | software provided by Processing. No modification is needed to this program; graph placement, 9 | axis-scaling, etc. are handled automatically. 10 | Multiple options for this listener are available including stand-alone applications as well 11 | as the source Processing script. 12 | 13 | The library, these listeners, a quick-start guide, documentation, and usage examples are 14 | available at: 15 | 16 | https://github.com/devinaconley/arduino-plotter 17 | 18 | ------------------------------------------------------------------------------------------- 19 | Plotter 20 | v2.4.1 21 | https://github.com/devinaconley/arduino-plotter 22 | by Devin Conley 23 | =========================================================================================== 24 | */ 25 | 26 | #include "Plotter.h" 27 | 28 | // Plotter 29 | 30 | Plotter::Plotter() 31 | { 32 | head = NULL; 33 | tail = NULL; 34 | numGraphs = 0; 35 | counter = 0; 36 | lastUpdated = millis(); 37 | } 38 | 39 | void Plotter::Begin() 40 | { 41 | Serial.begin( 115200 ); 42 | lastUpdated = millis(); 43 | } 44 | 45 | Plotter::~Plotter() 46 | { 47 | Graph * temp = head; 48 | Graph * tempNext; 49 | while ( temp->next ) 50 | { 51 | tempNext = temp->next; 52 | delete temp; 53 | temp = tempNext; 54 | } 55 | delete temp; 56 | } 57 | 58 | void Plotter::AddGraphHelper( const char * title, VariableWrapper * wrappers, int sz, bool xvy, int pointsDisplayed ) 59 | { 60 | Graph * temp = new Graph( title, wrappers, sz, xvy, pointsDisplayed ); 61 | if ( head ) 62 | { 63 | tail->next = temp; 64 | tail = temp; 65 | } 66 | else 67 | { 68 | head = temp; 69 | tail = temp; 70 | } 71 | 72 | numGraphs++; 73 | lastUpdated = millis(); 74 | } 75 | 76 | bool Plotter::Remove( int index ) 77 | { 78 | if ( numGraphs == 0 || index < 0 || numGraphs <= index ) 79 | { 80 | return false; 81 | } 82 | else 83 | { 84 | Graph * temp = head; 85 | if ( index == 0 ) 86 | { 87 | head = head->next; 88 | delete temp; 89 | } 90 | else 91 | { 92 | Graph * last = temp; 93 | for ( int i = 0; i < index; i++ ) 94 | { 95 | last = temp; 96 | temp = temp->next; 97 | } 98 | last->next = temp->next; 99 | numGraphs--; 100 | delete temp; 101 | } 102 | lastUpdated = millis(); 103 | return true; 104 | } 105 | } 106 | 107 | bool Plotter::SetColor( int index, const char * colorA ) 108 | { 109 | const char * colors[] = { colorA }; 110 | return SetColorHelper( index, 1, colors ); 111 | } 112 | 113 | bool Plotter::SetColor( int index, const char * colorA, const char * colorB ) 114 | { 115 | const char * colors[] = { colorA, colorB }; 116 | return SetColorHelper( index, 2, colors ); 117 | } 118 | 119 | bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC ) 120 | { 121 | const char * colors[] = { colorA, colorB, colorC }; 122 | return SetColorHelper( index, 3, colors ); 123 | } 124 | 125 | bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC, 126 | const char * colorD ) 127 | { 128 | const char * colors[] = { colorA, colorB, colorC, colorD }; 129 | return SetColorHelper( index, 4, colors ); 130 | } 131 | 132 | bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC, 133 | const char * colorD, const char * colorE ) 134 | { 135 | const char * colors[] = { colorA, colorB, colorC, colorD, colorE }; 136 | return SetColorHelper( index, 5, colors ); 137 | } 138 | 139 | bool Plotter::SetColor( int index, const char * colorA, const char * colorB, const char * colorC, 140 | const char * colorD, const char * colorE, const char * colorF ) 141 | { 142 | const char * colors[] = { colorA, colorB, colorC, colorD, colorE, colorF }; 143 | return SetColorHelper( index, 6, colors ); 144 | } 145 | 146 | bool Plotter::SetColorHelper( int index, int sz, const char * * colors ) 147 | { 148 | if ( numGraphs == 0 || index < 0 || numGraphs <= index ) 149 | { 150 | return false; 151 | } 152 | Graph * temp = head; 153 | for ( int i = 0; i < index; i++ ) 154 | { 155 | temp = temp->next; 156 | } 157 | bool res = temp->SetColor( sz, colors ); 158 | if ( res ) 159 | { 160 | lastUpdated = millis(); 161 | } 162 | return res; 163 | } 164 | 165 | void Plotter::Plot() 166 | { 167 | bool config = counter == 0; 168 | 169 | Serial.print( "{\"" ); Serial.print( TIME_KEY ); Serial.print( "\":" ); Serial.print( millis() ); 170 | 171 | if ( config ) 172 | { 173 | Serial.print( ",\"" ); Serial.print( NUM_GRAPH_KEY ); Serial.print( "\":" ); Serial.print( numGraphs ); 174 | Serial.print( ",\"" ); Serial.print( LAST_UPDATED_KEY ); Serial.print( "\":" ); Serial.print( lastUpdated ); 175 | } 176 | 177 | Serial.print( ",\"" ); Serial.print( GRAPHS_KEY ); Serial.print( "\":[" ); 178 | 179 | Graph * temp = head; 180 | while ( temp ) 181 | { 182 | temp->Plot( config ); 183 | temp = temp->next; 184 | if ( temp ) 185 | { 186 | Serial.print( "," ); 187 | } 188 | } 189 | Serial.print( "]}" ); Serial.println( OUTER_KEY ); 190 | 191 | counter++; 192 | if ( counter >= CONFIG_INTERVAL ) 193 | { 194 | counter = 0; 195 | } 196 | } 197 | 198 | // Graph 199 | 200 | Plotter::Graph::Graph( const char * title, VariableWrapper * wrappers, int size, bool xvy, int pointsDisplayed ) : 201 | next( NULL ), 202 | xvy( xvy ), 203 | size( size ), 204 | pointsDisplayed( pointsDisplayed ), 205 | title( title ), 206 | wrappers( wrappers ) 207 | {} 208 | 209 | Plotter::Graph::~Graph() 210 | { 211 | delete[] wrappers; 212 | } 213 | 214 | void Plotter::Graph::Plot( bool config ) 215 | { 216 | Serial.print( "{" ); 217 | 218 | if ( config ) 219 | { 220 | Serial.print( "\"" ); Serial.print( TITLE_KEY ); Serial.print( "\":" ); Serial.print( "\"" ); Serial.print( title ); Serial.print( "\"" ); 221 | Serial.print( ",\"" ); Serial.print( XVY_KEY ); Serial.print( "\":" ); Serial.print( xvy ); 222 | Serial.print( ",\"" ); Serial.print( POINTS_DISPLAYED_KEY ); Serial.print( "\":" ); Serial.print( pointsDisplayed ); 223 | Serial.print( ",\"" ); Serial.print( SIZE_KEY ); Serial.print( "\":" ); Serial.print( size ); 224 | Serial.print( ",\"" ); Serial.print( LABELS_KEY ); Serial.print( "\":[" ); 225 | for ( int i = 0; i < size; i++ ) 226 | { 227 | Serial.print( "\"" ); Serial.print( wrappers[i].GetLabel() ); Serial.print( "\"" ); 228 | if ( i + 1 < size ) 229 | { 230 | Serial.print( "," ); 231 | } 232 | } 233 | Serial.print( "],\"" ); Serial.print( COLORS_KEY ); Serial.print( "\":[" ); 234 | for ( int i = 0; i < size; i++ ) 235 | { 236 | Serial.print( "\"" ); Serial.print( wrappers[i].GetColor() ); Serial.print( "\"" ); 237 | if ( i + 1 < size ) 238 | { 239 | Serial.print( "," ); 240 | } 241 | } 242 | Serial.print( "]," ); 243 | } 244 | 245 | Serial.print( "\"" ); Serial.print( DATA_KEY ); Serial.print( "\":[" ); 246 | for ( int i = 0; i < size; i++ ) 247 | { 248 | Serial.print( wrappers[i].GetValue(), 8 ); 249 | if ( i + 1 < size ) 250 | { 251 | Serial.print( "," ); 252 | } 253 | } 254 | 255 | Serial.print( "]}" ); 256 | } 257 | 258 | bool Plotter::Graph::SetColor( int sz, const char * * colors ) 259 | { 260 | if ( sz != size && !xvy ) 261 | { 262 | return false; 263 | } 264 | 265 | if ( xvy ) 266 | { 267 | wrappers[0].SetColor( colors[0] ); 268 | } 269 | else 270 | { 271 | for ( int i = 0; i < size; i++ ) 272 | { 273 | wrappers[i].SetColor( colors[i] ); 274 | } 275 | } 276 | return true; 277 | } 278 | 279 | // VariableWrapper 280 | 281 | Plotter::VariableWrapper::VariableWrapper() : 282 | ref( NULL ), 283 | deref( NULL ) 284 | {} 285 | 286 | Plotter::VariableWrapper::VariableWrapper( const char * label, void * ref, double ( * deref )( void * ), const char * color ) : 287 | label( label ), 288 | color( color ), 289 | ref( ref ), 290 | deref( deref ) 291 | {} 292 | 293 | const char * Plotter::VariableWrapper::GetLabel() 294 | { 295 | return label; 296 | } 297 | 298 | double Plotter::VariableWrapper::GetValue() 299 | { 300 | return deref( ref ); 301 | } 302 | 303 | const char * Plotter::VariableWrapper::GetColor() 304 | { 305 | return color; 306 | } 307 | 308 | void Plotter::VariableWrapper::SetColor( const char * col ) 309 | { 310 | color = col; 311 | } 312 | -------------------------------------------------------------------------------- /src/Plotter.h: -------------------------------------------------------------------------------- 1 | /* 2 | =========================================================================================== 3 | Plotter is an Arduino library that allows easy multi-variable and multi-graph plotting. The 4 | library supports plots against time as well as 2-variable "X vs Y" graphing. 5 | ------------------------------------------------------------------------------------------- 6 | The library stores and handles all relevant graph information and variable references, 7 | and transfers information via the serial port to a listener program written with the 8 | software provided by Processing. No modification is needed to this program; graph placement, 9 | axis-scaling, etc. are handled automatically. 10 | Multiple options for this listener are available including stand-alone applications as well 11 | as the source Processing script. 12 | 13 | The library, these listeners, a quick-start guide, documentation, and usage examples are 14 | available at: 15 | 16 | https://github.com/devinaconley/arduino-plotter 17 | 18 | ------------------------------------------------------------------------------------------- 19 | Plotter 20 | v2.4.1 21 | https://github.com/devinaconley/arduino-plotter 22 | by Devin Conley 23 | =========================================================================================== 24 | */ 25 | 26 | #ifndef PLOTTER_H 27 | #define PLOTTER_H 28 | 29 | #include "Arduino.h" 30 | 31 | class Plotter 32 | { 33 | public: 34 | // The constructor for Plotter requires no arguments 35 | Plotter(); 36 | 37 | // Initialize Plotter 38 | void Begin(); 39 | 40 | /* 41 | Add a 1-variable graph vs. time 42 | 43 | Args: 44 | - title: const char * with title of graph 45 | - pointsDisplayed: number of points to be shown at a given time. Used to control time-scaling 46 | - labelA: const char * with label of the plotted variable 47 | - refA: reference to global variable that will be updated throughout program 48 | 49 | Similar methods for multi-variable graphing vs. time are declared below and follow the same format 50 | */ 51 | template 52 | void AddTimeGraph( const char * title, int pointsDisplayed, 53 | const char * labelA, A & refA ) 54 | { 55 | VariableWrapper * wrappers = new VariableWrapper[1]; 56 | wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); 57 | AddGraphHelper( title, wrappers, 1, false, pointsDisplayed ); 58 | } 59 | 60 | /* 61 | Add an X vs. Y graph 62 | 63 | Args: 64 | - title: const char * with title of graph 65 | - pointsDisplayed: number of points to be shown at a given time. Determines duration of data persistance 66 | - labelX: const char * with label of variable to be plotted along X-axis 67 | - refX: reference to global X-variable that will be updated throughout program 68 | - labelY: const char * with label of variable to be plotted along Y-axis 69 | - refY: reference to global Y-variable that will be updated throughout program 70 | */ 71 | template 72 | void AddXYGraph( const char * title, int pointsDisplayed, 73 | const char * labelX, X & refX, const char * labelY, Y & refY ) 74 | { 75 | VariableWrapper * wrappers = new VariableWrapper[2]; 76 | wrappers[0] = VariableWrapper( labelX, static_cast( &refX ), &Dereference, "green" ); 77 | wrappers[1] = VariableWrapper( labelY, static_cast( &refY ), &Dereference, "green" ); 78 | AddGraphHelper( title, wrappers, 2, true, pointsDisplayed ); 79 | } 80 | 81 | /* 82 | Plot data 83 | 84 | Function to be called in order to send current values of all global variables to listener application. This 85 | function will update all plots that have been added. 86 | 87 | It is recommended to call plot() at the end of your loop function. 88 | */ 89 | void Plot(); 90 | 91 | /* 92 | Remove Graph 93 | 94 | Args: 95 | - index: position of graph to remove (ie. passing 0 would remove the first graph added) 96 | Returns: 97 | - true, if successful 98 | */ 99 | bool Remove( int index ); 100 | 101 | /* 102 | Set Variable Colors 103 | 104 | Args: 105 | - index: position of graph to set colors for 106 | - colorA: new color to set 107 | Returns: 108 | - true, if successful 109 | */ 110 | bool SetColor( int index, const char * colorA ); 111 | 112 | // Add a 2-variable graph vs. time 113 | template 114 | void AddTimeGraph( const char * title, int pointsDisplayed, 115 | const char * labelA, A & refA, const char * labelB, B & refB ) 116 | { 117 | VariableWrapper * wrappers = new VariableWrapper[2]; 118 | wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); 119 | wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); 120 | AddGraphHelper( title, wrappers, 2, false, pointsDisplayed ); 121 | } 122 | 123 | // Add a 3-variable graph vs. time 124 | template 125 | void AddTimeGraph( const char * title, int pointsDisplayed, 126 | const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC ) 127 | { 128 | VariableWrapper * wrappers = new VariableWrapper[3]; 129 | wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); 130 | wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); 131 | wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); 132 | AddGraphHelper( title, wrappers, 3, false, pointsDisplayed ); 133 | } 134 | 135 | // Add a 4-variable graph vs. time 136 | template 137 | void AddTimeGraph( const char * title, int pointsDisplayed, 138 | const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC, 139 | const char * labelD, D & refD ) 140 | { 141 | VariableWrapper * wrappers = new VariableWrapper[4]; 142 | wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); 143 | wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); 144 | wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); 145 | wrappers[3] = VariableWrapper( labelD, static_cast( &refD ), &Dereference, "yellow" ); 146 | AddGraphHelper( title, wrappers, 4, false, pointsDisplayed ); 147 | } 148 | 149 | // Add a 5-variable graph vs. time 150 | template 151 | void AddTimeGraph( const char * title, int pointsDisplayed, 152 | const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC, 153 | const char * labelD, D & refD, const char * labelE, E & refE ) 154 | { 155 | VariableWrapper * wrappers = new VariableWrapper[5]; 156 | wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); 157 | wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); 158 | wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); 159 | wrappers[3] = VariableWrapper( labelD, static_cast( &refD ), &Dereference, "yellow" ); 160 | wrappers[4] = VariableWrapper( labelE, static_cast( &refE ), &Dereference, "pink" ); 161 | AddGraphHelper( title, wrappers, 5, false, pointsDisplayed ); 162 | } 163 | 164 | // Add a 6-variable graph vs. time 165 | template 166 | void AddTimeGraph( const char * title, int pointsDisplayed, 167 | const char * labelA, A & refA, const char * labelB, B & refB, const char * labelC, C & refC, 168 | const char * labelD, D & refD, const char * labelE, E & refE, const char * labelF, F & refF ) 169 | { 170 | VariableWrapper * wrappers = new VariableWrapper[6]; 171 | wrappers[0] = VariableWrapper( labelA, static_cast( &refA ), &Dereference, "green" ); 172 | wrappers[1] = VariableWrapper( labelB, static_cast( &refB ), &Dereference, "orange" ); 173 | wrappers[2] = VariableWrapper( labelC, static_cast( &refC ), &Dereference, "cyan" ); 174 | wrappers[3] = VariableWrapper( labelD, static_cast( &refD ), &Dereference, "yellow" ); 175 | wrappers[4] = VariableWrapper( labelE, static_cast( &refE ), &Dereference, "pink" ); 176 | wrappers[5] = VariableWrapper( labelF, static_cast( &refF ), &Dereference, "blue" ); 177 | AddGraphHelper( title, wrappers, 6, false, pointsDisplayed ); 178 | } 179 | 180 | // Set Colors for multivariable graphs 181 | bool SetColor( int index, const char * colorA, const char * colorB ); 182 | bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC ); 183 | bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC, 184 | const char * colorD ); 185 | bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC, 186 | const char * colorD, const char * colorE ); 187 | bool SetColor( int index, const char * colorA, const char * colorB, const char * colorC, 188 | const char * colorD, const char * colorE, const char * colorF ); 189 | 190 | // Destructor for Plotter class 191 | ~Plotter(); 192 | 193 | public: 194 | 195 | // Nested VariableWrapper class 196 | class VariableWrapper 197 | { 198 | public: 199 | VariableWrapper(); 200 | VariableWrapper( const char * label, void * ref, double ( * deref )( void * ), const char * color ); 201 | 202 | const char * GetLabel(); 203 | double GetValue(); 204 | const char * GetColor(); 205 | void SetColor( const char * col ); 206 | 207 | private: 208 | // Data 209 | const char * label; 210 | const char * color; 211 | void * ref; 212 | double ( * deref )( void * ); 213 | 214 | }; //-- VariableWrapper 215 | 216 | 217 | public: 218 | // Nested Graph node class 219 | class Graph 220 | { 221 | public: 222 | Graph( const char * title, VariableWrapper * wrappers, int size, bool xvy, int pointsDisplayed ); 223 | ~Graph(); 224 | void Plot( bool config ); 225 | bool SetColor( int sz, const char * * colors ); 226 | 227 | // Data 228 | Graph * next; 229 | 230 | private: 231 | bool xvy; 232 | int size; 233 | int pointsDisplayed; 234 | const char * title; 235 | VariableWrapper * wrappers; 236 | 237 | }; //-- Graph 238 | 239 | private: 240 | // Helpers 241 | void AddGraphHelper( const char * title, VariableWrapper * wrappers, int sz, bool xvy, int pointsDisplayed ); 242 | bool SetColorHelper( int index, int sz, const char * * colors ); 243 | 244 | template 245 | static double Dereference( void * ref ) 246 | { 247 | return static_cast( ( * static_cast( ref ) ) ); 248 | } 249 | 250 | 251 | // Data 252 | int numGraphs; 253 | unsigned long lastUpdated; 254 | int counter; 255 | Graph * head; 256 | Graph * tail; 257 | 258 | }; //-- Plotter 259 | 260 | // Constants 261 | static const int CONFIG_INTERVAL = 50; 262 | 263 | // Transmission Keys 264 | static const char * OUTER_KEY = "#"; 265 | static const char * TIME_KEY = "t"; 266 | static const char * NUM_GRAPH_KEY = "ng"; 267 | static const char * LAST_UPDATED_KEY = "lu"; 268 | static const char * GRAPHS_KEY = "g"; 269 | static const char * TITLE_KEY = "t"; 270 | static const char * XVY_KEY = "xvy"; 271 | static const char * POINTS_DISPLAYED_KEY = "pd"; 272 | static const char * SIZE_KEY = "sz"; 273 | static const char * LABELS_KEY = "l"; 274 | static const char * COLORS_KEY = "c"; 275 | static const char * DATA_KEY = "d"; 276 | 277 | #endif 278 | --------------------------------------------------------------------------------