11package io .pslab .activity ;
22
3+ import android .app .AlertDialog ;
4+ import android .content .DialogInterface ;
35import android .graphics .Point ;
46import android .os .Build ;
57import android .os .CountDownTimer ;
8+ import android .support .design .widget .Snackbar ;
69import android .support .v7 .app .AppCompatActivity ;
710import android .os .Bundle ;
811import android .view .Display ;
2427import com .triggertrap .seekarc .SeekArc ;
2528
2629import io .pslab .R ;
30+ import io .pslab .others .CSVLogger ;
31+ import io .pslab .others .CustomSnackBar ;
2732
2833public class RoboticArmActivity extends AppCompatActivity {
2934
@@ -32,9 +37,11 @@ public class RoboticArmActivity extends AppCompatActivity {
3237 private LinearLayout servo1TimeLine , servo2TimeLine , servo3TimeLine , servo4TimeLine ;
3338 private int degree ;
3439 private boolean editEnter = false ;
35- private Button playButton , pauseButton , stopButton ;
40+ private Button playPauseButton , stopButton , saveButton ;
3641 private HorizontalScrollView scrollView ;
3742 private CountDownTimer timeLine ;
43+ private boolean isPlaying = false ;
44+ private CSVLogger servoCSVLogger ;
3845
3946 @ Override
4047 protected void onCreate (Bundle savedInstanceState ) {
@@ -63,11 +70,12 @@ protected void onCreate(Bundle savedInstanceState) {
6370 servo2TimeLine = findViewById (R .id .servo2_timeline );
6471 servo3TimeLine = findViewById (R .id .servo3_timeline );
6572 servo4TimeLine = findViewById (R .id .servo4_timeline );
66- playButton = findViewById (R .id .timeline_play_button );
67- pauseButton = findViewById (R .id .timeline_pause_button );
73+ playPauseButton = findViewById (R .id .timeline_play_pause_button );
6874 stopButton = findViewById (R .id .timeline_stop_button );
75+ saveButton = findViewById (R .id .timeline_save_button );
6976 scrollView = findViewById (R .id .horizontal_scroll_view );
7077 LinearLayout timeLineControlsLayout = findViewById (R .id .servo_timeline_controls );
78+ servoCSVLogger = new CSVLogger (getResources ().getString (R .string .robotic_arm ));
7179
7280 LinearLayout .LayoutParams servoControllerParams = new LinearLayout .LayoutParams (screen_width / 4 - 4 , screen_height / 2 - 4 );
7381 servoControllerParams .setMargins (2 , 5 , 2 , 0 );
@@ -341,17 +349,25 @@ public void onFinish() {
341349 }
342350 };
343351
344- playButton .setOnClickListener (new View .OnClickListener () {
352+ playPauseButton .setOnClickListener (new View .OnClickListener () {
345353 @ Override
346354 public void onClick (View v ) {
347- timeLine .start ();
355+ if (isPlaying ) {
356+ isPlaying = false ;
357+ playPauseButton .setBackground (getResources ().getDrawable (R .drawable .ic_play_arrow_white_24dp ));
358+ timeLine .onFinish ();
359+ }else {
360+ isPlaying = true ;
361+ playPauseButton .setBackground (getResources ().getDrawable (R .drawable .ic_pause_white_24dp ));
362+ timeLine .start ();
363+ }
348364 }
349365 });
350366
351- pauseButton .setOnClickListener (new View .OnClickListener () {
367+ saveButton .setOnClickListener (new View .OnClickListener () {
352368 @ Override
353369 public void onClick (View v ) {
354- timeLine . onFinish ();
370+ saveTimeline ();
355371 }
356372 });
357373
@@ -366,6 +382,48 @@ public void onClick(View v) {
366382 });
367383 }
368384
385+ private void saveTimeline () {
386+ servoCSVLogger .prepareLogFile ();
387+ String data = "Servo1,Servo2,Servo3,Servo4\n " ;
388+ String degree1 , degree2 , degree3 , degree4 ;
389+ for (int i = 0 ; i < 60 ; i ++) {
390+ degree1 = degree2 = degree3 = degree4 = "0" ;
391+ if (((TextView )servo1TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().length () > 0 ) {
392+ degree1 = ((TextView ) servo1TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().toString ();
393+ }
394+ if (((TextView )servo2TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().length () > 0 ) {
395+ degree2 = ((TextView ) servo2TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().toString ();
396+ }
397+ if (((TextView )servo3TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().length () > 0 ) {
398+ degree3 = ((TextView ) servo3TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().toString ();
399+ }
400+ if (((TextView )servo4TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().length () > 0 ) {
401+ degree4 = ((TextView ) servo4TimeLine .getChildAt (i ).findViewById (R .id .timeline_box_degree_text )).getText ().toString ();
402+ }
403+ data += degree1 + "," + degree2 + "," + degree3 + "," + degree4 + "\n " ;
404+ }
405+ servoCSVLogger .writeCSVFile (data );
406+ CustomSnackBar .showSnackBar (findViewById (R .id .robotic_arm_relative_view ),
407+ getString (R .string .csv_store_text ) + " " + servoCSVLogger .getCurrentFilePath ()
408+ , getString (R .string .delete_capital ), new View .OnClickListener () {
409+ @ Override
410+ public void onClick (View view ) {
411+ new AlertDialog .Builder (RoboticArmActivity .this , R .style .AlertDialogStyle )
412+ .setTitle (R .string .delete_file )
413+ .setMessage (R .string .delete_warning )
414+ .setPositiveButton (R .string .ok , new DialogInterface .OnClickListener () {
415+ @ Override
416+ public void onClick (DialogInterface dialogInterface , int i ) {
417+ servoCSVLogger .deleteFile ();
418+ }
419+ })
420+ .setNegativeButton (R .string .cancel , null )
421+ .create ()
422+ .show ();
423+ }
424+ }, Snackbar .LENGTH_SHORT );
425+ }
426+
369427 private View .OnDragListener servo1DragListener = new View .OnDragListener () {
370428 @ Override
371429 public boolean onDrag (View v , DragEvent event ) {
0 commit comments