From: Martin Vajnar Date: Mon, 5 Aug 2013 19:26:29 +0000 (+0200) Subject: ROBOT_DEMO: fix sync. problems in HokuyoView X-Git-Url: https://rtime.felk.cvut.cz/gitweb/orte.git/commitdiff_plain/9c4431d90f2bdbb44063f1d48c270d89cf6dd0a4 ROBOT_DEMO: fix sync. problems in HokuyoView Set new data only if the previous batch has been drawn and clone the newly arrived data array. This prevents concurrent access problems when the previous batch is being drawn on the UI thread and new data arrives through ORTE's receive thread. --- diff --git a/orte/Robot_Demo/src/org/ocera/orte/demo/HokuyoView.java b/orte/Robot_Demo/src/org/ocera/orte/demo/HokuyoView.java index 6035076..07452b6 100644 --- a/orte/Robot_Demo/src/org/ocera/orte/demo/HokuyoView.java +++ b/orte/Robot_Demo/src/org/ocera/orte/demo/HokuyoView.java @@ -21,6 +21,7 @@ public class HokuyoView extends View { private Paint paint = new Paint(); private Path path = new Path(); private boolean isRunning = false; + private boolean hasBeenDrawn = true; public HokuyoView(Context context, AttributeSet attrs) { super(context, attrs); @@ -51,6 +52,9 @@ public class HokuyoView extends View { canvas.drawPath(path, paint); } + else + canvas.drawARGB(0, 0, 0, 0); + hasBeenDrawn = true; } public void run(boolean run) { @@ -58,8 +62,11 @@ public class HokuyoView extends View { } public void setData(int[] data) { - this.data = data; - postInvalidate(); + if (hasBeenDrawn) { + this.data = data.clone(); + hasBeenDrawn = false; + postInvalidate(); + } } public static double HOKUYO_INDEX_TO_DEG(int index) { diff --git a/orte/Robot_Demo/src/org/ocera/orte/demo/MainActivity.java b/orte/Robot_Demo/src/org/ocera/orte/demo/MainActivity.java index f4d299a..d2b61d1 100644 --- a/orte/Robot_Demo/src/org/ocera/orte/demo/MainActivity.java +++ b/orte/Robot_Demo/src/org/ocera/orte/demo/MainActivity.java @@ -130,6 +130,7 @@ public class MainActivity extends Activity { else if (item.getTitle().equals("Stop hokuyo")) { hokuyo_view.run(false); hokuyo_scan.cancel(); + hokuyo_view.invalidate(); item.setTitle("Start hokuyo"); } else if (item.getTitle().equals("Lift up")) {