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