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