SubMenu.ino 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <M5ez.h>
  2. // This example is intended to show how to nest menus within menus, even when
  3. // special button processing is needed on menu selections.
  4. // Standard Arduino setup function. ez.begin() takes care of startup and initialization
  5. void setup() {
  6. #include <themes/default.h> // per https://github.com/M5ez/M5ez#themes
  7. #include <themes/dark.h> // makes both themes available
  8. ez.begin(); // initializes everything
  9. }
  10. // Although the Arduino loop is usually called repeatedly, using an M5ez menu with no exit results in it being called once only
  11. //
  12. void loop() {
  13. Serial.println("entering loop()"); // Shows what function we're in on the Serial Monitor
  14. ezMenu topmenu("Top Menu"); // creates the menu but does nothing else
  15. topmenu.txtSmall(); // makes menu text smaller, neater
  16. topmenu.buttons("up # select # down"); // 'select' sets pickName and pickCaption, all set pickButton
  17. topmenu.addItem("dark | Dark Theme", dark); // ignore in while loop, calls dark() automatically
  18. topmenu.addItem("light | Light Theme", light); // ignore in while loop, calls light() automatically
  19. topmenu.addItem("Red"); // does nothing; handled manually in while loop
  20. topmenu.addItem("Green"); // does nothing; handled manually in while loop
  21. topmenu.addItem("Blue"); // does nothing; handled manually in while loop
  22. topmenu.addItem("Black"); // does nothing; handled manually in while loop
  23. topmenu.addItem("SubMenu"); // does nothing; handled manually in while loop
  24. while(topmenu.runOnce()) { // runOnce displays the menu and returns when a button is pressed
  25. // you can watch the results in the Serial Monitor
  26. Serial.printf("topmenu.pick = %d\n", topmenu.pick());
  27. Serial.printf("topmenu.pickButton = %s\n", topmenu.pickButton().c_str());
  28. Serial.printf("topmenu.pickName = %s\n", topmenu.pickName().c_str());
  29. Serial.printf("topmenu.pickCaption = %s\n\n", topmenu.pickCaption().c_str());
  30. String result = topmenu.pickName(); // find out what menu was selected when 'select' button was pressed
  31. if( result == "Red") ez.theme->menu_item_color = RED;
  32. else if(result == "Green") ez.theme->menu_item_color = GREEN;
  33. else if(result == "Blue") ez.theme->menu_item_color = BLUE;
  34. else if(result == "Black") ez.theme->menu_item_color = BLACK;
  35. else if(result == "SubMenu") subMenu(); // create a sub menu, but we can return to this menu when it exits
  36. }
  37. }
  38. // Function called automatically by selecting the 'Dark Theme' menu
  39. //
  40. void dark() { ez.theme->select("Dark"); } // called automatically when 'Dark Theme' menu is selected
  41. // Function called automatically by selecting the 'Light Theme' menu
  42. //
  43. void light() { ez.theme->select("Default"); } // called automatically when 'Light Theme' menu is selected
  44. // Display the submenu. When 'back' is selected, this menu is terminated and mainmenu resumes
  45. // the foo, bar and baz menus are just dummies; normally they would call a function of be handled in a runOnce loop
  46. //
  47. void subMenu() {
  48. Serial.println("entering subMenu()"); // Shows what function we're in on the Serial Monitor
  49. ezMenu submenu("Sub Menu"); // creates the menu but does nothing else
  50. submenu.txtSmall(); // makes menu text smaller, neater
  51. submenu.buttons("up # back # select ## down #"); // standard buttons for a submenu; long press on button A pops up one level
  52. submenu.addItem("foo"); // not handled at all, so nothing happens when this menu is selected except a redraw
  53. submenu.addItem("bar"); // not handled at all, so nothing happens when this menu is selected except a redraw
  54. submenu.addItem("baz"); // not handled at all, so nothing happens when this menu is selected except a redraw
  55. submenu.addItem("sub | subSubMenu"); // returns the name "sub" but displays the caption "subSubMenu"
  56. while(submenu.runOnce()) { // runOnce displays the menu and returns when a button is pressed
  57. // you can watch the results in the Serial Monitor
  58. Serial.printf("submenu.pick = %d\n", submenu.pick());
  59. Serial.printf("submenu.pickButton = %s\n", submenu.pickButton().c_str());
  60. Serial.printf("submenu.pickName = %s\n", submenu.pickName().c_str());
  61. Serial.printf("submenu.pickCaption = %s\n\n", submenu.pickCaption().c_str());
  62. if(submenu.pickName() == "sub") subSubMenu(); // submenu.pickName() == "sub", or submenu.pickCaption() == "subSubMenu", or submenu.pick == 4
  63. }
  64. }
  65. // Display an even more deaply nested menu. When 'back' is selected this menu is terminated and submenu resumes
  66. // the tic, tac and toe menus are just dummies; normally they would call a function of be handled in a runOnce loop
  67. //
  68. void subSubMenu() {
  69. Serial.println("entering subSubMenu()"); // Shows what function we're in on the Serial Monitor
  70. ezMenu subsubmenu("Sub Sub Menu"); // creates the menu but does nothing else
  71. subsubmenu.txtSmall(); // makes menu text smaller, neater
  72. subsubmenu.buttons("up # back # select ## down #"); // standard buttons for a submenu; long press on button A pops up one level
  73. subsubmenu.addItem("tic"); // not handled at all, so nothing happens when this menu is selected except a redraw
  74. subsubmenu.addItem("tac"); // not handled at all, so nothing happens when this menu is selected except a redraw
  75. subsubmenu.addItem("toe"); // not handled at all, so nothing happens when this menu is selected except a redraw
  76. subsubmenu.run(); // display menu and handle menus until 'back' is selected, then exit, returning to subMenu()
  77. }