Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FileSelectorWav.hpp
00001 //-------------------------------------------------------------- 00002 // FileSelector class ---- Header ---- 00003 // SD カード内のファイル名の一覧を表示し,ファイルを選択する 00004 // 00005 // 2016/06/30, Copyright (c) 2016 MIKAMI, Naoki 00006 //-------------------------------------------------------------- 00007 00008 #ifndef FILE_SELECTOR_HPP 00009 #define FILE_SELECTOR_HPP 00010 00011 #include "Label.hpp" 00012 #include "ButtonGroup.hpp" 00013 #include "SD_WavReader.hpp" 00014 #include <algorithm> // sort() で使用 00015 #include <string> 00016 00017 namespace Mikami 00018 { 00019 class FileSelector 00020 { 00021 public: 00022 FileSelector(uint8_t x0, uint8_t y0, int maxFiles, 00023 int maxNameLength, SD_WavReader &reader) 00024 : X_(x0), Y_(y0), W_H_(24), V_L_(36), 00025 MAX_FILES_(maxFiles), MAX_NAME_LENGTH_(maxNameLength), 00026 BASE_COLOR_(0xFF80FFA0), TOUCHED_COLOR_(0xFF80FFFF), 00027 fileNames_(maxFiles), 00028 rect_(NULL), lcd_(GuiBase::GetLcdPtr()), 00029 sdReader_(reader), prevFileCount_(0), prev_(-1) {} 00030 00031 ~FileSelector() 00032 { delete rect_; } 00033 00034 bool CreateTable(); 00035 00036 // ファイルを選択する 00037 bool Select(string &fileName); 00038 00039 // ファイルの一覧の表示 00040 void DisplayFileList(bool sortEnable = true); 00041 00042 // ファイルの一覧の消去 00043 void Erase(uint16_t x, uint16_t y, uint16_t width, uint16_t height, 00044 uint32_t color = GuiBase::ENUM_BACK); 00045 00046 private: 00047 const uint8_t X_, Y_, W_H_, V_L_; 00048 const int MAX_FILES_; 00049 const int MAX_NAME_LENGTH_; 00050 const uint32_t BASE_COLOR_; 00051 const uint32_t TOUCHED_COLOR_; 00052 00053 Array<string> fileNames_; 00054 ButtonGroup *rect_; 00055 Array<Label *> fileNameLabels_; 00056 LCD_DISCO_F746NG *lcd_; 00057 SD_WavReader &sdReader_; 00058 int fileCount_, prevFileCount_; 00059 int prev_; 00060 00061 // Label を生成 00062 void CreateLabels(); 00063 00064 // 拡張子を削除した文字列を取得 00065 string GetFileNameNoExt(int n); 00066 00067 // disallow copy constructor and assignment operator 00068 FileSelector(const FileSelector&); 00069 FileSelector& operator=(const FileSelector&); 00070 }; 00071 } 00072 /* 00073 namespace Mikami 00074 { 00075 class FileSelector 00076 { 00077 public: 00078 FileSelector(uint8_t x0, uint8_t y0, int maxFiles, 00079 int maxNameLength, SD_WavReader &reader) 00080 : X_(x0), Y_(y0), W_H_(24), V_L_(36), 00081 MAX_FILES_(maxFiles), MAX_NAME_LENGTH_(maxNameLength), 00082 BASE_COLOR_(0xFF80FFA0), TOUCHED_COLOR_(0xFF80FFFF), 00083 fileNames_(maxFiles), 00084 rect_(NULL), lcd_(GuiBase::GetLcdPtr()), 00085 sdReader_(reader), prevFileCount_(0), prev_(-1) {} 00086 00087 ~FileSelector() 00088 { delete rect_; } 00089 00090 bool CreateTable() 00091 { 00092 DIR* dp = opendir("/sd"); 00093 fileCount_ = 0; 00094 if (dp != NULL) 00095 { 00096 dirent* entry; 00097 for (int n=0; n<256; n++) 00098 { 00099 entry = readdir(dp); 00100 if (entry == NULL) break; 00101 00102 string strName = entry->d_name; 00103 if ( (strName.find(".wav") != string::npos) || 00104 (strName.find(".WAV") != string::npos) ) 00105 { 00106 sdReader_.Open(strName); // ファイルオープン 00107 00108 // PCM,16 ビットステレオ,標本化周波数 44.1 kHz 以外のファイルは除外 00109 if (sdReader_.IsWavFile()) 00110 fileNames_[fileCount_++] = strName; 00111 sdReader_.Close(); 00112 } 00113 00114 if (fileCount_ >= MAX_FILES_) break; 00115 } 00116 closedir(dp); 00117 } 00118 else 00119 return false; 00120 00121 if (fileCount_ == 0) return false; 00122 00123 if (rect_ != NULL) delete rect_; 00124 Array<string> nonString(fileCount_, ""); 00125 rect_ = new ButtonGroup(X_, Y_, W_H_, W_H_, fileCount_, 00126 nonString, 0, V_L_-W_H_, 1, 00127 -1, Font12, 0, GuiBase::ENUM_BACK, 00128 BASE_COLOR_, TOUCHED_COLOR_); 00129 for (int n=0; n<fileCount_; n++) rect_->Erase(n); 00130 CreateLabels(); 00131 prevFileCount_ = fileCount_; 00132 return true; 00133 } 00134 00135 // ファイルを選択する 00136 bool Select(string &fileName) 00137 { 00138 int n; 00139 if (rect_->GetTouchedNumber(n)) 00140 { 00141 fileNameLabels_[n]->Draw(GetFileNameNoExt(n), TOUCHED_COLOR_); 00142 if ((prev_ >= 0) && (prev_ != n)) 00143 fileNameLabels_[prev_]->Draw(GetFileNameNoExt(prev_)); 00144 prev_ = n; 00145 fileName = fileNames_[n]; 00146 return true; 00147 } 00148 else 00149 return false; 00150 } 00151 00152 // ファイルの一覧の表示 00153 void DisplayFileList(bool sortEnable = true) 00154 { 00155 if (sortEnable) 00156 std::sort((string *)fileNames_, 00157 (string *)fileNames_+fileCount_); 00158 00159 Erase(X_, Y_, MAX_NAME_LENGTH_*((sFONT *)(&Font16))->Width, 272-Y_); 00160 rect_->DrawAll(); 00161 for (int n=0; n<fileCount_; n++) 00162 fileNameLabels_[n]->Draw(GetFileNameNoExt(n)); 00163 } 00164 00165 // ファイルの一覧の消去 00166 void Erase(uint16_t x, uint16_t y, uint16_t width, uint16_t height, 00167 uint32_t color = GuiBase::ENUM_BACK) 00168 { 00169 lcd_->SetTextColor(color); 00170 lcd_->FillRect(x, y, width, height); 00171 } 00172 00173 private: 00174 const uint8_t X_, Y_, W_H_, V_L_; 00175 const int MAX_FILES_; 00176 const int MAX_NAME_LENGTH_; 00177 const uint32_t BASE_COLOR_; 00178 const uint32_t TOUCHED_COLOR_; 00179 00180 Array<string> fileNames_; 00181 ButtonGroup *rect_; 00182 Array<Label *> fileNameLabels_; 00183 LCD_DISCO_F746NG *lcd_; 00184 SD_WavReader &sdReader_; 00185 int fileCount_, prevFileCount_; 00186 int prev_; 00187 00188 // Label を生成 00189 void CreateLabels() 00190 { 00191 fileNameLabels_.SetSize(fileCount_); 00192 00193 for (int n=0; n<fileCount_; n++) 00194 fileNameLabels_[n] = new Label(X_+30, Y_+5+V_L_*n, "", 00195 Label::LEFT, Font16, 00196 BASE_COLOR_); 00197 } 00198 00199 // 拡張子を削除した文字列を取得 00200 string GetFileNameNoExt(int n) 00201 { 00202 string name = fileNames_[n]; 00203 name.erase(name.find(".")); 00204 return name.substr(0, MAX_NAME_LENGTH_); 00205 } 00206 00207 // disallow copy constructor and assignment operator 00208 FileSelector(const FileSelector&); 00209 FileSelector& operator=(const FileSelector&); 00210 }; 00211 } 00212 */ 00213 #endif // FILE_SELECTOR_HPP 00214
Generated on Fri Aug 5 2022 02:03:47 by
