xdsp_01_lcd.ino 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. xdsp_01_lcd.ino - Display LCD 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_I2C
  16. #ifdef USE_DISPLAY
  17. #ifdef USE_DISPLAY_LCD
  18. #define XDSP_01 1
  19. #define LCD_ADDRESS1 0x27 // LCD I2C address option 1
  20. #define LCD_ADDRESS2 0x3F // LCD I2C address option 2
  21. #include <Wire.h>
  22. #include <LiquidCrystal_I2C.h>
  23. LiquidCrystal_I2C *lcd;
  24. /*********************************************************************************************/
  25. void LcdInitMode(void)
  26. {
  27. lcd->init();
  28. lcd->clear();
  29. }
  30. void LcdInit(uint8_t mode)
  31. {
  32. switch(mode) {
  33. case DISPLAY_INIT_MODE:
  34. LcdInitMode();
  35. #ifdef USE_DISPLAY_MODES1TO5
  36. DisplayClearScreenBuffer();
  37. #endif // USE_DISPLAY_MODES1TO5
  38. break;
  39. case DISPLAY_INIT_PARTIAL:
  40. case DISPLAY_INIT_FULL:
  41. break;
  42. }
  43. }
  44. void LcdInitDriver(void)
  45. {
  46. if (!Settings.display_model) {
  47. if (I2cDevice(LCD_ADDRESS1)) {
  48. Settings.display_address[0] = LCD_ADDRESS1;
  49. Settings.display_model = XDSP_01;
  50. }
  51. else if (I2cDevice(LCD_ADDRESS2)) {
  52. Settings.display_address[0] = LCD_ADDRESS2;
  53. Settings.display_model = XDSP_01;
  54. }
  55. }
  56. if (XDSP_01 == Settings.display_model) {
  57. lcd = new LiquidCrystal_I2C(Settings.display_address[0], Settings.display_cols[0], Settings.display_rows);
  58. #ifdef USE_DISPLAY_MODES1TO5
  59. DisplayAllocScreenBuffer();
  60. #endif // USE_DISPLAY_MODES1TO5
  61. LcdInitMode();
  62. }
  63. }
  64. void LcdDrawStringAt(void)
  65. {
  66. lcd->setCursor(dsp_x, dsp_y);
  67. lcd->print(dsp_str);
  68. }
  69. void LcdDisplayOnOff(uint8_t on)
  70. {
  71. if (on) {
  72. lcd->backlight();
  73. } else {
  74. lcd->noBacklight();
  75. }
  76. }
  77. /*********************************************************************************************/
  78. #ifdef USE_DISPLAY_MODES1TO5
  79. void LcdCenter(byte row, char* txt)
  80. {
  81. int offset;
  82. int len;
  83. char line[Settings.display_cols[0] +2];
  84. memset(line, 0x20, Settings.display_cols[0]);
  85. line[Settings.display_cols[0]] = 0;
  86. len = strlen(txt);
  87. offset = (len < Settings.display_cols[0]) ? offset = (Settings.display_cols[0] - len) / 2 : 0;
  88. strlcpy(line +offset, txt, len);
  89. lcd->setCursor(0, row);
  90. lcd->print(line);
  91. }
  92. boolean LcdPrintLog(void)
  93. {
  94. boolean result = false;
  95. disp_refresh--;
  96. if (!disp_refresh) {
  97. disp_refresh = Settings.display_refresh;
  98. if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); }
  99. char* txt = DisplayLogBuffer('\337');
  100. if (txt != NULL) {
  101. uint8_t last_row = Settings.display_rows -1;
  102. for (byte i = 0; i < last_row; i++) {
  103. strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols);
  104. lcd->setCursor(0, i); // Col 0, Row i
  105. lcd->print(disp_screen_buffer[i +1]);
  106. }
  107. strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols);
  108. DisplayFillScreen(last_row);
  109. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "[%s]"), disp_screen_buffer[last_row]);
  110. AddLog(LOG_LEVEL_DEBUG);
  111. lcd->setCursor(0, last_row);
  112. lcd->print(disp_screen_buffer[last_row]);
  113. result = true;
  114. }
  115. }
  116. return result;
  117. }
  118. void LcdTime(void)
  119. {
  120. char line[Settings.display_cols[0] +1];
  121. snprintf_P(line, sizeof(line), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
  122. LcdCenter(0, line);
  123. snprintf_P(line, sizeof(line), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year);
  124. LcdCenter(1, line);
  125. }
  126. void LcdRefresh(void) // Every second
  127. {
  128. if (Settings.display_mode) { // Mode 0 is User text
  129. switch (Settings.display_mode) {
  130. case 1: // Time
  131. LcdTime();
  132. break;
  133. case 2: // Local
  134. case 4: // Mqtt
  135. LcdPrintLog();
  136. break;
  137. case 3: // Local
  138. case 5: { // Mqtt
  139. if (!LcdPrintLog()) { LcdTime(); }
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. #endif // USE_DISPLAY_MODES1TO5
  146. /*********************************************************************************************\
  147. * Interface
  148. \*********************************************************************************************/
  149. boolean Xdsp01(byte function)
  150. {
  151. boolean result = false;
  152. if (i2c_flg) {
  153. if (FUNC_DISPLAY_INIT_DRIVER == function) {
  154. LcdInitDriver();
  155. }
  156. else if (XDSP_01 == Settings.display_model) {
  157. switch (function) {
  158. case FUNC_DISPLAY_MODEL:
  159. result = true;
  160. break;
  161. case FUNC_DISPLAY_INIT:
  162. LcdInit(dsp_init);
  163. break;
  164. case FUNC_DISPLAY_POWER:
  165. LcdDisplayOnOff(disp_power);
  166. break;
  167. case FUNC_DISPLAY_CLEAR:
  168. lcd->clear();
  169. break;
  170. // case FUNC_DISPLAY_DRAW_HLINE:
  171. // break;
  172. // case FUNC_DISPLAY_DRAW_VLINE:
  173. // break;
  174. // case FUNC_DISPLAY_DRAW_CIRCLE:
  175. // break;
  176. // case FUNC_DISPLAY_FILL_CIRCLE:
  177. // break;
  178. // case FUNC_DISPLAY_DRAW_RECTANGLE:
  179. // break;
  180. // case FUNC_DISPLAY_FILL_RECTANGLE:
  181. // break;
  182. // case FUNC_DISPLAY_DRAW_FRAME:
  183. // break;
  184. // case FUNC_DISPLAY_TEXT_SIZE:
  185. // break;
  186. // case FUNC_DISPLAY_FONT_SIZE:
  187. // break;
  188. case FUNC_DISPLAY_DRAW_STRING:
  189. LcdDrawStringAt();
  190. break;
  191. case FUNC_DISPLAY_ONOFF:
  192. LcdDisplayOnOff(dsp_on);
  193. break;
  194. // case FUNC_DISPLAY_ROTATION:
  195. // break;
  196. #ifdef USE_DISPLAY_MODES1TO5
  197. case FUNC_DISPLAY_EVERY_SECOND:
  198. LcdRefresh();
  199. break;
  200. #endif // USE_DISPLAY_MODES1TO5
  201. }
  202. }
  203. }
  204. return result;
  205. }
  206. #endif // USE_DISPLAY_LCD
  207. #endif // USE_DISPLAY
  208. #endif // USE_I2C