Add Clear HOLD and HOLD display to panel (in cnc mode)
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / cnc / 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 "LcdBase.h"
10 #include "Panel.h"
11 #include "PanelScreen.h"
12 #include "MainMenuScreen.h"
13 #include "WatchScreen.h"
14 #include "libs/nuts_bolts.h"
15 #include "libs/utils.h"
16 #include "Robot.h"
17 #include "Conveyor.h"
18 #include "modules/robot/Conveyor.h"
19 #include "modules/utils/player/PlayerPublicAccess.h"
20 #include "NetworkPublicAccess.h"
21 #include "PublicData.h"
22 #include "SwitchPublicAccess.h"
23 #include "checksumm.h"
24 #include "StepperMotor.h"
25 #include "BaseSolution.h"
26
27 #ifndef NO_TOOLS_LASER
28 #include "Laser.h"
29 #endif
30
31 #include <math.h>
32 #include <string.h>
33 #include <string>
34 #include <stdio.h>
35 #include <algorithm>
36
37 using namespace std;
38
39 #define laser_checksum CHECKSUM("laser")
40
41 WatchScreen::WatchScreen()
42 {
43 speed_changed = false;
44 issue_change_speed = false;
45 ipstr = nullptr;
46 update_counts= 0;
47 }
48
49 WatchScreen::~WatchScreen()
50 {
51 delete[] ipstr;
52 }
53
54 void WatchScreen::on_enter()
55 {
56 THEPANEL->lcd->clear();
57 THEPANEL->setup_menu(8);
58 get_current_status();
59 get_wpos();
60 get_sd_play_info();
61 this->current_speed = lroundf(get_current_speed());
62 this->refresh_screen(false);
63 THEPANEL->enter_control_mode(1, 0.5);
64 THEPANEL->set_control_value(this->current_speed);
65 }
66
67 void WatchScreen::on_refresh()
68 {
69 // Exit if the button is clicked
70 if ( THEPANEL->click() ) {
71 THEPANEL->enter_screen(this->parent);
72 return;
73 }
74
75 // see if speed is being changed
76 if (THEPANEL->control_value_change()) {
77 this->current_speed = THEPANEL->get_control_value();
78 if (this->current_speed < 10) {
79 this->current_speed = 10;
80 THEPANEL->set_control_value(this->current_speed);
81 THEPANEL->reset_counter();
82 } else {
83 // flag the update to change the speed, we don't want to issue hundreds of M220s
84 // but we do want to display the change we are going to make
85 this->speed_changed = true; // flag indicating speed changed
86 this->refresh_screen(false);
87 }
88 }
89
90 // Update Only every 20 refreshes, 1 a second
91 update_counts++;
92 if ( update_counts % 20 == 0 ) {
93 get_sd_play_info();
94 get_wpos();
95 get_current_status();
96 if (this->speed_changed) {
97 this->issue_change_speed = true; // trigger actual command to change speed
98 this->speed_changed = false;
99 } else if (!this->issue_change_speed) { // change still queued
100 // read it in case it was changed via M220
101 this->current_speed = lroundf(get_current_speed());
102 THEPANEL->set_control_value(this->current_speed);
103 THEPANEL->reset_counter();
104 }
105
106 this->refresh_screen(THEPANEL->lcd->hasGraphics() ? true : false); // graphics screens should be cleared
107 }
108 }
109
110 void WatchScreen::get_wpos()
111 {
112 // get real time positions
113 float mpos[3];
114 THEROBOT->get_current_machine_position(mpos);
115 Robot::wcs_t wpos= THEROBOT->mcs2wcs(mpos);
116 this->wpos[0]= THEROBOT->from_millimeters(std::get<X_AXIS>(wpos));
117 this->wpos[1]= THEROBOT->from_millimeters(std::get<Y_AXIS>(wpos));
118 this->wpos[2]= THEROBOT->from_millimeters(std::get<Z_AXIS>(wpos));
119 this->mpos[0]= THEROBOT->from_millimeters(mpos[0]);
120 this->mpos[1]= THEROBOT->from_millimeters(mpos[1]);
121 this->mpos[2]= THEROBOT->from_millimeters(mpos[2]);
122
123 std::vector<Robot::wcs_t> v= THEROBOT->get_wcs_state();
124 char current_wcs= std::get<0>(v[0]);
125 this->wcs= wcs2gcode(current_wcs);
126 }
127
128 // queuing gcodes needs to be done from main loop
129 void WatchScreen::on_main_loop()
130 {
131 if (this->issue_change_speed) {
132 this->issue_change_speed = false;
133 set_speed();
134 }
135 PanelScreen::on_main_loop(); // in case any queued commands left
136 }
137
138 // fetch the data we are displaying
139 void WatchScreen::get_current_status()
140 {
141 // get spindle status
142 struct pad_switch s;
143 bool ok = PublicData::get_value( switch_checksum, fan_checksum, 0, &s );
144 if (ok) {
145 this->spindle_state = s.state;
146 } else {
147 // spindle probably disabled
148 this->spindle_state = false;
149 }
150 }
151
152 // fetch the data we are displaying
153 float WatchScreen::get_current_speed()
154 {
155 // in percent
156 return 6000.0F / THEROBOT->get_seconds_per_minute();
157 }
158
159 void WatchScreen::get_sd_play_info()
160 {
161 void *returned_data;
162 bool ok = PublicData::get_value( player_checksum, get_progress_checksum, &returned_data );
163 if (ok) {
164 struct pad_progress p = *static_cast<struct pad_progress *>(returned_data);
165 this->elapsed_time = p.elapsed_secs;
166 this->sd_pcnt_played = p.percent_complete;
167 THEPANEL->set_playing_file(p.filename);
168
169 } else {
170 this->elapsed_time = 0;
171 this->sd_pcnt_played = 0;
172 }
173 }
174
175 void WatchScreen::display_menu_line(uint16_t line)
176 {
177 // in menu mode
178 switch ( line ) {
179 case 0: THEPANEL->lcd->printf(" WCS MCS %s", THEROBOT->inch_mode ? "in" : "mm"); break;
180 case 1: THEPANEL->lcd->printf("X %8.3f %8.3f", wpos[0], mpos[0]); break;
181 case 2: THEPANEL->lcd->printf("Y %8.3f %8.3f", wpos[1], mpos[1]); break;
182 case 3: THEPANEL->lcd->printf("Z %8.3f %8.3f", wpos[2], mpos[2]); break;
183 case 4: THEPANEL->lcd->printf("%s F%6.1f/%6.1f", this->wcs.c_str(), // display requested feedrate and actual feedrate
184 THEROBOT->from_millimeters(THEROBOT->get_feed_rate()),
185 THEROBOT->from_millimeters(THEKERNEL->conveyor->get_current_feedrate()*60.0F));
186 break;
187 case 5: THEPANEL->lcd->printf("%3d%% %2lu:%02lu %3u%% sd", this->current_speed, this->elapsed_time / 60, this->elapsed_time % 60, this->sd_pcnt_played); break;
188 case 6:
189 if(THEPANEL->has_laser()){
190 #ifndef NO_TOOLS_LASER
191 Laser *plaser= nullptr;
192 if(PublicData::get_value(laser_checksum, (void *)&plaser) && plaser != nullptr) {
193 THEPANEL->lcd->printf("Laser S%1.4f/%1.2f%%", THEROBOT->get_s_value(), plaser->get_current_power());
194 }
195 #endif
196 }
197 break;
198 case 7: THEPANEL->lcd->printf("%19s", this->get_status()); break;
199 }
200 }
201
202 const char *WatchScreen::get_status()
203 {
204 if (THEPANEL->hasMessage())
205 return THEPANEL->getMessage().c_str();
206
207 if (THEKERNEL->is_halted())
208 return "ALARM";
209
210 if (THEKERNEL->get_feed_hold())
211 return "Hold";
212
213 if (THEPANEL->is_suspended())
214 return "Suspended";
215
216 if (THEPANEL->is_playing())
217 return THEPANEL->get_playing_file();
218
219 if (!THECONVEYOR->is_idle())
220 return "Running";
221
222 const char *ip = get_network();
223 if (ip == NULL) {
224 return "Idle";
225 } else {
226 return ip;
227 }
228 }
229
230 void WatchScreen::set_speed()
231 {
232 send_gcode("M220", 'S', this->current_speed);
233 }
234
235 const char *WatchScreen::get_network()
236 {
237 void *returned_data;
238
239 bool ok = PublicData::get_value( network_checksum, get_ip_checksum, &returned_data );
240 if (ok) {
241 uint8_t *ipaddr = (uint8_t *)returned_data;
242 char buf[20];
243 int n = snprintf(buf, sizeof(buf), "IP %d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
244 buf[n] = 0;
245 if (this->ipstr == nullptr) {
246 this->ipstr = new char[n + 1];
247 }
248 strcpy(this->ipstr, buf);
249
250 return this->ipstr;
251 }
252
253 return NULL;
254 }