PHung Tung / MyClasses_Functions
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileSelectorWav.cpp Source File

FileSelectorWav.cpp

00001 //--------------------------------------------------------------
00002 //  FileSelector class
00003 //      SD カード内のファイル名の一覧を表示し,ファイルを選択する
00004 //
00005 //  2016/06/30, Copyright (c) 2016 MIKAMI, Naoki
00006 //--------------------------------------------------------------
00007 
00008 #include "FileSelectorWav.hpp"
00009 
00010 namespace Mikami
00011 {
00012     bool FileSelector::CreateTable()
00013     {
00014         DIR* dp = opendir("/sd");
00015         fileCount_ = 0; 
00016         if (dp != NULL)
00017         {
00018             dirent* entry;
00019             for (int n=0; n<256; n++)
00020             {
00021                 entry = readdir(dp);
00022                 if (entry == NULL) break;
00023 
00024                 string strName = entry->d_name;
00025                 if ( (strName.find(".wav") != string::npos) ||
00026                      (strName.find(".WAV") != string::npos) )
00027                 {
00028                     sdReader_.Open(strName);        // ファイルオープン
00029 
00030                     // PCM,16 ビットステレオ,標本化周波数 44.1 kHz 以外のファイルは除外
00031                     if (sdReader_.IsWavFile())
00032                         fileNames_[fileCount_++] = strName;
00033                     sdReader_.Close();
00034                 }
00035 
00036                 if (fileCount_ >= MAX_FILES_) break;
00037             }
00038             closedir(dp); 
00039         }
00040         else
00041             return false;
00042 
00043         if (fileCount_ == 0) return false;
00044 
00045         if (rect_ != NULL) delete rect_;
00046         Array<string> nonString(fileCount_, "");
00047         rect_ = new ButtonGroup(X_, Y_, W_H_, W_H_, fileCount_,
00048                                 nonString, 0, V_L_-W_H_, 1,
00049                                 -1, Font12, 0, GuiBase::ENUM_BACK,
00050                                 BASE_COLOR_, TOUCHED_COLOR_);
00051         for (int n=0; n<fileCount_; n++) rect_->Erase(n);
00052         CreateLabels();   
00053         prevFileCount_ = fileCount_;
00054         return true;
00055     }
00056 
00057     // ファイルを選択する
00058     bool FileSelector::Select(string &fileName)
00059     {
00060         int n;
00061         if (rect_->GetTouchedNumber(n))
00062         {
00063             fileNameLabels_[n]->Draw(GetFileNameNoExt(n), TOUCHED_COLOR_);
00064             if ((prev_ >= 0) && (prev_ != n))
00065                 fileNameLabels_[prev_]->Draw(GetFileNameNoExt(prev_));
00066             prev_ = n;
00067             fileName = fileNames_[n];
00068             return true;
00069         }
00070         else
00071             return false;
00072     }
00073 
00074     // ファイルの一覧の表示
00075     void FileSelector::DisplayFileList(bool sortEnable)
00076     {
00077         if (sortEnable)
00078             std::sort((string *)fileNames_,
00079                       (string *)fileNames_+fileCount_); 
00080         
00081         Erase(X_, Y_, MAX_NAME_LENGTH_*((sFONT *)(&Font16))->Width, 272-Y_);
00082         rect_->DrawAll();
00083         for (int n=0; n<fileCount_; n++)
00084             fileNameLabels_[n]->Draw(GetFileNameNoExt(n));
00085     }
00086 
00087     // ファイルの一覧の消去
00088     void FileSelector::Erase(uint16_t x, uint16_t y, uint16_t width, uint16_t height,
00089                uint32_t color)
00090     {
00091         lcd_->SetTextColor(color);
00092         lcd_->FillRect(x, y, width, height);
00093     }
00094 
00095     // Label を生成
00096     void FileSelector::CreateLabels()
00097     {
00098         fileNameLabels_.SetSize(fileCount_);
00099                     
00100         for (int n=0; n<fileCount_; n++)
00101             fileNameLabels_[n] = new Label(X_+30, Y_+5+V_L_*n, "",
00102                                            Label::LEFT, Font16,
00103                                            BASE_COLOR_);
00104     }
00105 
00106     // 拡張子を削除した文字列を取得
00107     string FileSelector::GetFileNameNoExt(int n)
00108     {
00109         string name = fileNames_[n];
00110         name.erase(name.find("."));
00111         return name.substr(0, MAX_NAME_LENGTH_);
00112     }
00113 
00114 }