display.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "tools.h"
  26. #include "display.h"
  27. // here we should include all possible drivers
  28. extern struct display_s SSD1306_display;
  29. struct display_s *display;
  30. static const char *TAG = "display";
  31. #define min(a,b) (((a) < (b)) ? (a) : (b))
  32. #define max(a,b) (((a) > (b)) ? (a) : (b))
  33. #define DISPLAYER_STACK_SIZE 2048
  34. #define SCROLLABLE_SIZE 384
  35. #define HEADER_SIZE 64
  36. #define DEFAULT_SLEEP 3600
  37. static EXT_RAM_ATTR struct {
  38. TaskHandle_t task;
  39. SemaphoreHandle_t mutex;
  40. int pause, speed, by;
  41. enum { DISPLAYER_DOWN, DISPLAYER_IDLE, DISPLAYER_ACTIVE } state;
  42. char header[HEADER_SIZE + 1];
  43. char string[SCROLLABLE_SIZE + 1];
  44. int offset, boundary;
  45. char *metadata_config;
  46. bool timer, refresh;
  47. uint32_t elapsed, duration;
  48. TickType_t tick;
  49. } displayer;
  50. static void displayer_task(void *args);
  51. /****************************************************************************************
  52. *
  53. */
  54. void display_init(char *welcome) {
  55. bool init = false;
  56. char *item = config_alloc_get(NVS_TYPE_STR, "display_config");
  57. if (item && *item) {
  58. char * drivername=strstr(item,"driver");
  59. if (!drivername || (drivername && strcasestr(drivername,"SSD1306"))) {
  60. display = &SSD1306_display;
  61. if (display->init(item, welcome)) {
  62. init = true;
  63. ESP_LOGI(TAG, "Display initialization successful");
  64. } else {
  65. ESP_LOGE(TAG, "Display initialization failed");
  66. }
  67. } else {
  68. ESP_LOGE(TAG,"Unknown display driver name in display config: %s",item);
  69. }
  70. } else {
  71. ESP_LOGW(TAG, "no display");
  72. }
  73. if (init) {
  74. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  75. static EXT_RAM_ATTR StackType_t xStack[DISPLAYER_STACK_SIZE] __attribute__ ((aligned (4)));
  76. // start the task that will handle scrolling & counting
  77. displayer.mutex = xSemaphoreCreateMutex();
  78. displayer.by = 2;
  79. displayer.pause = 3600;
  80. displayer.speed = 33;
  81. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "displayer_thread", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  82. // set lines for "fixed" text mode
  83. display->set_font(1, DISPLAY_FONT_LINE_1, -3);
  84. display->set_font(2, DISPLAY_FONT_LINE_2, -3);
  85. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  86. }
  87. if (item) free(item);
  88. }
  89. /****************************************************************************************
  90. * This is not really thread-safe as displayer_task might be in the middle of line drawing
  91. * but it won't crash (I think) and making it thread-safe would be complicated for a
  92. * feature which is secondary (the LMS version of scrolling is thread-safe)
  93. */
  94. static void displayer_task(void *args) {
  95. int scroll_sleep = 0, timer_sleep;
  96. while (1) {
  97. // suspend ourselves if nothing to do
  98. if (displayer.state < DISPLAYER_ACTIVE) {
  99. if (displayer.state == DISPLAYER_IDLE) display->line(2, 0, DISPLAY_CLEAR | DISPLAY_UPDATE, displayer.string);
  100. vTaskSuspend(NULL);
  101. scroll_sleep = 0;
  102. display->clear();
  103. display->line(1, DISPLAY_LEFT, DISPLAY_UPDATE, displayer.header);
  104. } else if (displayer.refresh) {
  105. // little trick when switching master while in IDLE and missing it
  106. display->line(1, DISPLAY_LEFT, DISPLAY_CLEAR | DISPLAY_UPDATE, displayer.header);
  107. displayer.refresh = false;
  108. }
  109. // we have been waken up before our requested time
  110. if (scroll_sleep <= 10) {
  111. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  112. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  113. display->line(2, -displayer.offset, DISPLAY_CLEAR | DISPLAY_UPDATE, displayer.string);
  114. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  115. scroll_sleep = displayer.offset ? displayer.speed : displayer.pause;
  116. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  117. xSemaphoreGive(displayer.mutex);
  118. } else {
  119. scroll_sleep = DEFAULT_SLEEP;
  120. }
  121. }
  122. // handler elapsed track time
  123. if (displayer.timer && displayer.state == DISPLAYER_ACTIVE) {
  124. char counter[12];
  125. TickType_t tick = xTaskGetTickCount();
  126. uint32_t elapsed = (tick - displayer.tick) * portTICK_PERIOD_MS;
  127. if (elapsed >= 1000) {
  128. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  129. displayer.tick = tick;
  130. displayer.elapsed += elapsed / 1000;
  131. xSemaphoreGive(displayer.mutex);
  132. if (displayer.elapsed < 3600) sprintf(counter, "%2u:%02u", displayer.elapsed / 60, displayer.elapsed % 60);
  133. else sprintf(counter, "%2u:%2u:%02u", displayer.elapsed / 3600, (displayer.elapsed % 3600) / 60, displayer.elapsed % 60);
  134. display->line(1, DISPLAY_RIGHT, (DISPLAY_CLEAR | DISPLAY_ONLY_EOL) | DISPLAY_UPDATE, counter);
  135. timer_sleep = 1000;
  136. } else timer_sleep = max(1000 - elapsed, 0);
  137. } else timer_sleep = DEFAULT_SLEEP;
  138. // then sleep the min amount of time
  139. int sleep = min(scroll_sleep, timer_sleep);
  140. ESP_LOGD(TAG, "timers s:%d t:%d", scroll_sleep, timer_sleep);
  141. scroll_sleep -= sleep;
  142. vTaskDelay(sleep / portTICK_PERIOD_MS);
  143. }
  144. }
  145. /****************************************************************************************
  146. *
  147. */
  148. void displayer_metadata(char *artist, char *album, char *title) {
  149. char *string = displayer.string, *p;
  150. int len = SCROLLABLE_SIZE;
  151. if (!displayer.metadata_config) {
  152. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  153. return;
  154. }
  155. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  156. // format metadata parameters and write them directly
  157. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  158. char token[16], *q;
  159. int space = len;
  160. bool skip = false;
  161. displayer.string[0] = '\0';
  162. p = strchr(displayer.metadata_config, '=');
  163. while (p++) {
  164. // find token and copy what's after when reaching last one
  165. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  166. q = strchr(p, ',');
  167. strncat(string, p, q ? min(q - p, space) : space);
  168. break;
  169. }
  170. // copy what's before token (be safe)
  171. if ((q = strchr(p, '%')) == NULL) break;
  172. // skip whatever is after a token if this token is empty
  173. if (!skip) {
  174. strncat(string, p, min(q - p, space));
  175. space = len - strlen(string);
  176. }
  177. // then copy token's content
  178. if (!strncasecmp(q + 1, "artist", 6) && artist) strncat(string, p = artist, space);
  179. else if (!strncasecmp(q + 1, "album", 5) && album) strncat(string, p = album, space);
  180. else if (!strncasecmp(q + 1, "title", 5) && title) strncat(string, p = title, space);
  181. space = len - strlen(string);
  182. // flag to skip the data following an empty field
  183. if (*p) skip = false;
  184. else skip = true;
  185. // advance to next separator
  186. p = strchr(q + 1, '%');
  187. }
  188. } else {
  189. string = strdup(title ? title : "");
  190. }
  191. // get optional scroll speed
  192. if ((p = strcasestr(displayer.metadata_config, "speed")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.speed);
  193. displayer.offset = 0;
  194. utf8_decode(displayer.string);
  195. ESP_LOGI(TAG, "playing %s", displayer.string);
  196. displayer.boundary = display->stretch(2, displayer.string, SCROLLABLE_SIZE);
  197. xSemaphoreGive(displayer.mutex);
  198. }
  199. /****************************************************************************************
  200. *
  201. */
  202. void displayer_scroll(char *string, int speed) {
  203. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  204. if (speed) displayer.speed = speed;
  205. displayer.offset = 0;
  206. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  207. displayer.string[SCROLLABLE_SIZE] = '\0';
  208. displayer.boundary = display->stretch(2, displayer.string, SCROLLABLE_SIZE);
  209. xSemaphoreGive(displayer.mutex);
  210. }
  211. /****************************************************************************************
  212. *
  213. */
  214. void displayer_timer(enum displayer_time_e mode, int elapsed, int duration) {
  215. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  216. if (elapsed >= 0) displayer.elapsed = elapsed;
  217. if (duration >= 0) displayer.duration = duration;
  218. if (displayer.timer) displayer.tick = xTaskGetTickCount();
  219. xSemaphoreGive(displayer.mutex);
  220. }
  221. /****************************************************************************************
  222. * See above comment
  223. */
  224. void displayer_control(enum displayer_cmd_e cmd, ...) {
  225. va_list args;
  226. va_start(args, cmd);
  227. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  228. switch(cmd) {
  229. case DISPLAYER_ACTIVATE: {
  230. char *header = va_arg(args, char*);
  231. strncpy(displayer.header, header, HEADER_SIZE);
  232. displayer.header[HEADER_SIZE] = '\0';
  233. displayer.state = DISPLAYER_ACTIVE;
  234. displayer.timer = false;
  235. displayer.refresh = true;
  236. displayer.string[0] = '\0';
  237. displayer.elapsed = displayer.duration = 0;
  238. displayer.offset = displayer.boundary = 0;
  239. vTaskResume(displayer.task);
  240. break;
  241. }
  242. case DISPLAYER_SUSPEND:
  243. // task will display the line 2 from beginning and suspend
  244. displayer.state = DISPLAYER_IDLE;
  245. break;
  246. case DISPLAYER_SHUTDOWN:
  247. // let the task self-suspend (we might be doing i2c_write)
  248. displayer.state = DISPLAYER_DOWN;
  249. break;
  250. case DISPLAYER_TIMER_RUN:
  251. if (!displayer.timer) {
  252. displayer.timer = true;
  253. displayer.tick = xTaskGetTickCount();
  254. }
  255. break;
  256. case DISPLAYER_TIMER_PAUSE:
  257. displayer.timer = false;
  258. break;
  259. default:
  260. break;
  261. }
  262. xSemaphoreGive(displayer.mutex);
  263. va_end(args);
  264. }