xdsp_02_ssd1306.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. xdsp_02_ssd1306.ino - Display Oled ssd1306 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_SSD1306
  18. #define XDSP_02 2
  19. #define OLED_ADDRESS1 0x3C // Oled 128x32 I2C address
  20. #define OLED_ADDRESS2 0x3D // Oled 128x64 I2C address
  21. #define OLED_FONT_WIDTH 6
  22. #define OLED_FONT_HEIGTH 8
  23. //#define OLED_BUFFER_COLS 21 or 11 // Max number of columns in display shadow buffer
  24. //#define OLED_BUFFER_ROWS 8 or 16 // Max number of lines in display shadow buffer
  25. #include <Wire.h>
  26. #include <Adafruit_GFX.h>
  27. #include <Adafruit_SSD1306.h>
  28. Adafruit_SSD1306 *oled;
  29. uint8_t ssd1306_font_x = OLED_FONT_WIDTH;
  30. uint8_t ssd1306_font_y = OLED_FONT_HEIGTH;
  31. /*********************************************************************************************/
  32. void Ssd1306InitMode(void)
  33. {
  34. oled->setRotation(Settings.display_rotate); // 0
  35. oled->invertDisplay(false);
  36. oled->clearDisplay();
  37. oled->setTextWrap(false); // Allow text to run off edges
  38. oled->cp437(true);
  39. oled->setTextSize(Settings.display_size);
  40. oled->setTextColor(WHITE);
  41. oled->setCursor(0,0);
  42. oled->display();
  43. }
  44. void Ssd1306Init(uint8_t mode)
  45. {
  46. switch(mode) {
  47. case DISPLAY_INIT_MODE:
  48. Ssd1306InitMode();
  49. #ifdef USE_DISPLAY_MODES1TO5
  50. DisplayClearScreenBuffer();
  51. #endif // USE_DISPLAY_MODES1TO5
  52. break;
  53. case DISPLAY_INIT_PARTIAL:
  54. case DISPLAY_INIT_FULL:
  55. break;
  56. }
  57. }
  58. void Ssd1306InitDriver(void)
  59. {
  60. if (!Settings.display_model) {
  61. if (I2cDevice(OLED_ADDRESS1)) {
  62. Settings.display_address[0] = OLED_ADDRESS1;
  63. Settings.display_model = XDSP_02;
  64. }
  65. else if (I2cDevice(OLED_ADDRESS2)) {
  66. Settings.display_address[0] = OLED_ADDRESS2;
  67. Settings.display_model = XDSP_02;
  68. }
  69. }
  70. if (XDSP_02 == Settings.display_model) {
  71. oled = new Adafruit_SSD1306();
  72. oled->begin(SSD1306_SWITCHCAPVCC, Settings.display_address[0]);
  73. #ifdef USE_DISPLAY_MODES1TO5
  74. DisplayAllocScreenBuffer();
  75. #endif // USE_DISPLAY_MODES1TO5
  76. Ssd1306InitMode();
  77. }
  78. }
  79. void Ssd1306Clear(void)
  80. {
  81. oled->clearDisplay();
  82. oled->setCursor(0, 0);
  83. oled->display();
  84. }
  85. void Ssd1306DrawStringAt(uint16_t x, uint16_t y, char *str, uint16_t color, uint8_t flag)
  86. {
  87. if (!flag) {
  88. oled->setCursor(x, y);
  89. } else {
  90. oled->setCursor((x-1) * ssd1306_font_x * Settings.display_size, (y-1) * ssd1306_font_y * Settings.display_size);
  91. }
  92. oled->println(str);
  93. }
  94. void Ssd1306DisplayOnOff(uint8_t on)
  95. {
  96. if (on) {
  97. oled->ssd1306_command(SSD1306_DISPLAYON);
  98. } else {
  99. oled->ssd1306_command(SSD1306_DISPLAYOFF);
  100. }
  101. }
  102. void Ssd1306OnOff(void)
  103. {
  104. Ssd1306DisplayOnOff(disp_power);
  105. oled->display();
  106. }
  107. /*********************************************************************************************/
  108. #ifdef USE_DISPLAY_MODES1TO5
  109. void Ssd1306PrintLog(void)
  110. {
  111. disp_refresh--;
  112. if (!disp_refresh) {
  113. disp_refresh = Settings.display_refresh;
  114. if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); }
  115. char* txt = DisplayLogBuffer('\370');
  116. if (txt != NULL) {
  117. uint8_t last_row = Settings.display_rows -1;
  118. oled->clearDisplay();
  119. oled->setTextSize(Settings.display_size);
  120. oled->setCursor(0,0);
  121. for (byte i = 0; i < last_row; i++) {
  122. strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols);
  123. oled->println(disp_screen_buffer[i]);
  124. }
  125. strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols);
  126. DisplayFillScreen(last_row);
  127. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "[%s]"), disp_screen_buffer[last_row]);
  128. AddLog(LOG_LEVEL_DEBUG);
  129. oled->println(disp_screen_buffer[last_row]);
  130. oled->display();
  131. }
  132. }
  133. }
  134. void Ssd1306Time(void)
  135. {
  136. char line[12];
  137. oled->clearDisplay();
  138. oled->setTextSize(2);
  139. oled->setCursor(0, 0);
  140. snprintf_P(line, sizeof(line), PSTR(" %02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second); // [ 12:34:56 ]
  141. oled->println(line);
  142. 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); // [01-02-2018]
  143. oled->println(line);
  144. oled->display();
  145. }
  146. void Ssd1306Refresh(void) // Every second
  147. {
  148. if (Settings.display_mode) { // Mode 0 is User text
  149. switch (Settings.display_mode) {
  150. case 1: // Time
  151. Ssd1306Time();
  152. break;
  153. case 2: // Local
  154. case 3: // Local
  155. case 4: // Mqtt
  156. case 5: // Mqtt
  157. Ssd1306PrintLog();
  158. break;
  159. }
  160. }
  161. }
  162. #endif // USE_DISPLAY_MODES1TO5
  163. /*********************************************************************************************\
  164. * Interface
  165. \*********************************************************************************************/
  166. boolean Xdsp02(byte function)
  167. {
  168. boolean result = false;
  169. if (i2c_flg) {
  170. if (FUNC_DISPLAY_INIT_DRIVER == function) {
  171. Ssd1306InitDriver();
  172. }
  173. else if (XDSP_02 == Settings.display_model) {
  174. if (!dsp_color) { dsp_color = WHITE; }
  175. switch (function) {
  176. case FUNC_DISPLAY_MODEL:
  177. result = true;
  178. break;
  179. case FUNC_DISPLAY_INIT:
  180. Ssd1306Init(dsp_init);
  181. break;
  182. case FUNC_DISPLAY_POWER:
  183. Ssd1306OnOff();
  184. break;
  185. case FUNC_DISPLAY_CLEAR:
  186. Ssd1306Clear();
  187. break;
  188. case FUNC_DISPLAY_DRAW_HLINE:
  189. oled->writeFastHLine(dsp_x, dsp_y, dsp_len, dsp_color);
  190. break;
  191. case FUNC_DISPLAY_DRAW_VLINE:
  192. oled->writeFastVLine(dsp_x, dsp_y, dsp_len, dsp_color);
  193. break;
  194. case FUNC_DISPLAY_DRAW_LINE:
  195. oled->writeLine(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  196. break;
  197. case FUNC_DISPLAY_DRAW_CIRCLE:
  198. oled->drawCircle(dsp_x, dsp_y, dsp_rad, dsp_color);
  199. break;
  200. case FUNC_DISPLAY_FILL_CIRCLE:
  201. oled->fillCircle(dsp_x, dsp_y, dsp_rad, dsp_color);
  202. break;
  203. case FUNC_DISPLAY_DRAW_RECTANGLE:
  204. oled->drawRect(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  205. break;
  206. case FUNC_DISPLAY_FILL_RECTANGLE:
  207. oled->fillRect(dsp_x, dsp_y, dsp_x2, dsp_y2, dsp_color);
  208. break;
  209. case FUNC_DISPLAY_DRAW_FRAME:
  210. oled->display();
  211. break;
  212. case FUNC_DISPLAY_TEXT_SIZE:
  213. oled->setTextSize(Settings.display_size);
  214. break;
  215. case FUNC_DISPLAY_FONT_SIZE:
  216. // oled->setTextSize(Settings.display_font);
  217. break;
  218. case FUNC_DISPLAY_DRAW_STRING:
  219. Ssd1306DrawStringAt(dsp_x, dsp_y, dsp_str, dsp_color, dsp_flag);
  220. break;
  221. case FUNC_DISPLAY_ONOFF:
  222. Ssd1306DisplayOnOff(dsp_on);
  223. break;
  224. case FUNC_DISPLAY_ROTATION:
  225. oled->setRotation(Settings.display_rotate);
  226. break;
  227. #ifdef USE_DISPLAY_MODES1TO5
  228. case FUNC_DISPLAY_EVERY_SECOND:
  229. Ssd1306Refresh();
  230. break;
  231. #endif // USE_DISPLAY_MODES1TO5
  232. }
  233. }
  234. }
  235. return result;
  236. }
  237. #endif // USE_DISPLAY_SSD1306
  238. #endif // USE_DISPLAY
  239. #endif // USE_I2C