|
| 1 | +#include <SPI.h> |
| 2 | +#include <SdFat.h> |
| 3 | +#include <menu.h> |
| 4 | +#include <menuIO/serialIO.h> |
| 5 | +#include <plugin/SdFatMenu.h> |
| 6 | +//enable this include if using esp8266 |
| 7 | +// #include <menuIO/esp8266Out.h> |
| 8 | + |
| 9 | +using namespace Menu; |
| 10 | + |
| 11 | +//esp8266 SS |
| 12 | +#define SDCARD_SS 15 |
| 13 | +SdFat sd; |
| 14 | + |
| 15 | +//function to handle file select |
| 16 | +// declared here and implemented bellow because we need |
| 17 | +// to give it as event handler for `filePickMenu` |
| 18 | +// and we also need to refer to `filePickMenu` inside the function |
| 19 | +result filePick(eventMask event, navNode& nav, prompt &item); |
| 20 | + |
| 21 | + |
| 22 | +SDMenuT<CachedFSO<SdFat,32>> filePickMenu(sd,"SD Card","/",filePick,enterEvent); |
| 23 | + |
| 24 | +//implementing the handler here after filePick is defined... |
| 25 | +result filePick(eventMask event, navNode& nav, prompt &item) { |
| 26 | + // switch(event) {//for now events are filtered only for enter, so we dont need this checking |
| 27 | + // case enterCmd: |
| 28 | + if (nav.root->navFocus==(navTarget*)&filePickMenu) { |
| 29 | + Serial.println(); |
| 30 | + Serial.print("selected file:"); |
| 31 | + Serial.println(filePickMenu.selectedFile); |
| 32 | + Serial.print("from folder:"); |
| 33 | + Serial.println(filePickMenu.selectedFolder); |
| 34 | + } |
| 35 | + // break; |
| 36 | + // } |
| 37 | + return proceed; |
| 38 | +} |
| 39 | + |
| 40 | +#define MAX_DEPTH 2 |
| 41 | + |
| 42 | +MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle |
| 43 | + ,SUBMENU(filePickMenu) |
| 44 | + ,OP("Something else...",doNothing,noEvent) |
| 45 | + ,EXIT("<Back") |
| 46 | +); |
| 47 | + |
| 48 | +MENU_OUTPUTS(out,MAX_DEPTH |
| 49 | + ,SERIAL_OUT(Serial) |
| 50 | + ,NONE//must have 2 items at least |
| 51 | +); |
| 52 | + |
| 53 | +serialIn serial(Serial); |
| 54 | +NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out); |
| 55 | + |
| 56 | +void setup() { |
| 57 | + Serial.begin(115200); |
| 58 | + while (!Serial); |
| 59 | + Serial.print("Initializing SD card..."); |
| 60 | + if (!sd.begin(SDCARD_SS, SD_SCK_MHZ(50))) { |
| 61 | + sd.initErrorHalt(); |
| 62 | + } |
| 63 | + filePickMenu.begin();//need this after sd begin |
| 64 | + Serial.println("initialization done."); |
| 65 | +} |
| 66 | + |
| 67 | +constexpr int menuFPS=25; |
| 68 | +unsigned long nextPool=0; |
| 69 | +void loop() { |
| 70 | + unsigned long now=millis(); |
| 71 | + if(now>=nextPool) { |
| 72 | + nav.poll(); |
| 73 | + nextPool=now+1000/menuFPS; |
| 74 | + } |
| 75 | +} |
0 commit comments