Skip to content

Commit 3193334

Browse files
committed
SDCard plugin, use [..] and print only folder name (not path)
1 parent edfa5d2 commit 3193334

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

examples/SDCard/SDCard/SDCard.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ using namespace Menu;
1818

1919
//function to handle file select
2020
// declared here and implemented bellow because we need
21-
// to give it as event handler for `sdFolderMenu`
22-
// and we also need to refer to `sdFolderMenu` inside the function
23-
result sdFolder(eventMask event, navNode& nav, prompt &item);
21+
// to give it as event handler for `filePickMenu`
22+
// and we also need to refer to `filePickMenu` inside the function
23+
result filePick(eventMask event, navNode& nav, prompt &item);
2424

2525

26-
// SDMenu sdFolderMenu("SD Card","/",sdFolder,enterEvent);
26+
// SDMenu filePickMenu("SD Card","/",filePick,enterEvent);
2727
//caching 32 file entries
28-
CachedSDMenu<32> sdFolderMenu("SD Card","/",sdFolder,enterEvent);
28+
CachedSDMenu<32> filePickMenu("SD Card","/",filePick,enterEvent);
2929

30-
//implementing the handler here after sdFolder is defined...
31-
result sdFolder(eventMask event, navNode& nav, prompt &item) {
30+
//implementing the handler here after filePick is defined...
31+
result filePick(eventMask event, navNode& nav, prompt &item) {
3232
// switch(event) {//for now events are filtered only for enter, so we dont need this checking
3333
// case enterCmd:
34-
if (nav.root->navFocus==(navTarget*)&sdFolderMenu) {
34+
if (nav.root->navFocus==(navTarget*)&filePickMenu) {
3535
Serial.println();
3636
Serial.print("selected file:");
37-
Serial.println(sdFolderMenu.selectedFile);
37+
Serial.println(filePickMenu.selectedFile);
3838
Serial.print("from folder:");
39-
Serial.println(sdFolderMenu.folderName);
39+
Serial.println(filePickMenu.selectedFolder);
4040
}
4141
// break;
4242
// }
@@ -47,7 +47,7 @@ result sdFolder(eventMask event, navNode& nav, prompt &item) {
4747
#define MAX_DEPTH 15
4848

4949
MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
50-
,SUBMENU(sdFolderMenu)
50+
,SUBMENU(filePickMenu)
5151
,OP("Something else...",doNothing,noEvent)
5252
,EXIT("<Back")
5353
);
@@ -68,7 +68,7 @@ void setup() {
6868
Serial.println("initialization failed!");
6969
while (1);
7070
}
71-
sdFolderMenu.begin();//need this after sd begin
71+
filePickMenu.begin();//need this after sd begin
7272
Serial.println("initialization done.");
7373
}
7474

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoMenu library
2-
version=4.17.3
2+
version=4.17.4
33
author=Rui Azevedo, [email protected]
44
maintainer=neu-rah, [email protected]
55
sentence=Generic menu/interactivity system

src/plugin/SDMenu.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,17 @@ class CachedFSO:public FSO<SDC> {
149149
// instead of allocating options for each file we will instead customize a menu
150150
// to print the files list, we can opt to use objects for each file for a
151151
// faster reopening.. but its working quite fast
152-
// avr's will be limited to 127 file (not a good idea)
153-
// this can be changed without breaking compatibility thou, but wasting more memory.
154152
// On this example we assume the existence of an esc button as we are not drawing
155153
// an exit option (or [..] as would be appropriate for a file system)
156154
// not the mennu presents it self as the menu and as the options
157155
// ands does all drawing navigation.
156+
#define USE_BACKDOTS 1
157+
158158
template<typename FS>
159159
class SDMenuT:public menuNode,public FS {
160160
public:
161161
String folderName="/";//set this to other folder when needed
162+
String selectedFolder="/";
162163
String selectedFile="";
163164
// using menuNode::menuNode;//do not use default constructors as we wont allocate for data
164165
SDMenuT(typename FS::Type& sd,constText* title,const char* at,Menu::action act=doNothing,Menu::eventMask mask=noEvent)
@@ -175,16 +176,16 @@ class SDMenuT:public menuNode,public FS {
175176
switch(event) {
176177
case enterEvent:
177178
if (nav.root->navFocus!=nav.target) {//on sd card entry
178-
nav.sel=((SDMenuT<FS>*)(&item))->entryIdx(((SDMenuT<FS>*)(&item))->selectedFile);//restore context
179+
nav.sel=((SDMenuT<FS>*)(&item))->entryIdx(((SDMenuT<FS>*)(&item))->selectedFile)+USE_BACKDOTS;//restore context
179180
}
180181
}
181182
return proceed;
182183
}
183184

184185
void doNav(navNode& nav,navCmd cmd) {
185186
switch(cmd.cmd) {
186-
case enterCmd: {
187-
String selFile=SDMenuT<FS>::entry(nav.sel);
187+
case enterCmd: if (nav.sel>=USE_BACKDOTS) {
188+
String selFile=SDMenuT<FS>::entry(nav.sel-USE_BACKDOTS);
188189
if (selFile.endsWith("/")) {
189190
// Serial.print("\nOpen folder...");
190191
//open folder (reusing the menu)
@@ -196,6 +197,7 @@ class SDMenuT:public menuNode,public FS {
196197
//Serial.print("\nFile selected:");
197198
//select a file and return
198199
selectedFile=selFile;
200+
selectedFolder=folderName;
199201
nav.root->node().event(enterEvent);
200202
menuNode::doNav(nav,escCmd);
201203
}
@@ -210,7 +212,7 @@ class SDMenuT:public menuNode,public FS {
210212
folderName.remove(folderName.lastIndexOf("/",folderName.length()-2)+1);
211213
SDMenuT<FS>::goFolder(folderName);
212214
dirty=true;//redraw menu
213-
nav.sel=SDMenuT<FS>::entryIdx(fn);
215+
nav.sel=SDMenuT<FS>::entryIdx(fn)+USE_BACKDOTS;
214216
}
215217
return;
216218
}
@@ -224,11 +226,17 @@ class SDMenuT:public menuNode,public FS {
224226
menuNode::printTo(root,sel,out,idx,len,pn):
225227
out.printRaw(selectedFile.c_str(),len);
226228
} else if(idx==-1) {//when menu open (show folder name)
227-
((menuNodeShadow*)shadow)->sz=SDMenuT<FS>::count();
228-
return out.printRaw(folderName.c_str(),len);
229+
((menuNodeShadow*)shadow)->sz=SDMenuT<FS>::count()+USE_BACKDOTS;
230+
idx_t at=folderName.lastIndexOf("/",folderName.length()-2)+1;
231+
String fn=folderName.substring(at,folderName.length()-1);
232+
return out.printRaw(fn.c_str(),len);
233+
// return out.printRaw(folderName.c_str(),len);
234+
// return out.printRaw(SDMenuT<FS>::dir.name(),len);
229235
}
230236
//drawing options
231-
len-=out.printRaw(SDMenuT<FS>::entry(out.tops[root.level]+idx).c_str(),len);
237+
idx_t i=out.tops[root.level]+idx;
238+
if (i<USE_BACKDOTS) len-=out.printRaw("[..]",len);
239+
else len-=out.printRaw(SDMenuT<FS>::entry(out.tops[root.level]+idx-USE_BACKDOTS).c_str(),len);
232240
return len;
233241
}
234242
};

0 commit comments

Comments
 (0)