Merge pull request #204 from wolfmanjm/feature/rrd-glcd
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / WatchScreen.cpp
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 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.
4 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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 #include "libs/Kernel.h"
9 #include "Panel.h"
10 #include "PanelScreen.h"
11 #include "MainMenuScreen.h"
12 #include "WatchScreen.h"
13 #include "libs/nuts_bolts.h"
14 #include "libs/utils.h"
15 #include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
16 #include "modules/robot/RobotPublicAccess.h"
17 #include "modules/utils/player/PlayerPublicAccess.h"
18
19 #include <string>
20 using namespace std;
21
22 WatchScreen::WatchScreen(){
23 speed_changed= false;
24 }
25
26 void WatchScreen::on_enter(){
27 this->panel->lcd->clear();
28 this->panel->setup_menu(4);
29 get_temp_data();
30 get_current_pos(this->pos);
31 get_sd_play_info();
32 this->current_speed= get_current_speed();
33 this->refresh_screen(false);
34 this->panel->enter_control_mode(1,0.5);
35 this->panel->set_control_value(this->current_speed);
36 }
37
38 void WatchScreen::on_refresh(){
39 // Exit if the button is clicked
40 if( this->panel->click() ){
41 this->panel->enter_screen(this->parent);
42 return;
43 }
44
45 // see if speed is being changed
46 if(this->panel->control_value_change()) {
47 this->current_speed= this->panel->get_control_value();
48 if(this->current_speed < 1.0) {
49 this->current_speed= 1.0;
50 this->panel->set_control_value(this->current_speed);
51 this->panel->reset_counter();
52 }else{
53 // change actual speed
54 this->speed_changed= true; // flag main loop to isseu g code
55 this->refresh_screen(false);
56 }
57 }
58
59 // Update Only every 20 refreshes, 1 a second
60 static int update_counts = 0;
61 update_counts++;
62 if( update_counts % 20 == 0 ){
63 get_sd_play_info();
64 get_current_pos(this->pos);
65 get_temp_data();
66 this->current_speed= get_current_speed();
67 this->panel->set_control_value(this->current_speed); // in case it was changed via M220
68 this->panel->reset_counter();
69
70 this->refresh_screen(false);
71
72 // for LCDs with leds set them according to heater status
73 // TODO should be enabled and disabled and settable from config
74 this->panel->lcd->setLed(LED_BED_ON, this->bedtarget > 0);
75 this->panel->lcd->setLed(LED_HOTEND_ON, this->hotendtarget > 0);
76 //this->panel->lcd->setLed(LED_FAN_ON, this->fanon);
77 }
78 }
79
80 // queuing gcodes needs to be done from main loop
81 void WatchScreen::on_main_loop() {
82 if(!this->speed_changed) return;
83 this->speed_changed= false;
84 set_speed();
85 }
86
87 // fetch the data we are displaying
88 void WatchScreen::get_temp_data() {
89 void *returned_data;
90 bool ok;
91
92 ok= THEKERNEL->public_data->get_value( temperature_control_checksum, bed_checksum, current_temperature_checksum, &returned_data );
93 if(ok) {
94 struct pad_temperature temp= *static_cast<struct pad_temperature*>(returned_data);
95 this->bedtemp= round(temp.current_temperature);
96 if(this->bedtemp > 100000) this->bedtemp = -2;
97 this->bedtarget= round(temp.target_temperature);
98 //this->bedpwm= temp.pwm;
99 }else{
100 // temp probably disabled
101 this->bedtemp= -1;
102 this->bedtarget= -1;
103 }
104
105 ok= THEKERNEL->public_data->get_value( temperature_control_checksum, hotend_checksum, current_temperature_checksum, &returned_data );
106 if(ok) {
107 struct pad_temperature temp= *static_cast<struct pad_temperature*>(returned_data);
108 this->hotendtemp= round(temp.current_temperature);
109 if(this->hotendtemp > 100000) this->hotendtemp = -2;
110 this->hotendtarget= round(temp.target_temperature);
111 //this->hotendpwm= temp.pwm;
112 }else{
113 // temp probably disabled
114 this->hotendtemp= -1;
115 this->hotendtarget= -1;
116 }
117 }
118
119 // fetch the data we are displaying
120 double WatchScreen::get_current_speed() {
121 void *returned_data;
122
123 bool ok= THEKERNEL->public_data->get_value( robot_checksum, speed_override_percent_checksum, &returned_data );
124 if(ok) {
125 double cs= *static_cast<double *>(returned_data);
126 return cs;
127 }
128 return 0.0;
129 }
130
131 void WatchScreen::get_current_pos(double *cp){
132 void *returned_data;
133
134 bool ok= THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
135 if(ok) {
136 double *p= static_cast<double *>(returned_data);
137 cp[0]= p[0];
138 cp[1]= p[1];
139 cp[2]= p[2];
140 }
141 }
142
143 void WatchScreen::get_sd_play_info(){
144 void *returned_data;
145 bool ok= THEKERNEL->public_data->get_value( player_checksum, get_progress_checksum, &returned_data );
146 if(ok) {
147 struct pad_progress p= *static_cast<struct pad_progress*>(returned_data);
148 this->elapsed_time= p.elapsed_secs;
149 this->sd_pcnt_played= p.percent_complete;
150 }else{
151 this->elapsed_time= 0;
152 this->sd_pcnt_played= 0;
153 }
154 }
155
156 void WatchScreen::display_menu_line(uint16_t line){
157 // in menu mode
158 switch( line ){
159 case 0: this->panel->lcd->printf("H%03d/%03dc B%03d/%03dc", this->hotendtemp, this->hotendtarget, this->bedtemp, this->bedtarget); break;
160 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;
161 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;
162 case 3: this->panel->lcd->printf("%19s", this->get_status()); break;
163 }
164 }
165
166 const char* WatchScreen::get_status(){
167 if(THEKERNEL->pauser->paused())
168 return "Paused";
169
170 if(panel->is_playing())
171 return panel->get_playing_file();
172
173 return "Smoothie ready";
174 }
175
176 void WatchScreen::set_speed(){
177 // change pos by issuing a M220 Snnn
178 char buf[32];
179 int n= snprintf(buf, sizeof(buf), "M220 S%f", this->current_speed);
180 string g(buf, n);
181 send_gcode(g);
182 }