Merge pull request #255 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / WatchScreen.cpp
index 82ab10a..9a66d52 100644 (file)
@@ -1,8 +1,8 @@
-/*  
+/*
       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
       Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
       Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
+      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "libs/Kernel.h"
@@ -40,8 +40,11 @@ static const uint8_t icons[] = { // 115x19 - 3 bytes each: he1, he2, he3, bed, f
 0x0C,0x00,0x00,0x06,0x00,0x00,0x06,0x00,0x01,0xFF,0xFF,0x80,0x00,0x00,0x00
 };
 
+
+
 WatchScreen::WatchScreen(){
     speed_changed= false;
+    issue_change_speed= false;
 }
 
 void WatchScreen::on_enter(){
@@ -50,14 +53,14 @@ void WatchScreen::on_enter(){
     get_temp_data();
     get_current_pos(this->pos);
     get_sd_play_info();
-    this->current_speed= get_current_speed();
+    this->current_speed= lround(get_current_speed());
     this->refresh_screen(false);
     this->panel->enter_control_mode(1,0.5);
     this->panel->set_control_value(this->current_speed);
 }
 
 void WatchScreen::on_refresh(){
-    // Exit if the button is clicked 
+    // Exit if the button is clicked
     if( this->panel->click() ){
         this->panel->enter_screen(this->parent);
         return;
@@ -66,17 +69,18 @@ void WatchScreen::on_refresh(){
     // see if speed is being changed
     if(this->panel->control_value_change()) {
         this->current_speed= this->panel->get_control_value();
-        if(this->current_speed < 1.0) {
-            this->current_speed= 1.0;
+        if(this->current_speed < 10) {
+            this->current_speed= 10;
             this->panel->set_control_value(this->current_speed);
             this->panel->reset_counter();
         }else{
-            // change actual speed
-            this->speed_changed= true; // flag main loop to issue g code
+            // flag the update to change the speed, we don't want to issue hundreds of M220s
+            // but we do want to display the change we are going to make
+            this->speed_changed= true; // flag indicating speed changed
             this->refresh_screen(false);
         }
     }
-    
+
     // Update Only every 20 refreshes, 1 a second
     static int update_counts = 0;
     update_counts++;
@@ -84,11 +88,17 @@ void WatchScreen::on_refresh(){
         get_sd_play_info();
         get_current_pos(this->pos);
         get_temp_data();
-        this->current_speed= get_current_speed();
-        this->panel->set_control_value(this->current_speed); // in case it was changed via M220
-        this->panel->reset_counter();
+        if(this->speed_changed) {
+            this->issue_change_speed= true; // trigger actual command to change speed
+            this->speed_changed= false;
+        }else if(!this->issue_change_speed){ // change still queued
+            // read it in case it was changed via M220
+            this->current_speed= lround(get_current_speed());
+            this->panel->set_control_value(this->current_speed);
+            this->panel->reset_counter();
+        }
 
-        this->refresh_screen(false);
+        this->refresh_screen(this->panel->lcd->hasGraphics()?true:false); // graphics screens should be cleared
 
         // for LCDs with leds set them according to heater status
         // TODO should be enabled and disabled and settable from config
@@ -107,8 +117,8 @@ void WatchScreen::on_refresh(){
 
             if(this->bedtarget > 0)
                 this->panel->lcd->bltGlyph(32, 38, 23, 19, icons, 15, 64, 0);
-            
-            // fan always on for now
+
+            // fan appears always on for now
             this->panel->lcd->bltGlyph(96, 38, 23, 19, icons, 15, 96, 0);
         }
     }
@@ -116,9 +126,11 @@ void WatchScreen::on_refresh(){
 
 // queuing gcodes needs to be done from main loop
 void WatchScreen::on_main_loop() {
-    if(!this->speed_changed) return;
-    this->speed_changed= false;
-    set_speed();
+    if(this->issue_change_speed) {
+        this->issue_change_speed= false;
+        set_speed();
+    }
+    //time_idle();
 }
 
 // fetch the data we are displaying
@@ -156,7 +168,7 @@ void WatchScreen::get_temp_data() {
 // fetch the data we are displaying
 double WatchScreen::get_current_speed() {
     void *returned_data;
-    
+
     bool ok= THEKERNEL->public_data->get_value( robot_checksum, speed_override_percent_checksum, &returned_data );
     if(ok) {
         double cs= *static_cast<double *>(returned_data);
@@ -184,6 +196,8 @@ void WatchScreen::get_sd_play_info(){
         struct pad_progress p=  *static_cast<struct pad_progress*>(returned_data);
         this->elapsed_time= p.elapsed_secs;
         this->sd_pcnt_played= p.percent_complete;
+        this->panel->set_playing_file(p.filename);
+
     }else{
         this->elapsed_time= 0;
         this->sd_pcnt_played= 0;
@@ -195,25 +209,29 @@ void WatchScreen::display_menu_line(uint16_t line){
     switch( line ){
         case 0: this->panel->lcd->printf("H%03d/%03dc B%03d/%03dc", this->hotendtemp, this->hotendtarget, this->bedtemp, this->bedtarget); break;
         case 1: this->panel->lcd->printf("X%4d Y%4d Z%7.2f", (int)round(this->pos[0]), (int)round(this->pos[1]), this->pos[2]); break;
-        case 2: this->panel->lcd->printf("%3d%% %2d:%02d %3d%% sd", (int)round(this->current_speed), this->elapsed_time/60, this->elapsed_time%60, this->sd_pcnt_played); break;
+        case 2: this->panel->lcd->printf("%3d%% %2lu:%02lu %3u%% sd", this->current_speed, this->elapsed_time/60, this->elapsed_time%60, this->sd_pcnt_played); break;
         case 3: this->panel->lcd->printf("%19s", this->get_status()); break;
     }
 }
 
 const char* WatchScreen::get_status(){
+    if(panel->hasMessage()) {
+        return panel->getMessage().c_str();
+    }
+
     if(THEKERNEL->pauser->paused())
         return "Paused";
-    
+
     if(panel->is_playing())
         return panel->get_playing_file();
 
     return "Smoothie ready";
 }
 
-void WatchScreen::set_speed(){    
+void WatchScreen::set_speed(){
     // change pos by issuing a M220 Snnn
     char buf[32];
-    int n= snprintf(buf, sizeof(buf), "M220 S%f", this->current_speed);
+    int n= snprintf(buf, sizeof(buf), "M220 S%d", this->current_speed);
     string g(buf, n);
     send_gcode(g);
 }