turn off all heaters with panels cool down menu item
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / PrepareScreen.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 "LcdBase.h"
12 #include "PrepareScreen.h"
13 #include "ExtruderScreen.h"
14 #include "libs/nuts_bolts.h"
15 #include "libs/utils.h"
16 #include "checksumm.h"
17 #include "PublicDataRequest.h"
18 #include "PublicData.h"
19 #include "TemperatureControlPublicAccess.h"
20 #include "ModifyValuesScreen.h"
21 #include "TemperatureControlPool.h"
22
23 #include <string>
24 using namespace std;
25
26 PrepareScreen::PrepareScreen()
27 {
28 // Children screens
29 std::vector<struct pad_temperature> controllers;
30 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
31 if (ok && controllers.size() > 0) {
32 this->extruder_screen = (new ExtruderScreen())->set_parent(this);
33 }else{
34 this->extruder_screen= nullptr;
35 }
36 }
37
38 void PrepareScreen::on_enter()
39 {
40 THEPANEL->enter_menu_mode();
41 // if no heaters or extruder then don't show related menu items
42 THEPANEL->setup_menu((this->extruder_screen != nullptr) ? 9 : 5);
43 this->refresh_menu();
44 }
45
46 void PrepareScreen::on_refresh()
47 {
48 if ( THEPANEL->menu_change() ) {
49 this->refresh_menu();
50 }
51 if ( THEPANEL->click() ) {
52 this->clicked_menu_entry(THEPANEL->get_menu_current_line());
53 }
54 }
55
56 void PrepareScreen::display_menu_line(uint16_t line)
57 {
58 switch ( line ) {
59 case 0: THEPANEL->lcd->printf("Back" ); break;
60 case 1: THEPANEL->lcd->printf("Home All Axes" ); break;
61 case 2: THEPANEL->lcd->printf("Set Home" ); break;
62 case 3: THEPANEL->lcd->printf("Set Z0" ); break;
63 case 4: THEPANEL->lcd->printf("Motors off" ); break;
64 // these won't be accessed if no heaters or extruders
65 case 5: THEPANEL->lcd->printf("Pre Heat" ); break;
66 case 6: THEPANEL->lcd->printf("Cool Down" ); break;
67 case 7: THEPANEL->lcd->printf("Extruder..." ); break;
68 case 8: THEPANEL->lcd->printf("Set Temperature"); break;
69 }
70 }
71
72 void PrepareScreen::clicked_menu_entry(uint16_t line)
73 {
74 switch ( line ) {
75 case 0: THEPANEL->enter_screen(this->parent); break;
76 case 1: send_command("G28"); break;
77 case 2: send_command("G92 X0 Y0 Z0"); break;
78 case 3: send_command("G92 Z0"); break;
79 case 4: send_command("M84"); break;
80 case 5: this->preheat(); break;
81 case 6: this->cooldown(); break;
82 case 7: THEPANEL->enter_screen(this->extruder_screen); break;
83 case 8: setup_temperature_screen(); break;
84 }
85 }
86
87 void PrepareScreen::preheat()
88 {
89 float t = THEPANEL->get_default_hotend_temp();
90 PublicData::set_value( temperature_control_checksum, hotend_checksum, &t );
91 t = THEPANEL->get_default_bed_temp();
92 PublicData::set_value( temperature_control_checksum, bed_checksum, &t );
93 }
94
95 void PrepareScreen::cooldown()
96 {
97 float t = 0;
98 std::vector<struct pad_temperature> controllers;
99 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
100 if (ok) {
101 for (auto &c : controllers) {
102 PublicData::set_value( temperature_control_checksum, c.id, &t );
103 }
104 }
105 }
106
107 static float getTargetTemperature(uint16_t heater_cs)
108 {
109 struct pad_temperature temp;
110 bool ok = PublicData::get_value( temperature_control_checksum, current_temperature_checksum, heater_cs, &temp );
111
112 if (ok) {
113 return temp.target_temperature;
114 }
115
116 return 0.0F;
117 }
118
119 void PrepareScreen::setup_temperature_screen()
120 {
121 // setup temperature screen
122 auto mvs= new ModifyValuesScreen(true); // delete itself on exit
123 mvs->set_parent(this);
124
125 int cnt= 0;
126 // returns enabled temperature controllers
127 std::vector<struct pad_temperature> controllers;
128 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
129 if (ok) {
130 for (auto &c : controllers) {
131 // rename if two of the known types
132 const char *name;
133 if(c.designator == "T") name= "Hotend";
134 else if(c.designator == "B") name= "Bed";
135 else name= c.designator.c_str();
136 uint16_t i= c.id;
137
138 mvs->addMenuItem(name, // menu name
139 [i]() -> float { return getTargetTemperature(i); }, // getter
140 [i](float t) { PublicData::set_value( temperature_control_checksum, i, &t ); }, // setter
141 1.0F, // increment
142 0.0F, // Min
143 500.0F // Max
144 );
145 cnt++;
146 }
147 }
148
149 if(cnt > 0) {
150 THEPANEL->enter_screen(mvs);
151 }else{
152 // no heaters and probably no extruders either
153 delete mvs;
154 }
155 }