Enumerate hotends for display in panel temperature settings
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / ModifyValuesScreen.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 #include "ModifyValuesScreen.h"
8 #include "libs/Kernel.h"
9 #include "Config.h"
10 #include "checksumm.h"
11 #include "LcdBase.h"
12 #include "Panel.h"
13 #include "ConfigValue.h"
14
15 #include <string.h>
16
17 #include <algorithm>
18
19 using namespace std;
20
21 #define MENU_CONTROL_MODE 0
22 #define VALUE_CONTROL_MODE 1
23
24 ModifyValuesScreen::ModifyValuesScreen()
25 {
26 execute_function = -1;
27 control_mode = MENU_CONTROL_MODE;
28 }
29
30 ModifyValuesScreen::~ModifyValuesScreen()
31 {
32 // free up the strdup() name
33 for(auto i : menu_items) {
34 free(std::get<0>(i));
35 }
36 }
37
38 void ModifyValuesScreen::on_enter()
39 {
40 THEPANEL->enter_menu_mode();
41 THEPANEL->setup_menu(menu_items.size() + 1);
42 this->refresh_menu();
43 }
44
45 void ModifyValuesScreen::on_refresh()
46 {
47 if ( THEPANEL->menu_change() ) {
48 this->refresh_menu();
49 }
50
51 if (this->control_mode == VALUE_CONTROL_MODE) {
52
53 if ( THEPANEL->click() ) {
54 // done changing value
55 execute_function= selected_item; // this causes on_main_loop to change the value
56 this->control_mode = MENU_CONTROL_MODE;
57 THEPANEL->enter_menu_mode(true);
58
59 } else if (THEPANEL->control_value_change()) {
60 this->new_value = THEPANEL->get_control_value();
61 if(!isnan(this->min_value) && this->new_value < this->min_value) {
62 THEPANEL->set_control_value((this->new_value = this->min_value));
63 THEPANEL->reset_counter();
64 }
65 if(!isnan(this->max_value) && this->new_value > this->max_value) {
66 THEPANEL->set_control_value((this->new_value = this->max_value));
67 THEPANEL->reset_counter();
68 }
69 THEPANEL->lcd->setCursor(0, 2);
70 THEPANEL->lcd->printf("%10.3f ", this->new_value);
71 }
72
73 } else {
74 if ( THEPANEL->click() ) {
75 this->clicked_menu_entry(THEPANEL->get_menu_current_line());
76 }
77 }
78 }
79
80 void ModifyValuesScreen::display_menu_line(uint16_t line)
81 {
82 if (line == 0) {
83 THEPANEL->lcd->printf("Back");
84 } else {
85 line--;
86 const char *name = std::get<0>(menu_items[line]);
87 float value = std::get<1>(menu_items[line])();
88 THEPANEL->lcd->printf("%-12s %8.3f", name, value);
89 }
90 }
91
92 void ModifyValuesScreen::clicked_menu_entry(uint16_t line)
93 {
94 if (line == 0) {
95 THEPANEL->enter_screen(this->parent);
96
97 } else {
98 line--;
99 this->selected_item = line;
100 this->control_mode = VALUE_CONTROL_MODE;
101
102 const char *name = std::get<0>(menu_items[line]);
103 float value = std::get<1>(menu_items[line])();
104 float inc= std::get<3>(menu_items[line]);
105 THEPANEL->enter_control_mode(inc, inc / 10);
106 this->min_value= std::get<4>(menu_items[line]);
107 this->max_value= std::get<5>(menu_items[line]);
108
109 THEPANEL->set_control_value(value);
110 THEPANEL->lcd->clear();
111 THEPANEL->lcd->setCursor(0, 0);
112 THEPANEL->lcd->printf("%s", name);
113 THEPANEL->lcd->setCursor(0, 2);
114 THEPANEL->lcd->printf("%10.3f", value);
115 }
116 }
117
118 // queuing commands needs to be done from main loop
119 void ModifyValuesScreen::on_main_loop()
120 {
121 // issue command
122 if (execute_function == -1) return;
123
124 // execute the setter function for the specified menu item
125 std::get<2>(menu_items[execute_function])(new_value);
126 execute_function = -1;
127 }
128
129 void ModifyValuesScreen::addMenuItem(const char *name, std::function<float()> getter, std::function<void(float)> setter, float inc, float min, float max)
130 {
131 menu_items.push_back(make_tuple(strdup(name), getter, setter, inc, min, max));
132 }