display.c 11 KB

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