|
1 | 1 | /* -*- C++ -*- */
|
| 2 | +#pragma once |
2 | 3 | // a full automated SDCard file select
|
3 | 4 | // plugin for arduino menu library
|
4 | 5 | // IO: Serial
|
5 | 6 | // Feb 2019 - Rui Azevedo [[email protected]]
|
6 |
| - |
7 | 7 | #include <menu.h>
|
8 | 8 |
|
9 | 9 | using namespace Menu;
|
@@ -45,4 +45,123 @@ using namespace Menu;
|
45 | 45 | //print menu and items as this is a virtual data menu
|
46 | 46 | Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t pn);
|
47 | 47 | };
|
| 48 | + |
| 49 | + result SDMenu::sysHandler(SYS_FUNC_PARAMS) { |
| 50 | + switch(event) { |
| 51 | + case enterEvent: |
| 52 | + if (nav.root->navFocus!=nav.target) {//on sd card entry |
| 53 | + nav.sel=((SDMenu*)(&item))->selIdx;//restore context |
| 54 | + } |
| 55 | + } |
| 56 | + return proceed; |
| 57 | + } |
| 58 | + |
| 59 | + void SDMenu::doNav(navNode& nav,navCmd cmd) { |
| 60 | + switch(cmd.cmd) { |
| 61 | + case enterCmd: { |
| 62 | + String selFile=entry(nav.sel); |
| 63 | + if (selFile.endsWith("/")) { |
| 64 | + // Serial.print("\nOpen folder..."); |
| 65 | + //open folder (reusing the menu) |
| 66 | + folderName+=selFile; |
| 67 | + dirty=true;//redraw menu |
| 68 | + nav.sel=0; |
| 69 | + } else { |
| 70 | + selIdx=nav.sel; |
| 71 | + //Serial.print("\nFile selected:"); |
| 72 | + //select a file and return |
| 73 | + selectedFile=selFile; |
| 74 | + nav.root->node().event(enterEvent); |
| 75 | + menuNode::doNav(nav,escCmd); |
| 76 | + } |
| 77 | + return; |
| 78 | + } |
| 79 | + case escCmd: |
| 80 | + if(folderName=="/")//at root? |
| 81 | + menuNode::doNav(nav,escCmd);//then exit |
| 82 | + else {//previous folder |
| 83 | + idx_t at=folderName.lastIndexOf("/",folderName.length()-2)+1; |
| 84 | + String fn=folderName.substring(at,folderName.length()-1); |
| 85 | + folderName.remove(folderName.lastIndexOf("/",folderName.length()-2)+1); |
| 86 | + // Serial.println(folderName); |
| 87 | + dirty=true;//redraw menu |
| 88 | + nav.sel=entryIdx(fn); |
| 89 | + } |
| 90 | + selIdx=nav.sel; |
| 91 | + return; |
| 92 | + } |
| 93 | + menuNode::doNav(nav,cmd); |
| 94 | + selIdx=nav.sel; |
| 95 | + } |
| 96 | + |
| 97 | + idx_t SDMenu::count() { |
| 98 | + File dir=SD.open(folderName.c_str()); |
| 99 | + int cnt=0; |
| 100 | + while(true) { |
| 101 | + File file=dir.openNextFile(); |
| 102 | + if (!file) { |
| 103 | + file.close(); |
| 104 | + dir.close(); |
| 105 | + break; |
| 106 | + } |
| 107 | + file.close(); |
| 108 | + cnt++; |
| 109 | + } |
| 110 | + dir.close(); |
| 111 | + return cnt; |
| 112 | + } |
| 113 | + |
| 114 | + idx_t SDMenu::entryIdx(String name) { |
| 115 | + File dir=SD.open(folderName.c_str()); |
| 116 | + int cnt=0; |
| 117 | + while(true) { |
| 118 | + File file=dir.openNextFile(); |
| 119 | + if (!file) { |
| 120 | + file.close(); |
| 121 | + break; |
| 122 | + } |
| 123 | + if(name==file.name()) { |
| 124 | + file.close(); |
| 125 | + dir.close(); |
| 126 | + return cnt; |
| 127 | + } |
| 128 | + file.close(); |
| 129 | + cnt++; |
| 130 | + } |
| 131 | + dir.close(); |
| 132 | + return 0;//stay at menu start if not found |
| 133 | + } |
| 134 | + |
| 135 | + String SDMenu::entry(idx_t idx) { |
| 136 | + File dir=SD.open(folderName.c_str()); |
| 137 | + idx_t cnt=0; |
| 138 | + while(true) { |
| 139 | + File file=dir.openNextFile(); |
| 140 | + if (!file) { |
| 141 | + file.close(); |
| 142 | + break; |
| 143 | + } |
| 144 | + if(idx==cnt++) { |
| 145 | + String n=String(file.name())+(file.isDirectory()?"/":""); |
| 146 | + file.close(); |
| 147 | + dir.close(); |
| 148 | + return n; |
| 149 | + } |
| 150 | + file.close(); |
| 151 | + } |
| 152 | + dir.close(); |
| 153 | + return ""; |
| 154 | + } |
| 155 | + |
| 156 | + Used SDMenu::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t pn) { |
| 157 | + ((menuNodeShadow*)shadow)->sz=count(); |
| 158 | + if(root.navFocus!=this) //show given title |
| 159 | + return menuNode::printTo(root,sel,out,idx,len,pn); |
| 160 | + else if(idx==-1)//when menu open (show folder name) |
| 161 | + return out.printRaw(folderName.c_str(),len); |
| 162 | + //drawing options |
| 163 | + len-=out.printRaw(entry(idx).c_str(),len); |
| 164 | + return len; |
| 165 | + } |
| 166 | + |
48 | 167 | #endif
|
0 commit comments