@@ -354,6 +354,11 @@ class MarkerBar {
354354
355355 private DataSource markerBoard;
356356
357+ private boolean isAutoscale = false ;
358+ private float autoscaleMin;
359+ private float autoscaleMax;
360+ private int previousMillis = 0 ;
361+
357362 MarkerBar (PApplet _parent , int _yAxisMax , int markerWindow , float yLimit , int _x , int _y , int _w , int _h ) { // channel number, x/y location, height, width
358363
359364 yAxisMax = _yAxisMax;
@@ -420,6 +425,10 @@ class MarkerBar {
420425 // Used to update the accelerometerBar class
421426 public void update () {
422427 updateGPlotPoints();
428+
429+ if (isAutoscale) {
430+ adjustYAxis(- 1 );
431+ }
423432 }
424433
425434 public void draw () {
@@ -454,19 +463,42 @@ class MarkerBar {
454463 }
455464
456465 public void adjustYAxis (int _yAxisMax ) {
466+ if (_yAxisMax == - 1 ) {
467+ yAxisMax = 1 ;
468+ isAutoscale = true ;
469+ return ;
470+ }
471+ isAutoscale = false ;
457472 yAxisMax = _yAxisMax;
458473 plot. setYLim(- 0.2 , yAxisMax + .2 );
459474 }
460475
476+ void applyAutoscale () {
477+ // Do this once a second for all TimeSeries ChannelBars to save on resources
478+ int newMillis = millis ();
479+ boolean doAutoscale = newMillis > previousMillis + 1000 ;
480+ if (isAutoscale && currentBoard. isStreaming() && doAutoscale) {
481+ autoscaleMin = (int ) Math . floor(autoscaleMin);
482+ autoscaleMax = (int ) Math . ceil(autoscaleMax);
483+ previousMillis = newMillis;
484+ plot. setYLim(autoscaleMin, autoscaleMax); // <---- This is a very expensive method. Here is the bottleneck.
485+ }
486+ }
487+
461488 // Used to update the Points within the graph
462489 private void updateGPlotPoints () {
463490 List<double[]> allData = markerBoard. getData(nPoints);
464491 int markerChannel = markerBoard. getMarkerChannel();
465492
493+ autoscaleMax = - Float . MAX_VALUE ;
494+ autoscaleMin = Float . MAX_VALUE ;
495+
466496 for (int i = 0 ; i < nPoints; i++ ) {
467497 markerPointsArray. set(i, markerTimeArray[i], (float )allData. get(i)[markerChannel], " " );
498+ autoscaleMax = Math . max((float )allData. get(i)[markerChannel], autoscaleMax);
499+ autoscaleMin = Math . min((float )allData. get(i)[markerChannel], autoscaleMin);
468500 }
469-
501+ applyAutoscale();
470502 plot. setPoints(markerPointsArray, PLOT_LAYER );
471503 }
472504
@@ -526,12 +558,12 @@ public enum MarkerWindow implements IndexingInterface
526558// Enum for the Marker Vertical Scale in W_Marker class
527559public enum MarkerVertScale implements IndexingInterface
528560{
529- TWO (0 , 2 , " 2 " ),
530- FOUR (1 , 4 , " 4 " ),
531- EIGHT (2 , 8 , " 8 " ),
532- TEN (3 , 10 , " 10 " ),
533- FIFTEEN (4 , 15 , " 15 " ),
534- TWENTY (5 , 20 , " 20" );
561+ AUTO (0 , - 1 , " Auto " ),
562+ TWO (1 , 2 , " 2 " ),
563+ FOUR (2 , 4 , " 4 " ),
564+ EIGHT (3 , 8 , " 8 " ),
565+ TEN (4 , 10 , " 10 " ),
566+ TWENTY (6 , 20 , " 20" );
535567
536568 private int index;
537569 private int value;
0 commit comments