Merge pull request #275 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / modules / utils / panel / screens / CustomScreen.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 "CustomScreen.h"
8 #include "libs/Kernel.h"
9
10 #include <algorithm>
11
12 #define enable_checksum CHECKSUM("enable")
13 #define custom_menu_checksum CHECKSUM("custom_menu")
14 #define name_checksum CHECKSUM("name")
15 #define command_checksum CHECKSUM("command")
16
17 using namespace std;
18
19 CustomScreen::CustomScreen()
20 {
21 //printf("Setting up CustomScreen\n");
22 vector<uint16_t> modules;
23 THEKERNEL->config->get_module_list( &modules, custom_menu_checksum );
24
25 // load the custom menu items
26 for ( unsigned int i = 0; i < modules.size(); i++ ) {
27 if (THEKERNEL->config->value(custom_menu_checksum, modules[i], enable_checksum )->as_bool()) {
28 // Get Menu entry name
29 string name = THEKERNEL->config->value(custom_menu_checksum, modules[i], name_checksum )->as_string();
30 std::replace( name.begin(), name.end(), '_', ' '); // replace _ with space
31
32 // Get Command
33 string command = THEKERNEL->config->value(custom_menu_checksum, modules[i], command_checksum )->as_string();
34 std::replace( command.begin(), command.end(), '_', ' '); // replace _ with space
35 std::replace( command.begin(), command.end(), '|', '\n'); // replace | with \n for multiple commands
36
37 // put in menu item list
38 menu_items.push_back(make_tuple(strdup(name.c_str()), strdup(command.c_str())));
39 //printf("added menu %s, command %s\n", name.c_str(), command.c_str());
40 }
41 }
42 }
43
44 void CustomScreen::on_enter()
45 {
46 this->panel->enter_menu_mode();
47 this->panel->setup_menu(menu_items.size() + 1);
48 this->refresh_menu();
49 }
50
51 void CustomScreen::on_refresh()
52 {
53 if ( this->panel->menu_change() ) {
54 this->refresh_menu();
55 }
56 if ( this->panel->click() ) {
57 this->clicked_menu_entry(this->panel->get_menu_current_line());
58 }
59 }
60
61 void CustomScreen::display_menu_line(uint16_t line)
62 {
63 if (line == 0) {
64 this->panel->lcd->printf("Back");
65 } else {
66 this->panel->lcd->printf(std::get<0>(menu_items[line-1]));
67 }
68 }
69
70 void CustomScreen::clicked_menu_entry(uint16_t line)
71 {
72 if (line == 0) {
73 this->panel->enter_screen(this->parent);
74 } else {
75 command = std::get<1>(menu_items[line-1]);
76 }
77 }
78
79 // queuing commands needs to be done from main loop
80 void CustomScreen::on_main_loop()
81 {
82 // issue command
83 if (this->command.empty()) return;
84 send_command(this->command.c_str());
85 this->command.clear();
86 }