add elapsed time and estimated time to progress command
authorJim Morris <morris@wolfman.com>
Wed, 20 Feb 2013 11:27:37 +0000 (03:27 -0800)
committerJim Morris <morris@wolfman.com>
Wed, 20 Feb 2013 11:27:37 +0000 (03:27 -0800)
src/modules/utils/player/Player.cpp
src/modules/utils/player/Player.h

index 0c8e343..6d99adc 100644 (file)
@@ -37,10 +37,10 @@ void Player::on_console_line_received( void* argument ){
     // Act depending on command
     if (check_sum == play_command_checksum)
         this->play_command(  get_arguments(possible_command), new_message.stream );
-       else if (check_sum == progress_command_checksum)
-               this->progress_command(get_arguments(possible_command),new_message.stream );
-       else if (check_sum == abort_command_checksum)
-               this->abort_command(get_arguments(possible_command),new_message.stream );
+    else if (check_sum == progress_command_checksum)
+        this->progress_command(get_arguments(possible_command),new_message.stream );
+    else if (check_sum == abort_command_checksum)
+        this->abort_command(get_arguments(possible_command),new_message.stream );
     else if (check_sum == cd_command_checksum)
         this->cd_command(  get_arguments(possible_command), new_message.stream );
 }
@@ -78,34 +78,43 @@ void Player::play_command( string parameters, StreamOutput* stream ){
             fseek(this->current_file_handler, 0, SEEK_SET);
             stream->printf("  File size %ld\r\n", file_size);
     }
-    played_cnt= 0;
-
+    this->played_cnt= 0;
+    this->start_time= time(NULL);
 }
 
 void Player::progress_command( string parameters, StreamOutput* stream ){
-       if(!playing_file) {
-               stream->printf("Not currently playing\r\n");
-               return;
-       }
-
-       if(file_size > 0) {
-               int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
-               stream->printf("%d %% complete\r\n", pcnt);
-       }else{
-               stream->printf("File size is unknown\r\n");
-       }
+    if(!playing_file) {
+        stream->printf("Not currently playing\r\n");
+        return;
+    }
+
+    if(file_size > 0) {
+        int elapsed= time(NULL) - this->start_time;
+        int est= -1;
+        if(elapsed > 0) {
+            int bytespersec= played_cnt/elapsed;
+            if(bytespersec > 0)
+                est= (file_size - played_cnt) / bytespersec;
+        }
+        
+        int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
+        stream->printf("%d %% complete, elapsed time: %d s, est time: %d s\r\n", pcnt, elapsed, est);
+        
+    }else{
+        stream->printf("File size is unknown\r\n");
+    }
 }
 
 void Player::abort_command( string parameters, StreamOutput* stream ){
-       if(!playing_file) {
-               stream->printf("Not currently playing\r\n");
-               return;
-       }
-       playing_file = false;
-       played_cnt= 0;
-       file_size= 0;
-       fclose(current_file_handler);
-       stream->printf("Aborted playing file\r\n");
+    if(!playing_file) {
+        stream->printf("Not currently playing\r\n");
+        return;
+    }
+    playing_file = false;
+    played_cnt= 0;
+    file_size= 0;
+    fclose(current_file_handler);
+    stream->printf("Aborted playing file\r\n");
 }
 
 // Convert a path indication ( absolute or relative ) into a path ( absolute )
@@ -124,8 +133,8 @@ void Player::cd_command( string parameters, StreamOutput* stream ){
     if(d == NULL) {
 //        stream->printf("Could not open directory %s \r\n", folder.c_str() );
     }else{
-               this->current_path = folder;
-               closedir(d);
+        this->current_path = folder;
+        closedir(d);
     }
 }
 
@@ -148,17 +157,17 @@ void Player::on_main_loop(void* argument){
                 message.stream = this->current_stream;
                 // wait for the queue to have enough room that a serial message could still be received before sending
                 this->kernel->conveyor->wait_for_queue(2);
-                               this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
-                               played_cnt += buffer.size();
+                this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
+                played_cnt += buffer.size();
                 buffer.clear();
                 return;
             }else{
                 buffer += c;
             }
         };
-               this->playing_file = false;
-               played_cnt= 0;
-               file_size= 0;
+        this->playing_file = false;
+        played_cnt= 0;
+        file_size= 0;
         fclose(this->current_file_handler);
     }
 }
index dcea3fb..71f5a4e 100644 (file)
@@ -14,6 +14,7 @@
 #include "libs/utils.h"
 #include "libs/StreamOutput.h"
 
+#include "mbed.h"
 
 #define play_command_checksum           CHECKSUM("play")
 #define progress_command_checksum       CHECKSUM("progress")
@@ -35,6 +36,7 @@ class Player : public Module {
         void progress_command( string parameters, StreamOutput* stream );
         void abort_command( string parameters, StreamOutput* stream );
 
+    private:
         string current_path;
 
         bool on_boot_enable;
@@ -43,7 +45,8 @@ class Player : public Module {
         bool playing_file;
         StreamOutput* current_stream;
         FILE* current_file_handler;
-               long file_size, played_cnt;
+        long file_size, played_cnt;
+        time_t start_time;
 };
 
 #endif // PLAYER_H