xdsp_05_epaper_29.ino 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. xdsp_05_epaper_29.ino - 2.9 Inch display e-paper support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends, Gerhard Mutz and Waveshare
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_SPI
  16. #ifdef USE_DISPLAY
  17. #ifdef USE_DISPLAY_EPAPER
  18. #define XDSP_05 5
  19. #define EPD_TOP 12
  20. #define EPD_FONT_HEIGTH 12
  21. #define COLORED 0
  22. #define UNCOLORED 1
  23. // using font 8 is opional (num=3)
  24. // very badly readable, but may be useful for graphs
  25. #define USE_TINY_FONT
  26. #include <epd2in9.h>
  27. #include <epdpaint.h>
  28. unsigned char image[(EPD_HEIGHT * EPD_WIDTH) / 8];
  29. Paint paint(image, EPD_WIDTH, EPD_HEIGHT); // width should be the multiple of 8
  30. Epd epd;
  31. sFONT *selected_font;
  32. uint16_t epd_scroll;
  33. /*********************************************************************************************/
  34. void EpdInitMode(void)
  35. {
  36. // whiten display with full update
  37. epd.Init(lut_full_update);
  38. epd.ClearFrameMemory(0xFF); // bit set = white, bit reset = black
  39. epd.DisplayFrame();
  40. delay(3000);
  41. // switch to partial update
  42. epd.Init(lut_partial_update);
  43. // Clear image memory
  44. epd.ClearFrameMemory(0xFF); // bit set = white, bit reset = black
  45. epd.DisplayFrame();
  46. delay(500);
  47. selected_font = &Font12;
  48. paint.SetRotate(Settings.display_rotate);
  49. /*
  50. // Welcome text
  51. paint.Clear(UNCOLORED);
  52. paint.DrawStringAt(50, 50, "Waveshare E-Paper Display!", selected_font, COLORED);
  53. epd.SetFrameMemory(paint.GetImage(), 0, 0, paint.GetWidth(), paint.GetHeight());
  54. epd.DisplayFrame();
  55. delay(1000);
  56. */
  57. paint.Clear(UNCOLORED);
  58. epd_scroll = EPD_TOP;
  59. }
  60. void EpdInitPartial(void)
  61. {
  62. epd.Init(lut_partial_update);
  63. //paint.Clear(UNCOLORED);
  64. epd.DisplayFrame();
  65. delay(500);
  66. }
  67. void EpdInitFull(void)
  68. {
  69. epd.Init(lut_full_update);
  70. //paint.Clear(UNCOLORED);
  71. //epd.ClearFrameMemory(0xFF);
  72. epd.DisplayFrame();
  73. delay(3000);
  74. }
  75. void EpdInit(uint8_t mode)
  76. {
  77. switch(mode) {
  78. case DISPLAY_INIT_MODE:
  79. EpdInitMode();
  80. break;
  81. case DISPLAY_INIT_PARTIAL:
  82. EpdInitPartial();
  83. break;
  84. case DISPLAY_INIT_FULL:
  85. EpdInitFull();
  86. break;
  87. }
  88. }
  89. void EpdInitDriver(void)
  90. {
  91. if (!Settings.display_model) {
  92. Settings.display_model = XDSP_05;
  93. }
  94. if (XDSP_05 == Settings.display_model) {
  95. if ((pin[GPIO_SPI_CS] < 99) && (pin[GPIO_SPI_CLK] < 99) && (pin[GPIO_SPI_MOSI] < 99)) {
  96. epd.cs_pin = pin[GPIO_SPI_CS];
  97. epd.sclk_pin = pin[GPIO_SPI_CLK]; // 14
  98. epd.mosi_pin = pin[GPIO_SPI_MOSI]; // 13
  99. EpdInitMode();
  100. }
  101. else if ((pin[GPIO_SSPI_CS] < 99) && (pin[GPIO_SSPI_SCLK] < 99) && (pin[GPIO_SSPI_MOSI] < 99)) {
  102. epd.cs_pin = pin[GPIO_SSPI_CS];
  103. epd.sclk_pin = pin[GPIO_SSPI_SCLK];
  104. epd.mosi_pin = pin[GPIO_SSPI_MOSI];
  105. EpdInitMode();
  106. }
  107. }
  108. }
  109. /*********************************************************************************************/
  110. void EpdClear(void)
  111. {
  112. paint.Clear(UNCOLORED);
  113. }
  114. void EpdSetFont(uint8_t font)
  115. {
  116. if (1 == font) {
  117. selected_font = &Font12;
  118. } else {
  119. #ifdef USE_TINY_FONT
  120. if (2 == font) {
  121. selected_font = &Font24;
  122. } else {
  123. selected_font = &Font8;
  124. }
  125. #else
  126. selected_font = &Font24;
  127. #endif
  128. }
  129. }
  130. void EpdDisplayFrame(void)
  131. {
  132. epd.SetFrameMemory(paint.GetImage(), 0, 0, paint.GetWidth(), paint.GetHeight());
  133. epd.DisplayFrame();
  134. }
  135. void EpdDrawStringAt(uint16_t x, uint16_t y, char *str, uint8_t color, uint8_t flag)
  136. {
  137. if (!flag) {
  138. paint.DrawStringAt(x, y, str, selected_font, color);
  139. } else {
  140. paint.DrawStringAt((x-1) * selected_font->Width, (y-1) * selected_font->Height, str, selected_font, color);
  141. }
  142. }
  143. // not needed
  144. void EpdDisplayOnOff(uint8_t on)
  145. {
  146. }
  147. void EpdOnOff(void)
  148. {
  149. EpdDisplayOnOff(disp_power);
  150. }
  151. /*********************************************************************************************/
  152. #ifdef USE_DISPLAY_MODES1TO5
  153. void EpdPrintLog(void)
  154. {
  155. disp_refresh--;
  156. if (!disp_refresh) {
  157. disp_refresh = Settings.display_refresh;
  158. if (Settings.display_rotate) {
  159. if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); }
  160. }
  161. char* txt = DisplayLogBuffer('\040');
  162. if (txt != NULL) {
  163. byte size = Settings.display_size;
  164. uint16_t theight = size * EPD_FONT_HEIGTH;
  165. EpdSetFont(size);
  166. uint8_t last_row = Settings.display_rows -1;
  167. // epd_scroll = theight; // Start below header
  168. epd_scroll = 0; // Start at top with no header
  169. for (byte i = 0; i < last_row; i++) {
  170. strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols);
  171. EpdDrawStringAt(0, epd_scroll, disp_screen_buffer[i], COLORED, 0);
  172. epd_scroll += theight;
  173. }
  174. strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols);
  175. DisplayFillScreen(last_row);
  176. EpdDrawStringAt(0, epd_scroll, disp_screen_buffer[last_row], COLORED, 0);
  177. // EpdDisplayFrame();
  178. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "[%s]"), txt);
  179. AddLog(LOG_LEVEL_DEBUG);
  180. }
  181. }
  182. }
  183. void EpdRefresh(void) // Every second
  184. {
  185. if (Settings.display_mode) { // Mode 0 is User text
  186. /*
  187. char tftdt[Settings.display_cols[0] +1];
  188. char date4[11]; // 24-04-2017
  189. char space[Settings.display_cols[0] - 17];
  190. char time[9]; // 13:45:43
  191. EpdSetFont(1);
  192. snprintf_P(date4, sizeof(date4), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year);
  193. memset(space, 0x20, sizeof(space));
  194. space[sizeof(space) -1] = '\0';
  195. snprintf_P(time, sizeof(time), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
  196. snprintf_P(tftdt, sizeof(tftdt), PSTR("%s%s%s"), date4, space, time);
  197. EpdDrawStringAt(0, 0, tftdt, COLORED, 0);
  198. */
  199. switch (Settings.display_mode) {
  200. case 1: // Text
  201. case 2: // Local
  202. case 3: // Local
  203. case 4: // Mqtt
  204. case 5: // Mqtt
  205. EpdPrintLog();
  206. EpdDisplayFrame();
  207. break;
  208. }
  209. // EpdDisplayFrame();
  210. }
  211. }
  212. #endif // USE_DISPLAY_MODES1TO5
  213. /*********************************************************************************************\
  214. * Interface
  215. \*********************************************************************************************/
  216. boolean Xdsp05(byte function)
  217. {
  218. boolean result = false;
  219. if (spi_flg || soft_spi_flg) {
  220. if (FUNC_DISPLAY_INIT_DRIVER == function) {
  221. EpdInitDriver();
  222. }
  223. else if (XDSP_05 == Settings.display_model) {
  224. if (!dsp_color) { dsp_color = COLORED; }
  225. switch (function) {
  226. case FUNC_DISPLAY_MODEL:
  227. result = true;
  228. break;
  229. case FUNC_DISPLAY_INIT:
  230. EpdInit(dsp_init);
  231. break;
  232. case FUNC_DISPLAY_POWER:
  233. EpdOnOff();
  234. break;
  235. case FUNC_DISPLAY_CLEAR:
  236. EpdClear();
  237. break;
  238. case FUNC_DISPLAY_DRAW_HLINE:
  239. paint.DrawHorizontalLine(dsp_x, dsp_y, dsp_len, dsp_color);
  240. break;
  241. case FUNC_DISPLAY_DRAW_VLINE:
  242. paint.DrawVerticalLine(dsp_x, dsp_y, dsp_len, dsp_color);
  243. break;
  244. case FUNC_DISPLAY_DRAW_LINE:
  245. paint.DrawLine(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  246. break;
  247. case FUNC_DISPLAY_DRAW_CIRCLE:
  248. paint.DrawCircle(dsp_x, dsp_y, dsp_rad, dsp_color);
  249. break;
  250. case FUNC_DISPLAY_FILL_CIRCLE:
  251. paint.DrawFilledCircle(dsp_x, dsp_y, dsp_rad, dsp_color);
  252. break;
  253. case FUNC_DISPLAY_DRAW_RECTANGLE:
  254. paint.DrawRectangle(dsp_x, dsp_y, dsp_x + dsp_x2, dsp_y + dsp_y2, dsp_color);
  255. break;
  256. case FUNC_DISPLAY_FILL_RECTANGLE:
  257. paint.DrawFilledRectangle(dsp_x, dsp_y, dsp_x + dsp_x2, dsp_y + dsp_y2, dsp_color);
  258. break;
  259. case FUNC_DISPLAY_DRAW_FRAME:
  260. EpdDisplayFrame();
  261. break;
  262. case FUNC_DISPLAY_TEXT_SIZE:
  263. // EpdSetFontorSize(Settings.display_size);
  264. break;
  265. case FUNC_DISPLAY_FONT_SIZE:
  266. EpdSetFont(Settings.display_font);
  267. break;
  268. case FUNC_DISPLAY_DRAW_STRING:
  269. EpdDrawStringAt(dsp_x, dsp_y, dsp_str, dsp_color, dsp_flag);
  270. break;
  271. case FUNC_DISPLAY_ONOFF:
  272. EpdDisplayOnOff(dsp_on);
  273. break;
  274. case FUNC_DISPLAY_ROTATION:
  275. paint.SetRotate(Settings.display_rotate);
  276. break;
  277. #ifdef USE_DISPLAY_MODES1TO5
  278. case FUNC_DISPLAY_EVERY_SECOND:
  279. EpdRefresh();
  280. break;
  281. #endif // USE_DISPLAY_MODES1TO5
  282. }
  283. }
  284. }
  285. return result;
  286. }
  287. #endif // USE_DISPLAY_EPAPER
  288. #endif // USE_DISPLAY
  289. #endif // USE_SPI