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