xdsp_03_matrix.ino 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. xdsp_03_matrix.ino - Display 8x8 matrix 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_MATRIX
  18. #define XDSP_03 3
  19. #define MTX_MAX_SCREEN_BUFFER 80
  20. #include <Wire.h>
  21. #include <Adafruit_GFX.h>
  22. #include <Adafruit_LEDBackpack.h> // 8x8 Matrix
  23. Adafruit_8x8matrix *matrix[8];
  24. uint8_t mtx_matrices = 0;
  25. uint8_t mtx_state = 0;
  26. uint8_t mtx_counter = 0;
  27. int16_t mtx_x = 0;
  28. int16_t mtx_y = 0;
  29. //char mtx_buffer[MTX_MAX_SCREEN_BUFFER];
  30. char *mtx_buffer = NULL;
  31. uint8_t mtx_mode = 0;
  32. uint8_t mtx_loop = 0;
  33. uint8_t mtx_done = 0;
  34. /*********************************************************************************************/
  35. void MatrixWrite(void)
  36. {
  37. for (byte i = 0; i < mtx_matrices; i++) {
  38. matrix[i]->writeDisplay();
  39. }
  40. }
  41. void MatrixClear(void)
  42. {
  43. for (byte i = 0; i < mtx_matrices; i++) {
  44. matrix[i]->clear();
  45. }
  46. MatrixWrite();
  47. }
  48. void MatrixFixed(char* txt)
  49. {
  50. for (byte i = 0; i < mtx_matrices; i++) {
  51. matrix[i]->clear();
  52. matrix[i]->setCursor(-i *8, 0);
  53. matrix[i]->print(txt);
  54. matrix[i]->setBrightness(Settings.display_dimmer);
  55. }
  56. MatrixWrite();
  57. }
  58. void MatrixCenter(char* txt)
  59. {
  60. int offset;
  61. int len = strlen(txt);
  62. offset = (len < 8) ? offset = ((mtx_matrices *8) - (len *6)) / 2 : 0;
  63. for (byte i = 0; i < mtx_matrices; i++) {
  64. matrix[i]->clear();
  65. matrix[i]->setCursor(-(i *8)+offset, 0);
  66. matrix[i]->print(txt);
  67. matrix[i]->setBrightness(Settings.display_dimmer);
  68. }
  69. MatrixWrite();
  70. }
  71. void MatrixScrollLeft(char* txt, int loop)
  72. {
  73. switch (mtx_state) {
  74. case 1:
  75. mtx_state = 2;
  76. // Horiz. position of text -- starts off right edge
  77. mtx_x = 8 * mtx_matrices;
  78. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "[%s]"), txt);
  79. AddLog(LOG_LEVEL_DEBUG);
  80. disp_refresh = Settings.display_refresh;
  81. case 2:
  82. disp_refresh--;
  83. if (!disp_refresh) {
  84. disp_refresh = Settings.display_refresh;
  85. for (byte i = 0; i < mtx_matrices; i++) {
  86. matrix[i]->clear();
  87. matrix[i]->setCursor(mtx_x - i *8, 0);
  88. matrix[i]->print(txt);
  89. matrix[i]->setBrightness(Settings.display_dimmer);
  90. }
  91. MatrixWrite();
  92. // Move text position left by 1 pixel.
  93. mtx_x--;
  94. int16_t len = strlen(txt);
  95. if (mtx_x < -(len *6)) { mtx_state = loop; }
  96. }
  97. break;
  98. }
  99. }
  100. void MatrixScrollUp(char* txt, int loop)
  101. {
  102. int wordcounter = 0;
  103. char tmpbuf[200];
  104. char *words[100];
  105. // char separators[] = " ,.;:!?";
  106. // char separators[] = " ";
  107. // char separators[] = " /|";
  108. char separators[] = " /";
  109. switch (mtx_state) {
  110. case 1:
  111. mtx_state = 2;
  112. // Vertical position of text -- starts off left bottom edge
  113. mtx_y = 8;
  114. mtx_counter = 0;
  115. disp_refresh = Settings.display_refresh;
  116. case 2:
  117. disp_refresh--;
  118. if (!disp_refresh) {
  119. disp_refresh = Settings.display_refresh;
  120. strlcpy(tmpbuf, txt, sizeof(tmpbuf));
  121. char *p = strtok(tmpbuf, separators);
  122. while (p != NULL && wordcounter < 40) {
  123. words[wordcounter++] = p;
  124. p = strtok(NULL, separators);
  125. }
  126. for (byte i = 0; i < mtx_matrices; i++) {
  127. matrix[i]->clear();
  128. for (byte j = 0; j < wordcounter; j++) {
  129. matrix[i]->setCursor(-i *8, mtx_y + (j *8));
  130. matrix[i]->println(words[j]);
  131. }
  132. matrix[i]->setBrightness(Settings.display_dimmer);
  133. }
  134. MatrixWrite();
  135. if (((mtx_y %8) == 0) && mtx_counter) {
  136. mtx_counter--;
  137. } else {
  138. mtx_y--; // Move text position up by 1 pixel.
  139. mtx_counter = STATES * 1; // Hold text for 1 seconds
  140. }
  141. if (mtx_y < -(wordcounter *8)) { mtx_state = loop; }
  142. }
  143. break;
  144. }
  145. }
  146. /*********************************************************************************************/
  147. void MatrixInitMode(void)
  148. {
  149. for (byte i = 0; i < mtx_matrices; i++) {
  150. matrix[i]->setRotation(Settings.display_rotate); // 1
  151. matrix[i]->setBrightness(Settings.display_dimmer);
  152. matrix[i]->blinkRate(0); // 0 - 3
  153. matrix[i]->setTextWrap(false); // Allow text to run off edges
  154. // matrix[i]->setTextSize(Settings.display_size);
  155. // matrix[i]->setTextColor(LED_RED);
  156. matrix[i]->cp437(true);
  157. }
  158. MatrixClear();
  159. }
  160. void MatrixInit(uint8_t mode)
  161. {
  162. switch(mode) {
  163. case DISPLAY_INIT_MODE:
  164. MatrixInitMode();
  165. break;
  166. case DISPLAY_INIT_PARTIAL:
  167. case DISPLAY_INIT_FULL:
  168. break;
  169. }
  170. }
  171. void MatrixInitDriver(void)
  172. {
  173. mtx_buffer = (char*)(malloc(MTX_MAX_SCREEN_BUFFER));
  174. if (mtx_buffer != NULL) {
  175. if (!Settings.display_model) {
  176. if (I2cDevice(Settings.display_address[1])) {
  177. Settings.display_model = XDSP_03;
  178. }
  179. }
  180. if (XDSP_03 == Settings.display_model) {
  181. mtx_state = 1;
  182. for (mtx_matrices = 0; mtx_matrices < 8; mtx_matrices++) {
  183. if (Settings.display_address[mtx_matrices]) {
  184. matrix[mtx_matrices] = new Adafruit_8x8matrix();
  185. matrix[mtx_matrices]->begin(Settings.display_address[mtx_matrices]);
  186. } else {
  187. break;
  188. }
  189. }
  190. MatrixInitMode();
  191. }
  192. }
  193. }
  194. void MatrixOnOff(void)
  195. {
  196. if (!disp_power) { MatrixClear(); }
  197. }
  198. void MatrixDrawStringAt(uint16_t x, uint16_t y, char *str, uint16_t color, uint8_t flag)
  199. {
  200. snprintf(mtx_buffer, MTX_MAX_SCREEN_BUFFER, str);
  201. mtx_mode = x &1; // Use x for selecting scroll up (0) or scroll left (1)
  202. mtx_loop = y &1; // Use y for selecting no loop (0) or loop (1)
  203. if (!mtx_state) { mtx_state = 1; }
  204. }
  205. /*********************************************************************************************/
  206. #ifdef USE_DISPLAY_MODES1TO5
  207. void MatrixPrintLog(uint8_t direction)
  208. {
  209. char* txt = (!mtx_done) ? DisplayLogBuffer('\370') : mtx_buffer;
  210. if (txt != NULL) {
  211. if (!mtx_state) { mtx_state = 1; }
  212. if (!mtx_done) {
  213. // Remove extra spaces
  214. uint8_t space = 0;
  215. uint8_t max_cols = (disp_log_buffer_cols < MTX_MAX_SCREEN_BUFFER) ? disp_log_buffer_cols : MTX_MAX_SCREEN_BUFFER;
  216. mtx_buffer[0] = '\0';
  217. uint8_t i = 0;
  218. while ((txt[i] != '\0') && (i < max_cols)) {
  219. if (txt[i] == ' ') {
  220. space++;
  221. } else {
  222. space = 0;
  223. }
  224. if (space < 2) {
  225. strncat(mtx_buffer, (const char*)txt +i, (strlen(mtx_buffer) < MTX_MAX_SCREEN_BUFFER -1) ? 1 : 0);
  226. }
  227. i++;
  228. }
  229. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "[%s]"), mtx_buffer);
  230. AddLog(LOG_LEVEL_DEBUG);
  231. mtx_done = 1;
  232. }
  233. if (direction) {
  234. MatrixScrollUp(mtx_buffer, 0);
  235. } else {
  236. MatrixScrollLeft(mtx_buffer, 0);
  237. }
  238. if (!mtx_state) { mtx_done = 0; }
  239. } else {
  240. char disp_time[9]; // 13:45:43
  241. snprintf_P(disp_time, sizeof(disp_time), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
  242. MatrixFixed(disp_time);
  243. }
  244. }
  245. #endif // USE_DISPLAY_MODES1TO5
  246. void MatrixRefresh(void) // Every second
  247. {
  248. if (disp_power) {
  249. switch (Settings.display_mode) {
  250. case 0: {
  251. switch (mtx_mode) {
  252. case 0:
  253. MatrixScrollLeft(mtx_buffer, mtx_loop);
  254. break;
  255. case 1:
  256. MatrixScrollUp(mtx_buffer, mtx_loop);
  257. break;
  258. }
  259. break;
  260. }
  261. #ifdef USE_DISPLAY_MODES1TO5
  262. case 2: {
  263. char disp_date[9]; // 24-04-17
  264. snprintf_P(disp_date, sizeof(disp_date), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%02d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year -2000);
  265. MatrixFixed(disp_date);
  266. break;
  267. }
  268. case 3: {
  269. char disp_day[10]; // Mon
  270. snprintf_P(disp_day, sizeof(disp_day), PSTR("%d %s"), RtcTime.day_of_month, RtcTime.name_of_month);
  271. MatrixCenter(disp_day);
  272. break;
  273. }
  274. case 4:
  275. MatrixPrintLog(0);
  276. break;
  277. case 1: // Time and user text
  278. case 5: // Time, user text and MQTT
  279. MatrixPrintLog(1);
  280. break;
  281. #endif // USE_DISPLAY_MODES1TO5
  282. }
  283. }
  284. }
  285. /*********************************************************************************************\
  286. * Interface
  287. \*********************************************************************************************/
  288. boolean Xdsp03(byte function)
  289. {
  290. boolean result = false;
  291. if (i2c_flg) {
  292. if (FUNC_DISPLAY_INIT_DRIVER == function) {
  293. MatrixInitDriver();
  294. }
  295. else if (XDSP_03 == Settings.display_model) {
  296. switch (function) {
  297. case FUNC_DISPLAY_MODEL:
  298. result = true;
  299. break;
  300. case FUNC_DISPLAY_INIT:
  301. MatrixInit(dsp_init);
  302. break;
  303. case FUNC_DISPLAY_EVERY_50_MSECOND:
  304. MatrixRefresh();
  305. break;
  306. case FUNC_DISPLAY_POWER:
  307. MatrixOnOff();
  308. break;
  309. case FUNC_DISPLAY_DRAW_STRING:
  310. MatrixDrawStringAt(dsp_x, dsp_y, dsp_str, dsp_color, dsp_flag);
  311. break;
  312. }
  313. }
  314. }
  315. return result;
  316. }
  317. #endif // USE_DISPLAY_MATRIX
  318. #endif // USE_DISPLAY
  319. #endif // USE_I2C