Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileScreen.cpp Source File

FileScreen.cpp

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       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.
00004       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.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #include "libs/Kernel.h"
00009 #include "Panel.h"
00010 #include "PanelScreen.h"
00011 #include "MainMenuScreen.h"
00012 #include "FileScreen.h"
00013 #include "libs/nuts_bolts.h"
00014 #include "libs/utils.h"
00015 #include <string>
00016 #include "libs/SerialMessage.h"
00017 
00018 #include "DirHandle.h"
00019 #include "mri.h"
00020 
00021 using namespace std;
00022 
00023 FileScreen::FileScreen()
00024 {
00025     this->current_folder = "";
00026     this->start_play = false;
00027 }
00028 
00029 // When entering this screen
00030 void FileScreen::on_enter()
00031 {
00032     this->panel->lcd->clear();
00033 
00034     // Default folder to enter
00035     if ( this->current_folder.compare("") == 0 ) {
00036         this->enter_folder("/");
00037     } else {
00038         this->enter_folder(this->current_folder);
00039     }
00040 }
00041 
00042 // For every ( potential ) refresh of the screen
00043 void FileScreen::on_refresh()
00044 {
00045     if ( this->panel->menu_change() ) {
00046         this->refresh_menu();
00047     }
00048     if ( this->panel->click() ) {
00049         this->clicked_line(this->panel->get_menu_current_line());
00050     }
00051 }
00052 
00053 // Enter a new folder
00054 void FileScreen::enter_folder(std::string folder)
00055 {
00056 
00057     // Rembember where we are
00058     this->current_folder = folder;
00059 
00060     // We need the number of lines to setup the menu
00061     uint16_t number_of_files_in_folder = this->count_folder_content(this->current_folder);
00062 
00063     // Setup menu
00064     this->panel->setup_menu(number_of_files_in_folder + 1); // same number of files as menu items
00065     this->panel->enter_menu_mode();
00066 
00067     // Display menu
00068     this->refresh_menu();
00069 }
00070 
00071 // Called by the panel when refreshing the menu, display .. then all files in the current dir
00072 void FileScreen::display_menu_line(uint16_t line)
00073 {
00074     if ( line == 0 ) {
00075         this->panel->lcd->printf("..");
00076     } else {
00077         this->panel->lcd->printf("%s", (this->file_at(line - 1).substr(0, 18)).c_str());
00078     }
00079 }
00080 
00081 // When a line is clicked in the menu, act
00082 void FileScreen::clicked_line(uint16_t line)
00083 {
00084     if ( line == 0 ) {
00085         if ( this->current_folder.compare("/") == 0 ) {
00086             // Exit file navigation
00087             this->panel->enter_screen(this->parent);
00088         } else {
00089             // Go up one folder
00090             this->current_folder = this->current_folder.substr(0, this->current_folder.find_last_of('/') + 1);
00091             if ( this->current_folder[this->current_folder.length() - 1] == '/' && this->current_folder.length() != 1 ) {
00092                 this->current_folder.erase(this->current_folder.length() - 1, 1);
00093             }
00094             this->enter_folder(this->current_folder);
00095         }
00096     } else {
00097         //printf("enter file\r\n");
00098         // Enter file
00099         string path = this->current_folder;
00100         if ( path.compare("/") == 0 ) {
00101             path = "";
00102         }
00103         path = path + "/" + this->file_at( line - 1 );
00104         if ( this->is_a_folder( path ) ) {
00105             this->enter_folder(path);
00106             return;
00107         }
00108 
00109         // start printing that file...
00110         this->play_path = path;
00111         this->start_play = true;
00112     }
00113 
00114 }
00115 
00116 // Check wether a line is a folder or a file
00117 bool FileScreen::is_a_folder( string path )
00118 {
00119     // In the special case of /local/ ( the mbed flash chip ) we don't have sub-folders, everything is a file
00120     if ( path.substr(0, 7).compare("/local/") == 0 ) {
00121         return false;
00122     }
00123     // Else, check if it's a folder or not
00124     DIR *d;
00125     d = opendir(path.c_str());
00126     if (d == NULL) {
00127         return false;
00128     } else {
00129         closedir(d);
00130         return true;
00131     }
00132 }
00133 
00134 // Find the "line"th file in the current folder
00135 string FileScreen::file_at(uint16_t line)
00136 {
00137     DIR *d;
00138     struct dirent *p;
00139     uint16_t count = 0;
00140     d = opendir(this->current_folder.c_str());
00141     if (d != NULL) {
00142         while ((p = readdir(d)) != NULL) {
00143             if ( count == line ) {
00144                 string to_return =  lc(string(p->d_name));
00145                 //printf("line: %u string:%s\r\n", line, to_return.c_str());
00146                 //if( to_return[to_return.length()-1] == '.' ){ to_return[to_return.length()-1] = 0x00; }
00147                 closedir(d);
00148                 return to_return;
00149             }
00150             count++;
00151         }
00152     }
00153 
00154     if (d != NULL) closedir(d);
00155     return "";
00156 }
00157 
00158 // Count how many files there are in the current folder
00159 uint16_t FileScreen::count_folder_content(std::string folder)
00160 {
00161     DIR *d;
00162     struct dirent *p;
00163     uint16_t count = 0;
00164     d = opendir(folder.c_str());
00165     if (d != NULL) {
00166         while ((p = readdir(d)) != NULL) {
00167             count++;
00168         }
00169         closedir(d);
00170         return count;
00171     } else {
00172         return 0;
00173     }
00174 }
00175 void FileScreen::on_main_loop()
00176 {
00177     if (this->start_play) {
00178         this->start_play = false;
00179         this->panel->set_playing_file(this->play_path);
00180         this->play(this->play_path);
00181         this->panel->enter_screen(this->parent);
00182         return;
00183     }
00184 }
00185 
00186 void FileScreen::play(string path)
00187 {
00188     struct SerialMessage message;
00189     message.message = string("play ") + path + " -q";
00190     message.stream = &(StreamOutput::NullStream);
00191     THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
00192 }