display.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * (c) 2004,2006 Richard Titmuss for SlimProtoLib
  3. * (c) Philippe G. 2019, philippe_44@outlook.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdint.h>
  22. #include <arpa/inet.h>
  23. #include "esp_log.h"
  24. #include "config.h"
  25. #include "display.h"
  26. // here we should include all possible drivers
  27. extern struct display_s SSD1306_display;
  28. struct display_s *display;
  29. static const char *TAG = "display";
  30. #define min(a,b) (((a) < (b)) ? (a) : (b))
  31. #define DISPLAYER_STACK_SIZE 2048
  32. #define SCROLLABLE_SIZE 384
  33. static EXT_RAM_ATTR struct {
  34. TaskHandle_t task;
  35. SemaphoreHandle_t mutex;
  36. int pause, speed, by;
  37. enum { DISPLAYER_DISABLED, DISPLAYER_IDLE, DISPLAYER_ACTIVE } state;
  38. char string[SCROLLABLE_SIZE + 1];
  39. int offset, boundary;
  40. char *metadata_config;
  41. } displayer;
  42. static void displayer_task(void *args);
  43. /****************************************************************************************
  44. *
  45. */
  46. void display_init(char *welcome) {
  47. bool init = false;
  48. char *item = config_alloc_get(NVS_TYPE_STR, "display_config");
  49. if (item && *item) {
  50. char * drivername=strstr(item,"driver");
  51. if (!drivername || (drivername && strcasestr(drivername,"SSD1306"))) {
  52. display = &SSD1306_display;
  53. if (display->init(item, welcome)) {
  54. init = true;
  55. ESP_LOGI(TAG, "Display initialization successful");
  56. } else {
  57. ESP_LOGE(TAG, "Display initialization failed");
  58. }
  59. } else {
  60. ESP_LOGE(TAG,"Unknown display driver name in display config: %s",item);
  61. }
  62. } else {
  63. ESP_LOGW(TAG, "no display");
  64. }
  65. if (init) {
  66. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  67. static EXT_RAM_ATTR StackType_t xStack[DISPLAYER_STACK_SIZE] __attribute__ ((aligned (4)));
  68. // start the task that will handle scrolling & counting
  69. displayer.mutex = xSemaphoreCreateMutex();
  70. displayer.by = 2;
  71. displayer.pause = 3600;
  72. displayer.speed = 33;
  73. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "displayer_thread", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  74. // set lines for "fixed" text mode
  75. display->set_font(1, DISPLAY_FONT_TINY, 0);
  76. display->set_font(2, DISPLAY_FONT_LARGE, 0);
  77. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  78. }
  79. if (item) free(item);
  80. }
  81. /****************************************************************************************
  82. *
  83. */
  84. static void displayer_task(void *args) {
  85. while (1) {
  86. int scroll_pause = displayer.pause;
  87. // suspend ourselves if nothing to do
  88. if (displayer.state == DISPLAYER_DISABLED) {
  89. vTaskSuspend(NULL);
  90. display->clear();
  91. }
  92. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  93. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  94. display->line(2, DISPLAY_LEFT, -displayer.offset, DISPLAY_CLEAR | DISPLAY_UPDATE, displayer.string);
  95. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  96. scroll_pause = displayer.offset ? displayer.speed : displayer.pause;
  97. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  98. xSemaphoreGive(displayer.mutex);
  99. } else if (displayer.state == DISPLAYER_IDLE) {
  100. display->line(2, DISPLAY_LEFT, 0, DISPLAY_CLEAR | DISPLAY_UPDATE, displayer.string);
  101. scroll_pause = displayer.pause;
  102. }
  103. vTaskDelay(scroll_pause / portTICK_PERIOD_MS);
  104. }
  105. }
  106. /****************************************************************************************
  107. *
  108. */
  109. void displayer_metadata(char *artist, char *album, char *title) {
  110. char *string = displayer.string, *p;
  111. int len = SCROLLABLE_SIZE;
  112. if (!displayer.metadata_config) {
  113. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  114. return;
  115. }
  116. // format metadata parameters and write them directly
  117. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  118. char token[16], *q;
  119. int space = len;
  120. bool skip = false;
  121. displayer.string[0] = '\0';
  122. p = strchr(displayer.metadata_config, '=');
  123. while (p++) {
  124. // find token and copy what's after when reaching last one
  125. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  126. q = strchr(p, ',');
  127. strncat(string, p, q ? min(q - p, space) : space);
  128. break;
  129. }
  130. // copy what's before token (be safe)
  131. if ((q = strchr(p, '%')) == NULL) break;
  132. // skip whatever is after a token if this token is empty
  133. if (!skip) {
  134. strncat(string, p, min(q - p, space));
  135. space = len - strlen(string);
  136. }
  137. // then copy token's content
  138. if (strcasestr(q, "artist") && artist) strncat(string, p = artist, space);
  139. else if (strcasestr(q, "album") && album) strncat(string, p = album, space);
  140. else if (strcasestr(q, "title") && title) strncat(string, p = title, space);
  141. space = len - strlen(string);
  142. // flag to skip the data following an empty field
  143. if (*p) skip = false;
  144. else skip = true;
  145. // advance to next separator
  146. p = strchr(q + 1, '%');
  147. }
  148. } else {
  149. string = strdup(title ? title : "");
  150. }
  151. // get optional scroll speed
  152. if ((p = strcasestr(displayer.metadata_config, "speed")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.speed);
  153. // just starts displayer directly
  154. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  155. displayer.offset = 0;
  156. displayer.state = DISPLAYER_ACTIVE;
  157. displayer.boundary = display->stretch(2, displayer.string, SCROLLABLE_SIZE);
  158. xSemaphoreGive(displayer.mutex);
  159. }
  160. /****************************************************************************************
  161. * This is not really thread-safe as displayer_task might be in the middle of line drawing
  162. * but it won't crash (I think) and making it thread-safe would be complicated for a
  163. * feature which is secondary (the LMS version of scrolling is thread-safe)
  164. */
  165. void displayer_scroll(char *string, int speed) {
  166. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  167. if (speed) displayer.speed = speed;
  168. displayer.offset = 0;
  169. displayer.state = DISPLAYER_ACTIVE;
  170. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  171. displayer.string[SCROLLABLE_SIZE] = '\0';
  172. displayer.boundary = display->stretch(2, displayer.string, SCROLLABLE_SIZE);
  173. xSemaphoreGive(displayer.mutex);
  174. }
  175. /****************************************************************************************
  176. * See above comment
  177. */
  178. void displayer_control(enum displayer_cmd_e cmd) {
  179. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  180. displayer.offset = 0;
  181. displayer.boundary = 0;
  182. switch(cmd) {
  183. case DISPLAYER_SHUTDOWN:
  184. displayer.state = DISPLAYER_DISABLED;
  185. vTaskSuspend(displayer.task);
  186. break;
  187. case DISPLAYER_ACTIVATE:
  188. displayer.state = DISPLAYER_ACTIVE;
  189. displayer.string[0] = '\0';
  190. vTaskResume(displayer.task);
  191. break;
  192. case DISPLAYER_SUSPEND:
  193. displayer.state = DISPLAYER_IDLE;
  194. break;
  195. default:
  196. break;
  197. }
  198. xSemaphoreGive(displayer.mutex);
  199. }