display.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdint.h>
  11. #include <arpa/inet.h>
  12. #include "esp_log.h"
  13. #include "globdefs.h"
  14. #include "platform_config.h"
  15. #include "tools.h"
  16. #include "display.h"
  17. #include "gds.h"
  18. #include "gds_default_if.h"
  19. #include "gds_draw.h"
  20. #include "gds_text.h"
  21. #include "gds_font.h"
  22. #include "gds_image.h"
  23. static const char *TAG = "display";
  24. #define min(a,b) (((a) < (b)) ? (a) : (b))
  25. #define max(a,b) (((a) > (b)) ? (a) : (b))
  26. #define DISPLAYER_STACK_SIZE (3*1024)
  27. #define SCROLLABLE_SIZE 384
  28. #define HEADER_SIZE 64
  29. #define DEFAULT_SLEEP 3600
  30. #define ARTWORK_BORDER 1
  31. extern const uint8_t default_artwork[] asm("_binary_note_jpg_start");
  32. static EXT_RAM_ATTR struct {
  33. TaskHandle_t task;
  34. SemaphoreHandle_t mutex;
  35. int pause, speed, by;
  36. enum { DISPLAYER_DOWN, DISPLAYER_IDLE, DISPLAYER_ACTIVE } state;
  37. char header[HEADER_SIZE + 1];
  38. char string[SCROLLABLE_SIZE + 1];
  39. int offset, boundary;
  40. char *metadata_config;
  41. bool timer, refresh;
  42. uint32_t elapsed;
  43. struct {
  44. uint32_t value;
  45. char string[8]; // H:MM:SS
  46. bool visible;
  47. } duration;
  48. struct {
  49. bool enable, active;
  50. bool fit;
  51. bool updated;
  52. int tick;
  53. int offset;
  54. } artwork;
  55. TickType_t tick;
  56. } displayer;
  57. static const char *known_drivers[] = {"SH1106",
  58. "SSD1306",
  59. "SSD1322",
  60. "SSD1326",
  61. "SSD1327",
  62. "SSD1675",
  63. "SSD1351",
  64. "ST7735",
  65. "ST7789",
  66. "ILI9341",
  67. NULL
  68. };
  69. static void displayer_task(void *args);
  70. struct GDS_Device *display;
  71. extern GDS_DetectFunc SSD1306_Detect, SSD132x_Detect, SH1106_Detect, SSD1675_Detect, SSD1322_Detect, SSD1351_Detect, ST77xx_Detect, ILI9341_Detect;
  72. GDS_DetectFunc *drivers[] = { SH1106_Detect, SSD1306_Detect, SSD132x_Detect, SSD1675_Detect, SSD1322_Detect, SSD1351_Detect, ST77xx_Detect, ILI9341_Detect, NULL };
  73. /****************************************************************************************
  74. *
  75. */
  76. void display_init(char *welcome) {
  77. bool init = false;
  78. char *config = config_alloc_get_str("display_config", CONFIG_DISPLAY_CONFIG, "N/A");
  79. int width = -1, height = -1, backlight_pin = -1;
  80. char *drivername = strstr(config, "driver");
  81. PARSE_PARAM(config, "width", '=', width);
  82. PARSE_PARAM(config, "height", '=', height);
  83. PARSE_PARAM(config, "back", '=', backlight_pin);
  84. // query drivers to see if we have a match
  85. ESP_LOGI(TAG, "Trying to configure display with %s", config);
  86. if (backlight_pin >= 0) {
  87. struct GDS_BacklightPWM PWMConfig = { .Channel = pwm_system.base_channel++, .Timer = pwm_system.timer, .Max = pwm_system.max, .Init = false };
  88. display = GDS_AutoDetect(drivername, drivers, &PWMConfig);
  89. } else {
  90. display = GDS_AutoDetect(drivername, drivers, NULL);
  91. }
  92. // so far so good
  93. if (display && width > 0 && height > 0) {
  94. int RST_pin = -1;
  95. PARSE_PARAM(config, "reset", '=', RST_pin);
  96. // Detect driver interface
  97. if (strcasestr(config, "I2C") && i2c_system_port != -1) {
  98. int address = 0x3C;
  99. PARSE_PARAM(config, "address", '=', address);
  100. init = true;
  101. GDS_I2CInit( i2c_system_port, -1, -1, i2c_system_speed ) ;
  102. GDS_I2CAttachDevice( display, width, height, address, RST_pin, backlight_pin );
  103. ESP_LOGI(TAG, "Display is I2C on port %u", address);
  104. } else if (strcasestr(config, "SPI") && spi_system_host != -1) {
  105. int CS_pin = -1, speed = 0;
  106. PARSE_PARAM(config, "cs", '=', CS_pin);
  107. PARSE_PARAM(config, "speed", '=', speed);
  108. init = true;
  109. GDS_SPIInit( spi_system_host, spi_system_dc_gpio );
  110. GDS_SPIAttachDevice( display, width, height, CS_pin, RST_pin, backlight_pin, speed );
  111. ESP_LOGI(TAG, "Display is SPI host %u with cs:%d", spi_system_host, CS_pin);
  112. } else {
  113. display = NULL;
  114. ESP_LOGI(TAG, "Unsupported display interface or serial link not configured");
  115. }
  116. } else {
  117. display = NULL;
  118. ESP_LOGW(TAG, "No display driver");
  119. }
  120. if (init) {
  121. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  122. static EXT_RAM_ATTR StackType_t xStack[DISPLAYER_STACK_SIZE] __attribute__ ((aligned (4)));
  123. GDS_SetLayout(display, strcasestr(config, "HFlip"), strcasestr(config, "VFlip"), strcasestr(config, "rotate"));
  124. GDS_SetFont(display, &Font_line_2);
  125. GDS_TextPos(display, GDS_FONT_DEFAULT, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, welcome);
  126. // start the task that will handle scrolling & counting
  127. displayer.mutex = xSemaphoreCreateMutex();
  128. displayer.by = 2;
  129. displayer.pause = 3600;
  130. displayer.speed = 33;
  131. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "common_displayer", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  132. // set lines for "fixed" text mode
  133. GDS_TextSetFontAuto(display, 1, GDS_FONT_LINE_1, -3);
  134. GDS_TextSetFontAuto(display, 2, GDS_FONT_LINE_2, -3);
  135. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  136. // leave room for artwork is display is horizontal-style
  137. if (strcasestr(displayer.metadata_config, "artwork")) {
  138. displayer.artwork.enable = true;
  139. displayer.artwork.fit = true;
  140. if (height <= 64 && width > height * 2) displayer.artwork.offset = width - height - ARTWORK_BORDER;
  141. PARSE_PARAM(displayer.metadata_config, "artwork", ':', displayer.artwork.fit);
  142. }
  143. }
  144. free(config);
  145. }
  146. /****************************************************************************************
  147. * This is not thread-safe as displayer_task might be in the middle of line drawing
  148. * but it won't crash (I think) and making it thread-safe would be complicated for a
  149. * feature which is secondary (the LMS version of scrolling is thread-safe)
  150. */
  151. static void displayer_task(void *args) {
  152. int scroll_sleep = 0, timer_sleep;
  153. while (1) {
  154. // suspend ourselves if nothing to do
  155. if (displayer.state < DISPLAYER_ACTIVE) {
  156. if (displayer.state == DISPLAYER_IDLE) GDS_TextLine(display, 2, 0, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.string);
  157. vTaskSuspend(NULL);
  158. scroll_sleep = 0;
  159. GDS_ClearExt(display, true);
  160. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_UPDATE, displayer.header);
  161. } else if (displayer.refresh) {
  162. // little trick when switching master while in IDLE and missing it
  163. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.header);
  164. displayer.refresh = false;
  165. }
  166. // we have been waken up before our requested time
  167. if (scroll_sleep <= 10) {
  168. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  169. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  170. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  171. // need to work with local copies as we don't want to suspend caller
  172. int offset = -displayer.offset;
  173. char *string = strdup(displayer.string);
  174. scroll_sleep = displayer.offset ? displayer.speed : displayer.pause;
  175. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  176. xSemaphoreGive(displayer.mutex);
  177. // now display using safe copies, can be lengthy
  178. GDS_TextLine(display, 2, offset, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, string);
  179. free(string);
  180. } else {
  181. scroll_sleep = DEFAULT_SLEEP;
  182. }
  183. }
  184. // handler elapsed track time
  185. if (displayer.timer && displayer.state == DISPLAYER_ACTIVE) {
  186. char line[19] = "-", *_line = line + 1; // [-]H:MM:SS / H:MM:SS
  187. TickType_t tick = xTaskGetTickCount();
  188. uint32_t elapsed = (tick - displayer.tick) * portTICK_PERIOD_MS;
  189. if (elapsed >= 1000) {
  190. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  191. displayer.tick = tick;
  192. elapsed = displayer.elapsed += elapsed / 1000;
  193. xSemaphoreGive(displayer.mutex);
  194. // when we have duration but no space, display remaining time
  195. if (displayer.duration.value && !displayer.duration.visible) elapsed = displayer.duration.value - elapsed;
  196. if (elapsed < 3600) sprintf(_line, "%u:%02u", elapsed / 60, elapsed % 60);
  197. else sprintf(_line, "%u:%02u:%02u", (elapsed / 3600) % 100, (elapsed % 3600) / 60, elapsed % 60);
  198. // concatenate if we have room for elapsed / duration
  199. if (displayer.duration.visible) {
  200. strcat(_line, "/");
  201. strcat(_line, displayer.duration.string);
  202. } else if (displayer.duration.value) {
  203. _line--;
  204. }
  205. // just re-write the whole line it's easier
  206. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, displayer.header);
  207. GDS_TextLine(display, 1, GDS_TEXT_RIGHT, GDS_TEXT_UPDATE, _line);
  208. // if we have not received artwork after 5s, display a default icon
  209. if (displayer.artwork.active && !displayer.artwork.updated && tick - displayer.artwork.tick > pdMS_TO_TICKS(5000)) {
  210. ESP_LOGI(TAG, "no artwork received, setting default");
  211. displayer_artwork((uint8_t*) default_artwork);
  212. }
  213. timer_sleep = 1000;
  214. } else timer_sleep = max(1000 - elapsed, 0);
  215. } else timer_sleep = DEFAULT_SLEEP;
  216. // then sleep the min amount of time
  217. int sleep = min(scroll_sleep, timer_sleep);
  218. ESP_LOGD(TAG, "timers s:%d t:%d", scroll_sleep, timer_sleep);
  219. scroll_sleep -= sleep;
  220. vTaskDelay(sleep / portTICK_PERIOD_MS);
  221. }
  222. }
  223. /****************************************************************************************
  224. *
  225. */
  226. void displayer_artwork(uint8_t *data) {
  227. if (!displayer.artwork.active) return;
  228. int x = displayer.artwork.offset ? displayer.artwork.offset + ARTWORK_BORDER : 0;
  229. int y = x ? 0 : 32;
  230. GDS_ClearWindow(display, x, y, -1, -1, GDS_COLOR_BLACK);
  231. if (data) {
  232. displayer.artwork.updated = true;
  233. GDS_DrawJPEG(display, data, x, y, GDS_IMAGE_CENTER | (displayer.artwork.fit ? GDS_IMAGE_FIT : 0));
  234. } else {
  235. displayer.artwork.updated = false;
  236. displayer.artwork.tick = xTaskGetTickCount();
  237. }
  238. }
  239. /****************************************************************************************
  240. *
  241. */
  242. bool displayer_can_artwork(void) {
  243. return displayer.artwork.active;
  244. }
  245. /****************************************************************************************
  246. *
  247. */
  248. void displayer_metadata(char *artist, char *album, char *title) {
  249. char *string = displayer.string, *p;
  250. int len = SCROLLABLE_SIZE;
  251. // need a display!
  252. if (!display) return;
  253. // just do title if there is no config set
  254. if (!displayer.metadata_config) {
  255. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  256. return;
  257. }
  258. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  259. // format metadata parameters and write them directly
  260. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  261. char token[16], *q;
  262. int space = len;
  263. bool skip = false;
  264. displayer.string[0] = '\0';
  265. p = strchr(displayer.metadata_config, '=');
  266. while (p++) {
  267. // find token and copy what's after when reaching last one
  268. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  269. q = strchr(p, ',');
  270. strncat(string, p, q ? min(q - p, space) : space);
  271. break;
  272. }
  273. // copy what's before token (be safe)
  274. if ((q = strchr(p, '%')) == NULL) break;
  275. // skip whatever is after a token if this token is empty
  276. if (!skip) {
  277. strncat(string, p, min(q - p, space));
  278. space = len - strlen(string);
  279. }
  280. // then copy token's content
  281. if (!strncasecmp(q + 1, "artist", 6) && artist) strncat(string, p = artist, space);
  282. else if (!strncasecmp(q + 1, "album", 5) && album) strncat(string, p = album, space);
  283. else if (!strncasecmp(q + 1, "title", 5) && title) strncat(string, p = title, space);
  284. space = len - strlen(string);
  285. // flag to skip the data following an empty field
  286. if (*p) skip = false;
  287. else skip = true;
  288. // advance to next separator
  289. p = strchr(q + 1, '%');
  290. }
  291. } else {
  292. strncpy(string, title ? title : "", SCROLLABLE_SIZE);
  293. }
  294. // get optional scroll speed & pause
  295. PARSE_PARAM(displayer.metadata_config, "speed", '=', displayer.speed);
  296. PARSE_PARAM(displayer.metadata_config, "pause", '=', displayer.pause);
  297. displayer.offset = 0;
  298. utf8_decode(displayer.string);
  299. ESP_LOGI(TAG, "playing %s", displayer.string);
  300. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  301. xSemaphoreGive(displayer.mutex);
  302. }
  303. /****************************************************************************************
  304. *
  305. */
  306. void displayer_scroll(char *string, int speed, int pause) {
  307. // need a display!
  308. if (!display) return;
  309. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  310. if (speed) displayer.speed = speed;
  311. if (pause) displayer.pause = pause;
  312. displayer.offset = 0;
  313. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  314. displayer.string[SCROLLABLE_SIZE] = '\0';
  315. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  316. xSemaphoreGive(displayer.mutex);
  317. }
  318. /****************************************************************************************
  319. *
  320. */
  321. void displayer_timer(enum displayer_time_e mode, int elapsed, int duration) {
  322. // need a display!
  323. if (!display) return;
  324. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  325. if (displayer.timer) displayer.tick = xTaskGetTickCount();
  326. if (elapsed >= 0) displayer.elapsed = elapsed / 1000;
  327. if (duration > 0) {
  328. displayer.duration.visible = true;
  329. displayer.duration.value = duration / 1000;
  330. if (displayer.duration.value > 3600) sprintf(displayer.duration.string, "%u:%02u:%02u", (displayer.duration.value / 3600) % 10,
  331. (displayer.duration.value % 3600) / 60, displayer.duration.value % 60);
  332. else sprintf(displayer.duration.string, "%u:%02u", displayer.duration.value / 60, displayer.duration.value % 60);
  333. char *buf;
  334. asprintf(&buf, "%s %s/%s", displayer.header, displayer.duration.string, displayer.duration.string);
  335. if (GDS_GetTextWidth(display, 1, 0, buf) > GDS_GetWidth(display)) {
  336. ESP_LOGW(TAG, "Can't fit duration %s (%d) on screen using elapsed only", buf, GDS_GetTextWidth(display, 1, 0, buf));
  337. displayer.duration.visible = false;
  338. }
  339. free(buf);
  340. } else if (!duration) {
  341. displayer.duration.visible = false;
  342. displayer.duration.value = 0;
  343. }
  344. xSemaphoreGive(displayer.mutex);
  345. }
  346. /****************************************************************************************
  347. * See above comment
  348. */
  349. void displayer_control(enum displayer_cmd_e cmd, ...) {
  350. va_list args;
  351. if (!display) return;
  352. va_start(args, cmd);
  353. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  354. switch(cmd) {
  355. case DISPLAYER_ACTIVATE: {
  356. char *header = va_arg(args, char*);
  357. displayer.artwork.active = displayer.artwork.enable && va_arg(args, int);
  358. strncpy(displayer.header, header, HEADER_SIZE);
  359. displayer.header[HEADER_SIZE] = '\0';
  360. displayer.state = DISPLAYER_ACTIVE;
  361. displayer.timer = false;
  362. displayer.refresh = true;
  363. displayer.string[0] = '\0';
  364. displayer.elapsed = displayer.duration.value = 0;
  365. displayer.duration.visible = false;
  366. displayer.offset = displayer.boundary = 0;
  367. display_bus(&displayer, DISPLAY_BUS_TAKE);
  368. if (displayer.artwork.active) GDS_SetTextWidth(display, displayer.artwork.offset);
  369. vTaskResume(displayer.task);
  370. break;
  371. }
  372. case DISPLAYER_SUSPEND:
  373. // task will display the line 2 from beginning and suspend
  374. displayer.state = DISPLAYER_IDLE;
  375. displayer_artwork(NULL);
  376. display_bus(&displayer, DISPLAY_BUS_GIVE);
  377. break;
  378. case DISPLAYER_SHUTDOWN:
  379. // let the task self-suspend (we might be doing i2c_write)
  380. GDS_SetTextWidth(display, 0);
  381. displayer_artwork(NULL);
  382. displayer.state = DISPLAYER_DOWN;
  383. display_bus(&displayer, DISPLAY_BUS_GIVE);
  384. break;
  385. case DISPLAYER_TIMER_RUN:
  386. if (!displayer.timer) {
  387. display_bus(&displayer, DISPLAY_BUS_TAKE);
  388. displayer.timer = true;
  389. displayer.tick = xTaskGetTickCount();
  390. }
  391. break;
  392. case DISPLAYER_TIMER_PAUSE:
  393. displayer.timer = false;
  394. break;
  395. default:
  396. break;
  397. }
  398. xSemaphoreGive(displayer.mutex);
  399. va_end(args);
  400. }
  401. /****************************************************************************************
  402. *
  403. */
  404. bool display_is_valid_driver(const char * driver){
  405. return display_conf_get_driver_name(driver)!=NULL;
  406. }
  407. /****************************************************************************************
  408. *
  409. */
  410. const char *display_conf_get_driver_name(const char * driver){
  411. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  412. if(strcasestr(driver,known_drivers[i])){
  413. return known_drivers[i];
  414. }
  415. }
  416. return NULL;
  417. }
  418. /****************************************************************************************
  419. *
  420. */
  421. char * display_get_supported_drivers(void){
  422. int total_size = 1;
  423. char * supported_drivers=NULL;
  424. const char * separator = "|";
  425. int separator_len = strlen(separator);
  426. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  427. total_size += strlen(known_drivers[i])+separator_len;
  428. }
  429. total_size+=2;
  430. supported_drivers = malloc(total_size);
  431. memset(supported_drivers,0x00,total_size);
  432. strcat(supported_drivers,"<");
  433. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  434. supported_drivers = strcat(supported_drivers,known_drivers[i]);
  435. supported_drivers = strcat(supported_drivers,separator);
  436. }
  437. strcat(supported_drivers,">");
  438. return supported_drivers;
  439. }