2
0

display.c 9.5 KB

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