summaryrefslogtreecommitdiff
path: root/main.ino
blob: d8f50b17201e9452eea858954c81ebe345b1d537 (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. #include <U8g2_for_Adafruit_GFX.h>
  32. // source: https://github.com/ZinggJM/GxEPD2/blob/master/examples/GxEPD2_Example/GxEPD2_display_selection.h#L192
  33. GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> displayEPaper(GxEPD2_290(EPD_CS, EPD_DC, EPD_RSET, EPD_BUSY));
  34. U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
  35. #include <GEM_u8g2.h>
  36. // Create variables that will be editable through the menu and assign them initial values
  37. int number = -512;
  38. boolean enablePrint = false;
  39. // Create two menu item objects of class GEMItem, linked to number and enablePrint variables
  40. GEMItem menuItemInt("Number:", number);
  41. GEMItem menuItemBool("Enable print:", enablePrint);
  42. // Create menu button that will trigger printData() function. It will print value of our number variable
  43. // to Serial monitor if enablePrint is true. We will write (define) this function later. However, we should
  44. // forward-declare it in order to pass to GEMItem constructor
  45. void printData(); // Forward declaration
  46. GEMItem menuItemButton("Print", printData);
  47. // Create menu page object of class GEMPage. Menu page holds menu items (GEMItem) and represents menu level.
  48. // Menu can have multiple menu pages (linked to each other) with multiple menu items each
  49. GEMPage menuPageMain("Main Menu");
  50. // Create menu object of class GEM_u8g2. Supply its constructor with reference to u8g2 object we created earlier
  51. GEM_u8g2 menu(u8g2);
  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. u8g2Fonts.begin(display);
  68. menu.init();
  69. setupMenu();
  70. menu.drawMenu();
  71. }
  72. void loop() {
  73. led.loop();
  74. button_move.read();
  75. button_select.read();
  76. }
  77. #if (ESP32)
  78. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  79. #endif
  80. void onButtonMovePressed() {
  81. Serial.println("button MOVE clicked");
  82. led.blink2();
  83. }
  84. #if (ESP32)
  85. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  86. #endif
  87. void onButtonMoveLongpressed() {
  88. Serial.println("button MOVE long-clicked");
  89. led.off();
  90. }
  91. #if (ESP32)
  92. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  93. #endif
  94. void onButtonSelectPressed() {
  95. Serial.println("button SELECT clicked");
  96. led.on();
  97. }
  98. void setupMenu() {
  99. // Add menu items to menu page
  100. menuPageMain.addMenuItem(menuItemInt);
  101. menuPageMain.addMenuItem(menuItemBool);
  102. menuPageMain.addMenuItem(menuItemButton);
  103. // Add menu page to menu and set it as current
  104. menu.setMenuPageCurrent(menuPageMain);
  105. }
  106. void printData() {
  107. // If enablePrint flag is set to true (checkbox on screen is checked)...
  108. if (enablePrint) {
  109. // ...print the number to Serial
  110. // Note, there might be some problems w/ displaying Cyrillic characters in Serial monitor,
  111. // in which case adjusting baud rate may help
  112. Serial.print("Число: ");
  113. Serial.println(number);
  114. } else {
  115. Serial.println("Печать не разрешена:(");
  116. }
  117. }