summaryrefslogtreecommitdiff
path: root/main.ino
blob: 41c61eef72fecf5d290fa5d2152b270c3c468998 (plain)
  1. // Pill dispenser
  2. // Inspired by article "Blink Without Delay"
  3. // https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay
  4. // Inspired by basic usage documentation of GEM library
  5. // https://github.com/Spirik/GEM
  6. #include <EasyButton.h>
  7. #include <BlinkControl.h>
  8. const int longpress_duration = 2000; // time in milliseconds
  9. // use built-in buttons and LEDs when possible
  10. #if defined(ESP32)
  11. // dirty hack: assume that any ESP32 board is a Lilygo T5 V2.2
  12. #define LILYGO_T5_V22
  13. #include "boards.h" // source: https://github.com/lewisxhe/GxEPD/blob/master/src/boards.h
  14. #define HAS_DISPLAY
  15. const byte BUTTON_PIN_MOVE = BUTTON_1;
  16. const byte BUTTON_PIN_SELECT = BUTTON_2;
  17. BlinkControl led(LED_PIN);
  18. #else
  19. const byte BUTTON_PIN_MOVE = 2;
  20. const byte BUTTON_PIN_SELECT = 3;
  21. BlinkControl led(LED_BUILTIN); // pin 13 is built-in LED on many Arduino boards
  22. #endif
  23. // serial port speed
  24. #define BAUDRATE 115200
  25. EasyButton button_move(BUTTON_PIN_MOVE);
  26. EasyButton button_select(BUTTON_PIN_SELECT);
  27. #if defined(HAS_DISPLAY)
  28. #define ENABLE_GxEPD2_GFX 0
  29. #include <GxEPD2_BW.h>
  30. #include <Fonts/FreeMonoBold9pt7b.h>
  31. // source: https://github.com/ZinggJM/GxEPD2/blob/master/examples/GxEPD2_Example/GxEPD2_display_selection.h#L192
  32. GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> displayEPaper(GxEPD2_290(EPD_CS, EPD_DC, EPD_RSET, EPD_BUSY));
  33. // FIXME: really disable GLCD, since it cannot build on ESP32
  34. // as dirty workaround, edit file `libraries/GEM/src/config.h`
  35. #define GEM_DISABLE_GLCD 1
  36. #include <GEM_adafruit_gfx.h>
  37. // Create variables that will be editable through the menu and assign them initial values
  38. int number = -512;
  39. boolean enablePrint = false;
  40. // Create two menu item objects of class GEMItem, linked to number and enablePrint variables
  41. GEMItem menuItemInt("Number:", number);
  42. GEMItem menuItemBool("Enable print:", enablePrint);
  43. // Create menu button that will trigger printData() function. It will print value of our number variable
  44. // to Serial monitor if enablePrint is true. We will write (define) this function later. However, we should
  45. // forward-declare it in order to pass to GEMItem constructor
  46. void printData(); // Forward declaration
  47. GEMItem menuItemButton("Print", printData);
  48. // Create menu page object of class GEMPage. Menu page holds menu items (GEMItem) and represents menu level.
  49. // Menu can have multiple menu pages (linked to each other) with multiple menu items each
  50. GEMPage menuPageMain("Main Menu");
  51. GEM_adafruit_gfx menu(displayEPaper, GEM_POINTER_ROW, GEM_ITEMS_COUNT_AUTO);
  52. #endif
  53. void setup() {
  54. Serial.begin(BAUDRATE);
  55. while (!Serial) {
  56. ;
  57. }
  58. Serial.println();
  59. button_move.begin();
  60. button_select.begin();
  61. button_move.onPressed(onButtonMovePressed);
  62. button_move.onPressedFor(longpress_duration, onButtonMoveLongpressed);
  63. button_select.onPressed(onButtonSelectPressed);
  64. led.begin();
  65. led.breathe(2000);
  66. displayEPaper.init(115200, true, 2, false);
  67. displayEPaper.setTextColor(GxEPD_BLACK);
  68. displayEPaper.firstPage();
  69. do
  70. {
  71. displayEPaper.fillScreen(GxEPD_WHITE);
  72. // comment out next line to have no or minimal Adafruit_GFX code
  73. displayEPaper.print("Hello World!");
  74. }
  75. while (displayEPaper.nextPage());
  76. menu.init();
  77. setupMenu();
  78. menu.drawMenu();
  79. }
  80. void loop() {
  81. led.loop();
  82. button_move.read();
  83. button_select.read();
  84. }
  85. #if (ESP32)
  86. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  87. #endif
  88. void onButtonMovePressed() {
  89. Serial.println("button MOVE clicked");
  90. led.blink2();
  91. }
  92. #if (ESP32)
  93. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  94. #endif
  95. void onButtonMoveLongpressed() {
  96. Serial.println("button MOVE long-clicked");
  97. led.off();
  98. }
  99. #if (ESP32)
  100. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  101. #endif
  102. void onButtonSelectPressed() {
  103. Serial.println("button SELECT clicked");
  104. led.on();
  105. }
  106. void setupMenu() {
  107. // Add menu items to menu page
  108. menuPageMain.addMenuItem(menuItemInt);
  109. menuPageMain.addMenuItem(menuItemBool);
  110. menuPageMain.addMenuItem(menuItemButton);
  111. // Add menu page to menu and set it as current
  112. menu.setMenuPageCurrent(menuPageMain);
  113. }
  114. void printData() {
  115. // If enablePrint flag is set to true (checkbox on screen is checked)...
  116. if (enablePrint) {
  117. // ...print the number to Serial
  118. Serial.print("Number is: ");
  119. Serial.println(number);
  120. } else {
  121. Serial.println("Printing is disabled, sorry:(");
  122. }
  123. }