Skip to content

Commit 8177477

Browse files
committed
reuse SD folder object
cleanup draw, reduce excessive counting bug
1 parent e832bf1 commit 8177477

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

examples/SDCard/SDCard/SDCard.ino

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void setup() {
6565
Serial.println("initialization failed!");
6666
while (1);
6767
}
68+
sdFolderMenu.begin();//need this after sd begin
6869
Serial.println("initialization done.");
6970
}
7071

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoMenu library
2-
version=4.17.1
2+
version=4.17.2
33
author=Rui Azevedo, [email protected]
44
maintainer=neu-rah, [email protected]
55
sentence=Generic menu/interactivity system

src/plugin/SDMenu.h

+36-15
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ using namespace Menu;
2525
public:
2626
SDC& sdc;
2727
//idx_t selIdx=0;//preserve selection context, because we preserve folder ctx too
28-
//we should use filename instead!
28+
//we should use filename instead! idx is useful for delete operations thou...
29+
30+
File dir;
2931

3032
String folderName="/";//set this to other folder when needed
3133
String selectedFile="";
3234
// using menuNode::menuNode;//do not use default constructors as we wont allocate for data
33-
virtual ~SDMenuT() {}
35+
virtual ~SDMenuT() {dir.close();}
3436
SDMenuT(SDC& sd,constText* title,const char* at,Menu::action act=doNothing,Menu::eventMask mask=noEvent)
3537
:menuNode(title,0,NULL,act,mask,
3638
wrapStyle,(systemStyles)(_menuData|_canNav))
3739
,sdc(sd)
3840
,folderName(at)
39-
{}
41+
// ,dir(sdc.open(at))
42+
{
43+
// Serial.println("open dir, construction");
44+
// dir=sdc.open(at);
45+
}
46+
47+
void begin() {
48+
dir=sdc.open(folderName);
49+
}
4050

4151
//this requires latest menu version to virtualize data tables
4252
prompt& operator[](idx_t i) const override {return *(prompt*)this;}//this will serve both as menu and as its own prompt
@@ -72,6 +82,9 @@ using namespace Menu;
7282
// Serial.print("\nOpen folder...");
7383
//open folder (reusing the menu)
7484
folderName+=selFile;
85+
dir.close();
86+
// Serial.println("reopen dir, context");
87+
dir=sdc.open(folderName);
7588
dirty=true;//redraw menu
7689
nav.sel=0;
7790
} else {
@@ -91,6 +104,9 @@ using namespace Menu;
91104
String fn=folderName.substring(at,folderName.length()-1);
92105
folderName.remove(folderName.lastIndexOf("/",folderName.length()-2)+1);
93106
// Serial.println(folderName);
107+
dir.close();
108+
// Serial.println("reopen dir, back-nav");
109+
dir=sdc.open(folderName);
94110
dirty=true;//redraw menu
95111
nav.sel=entryIdx(fn);
96112
}
@@ -101,25 +117,29 @@ using namespace Menu;
101117

102118
template<typename SDC>
103119
idx_t SDMenuT<SDC>::count() {
104-
File dir=sdc.open(folderName.c_str());
120+
// Serial.print("count:");
121+
//File dir=sdc.open(folderName.c_str());
122+
dir.rewindDirectory();
105123
int cnt=0;
106124
while(true) {
107125
File file=dir.openNextFile();
108126
if (!file) {
109127
file.close();
110-
dir.close();
128+
//dir.close();
111129
break;
112130
}
113131
file.close();
114132
cnt++;
115133
}
116-
dir.close();
134+
// Serial.println(cnt);
135+
//dir.close();
117136
return cnt;
118137
}
119138

120139
template<typename SDC>
121140
idx_t SDMenuT<SDC>::entryIdx(String name) {
122-
File dir=sdc.open(folderName.c_str());
141+
// File dir=sdc.open(folderName.c_str());
142+
dir.rewindDirectory();
123143
int cnt=0;
124144
while(true) {
125145
File file=dir.openNextFile();
@@ -129,19 +149,20 @@ using namespace Menu;
129149
}
130150
if(name==file.name()) {
131151
file.close();
132-
dir.close();
152+
//dir.close();
133153
return cnt;
134154
}
135155
file.close();
136156
cnt++;
137157
}
138-
dir.close();
158+
//dir.close();
139159
return 0;//stay at menu start if not found
140160
}
141161

142162
template<typename SDC>
143163
String SDMenuT<SDC>::entry(idx_t idx) {
144-
File dir=sdc.open(folderName.c_str());
164+
// File dir=sdc.open(folderName.c_str());
165+
dir.rewindDirectory();
145166
idx_t cnt=0;
146167
while(true) {
147168
File file=dir.openNextFile();
@@ -152,25 +173,25 @@ using namespace Menu;
152173
if(idx==cnt++) {
153174
String n=String(file.name())+(file.isDirectory()?"/":"");
154175
file.close();
155-
dir.close();
176+
//dir.close();
156177
return n;
157178
}
158179
file.close();
159180
}
160-
dir.close();
181+
//dir.close();
161182
return "";
162183
}
163184

164185
template<typename SDC>
165186
Used SDMenuT<SDC>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t pn) {
166-
((menuNodeShadow*)shadow)->sz=count();
167187
if(root.navFocus!=this) {//show given title or filename if selected
168188
return selectedFile==""?
169189
menuNode::printTo(root,sel,out,idx,len,pn):
170190
out.printRaw(selectedFile.c_str(),len);
171-
}
172-
else if(idx==-1)//when menu open (show folder name)
191+
} else if(idx==-1) {//when menu open (show folder name)
192+
((menuNodeShadow*)shadow)->sz=count();
173193
return out.printRaw(folderName.c_str(),len);
194+
}
174195
//drawing options
175196
len-=out.printRaw(entry(idx).c_str(),len);
176197
return len;

0 commit comments

Comments
 (0)