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