Merge pull request #275 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / ControlScreen.cpp
CommitLineData
446deda2 1/*
35089dc7
JM
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.
446deda2 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
35089dc7
JM
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"
18using namespace std;
19
862fc625
JM
20ControlScreen::ControlScreen()
21{
35089dc7
JM
22 this->control_mode = NULL_CONTROL_MODE;
23}
24
862fc625
JM
25void ControlScreen::on_enter()
26{
35089dc7 27 this->panel->enter_menu_mode();
f65ce58f 28 this->panel->setup_menu(4);
58d6d841
JM
29 get_current_pos(this->pos);
30 this->refresh_menu();
862fc625 31 this->pos_changed = false;
35089dc7
JM
32}
33
34// called in on_idle()
862fc625
JM
35void ControlScreen::on_refresh()
36{
37 if ( this->panel->menu_change() ) {
35089dc7 38 this->refresh_menu();
58d6d841
JM
39 }
40
862fc625 41 if (this->control_mode == AXIS_CONTROL_MODE) {
446deda2 42
862fc625 43 if ( this->panel->click() ) {
58d6d841
JM
44 this->enter_menu_control();
45 this->refresh_menu();
446deda2 46
862fc625
JM
47 } else if (this->panel->control_value_change()) {
48 this->pos[this->controlled_axis - 'X'] = this->panel->get_control_value();
49 this->panel->lcd->setCursor(0, 2);
58d6d841 50 this->display_axis_line(this->controlled_axis);
862fc625 51 this->pos_changed = true; // make the gcode in main_loop
58d6d841 52 }
446deda2 53
862fc625
JM
54 } else {
55 if ( this->panel->click() ) {
446deda2 56 this->clicked_menu_entry(this->panel->get_menu_current_line());
58d6d841
JM
57 }
58 }
35089dc7
JM
59}
60
61// queuing gcodes needs to be done from main loop
862fc625
JM
62void ControlScreen::on_main_loop()
63{
58d6d841 64 // change actual axis value
862fc625
JM
65 if (!this->pos_changed) return;
66 this->pos_changed = false;
446deda2 67
862fc625 68 set_current_pos(this->controlled_axis, this->pos[this->controlled_axis - 'X']);
35089dc7
JM
69}
70
862fc625
JM
71void ControlScreen::display_menu_line(uint16_t line)
72{
58d6d841 73 // in menu mode
862fc625 74 switch ( line ) {
446deda2
JM
75 case 0: this->panel->lcd->printf("Back"); break;
76 case 1: this->display_axis_line('X'); break;
77 case 2: this->display_axis_line('Y'); break;
78 case 3: this->display_axis_line('Z'); break;
35089dc7
JM
79 }
80}
81
862fc625
JM
82void ControlScreen::display_axis_line(char axis)
83{
84 this->panel->lcd->printf("Move %c %8.3f", axis, this->pos[axis - 'X']);
35089dc7
JM
85}
86
87
862fc625
JM
88void ControlScreen::clicked_menu_entry(uint16_t line)
89{
90 switch ( line ) {
35089dc7
JM
91 case 0: this->panel->enter_screen(this->parent ); break;
92 case 1: this->enter_axis_control('X'); break;
93 case 2: this->enter_axis_control('Y'); break;
94 case 3: this->enter_axis_control('Z'); break;
95 }
96}
97
862fc625
JM
98void ControlScreen::enter_axis_control(char axis)
99{
35089dc7
JM
100 this->control_mode = AXIS_CONTROL_MODE;
101 this->controlled_axis = axis;
862fc625
JM
102 this->panel->enter_control_mode(this->jog_increment, this->jog_increment / 10);
103 this->panel->set_control_value(this->pos[axis - 'X']);
58d6d841 104 this->panel->lcd->clear();
862fc625 105 this->panel->lcd->setCursor(0, 2);
446deda2 106 this->display_axis_line(this->controlled_axis);
35089dc7
JM
107}
108
862fc625
JM
109void ControlScreen::enter_menu_control()
110{
58d6d841
JM
111 this->control_mode = NULL_CONTROL_MODE;
112 this->panel->enter_menu_mode();
35089dc7
JM
113}
114
862fc625
JM
115void ControlScreen::get_current_pos(double *cp)
116{
58d6d841
JM
117 void *returned_data;
118
862fc625
JM
119 bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
120 if (ok) {
121 double *p = static_cast<double *>(returned_data);
122 cp[0] = p[0];
123 cp[1] = p[1];
124 cp[2] = p[2];
58d6d841 125 }
35089dc7 126}
82d1ceb3 127
862fc625
JM
128void ControlScreen::set_current_pos(char axis, double p)
129{
58d6d841
JM
130 // change pos by issuing a G0 Xnnn
131 char buf[32];
862fc625 132 int n = snprintf(buf, sizeof(buf), "G0 %c%f F%d", axis, p, (int)round(panel->get_jogging_speed(axis)));
58d6d841
JM
133 string g(buf, n);
134 send_gcode(g);
35089dc7 135}