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