display.c 9.4 KB

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