![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Chapter003_TRobot
Dependencies: F746_GUI Array_Matrix TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG SDFileSystem_Warning_Fixed FrequencyResponseDrawer F746_SAI_IO
MyClasses_Functions/FileSelectorWav.hpp@11:769d986c10fa, 2016-07-04 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Jul 04 05:59:44 2016 +0000
- Revision:
- 11:769d986c10fa
- Parent:
- 5:3e8ca1ed31a1
12
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:04b43b777fae | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 11:769d986c10fa | 2 | // FileSelector class ---- Header ---- |
MikamiUitOpen | 0:04b43b777fae | 3 | // SD カード内のファイル名の一覧を表示し,ファイルを選択する |
MikamiUitOpen | 0:04b43b777fae | 4 | // |
MikamiUitOpen | 11:769d986c10fa | 5 | // 2016/06/30, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:04b43b777fae | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:04b43b777fae | 7 | |
MikamiUitOpen | 0:04b43b777fae | 8 | #ifndef FILE_SELECTOR_HPP |
MikamiUitOpen | 0:04b43b777fae | 9 | #define FILE_SELECTOR_HPP |
MikamiUitOpen | 0:04b43b777fae | 10 | |
MikamiUitOpen | 0:04b43b777fae | 11 | #include "Label.hpp" |
MikamiUitOpen | 0:04b43b777fae | 12 | #include "ButtonGroup.hpp" |
MikamiUitOpen | 0:04b43b777fae | 13 | #include "SD_WavReader.hpp" |
MikamiUitOpen | 0:04b43b777fae | 14 | #include <algorithm> // sort() で使用 |
MikamiUitOpen | 0:04b43b777fae | 15 | #include <string> |
MikamiUitOpen | 0:04b43b777fae | 16 | |
MikamiUitOpen | 0:04b43b777fae | 17 | namespace Mikami |
MikamiUitOpen | 0:04b43b777fae | 18 | { |
MikamiUitOpen | 0:04b43b777fae | 19 | class FileSelector |
MikamiUitOpen | 0:04b43b777fae | 20 | { |
MikamiUitOpen | 0:04b43b777fae | 21 | public: |
MikamiUitOpen | 0:04b43b777fae | 22 | FileSelector(uint8_t x0, uint8_t y0, int maxFiles, |
MikamiUitOpen | 0:04b43b777fae | 23 | int maxNameLength, SD_WavReader &reader) |
MikamiUitOpen | 0:04b43b777fae | 24 | : X_(x0), Y_(y0), W_H_(24), V_L_(36), |
MikamiUitOpen | 0:04b43b777fae | 25 | MAX_FILES_(maxFiles), MAX_NAME_LENGTH_(maxNameLength), |
MikamiUitOpen | 0:04b43b777fae | 26 | BASE_COLOR_(0xFF80FFA0), TOUCHED_COLOR_(0xFF80FFFF), |
MikamiUitOpen | 11:769d986c10fa | 27 | fileNames_(maxFiles), |
MikamiUitOpen | 11:769d986c10fa | 28 | rect_(NULL), lcd_(GuiBase::GetLcdPtr()), |
MikamiUitOpen | 0:04b43b777fae | 29 | sdReader_(reader), prevFileCount_(0), prev_(-1) {} |
MikamiUitOpen | 0:04b43b777fae | 30 | |
MikamiUitOpen | 0:04b43b777fae | 31 | ~FileSelector() |
MikamiUitOpen | 11:769d986c10fa | 32 | { delete rect_; } |
MikamiUitOpen | 11:769d986c10fa | 33 | |
MikamiUitOpen | 11:769d986c10fa | 34 | bool CreateTable(); |
MikamiUitOpen | 11:769d986c10fa | 35 | |
MikamiUitOpen | 11:769d986c10fa | 36 | // ファイルを選択する |
MikamiUitOpen | 11:769d986c10fa | 37 | bool Select(string &fileName); |
MikamiUitOpen | 11:769d986c10fa | 38 | |
MikamiUitOpen | 11:769d986c10fa | 39 | // ファイルの一覧の表示 |
MikamiUitOpen | 11:769d986c10fa | 40 | void DisplayFileList(bool sortEnable = true); |
MikamiUitOpen | 11:769d986c10fa | 41 | |
MikamiUitOpen | 11:769d986c10fa | 42 | // ファイルの一覧の消去 |
MikamiUitOpen | 11:769d986c10fa | 43 | void Erase(uint16_t x, uint16_t y, uint16_t width, uint16_t height, |
MikamiUitOpen | 11:769d986c10fa | 44 | uint32_t color = GuiBase::ENUM_BACK); |
MikamiUitOpen | 11:769d986c10fa | 45 | |
MikamiUitOpen | 11:769d986c10fa | 46 | private: |
MikamiUitOpen | 11:769d986c10fa | 47 | const uint8_t X_, Y_, W_H_, V_L_; |
MikamiUitOpen | 11:769d986c10fa | 48 | const int MAX_FILES_; |
MikamiUitOpen | 11:769d986c10fa | 49 | const int MAX_NAME_LENGTH_; |
MikamiUitOpen | 11:769d986c10fa | 50 | const uint32_t BASE_COLOR_; |
MikamiUitOpen | 11:769d986c10fa | 51 | const uint32_t TOUCHED_COLOR_; |
MikamiUitOpen | 11:769d986c10fa | 52 | |
MikamiUitOpen | 11:769d986c10fa | 53 | Array<string> fileNames_; |
MikamiUitOpen | 11:769d986c10fa | 54 | ButtonGroup *rect_; |
MikamiUitOpen | 11:769d986c10fa | 55 | Array<Label *> fileNameLabels_; |
MikamiUitOpen | 11:769d986c10fa | 56 | LCD_DISCO_F746NG *lcd_; |
MikamiUitOpen | 11:769d986c10fa | 57 | SD_WavReader &sdReader_; |
MikamiUitOpen | 11:769d986c10fa | 58 | int fileCount_, prevFileCount_; |
MikamiUitOpen | 11:769d986c10fa | 59 | int prev_; |
MikamiUitOpen | 11:769d986c10fa | 60 | |
MikamiUitOpen | 11:769d986c10fa | 61 | // Label を生成 |
MikamiUitOpen | 11:769d986c10fa | 62 | void CreateLabels(); |
MikamiUitOpen | 11:769d986c10fa | 63 | |
MikamiUitOpen | 11:769d986c10fa | 64 | // 拡張子を削除した文字列を取得 |
MikamiUitOpen | 11:769d986c10fa | 65 | string GetFileNameNoExt(int n); |
MikamiUitOpen | 11:769d986c10fa | 66 | |
MikamiUitOpen | 11:769d986c10fa | 67 | // disallow copy constructor and assignment operator |
MikamiUitOpen | 11:769d986c10fa | 68 | FileSelector(const FileSelector&); |
MikamiUitOpen | 11:769d986c10fa | 69 | FileSelector& operator=(const FileSelector&); |
MikamiUitOpen | 11:769d986c10fa | 70 | }; |
MikamiUitOpen | 11:769d986c10fa | 71 | } |
MikamiUitOpen | 11:769d986c10fa | 72 | /* |
MikamiUitOpen | 11:769d986c10fa | 73 | namespace Mikami |
MikamiUitOpen | 11:769d986c10fa | 74 | { |
MikamiUitOpen | 11:769d986c10fa | 75 | class FileSelector |
MikamiUitOpen | 11:769d986c10fa | 76 | { |
MikamiUitOpen | 11:769d986c10fa | 77 | public: |
MikamiUitOpen | 11:769d986c10fa | 78 | FileSelector(uint8_t x0, uint8_t y0, int maxFiles, |
MikamiUitOpen | 11:769d986c10fa | 79 | int maxNameLength, SD_WavReader &reader) |
MikamiUitOpen | 11:769d986c10fa | 80 | : X_(x0), Y_(y0), W_H_(24), V_L_(36), |
MikamiUitOpen | 11:769d986c10fa | 81 | MAX_FILES_(maxFiles), MAX_NAME_LENGTH_(maxNameLength), |
MikamiUitOpen | 11:769d986c10fa | 82 | BASE_COLOR_(0xFF80FFA0), TOUCHED_COLOR_(0xFF80FFFF), |
MikamiUitOpen | 11:769d986c10fa | 83 | fileNames_(maxFiles), |
MikamiUitOpen | 11:769d986c10fa | 84 | rect_(NULL), lcd_(GuiBase::GetLcdPtr()), |
MikamiUitOpen | 11:769d986c10fa | 85 | sdReader_(reader), prevFileCount_(0), prev_(-1) {} |
MikamiUitOpen | 11:769d986c10fa | 86 | |
MikamiUitOpen | 11:769d986c10fa | 87 | ~FileSelector() |
MikamiUitOpen | 11:769d986c10fa | 88 | { delete rect_; } |
MikamiUitOpen | 0:04b43b777fae | 89 | |
MikamiUitOpen | 0:04b43b777fae | 90 | bool CreateTable() |
MikamiUitOpen | 0:04b43b777fae | 91 | { |
MikamiUitOpen | 0:04b43b777fae | 92 | DIR* dp = opendir("/sd"); |
MikamiUitOpen | 0:04b43b777fae | 93 | fileCount_ = 0; |
MikamiUitOpen | 0:04b43b777fae | 94 | if (dp != NULL) |
MikamiUitOpen | 0:04b43b777fae | 95 | { |
MikamiUitOpen | 0:04b43b777fae | 96 | dirent* entry; |
MikamiUitOpen | 0:04b43b777fae | 97 | for (int n=0; n<256; n++) |
MikamiUitOpen | 0:04b43b777fae | 98 | { |
MikamiUitOpen | 0:04b43b777fae | 99 | entry = readdir(dp); |
MikamiUitOpen | 0:04b43b777fae | 100 | if (entry == NULL) break; |
MikamiUitOpen | 0:04b43b777fae | 101 | |
MikamiUitOpen | 0:04b43b777fae | 102 | string strName = entry->d_name; |
MikamiUitOpen | 0:04b43b777fae | 103 | if ( (strName.find(".wav") != string::npos) || |
MikamiUitOpen | 0:04b43b777fae | 104 | (strName.find(".WAV") != string::npos) ) |
MikamiUitOpen | 0:04b43b777fae | 105 | { |
MikamiUitOpen | 0:04b43b777fae | 106 | sdReader_.Open(strName); // ファイルオープン |
MikamiUitOpen | 0:04b43b777fae | 107 | |
MikamiUitOpen | 0:04b43b777fae | 108 | // PCM,16 ビットステレオ,標本化周波数 44.1 kHz 以外のファイルは除外 |
MikamiUitOpen | 0:04b43b777fae | 109 | if (sdReader_.IsWavFile()) |
MikamiUitOpen | 11:769d986c10fa | 110 | fileNames_[fileCount_++] = strName; |
MikamiUitOpen | 0:04b43b777fae | 111 | sdReader_.Close(); |
MikamiUitOpen | 0:04b43b777fae | 112 | } |
MikamiUitOpen | 0:04b43b777fae | 113 | |
MikamiUitOpen | 0:04b43b777fae | 114 | if (fileCount_ >= MAX_FILES_) break; |
MikamiUitOpen | 0:04b43b777fae | 115 | } |
MikamiUitOpen | 0:04b43b777fae | 116 | closedir(dp); |
MikamiUitOpen | 0:04b43b777fae | 117 | } |
MikamiUitOpen | 0:04b43b777fae | 118 | else |
MikamiUitOpen | 0:04b43b777fae | 119 | return false; |
MikamiUitOpen | 11:769d986c10fa | 120 | |
MikamiUitOpen | 0:04b43b777fae | 121 | if (fileCount_ == 0) return false; |
MikamiUitOpen | 0:04b43b777fae | 122 | |
MikamiUitOpen | 0:04b43b777fae | 123 | if (rect_ != NULL) delete rect_; |
MikamiUitOpen | 11:769d986c10fa | 124 | Array<string> nonString(fileCount_, ""); |
MikamiUitOpen | 0:04b43b777fae | 125 | rect_ = new ButtonGroup(X_, Y_, W_H_, W_H_, fileCount_, |
MikamiUitOpen | 11:769d986c10fa | 126 | nonString, 0, V_L_-W_H_, 1, |
MikamiUitOpen | 0:04b43b777fae | 127 | -1, Font12, 0, GuiBase::ENUM_BACK, |
MikamiUitOpen | 0:04b43b777fae | 128 | BASE_COLOR_, TOUCHED_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 129 | for (int n=0; n<fileCount_; n++) rect_->Erase(n); |
MikamiUitOpen | 0:04b43b777fae | 130 | CreateLabels(); |
MikamiUitOpen | 0:04b43b777fae | 131 | prevFileCount_ = fileCount_; |
MikamiUitOpen | 0:04b43b777fae | 132 | return true; |
MikamiUitOpen | 0:04b43b777fae | 133 | } |
MikamiUitOpen | 0:04b43b777fae | 134 | |
MikamiUitOpen | 0:04b43b777fae | 135 | // ファイルを選択する |
MikamiUitOpen | 0:04b43b777fae | 136 | bool Select(string &fileName) |
MikamiUitOpen | 0:04b43b777fae | 137 | { |
MikamiUitOpen | 0:04b43b777fae | 138 | int n; |
MikamiUitOpen | 0:04b43b777fae | 139 | if (rect_->GetTouchedNumber(n)) |
MikamiUitOpen | 0:04b43b777fae | 140 | { |
MikamiUitOpen | 0:04b43b777fae | 141 | fileNameLabels_[n]->Draw(GetFileNameNoExt(n), TOUCHED_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 142 | if ((prev_ >= 0) && (prev_ != n)) |
MikamiUitOpen | 0:04b43b777fae | 143 | fileNameLabels_[prev_]->Draw(GetFileNameNoExt(prev_)); |
MikamiUitOpen | 0:04b43b777fae | 144 | prev_ = n; |
MikamiUitOpen | 11:769d986c10fa | 145 | fileName = fileNames_[n]; |
MikamiUitOpen | 0:04b43b777fae | 146 | return true; |
MikamiUitOpen | 0:04b43b777fae | 147 | } |
MikamiUitOpen | 0:04b43b777fae | 148 | else |
MikamiUitOpen | 0:04b43b777fae | 149 | return false; |
MikamiUitOpen | 0:04b43b777fae | 150 | } |
MikamiUitOpen | 0:04b43b777fae | 151 | |
MikamiUitOpen | 0:04b43b777fae | 152 | // ファイルの一覧の表示 |
MikamiUitOpen | 11:769d986c10fa | 153 | void DisplayFileList(bool sortEnable = true) |
MikamiUitOpen | 0:04b43b777fae | 154 | { |
MikamiUitOpen | 11:769d986c10fa | 155 | if (sortEnable) |
MikamiUitOpen | 11:769d986c10fa | 156 | std::sort((string *)fileNames_, |
MikamiUitOpen | 11:769d986c10fa | 157 | (string *)fileNames_+fileCount_); |
MikamiUitOpen | 0:04b43b777fae | 158 | |
MikamiUitOpen | 11:769d986c10fa | 159 | Erase(X_, Y_, MAX_NAME_LENGTH_*((sFONT *)(&Font16))->Width, 272-Y_); |
MikamiUitOpen | 0:04b43b777fae | 160 | rect_->DrawAll(); |
MikamiUitOpen | 0:04b43b777fae | 161 | for (int n=0; n<fileCount_; n++) |
MikamiUitOpen | 0:04b43b777fae | 162 | fileNameLabels_[n]->Draw(GetFileNameNoExt(n)); |
MikamiUitOpen | 0:04b43b777fae | 163 | } |
MikamiUitOpen | 0:04b43b777fae | 164 | |
MikamiUitOpen | 11:769d986c10fa | 165 | // ファイルの一覧の消去 |
MikamiUitOpen | 5:3e8ca1ed31a1 | 166 | void Erase(uint16_t x, uint16_t y, uint16_t width, uint16_t height, |
MikamiUitOpen | 5:3e8ca1ed31a1 | 167 | uint32_t color = GuiBase::ENUM_BACK) |
MikamiUitOpen | 5:3e8ca1ed31a1 | 168 | { |
MikamiUitOpen | 5:3e8ca1ed31a1 | 169 | lcd_->SetTextColor(color); |
MikamiUitOpen | 5:3e8ca1ed31a1 | 170 | lcd_->FillRect(x, y, width, height); |
MikamiUitOpen | 5:3e8ca1ed31a1 | 171 | } |
MikamiUitOpen | 11:769d986c10fa | 172 | |
MikamiUitOpen | 0:04b43b777fae | 173 | private: |
MikamiUitOpen | 0:04b43b777fae | 174 | const uint8_t X_, Y_, W_H_, V_L_; |
MikamiUitOpen | 0:04b43b777fae | 175 | const int MAX_FILES_; |
MikamiUitOpen | 0:04b43b777fae | 176 | const int MAX_NAME_LENGTH_; |
MikamiUitOpen | 0:04b43b777fae | 177 | const uint32_t BASE_COLOR_; |
MikamiUitOpen | 0:04b43b777fae | 178 | const uint32_t TOUCHED_COLOR_; |
MikamiUitOpen | 0:04b43b777fae | 179 | |
MikamiUitOpen | 11:769d986c10fa | 180 | Array<string> fileNames_; |
MikamiUitOpen | 0:04b43b777fae | 181 | ButtonGroup *rect_; |
MikamiUitOpen | 11:769d986c10fa | 182 | Array<Label *> fileNameLabels_; |
MikamiUitOpen | 0:04b43b777fae | 183 | LCD_DISCO_F746NG *lcd_; |
MikamiUitOpen | 0:04b43b777fae | 184 | SD_WavReader &sdReader_; |
MikamiUitOpen | 0:04b43b777fae | 185 | int fileCount_, prevFileCount_; |
MikamiUitOpen | 0:04b43b777fae | 186 | int prev_; |
MikamiUitOpen | 0:04b43b777fae | 187 | |
MikamiUitOpen | 0:04b43b777fae | 188 | // Label を生成 |
MikamiUitOpen | 0:04b43b777fae | 189 | void CreateLabels() |
MikamiUitOpen | 0:04b43b777fae | 190 | { |
MikamiUitOpen | 11:769d986c10fa | 191 | fileNameLabels_.SetSize(fileCount_); |
MikamiUitOpen | 11:769d986c10fa | 192 | |
MikamiUitOpen | 0:04b43b777fae | 193 | for (int n=0; n<fileCount_; n++) |
MikamiUitOpen | 0:04b43b777fae | 194 | fileNameLabels_[n] = new Label(X_+30, Y_+5+V_L_*n, "", |
MikamiUitOpen | 0:04b43b777fae | 195 | Label::LEFT, Font16, |
MikamiUitOpen | 0:04b43b777fae | 196 | BASE_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 197 | } |
MikamiUitOpen | 0:04b43b777fae | 198 | |
MikamiUitOpen | 0:04b43b777fae | 199 | // 拡張子を削除した文字列を取得 |
MikamiUitOpen | 0:04b43b777fae | 200 | string GetFileNameNoExt(int n) |
MikamiUitOpen | 0:04b43b777fae | 201 | { |
MikamiUitOpen | 11:769d986c10fa | 202 | string name = fileNames_[n]; |
MikamiUitOpen | 0:04b43b777fae | 203 | name.erase(name.find(".")); |
MikamiUitOpen | 0:04b43b777fae | 204 | return name.substr(0, MAX_NAME_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 205 | } |
MikamiUitOpen | 0:04b43b777fae | 206 | |
MikamiUitOpen | 0:04b43b777fae | 207 | // disallow copy constructor and assignment operator |
MikamiUitOpen | 0:04b43b777fae | 208 | FileSelector(const FileSelector&); |
MikamiUitOpen | 0:04b43b777fae | 209 | FileSelector& operator=(const FileSelector&); |
MikamiUitOpen | 0:04b43b777fae | 210 | }; |
MikamiUitOpen | 0:04b43b777fae | 211 | } |
MikamiUitOpen | 11:769d986c10fa | 212 | */ |
MikamiUitOpen | 0:04b43b777fae | 213 | #endif // FILE_SELECTOR_HPP |
MikamiUitOpen | 11:769d986c10fa | 214 |