summaryrefslogtreecommitdiff
path: root/main.ino
blob: 6a1538a661802595c2a892311504c5d679c7f955 (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 1
  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. #include <GEM_adafruit_gfx.h>
  34. // Create variables that will be editable through the menu and assign them initial values
  35. int number = -512;
  36. boolean enablePrint = false;
  37. // Create two menu item objects of class GEMItem, linked to number and enablePrint variables
  38. GEMItem menuItemInt("Number:", number);
  39. GEMItem menuItemBool("Enable print:", enablePrint);
  40. // Create menu button that will trigger printData() function. It will print value of our number variable
  41. // to Serial monitor if enablePrint is true. We will write (define) this function later. However, we should
  42. // forward-declare it in order to pass to GEMItem constructor
  43. void printData(); // Forward declaration
  44. GEMItem menuItemButton("Print", printData);
  45. // Create menu page object of class GEMPage. Menu page holds menu items (GEMItem) and represents menu level.
  46. // Menu can have multiple menu pages (linked to each other) with multiple menu items each
  47. GEMPage menuPageMain("Main Menu");
  48. GEM_adafruit_gfx menu(displayEPaper, GEM_POINTER_ROW, GEM_ITEMS_COUNT_AUTO);
  49. #endif
  50. void setup() {
  51. Serial.begin(BAUDRATE);
  52. while (!Serial) {
  53. ;
  54. }
  55. Serial.println();
  56. button_move.begin();
  57. button_select.begin();
  58. button_move.onPressed(onButtonMovePressed);
  59. button_move.onPressedFor(longpress_duration, onButtonMoveLongpressed);
  60. button_select.onPressed(onButtonSelectPressed);
  61. led.begin();
  62. led.breathe(2000);
  63. displayEPaper.init(115200, true, 2, false);
  64. displayEPaper.setTextColor(GxEPD_BLACK);
  65. displayEPaper.firstPage();
  66. do
  67. {
  68. displayEPaper.fillScreen(GxEPD_WHITE);
  69. // comment out next line to have no or minimal Adafruit_GFX code
  70. displayEPaper.print("Hello World!");
  71. }
  72. while (displayEPaper.nextPage());
  73. menu.init();
  74. setupMenu();
  75. menu.drawMenu();
  76. }
  77. void loop() {
  78. led.loop();
  79. button_move.read();
  80. button_select.read();
  81. }
  82. #if (ESP32)
  83. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  84. #endif
  85. void onButtonMovePressed() {
  86. Serial.println("button MOVE clicked");
  87. led.blink2();
  88. }
  89. #if (ESP32)
  90. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  91. #endif
  92. void onButtonMoveLongpressed() {
  93. Serial.println("button MOVE long-clicked");
  94. led.off();
  95. }
  96. #if (ESP32)
  97. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  98. #endif
  99. void onButtonSelectPressed() {
  100. Serial.println("button SELECT clicked");
  101. led.on();
  102. }
  103. void setupMenu() {
  104. // Add menu items to menu page
  105. menuPageMain.addMenuItem(menuItemInt);
  106. menuPageMain.addMenuItem(menuItemBool);
  107. menuPageMain.addMenuItem(menuItemButton);
  108. // Add menu page to menu and set it as current
  109. menu.setMenuPageCurrent(menuPageMain);
  110. }
  111. void printData() {
  112. // If enablePrint flag is set to true (checkbox on screen is checked)...
  113. if (enablePrint) {
  114. // ...print the number to Serial
  115. Serial.print("Number is: ");
  116. Serial.println(number);
  117. } else {
  118. Serial.println("Printing is disabled, sorry:(");
  119. }
  120. }