xdsp_04_ili9341.ino 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. xdsp_04_ili9341.ino - Display Tft Ili9341 support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends and Adafruit
  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_ILI9341
  18. #define XDSP_04 4
  19. #define TFT_TOP 16
  20. #define TFT_BOTTOM 16
  21. #define TFT_FONT_WIDTH 6
  22. #define TFT_FONT_HEIGTH 8 // Adafruit minimal font heigth pixels
  23. #include <SPI.h>
  24. #include <Adafruit_GFX.h>
  25. #include <Adafruit_ILI9341.h> // TFT 320x240 and 480x320
  26. Adafruit_ILI9341 *tft;
  27. uint16_t tft_scroll;
  28. /*********************************************************************************************/
  29. void Ili9341InitMode(void)
  30. {
  31. tft->setRotation(Settings.display_rotate); // 0
  32. tft->invertDisplay(0);
  33. tft->fillScreen(ILI9341_BLACK);
  34. tft->setTextWrap(false); // Allow text to run off edges
  35. tft->cp437(true);
  36. if (!Settings.display_mode) {
  37. tft->setCursor(0, 0);
  38. tft->setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  39. tft->setTextSize(1);
  40. } else {
  41. tft->setScrollMargins(TFT_TOP, TFT_BOTTOM);
  42. tft->setCursor(0, 0);
  43. tft->setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
  44. tft->setTextSize(2);
  45. // tft->println("HEADER");
  46. tft_scroll = TFT_TOP;
  47. }
  48. }
  49. void Ili9341Init(uint8_t mode)
  50. {
  51. switch(mode) {
  52. case DISPLAY_INIT_MODE:
  53. Ili9341InitMode();
  54. #ifdef USE_DISPLAY_MODES1TO5
  55. if (Settings.display_rotate) {
  56. DisplayClearScreenBuffer();
  57. }
  58. #endif // USE_DISPLAY_MODES1TO5
  59. break;
  60. case DISPLAY_INIT_PARTIAL:
  61. case DISPLAY_INIT_FULL:
  62. break;
  63. }
  64. }
  65. void Ili9341InitDriver(void)
  66. {
  67. if (!Settings.display_model) {
  68. Settings.display_model = XDSP_04;
  69. }
  70. if (XDSP_04 == Settings.display_model) {
  71. tft = new Adafruit_ILI9341(pin[GPIO_SPI_CS], pin[GPIO_SPI_DC]);
  72. tft->begin();
  73. #ifdef USE_DISPLAY_MODES1TO5
  74. if (Settings.display_rotate) {
  75. DisplayAllocScreenBuffer();
  76. }
  77. #endif // USE_DISPLAY_MODES1TO5
  78. Ili9341InitMode();
  79. }
  80. }
  81. void Ili9341Clear(void)
  82. {
  83. tft->fillScreen(ILI9341_BLACK);
  84. tft->setCursor(0, 0);
  85. }
  86. void Ili9341DrawStringAt(uint16_t x, uint16_t y, char *str, uint16_t color, uint8_t flag)
  87. {
  88. uint16_t active_color = ILI9341_WHITE;
  89. tft->setTextSize(Settings.display_size);
  90. if (!flag) {
  91. tft->setCursor(x, y);
  92. } else {
  93. tft->setCursor((x-1) * TFT_FONT_WIDTH * Settings.display_size, (y-1) * TFT_FONT_HEIGTH * Settings.display_size);
  94. }
  95. if (color) { active_color = color; }
  96. tft->setTextColor(active_color, ILI9341_BLACK);
  97. tft->println(str);
  98. }
  99. void Ili9341DisplayOnOff(uint8_t on)
  100. {
  101. // tft->showDisplay(on);
  102. // tft->invertDisplay(on);
  103. if (pin[GPIO_BACKLIGHT] < 99) {
  104. pinMode(pin[GPIO_BACKLIGHT], OUTPUT);
  105. digitalWrite(pin[GPIO_BACKLIGHT], on);
  106. }
  107. }
  108. void Ili9341OnOff(void)
  109. {
  110. Ili9341DisplayOnOff(disp_power);
  111. }
  112. /*********************************************************************************************/
  113. #ifdef USE_DISPLAY_MODES1TO5
  114. void Ili9341PrintLog(void)
  115. {
  116. disp_refresh--;
  117. if (!disp_refresh) {
  118. disp_refresh = Settings.display_refresh;
  119. if (Settings.display_rotate) {
  120. if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); }
  121. }
  122. char* txt = DisplayLogBuffer('\370');
  123. if (txt != NULL) {
  124. byte size = Settings.display_size;
  125. uint16_t theight = size * TFT_FONT_HEIGTH;
  126. tft->setTextSize(size);
  127. tft->setTextColor(ILI9341_CYAN, ILI9341_BLACK); // Add background color to solve flicker
  128. if (!Settings.display_rotate) { // Use hardware scroll
  129. tft->setCursor(0, tft_scroll);
  130. tft->fillRect(0, tft_scroll, tft->width(), theight, ILI9341_BLACK); // Erase line
  131. tft->print(txt);
  132. tft_scroll += theight;
  133. if (tft_scroll >= (tft->height() - TFT_BOTTOM)) {
  134. tft_scroll = TFT_TOP;
  135. }
  136. tft->scrollTo(tft_scroll);
  137. } else {
  138. uint8_t last_row = Settings.display_rows -1;
  139. tft_scroll = theight; // Start below header
  140. tft->setCursor(0, tft_scroll);
  141. for (byte i = 0; i < last_row; i++) {
  142. strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols);
  143. // tft->fillRect(0, tft_scroll, tft->width(), theight, ILI9341_BLACK); // Erase line
  144. tft->print(disp_screen_buffer[i]);
  145. tft_scroll += theight;
  146. tft->setCursor(0, tft_scroll);
  147. }
  148. strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols);
  149. DisplayFillScreen(last_row);
  150. tft->print(disp_screen_buffer[last_row]);
  151. }
  152. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "[%s]"), txt);
  153. AddLog(LOG_LEVEL_DEBUG);
  154. }
  155. }
  156. }
  157. void Ili9341Refresh(void) // Every second
  158. {
  159. if (Settings.display_mode) { // Mode 0 is User text
  160. char tftdt[Settings.display_cols[0] +1];
  161. char date4[11]; // 24-04-2017
  162. char space[Settings.display_cols[0] - 17];
  163. char time[9]; // 13:45:43
  164. tft->setTextSize(2);
  165. tft->setTextColor(ILI9341_YELLOW, ILI9341_RED); // Add background color to solve flicker
  166. tft->setCursor(0, 0);
  167. 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);
  168. memset(space, 0x20, sizeof(space));
  169. space[sizeof(space) -1] = '\0';
  170. snprintf_P(time, sizeof(time), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
  171. snprintf_P(tftdt, sizeof(tftdt), PSTR("%s%s%s"), date4, space, time);
  172. tft->print(tftdt);
  173. switch (Settings.display_mode) {
  174. case 1: // Text
  175. case 2: // Local
  176. case 3: // Local
  177. case 4: // Mqtt
  178. case 5: // Mqtt
  179. Ili9341PrintLog();
  180. break;
  181. }
  182. }
  183. }
  184. #endif // USE_DISPLAY_MODES1TO5
  185. /*********************************************************************************************\
  186. * Interface
  187. \*********************************************************************************************/
  188. boolean Xdsp04(byte function)
  189. {
  190. boolean result = false;
  191. if (spi_flg) {
  192. if (FUNC_DISPLAY_INIT_DRIVER == function) {
  193. Ili9341InitDriver();
  194. }
  195. else if (XDSP_04 == Settings.display_model) {
  196. if (!dsp_color) { dsp_color = ILI9341_WHITE; }
  197. switch (function) {
  198. case FUNC_DISPLAY_MODEL:
  199. result = true;
  200. break;
  201. case FUNC_DISPLAY_INIT:
  202. Ili9341Init(dsp_init);
  203. break;
  204. case FUNC_DISPLAY_POWER:
  205. Ili9341OnOff();
  206. break;
  207. case FUNC_DISPLAY_CLEAR:
  208. Ili9341Clear();
  209. break;
  210. case FUNC_DISPLAY_DRAW_HLINE:
  211. tft->writeFastHLine(dsp_x, dsp_y, dsp_len, dsp_color);
  212. break;
  213. case FUNC_DISPLAY_DRAW_VLINE:
  214. tft->writeFastVLine(dsp_x, dsp_y, dsp_len, dsp_color);
  215. break;
  216. case FUNC_DISPLAY_DRAW_LINE:
  217. tft->writeLine(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  218. break;
  219. case FUNC_DISPLAY_DRAW_CIRCLE:
  220. tft->drawCircle(dsp_x, dsp_y, dsp_rad, dsp_color);
  221. break;
  222. case FUNC_DISPLAY_FILL_CIRCLE:
  223. tft->fillCircle(dsp_x, dsp_y, dsp_rad, dsp_color);
  224. break;
  225. case FUNC_DISPLAY_DRAW_RECTANGLE:
  226. tft->drawRect(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  227. break;
  228. case FUNC_DISPLAY_FILL_RECTANGLE:
  229. tft->fillRect(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  230. break;
  231. // case FUNC_DISPLAY_DRAW_FRAME:
  232. // oled->display();
  233. // break;
  234. case FUNC_DISPLAY_TEXT_SIZE:
  235. tft->setTextSize(Settings.display_size);
  236. break;
  237. case FUNC_DISPLAY_FONT_SIZE:
  238. // tft->setTextSize(Settings.display_font);
  239. break;
  240. case FUNC_DISPLAY_DRAW_STRING:
  241. Ili9341DrawStringAt(dsp_x, dsp_y, dsp_str, dsp_color, dsp_flag);
  242. break;
  243. case FUNC_DISPLAY_ONOFF:
  244. Ili9341DisplayOnOff(dsp_on);
  245. break;
  246. case FUNC_DISPLAY_ROTATION:
  247. tft->setRotation(Settings.display_rotate);
  248. break;
  249. #ifdef USE_DISPLAY_MODES1TO5
  250. case FUNC_DISPLAY_EVERY_SECOND:
  251. Ili9341Refresh();
  252. break;
  253. #endif // USE_DISPLAY_MODES1TO5
  254. }
  255. }
  256. }
  257. return result;
  258. }
  259. #endif // USE_DISPLAY_ILI9341
  260. #endif // USE_DISPLAY
  261. #endif // USE_SPI