Enumerate hotends for display in panel temperature settings
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / ControlScreen.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 "libs/SerialMessage.h"
10 #include "Panel.h"
11 #include "PanelScreen.h"
12 #include "MainMenuScreen.h"
13 #include "ControlScreen.h"
14 #include "libs/nuts_bolts.h"
15 #include "libs/utils.h"
16 #include <string>
17 #include "modules/robot/RobotPublicAccess.h"
18 #include "PublicData.h"
19 #include "checksumm.h"
20
21 #include <math.h>
22
23 using namespace std;
24
25 #define NULL_CONTROL_MODE 0
26 #define AXIS_CONTROL_MODE 1
27 #define INCREMENT_SELECTION_MODE 2
28
29 ControlScreen::ControlScreen()
30 {
31 this->control_mode = NULL_CONTROL_MODE;
32 }
33
34 void ControlScreen::on_enter()
35 {
36 THEPANEL->enter_menu_mode();
37 THEPANEL->setup_menu(4);
38 get_current_pos(this->pos);
39 this->refresh_menu();
40 this->pos_changed = false;
41 }
42
43 // called in on_idle()
44 void ControlScreen::on_refresh()
45 {
46 if ( THEPANEL->menu_change() ) {
47 this->refresh_menu();
48 }
49
50 if (this->control_mode == AXIS_CONTROL_MODE) {
51
52 if ( THEPANEL->click() ) {
53 this->enter_menu_control();
54 this->refresh_menu();
55
56 } else if (THEPANEL->control_value_change()) {
57 this->pos[this->controlled_axis - 'X'] = THEPANEL->get_control_value();
58 THEPANEL->lcd->setCursor(0, 2);
59 this->display_axis_line(this->controlled_axis);
60 this->pos_changed = true; // make the gcode in main_loop
61 }
62
63 } else {
64 if ( THEPANEL->click() ) {
65 this->clicked_menu_entry(THEPANEL->get_menu_current_line());
66 }
67 }
68 }
69
70 // queuing gcodes needs to be done from main loop
71 void ControlScreen::on_main_loop()
72 {
73 // change actual axis value
74 if (!this->pos_changed) return;
75 this->pos_changed = false;
76
77 set_current_pos(this->controlled_axis, this->pos[this->controlled_axis - 'X']);
78 }
79
80 void ControlScreen::display_menu_line(uint16_t line)
81 {
82 // in menu mode
83 switch ( line ) {
84 case 0: THEPANEL->lcd->printf("Back"); break;
85 case 1: this->display_axis_line('X'); break;
86 case 2: this->display_axis_line('Y'); break;
87 case 3: this->display_axis_line('Z'); break;
88 }
89 }
90
91 void ControlScreen::display_axis_line(char axis)
92 {
93 THEPANEL->lcd->printf("Move %c %8.3f", axis, this->pos[axis - 'X']);
94 }
95
96
97 void ControlScreen::clicked_menu_entry(uint16_t line)
98 {
99 switch ( line ) {
100 case 0: THEPANEL->enter_screen(this->parent ); break;
101 case 1: this->enter_axis_control('X'); break;
102 case 2: this->enter_axis_control('Y'); break;
103 case 3: this->enter_axis_control('Z'); break;
104 }
105 }
106
107 void ControlScreen::enter_axis_control(char axis)
108 {
109 this->control_mode = AXIS_CONTROL_MODE;
110 this->controlled_axis = axis;
111 THEPANEL->enter_control_mode(this->jog_increment, this->jog_increment / 10);
112 THEPANEL->set_control_value(this->pos[axis - 'X']);
113 THEPANEL->lcd->clear();
114 THEPANEL->lcd->setCursor(0, 2);
115 this->display_axis_line(this->controlled_axis);
116 }
117
118 void ControlScreen::enter_menu_control()
119 {
120 this->control_mode = NULL_CONTROL_MODE;
121 THEPANEL->enter_menu_mode();
122 }
123
124 void ControlScreen::get_current_pos(float *cp)
125 {
126 void *returned_data;
127
128 bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
129 if (ok) {
130 float *p = static_cast<float *>(returned_data);
131 cp[0] = p[0];
132 cp[1] = p[1];
133 cp[2] = p[2];
134 }
135 }
136
137 void ControlScreen::set_current_pos(char axis, float p)
138 {
139 // change pos by issuing a G0 Xnnn
140 char buf[32];
141 int n = snprintf(buf, sizeof(buf), "G0 %c%f F%d", axis, p, (int)round(THEPANEL->get_jogging_speed(axis)));
142 string g(buf, n);
143 send_gcode(g);
144 }