Skip to content

Commit af4ca85

Browse files
committed
Added functionality and exposed methods for configuring colorson each variable
1 parent 0efa0cf commit af4ca85

File tree

6 files changed

+217
-42
lines changed

6 files changed

+217
-42
lines changed

listener/Graph.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public Graph( PApplet parent, float posY, float posX, float height, float width,
4141
this.title = title;
4242
this.labels = labels;
4343
this.colors = colors;
44-
45-
this.parent.println( "Constructed new graph: ", this.title, this.posY, " ", this.posX);
46-
44+
45+
// this.parent.println( "Constructed new graph: ", this.title, " at ", this.posY, " ", this.posX );
46+
4747
// Initialize
4848
this.index = 0;
4949
this.data = new double[maxPoints][numVars][2];
@@ -199,7 +199,7 @@ private void DrawXYStuff()
199199
// X and Y labels
200200
this.parent.textSize( LABEL_SZ );
201201
this.parent.textAlign( this.parent.LEFT, this.parent.TOP );
202-
this.parent.text( this.labels[1], this.posY + 10, this.posY + 10);
202+
this.parent.text( this.labels[1], this.posX + 10, this.posY + 10);
203203

204204
this.parent.textAlign( this.parent.RIGHT, this.parent.BOTTOM );
205205
this.parent.text( this.labels[0], this.posX + this.width - 10, this.posY + this.height - 3*NUM_SZ);

listener/listener.pde

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@
2323
*/
2424

2525
import processing.serial.*;
26+
import java.util.Map;
2627

2728
//CONSTANTS
28-
final int[] COLORS = {#00FF00, #FF0000, #0000FF, #FEFF00, #FF9900, #FF00FF}; //int color codes
2929
final char OUTER_KEY = '#';
3030
final String INNER_KEY = "@";
3131
final int MARGIN_SZ = 20; // between plots
3232
final int BG_COL = 75; // background
3333
final int PORT_INTERVAL = 5000; // time to sit on each port
3434
final int BAUD_RATE = 115200;
35+
final HashMap<String, Integer> COLORMAP = new HashMap<String, Integer>()
36+
{
37+
{
38+
put( "red", color( 255, 0, 0 ) );
39+
put( "green", color( 0, 255, 0 ) );
40+
put( "blue", color( 0, 0, 255 ) );
41+
put( "orange", color( 255, 153, 51 ) );
42+
put( "yellow", color( 255, 255, 0 ) );
43+
put( "pink", color( 255, 51, 204 ) );
44+
put( "purple", color( 172, 0, 230 ) );
45+
put( "cyan", color( 0, 255, 255 ) );
46+
}
47+
};
3548

3649
// Setup and config Globals
3750
int h;
@@ -143,10 +156,17 @@ void serialEvent( Serial ser )
143156
int maxPoints = Integer.parseInt( arraySub[2] );
144157
int numVars = Integer.parseInt( arraySub[3] );
145158
String[] labelsTemp = new String[numVars];
159+
int[] colorsTemp = new int[numVars];
146160

147161
for ( int j = 0; j < numVars; j++ )
148162
{
149-
labelsTemp[j] = arraySub[4 + 2*j];
163+
labelsTemp[j] = arraySub[4 + 3*j];
164+
colorsTemp[j] = COLORMAP.get( arraySub[5 + 3*j] );
165+
if ( colorsTemp[j] == 0 )
166+
{
167+
colorsTemp[j] = COLORMAP.get( "green" );
168+
}
169+
println( colorsTemp[j] );
150170
}
151171

152172
if ( xvyTemp )
@@ -155,8 +175,8 @@ void serialEvent( Serial ser )
155175
}
156176

157177
// Create new Graph
158-
Graph temp = new Graph(this, posGraphs[i][0], posGraphs[i][1], posGraphs[i][2], posGraphs[i][3],
159-
xvyTemp, numVars, maxPoints, title, labelsTemp, COLORS);
178+
Graph temp = new Graph( this, posGraphs[i][0], posGraphs[i][1], posGraphs[i][2], posGraphs[i][3],
179+
xvyTemp, numVars, maxPoints, title, labelsTemp, colorsTemp );
160180
graphs.add( temp );
161181
}
162182
println("Added ", graphs.size() );
@@ -180,11 +200,11 @@ void serialEvent( Serial ser )
180200
{
181201
String[] arraySub = arrayMain[i+1].split( INNER_KEY );
182202

183-
double[] tempData = new double[ (arraySub.length - 5) / 2 ];
203+
double[] tempData = new double[ (arraySub.length - 5) / 3 ];
184204

185205
// Update graph objects with new data
186206
int q = 0;
187-
for ( int j = 5; j < arraySub.length; j += 2 )
207+
for ( int j = 6; j < arraySub.length; j += 3 )
188208
{
189209
tempData[q] = Double.parseDouble( arraySub[j] );
190210
q++;

plotter/Plotter.cpp

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void Plotter::AddGraphHelper( String title, VariableWrapper * wrappers, int sz,
7373
lastUpdated = millis();
7474
}
7575

76-
bool Plotter::Remove(int index)
76+
bool Plotter::Remove( int index )
7777
{
7878
if ( numGraphs == 0 || index < 0 || numGraphs <= index )
7979
{
@@ -105,6 +105,64 @@ bool Plotter::Remove(int index)
105105
}
106106
}
107107

108+
bool Plotter::SetColor( int index, String colorA )
109+
{
110+
String colors[] = { colorA };
111+
return SetColorHelper( index, 1, colors );
112+
}
113+
114+
bool Plotter::SetColor( int index, String colorA, String colorB )
115+
{
116+
String colors[] = { colorA, colorB };
117+
return SetColorHelper( index, 2, colors );
118+
}
119+
120+
bool Plotter::SetColor( int index, String colorA, String colorB, String colorC )
121+
{
122+
String colors[] = { colorA, colorB, colorC };
123+
return SetColorHelper( index, 3, colors );
124+
}
125+
126+
bool Plotter::SetColor( int index, String colorA, String colorB, String colorC,
127+
String colorD )
128+
{
129+
String colors[] = { colorA, colorB, colorC, colorD };
130+
return SetColorHelper( index, 4, colors );
131+
}
132+
133+
bool Plotter::SetColor( int index, String colorA, String colorB, String colorC,
134+
String colorD, String colorE )
135+
{
136+
String colors[] = { colorA, colorB, colorC, colorD, colorE };
137+
return SetColorHelper( index, 5, colors );
138+
}
139+
140+
bool Plotter::SetColor( int index, String colorA, String colorB, String colorC,
141+
String colorD, String colorE, String colorF )
142+
{
143+
String colors[] = { colorA, colorB, colorC, colorD, colorE, colorF };
144+
return SetColorHelper( index, 5, colors );
145+
}
146+
147+
bool Plotter::SetColorHelper( int index, int sz, String * colors )
148+
{
149+
if ( numGraphs == 0 || index < 0 || numGraphs <= index )
150+
{
151+
return false;
152+
}
153+
Graph * temp = head;
154+
for ( int i = 0; i < index; i++ )
155+
{
156+
temp = temp->next;
157+
}
158+
bool res = temp->SetColor( sz, colors );
159+
if ( res )
160+
{
161+
lastUpdated = millis();
162+
}
163+
return res;
164+
}
165+
108166
void Plotter::Plot()
109167
{
110168
String code = OUTER_KEY;
@@ -146,21 +204,44 @@ void Plotter::Graph::Plot()
146204
for (int i = 0; i < size; i++)
147205
{
148206
Serial.print( wrappers[i].GetLabel() ); Serial.print( INNER_KEY );
207+
Serial.print( wrappers[i].GetColor() ); Serial.print( INNER_KEY );
149208
Serial.print( wrappers[i].GetValue() ); Serial.print( INNER_KEY );
150209
}
151210
}
152211

212+
bool Plotter::Graph::SetColor( int sz, String * colors )
213+
{
214+
if ( sz != size && !xvy )
215+
{
216+
return false;
217+
}
218+
219+
if ( xvy )
220+
{
221+
wrappers[0].SetColor( colors[0] );
222+
}
223+
else
224+
{
225+
for ( int i = 0; i < size; i++ )
226+
{
227+
wrappers[i].SetColor( colors[i] );
228+
}
229+
}
230+
return true;
231+
}
232+
153233
// VariableWrapper
154234

155235
Plotter::VariableWrapper::VariableWrapper() :
156236
ref( NULL ),
157237
deref( NULL )
158238
{}
159239

160-
Plotter::VariableWrapper::VariableWrapper( String label, void * ref, double ( * deref )( void * ) ) :
240+
Plotter::VariableWrapper::VariableWrapper( String label, void * ref, double ( * deref )( void * ), String color ) :
161241
label( label ),
162-
ref ( ref ),
163-
deref ( deref )
242+
ref( ref ),
243+
deref( deref ),
244+
color( color )
164245
{}
165246

166247
String Plotter::VariableWrapper::GetLabel()
@@ -172,3 +253,13 @@ double Plotter::VariableWrapper::GetValue()
172253
{
173254
return deref( ref );
174255
}
256+
257+
String Plotter::VariableWrapper::GetColor()
258+
{
259+
return color;
260+
}
261+
262+
void Plotter::VariableWrapper::SetColor( String col )
263+
{
264+
color = col;
265+
}

0 commit comments

Comments
 (0)