epd2in9_V2.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @filename : epd2in9_V2-demo.ino
  3. * @brief : 2.9inch e-paper V2 display demo
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare Nov 09 2020
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documnetation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <SPI.h>
  27. #include "epd2in9_V2.h"
  28. #include "epdpaint.h"
  29. #include "imagedata.h"
  30. #define COLORED 0
  31. #define UNCOLORED 1
  32. /**
  33. * Due to RAM not enough in Arduino UNO, a frame buffer is not allowed.
  34. * In this case, a smaller image buffer is allocated and you have to
  35. * update a partial display several times.
  36. * 1 byte = 8 pixels, therefore you have to set 8*N pixels at a time.
  37. */
  38. unsigned char image[1024];
  39. Paint paint(image, 0, 0); // width should be the multiple of 8
  40. Epd epd;
  41. unsigned long time_start_ms;
  42. unsigned long time_now_s;
  43. void setup() {
  44. // put your setup code here, to run once:
  45. Serial.begin(115200);
  46. if (epd.Init() != 0) {
  47. Serial.print("e-Paper init failed");
  48. return;
  49. }
  50. epd.ClearFrameMemory(0xFF); // bit set = white, bit reset = black
  51. epd.DisplayFrame();
  52. paint.SetRotate(ROTATE_0);
  53. paint.SetWidth(128);
  54. paint.SetHeight(24);
  55. /* For simplicity, the arguments are explicit numerical coordinates */
  56. paint.Clear(COLORED);
  57. paint.DrawStringAt(0, 4, "Hello world!", &Font12, UNCOLORED);
  58. epd.SetFrameMemory(paint.GetImage(), 0, 10, paint.GetWidth(), paint.GetHeight());
  59. paint.Clear(UNCOLORED);
  60. paint.DrawStringAt(0, 4, "e-Paper Demo", &Font12, COLORED);
  61. epd.SetFrameMemory(paint.GetImage(), 0, 30, paint.GetWidth(), paint.GetHeight());
  62. paint.SetWidth(64);
  63. paint.SetHeight(64);
  64. paint.Clear(UNCOLORED);
  65. paint.DrawRectangle(0, 0, 40, 50, COLORED);
  66. paint.DrawLine(0, 0, 40, 50, COLORED);
  67. paint.DrawLine(40, 0, 0, 50, COLORED);
  68. epd.SetFrameMemory(paint.GetImage(), 8, 60, paint.GetWidth(), paint.GetHeight());
  69. paint.Clear(UNCOLORED);
  70. paint.DrawCircle(32, 32, 30, COLORED);
  71. epd.SetFrameMemory(paint.GetImage(), 56, 60, paint.GetWidth(), paint.GetHeight());
  72. paint.Clear(UNCOLORED);
  73. paint.DrawFilledRectangle(0, 0, 40, 50, COLORED);
  74. epd.SetFrameMemory(paint.GetImage(), 8, 130, paint.GetWidth(), paint.GetHeight());
  75. paint.Clear(UNCOLORED);
  76. paint.DrawFilledCircle(32, 32, 30, COLORED);
  77. epd.SetFrameMemory(paint.GetImage(), 56, 130, paint.GetWidth(), paint.GetHeight());
  78. epd.DisplayFrame();
  79. delay(2000);
  80. if (epd.Init() != 0) {
  81. Serial.print("e-Paper init failed ");
  82. return;
  83. }
  84. /**
  85. * there are 2 memory areas embedded in the e-paper display
  86. * and once the display is refreshed, the memory area will be auto-toggled,
  87. * i.e. the next action of SetFrameMemory will set the other memory area
  88. * therefore you have to set the frame memory and refresh the display twice.
  89. */
  90. epd.SetFrameMemory_Base(IMAGE_DATA);
  91. epd.DisplayFrame();
  92. time_start_ms = millis();
  93. }
  94. void loop() {
  95. // put your main code here, to run repeatedly:
  96. time_now_s = (millis() - time_start_ms) / 1000;
  97. char time_string[] = {'0', '0', ':', '0', '0', '\0'};
  98. time_string[0] = time_now_s / 60 / 10 + '0';
  99. time_string[1] = time_now_s / 60 % 10 + '0';
  100. time_string[3] = time_now_s % 60 / 10 + '0';
  101. time_string[4] = time_now_s % 60 % 10 + '0';
  102. paint.SetWidth(32);
  103. paint.SetHeight(96);
  104. paint.SetRotate(ROTATE_90);
  105. paint.Clear(UNCOLORED);
  106. paint.DrawStringAt(0, 4, time_string, &Font24, COLORED);
  107. epd.SetFrameMemory_Partial(paint.GetImage(), 80, 72, paint.GetWidth(), paint.GetHeight());
  108. epd.DisplayFrame_Partial();
  109. // delay(300);
  110. }