summaryrefslogtreecommitdiff
path: root/main.ino
blob: 8067cb4cd67e22da82d02309675a103180b67bf9 (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 u8g2;
  35. #endif
  36. void setup() {
  37. Serial.begin(BAUDRATE);
  38. while (!Serial) {
  39. ;
  40. }
  41. Serial.println();
  42. button_move.begin();
  43. button_select.begin();
  44. button_move.onPressed(onButtonMovePressed);
  45. button_move.onPressedFor(longpress_duration, onButtonMoveLongpressed);
  46. button_select.onPressed(onButtonSelectPressed);
  47. led.begin();
  48. led.breathe(2000);
  49. displayEPaper.init(115200, true, 2, false);
  50. u8g2.begin(displayEPaper);
  51. }
  52. void loop() {
  53. led.loop();
  54. button_move.read();
  55. button_select.read();
  56. }
  57. #if (ESP32)
  58. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  59. #endif
  60. void onButtonMovePressed() {
  61. Serial.println("button MOVE clicked");
  62. led.blink2();
  63. }
  64. #if (ESP32)
  65. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  66. #endif
  67. void onButtonMoveLongpressed() {
  68. Serial.println("button MOVE long-clicked");
  69. led.off();
  70. }
  71. #if (ESP32)
  72. IRAM_ATTR // avoid crash e.g. when other code reads SD card
  73. #endif
  74. void onButtonSelectPressed() {
  75. Serial.println("button SELECT clicked");
  76. led.on();
  77. }