display.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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, mode = 0;
  106. PARSE_PARAM(config, "cs", '=', CS_pin);
  107. PARSE_PARAM(config, "speed", '=', speed);
  108. PARSE_PARAM(config, "mode", '=', mode);
  109. init = true;
  110. GDS_SPIInit( spi_system_host, spi_system_dc_gpio );
  111. GDS_SPIAttachDevice( display, width, height, CS_pin, RST_pin, backlight_pin, speed, mode );
  112. ESP_LOGI(TAG, "Display is SPI host %u with cs:%d", spi_system_host, CS_pin);
  113. } else {
  114. display = NULL;
  115. ESP_LOGI(TAG, "Unsupported display interface or serial link not configured");
  116. }
  117. } else {
  118. display = NULL;
  119. ESP_LOGW(TAG, "No display driver");
  120. }
  121. if (init) {
  122. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  123. static EXT_RAM_ATTR StackType_t xStack[DISPLAYER_STACK_SIZE] __attribute__ ((aligned (4)));
  124. struct GDS_Layout Layout = {
  125. .HFlip = strcasestr(config, "HFlip"),
  126. .VFlip = strcasestr(config, "VFlip"),
  127. .Rotate = strcasestr(config, "rotate"),
  128. .Invert = strcasestr(config, "invert"),
  129. .ColorSwap = strcasestr(config, "cswap"),
  130. };
  131. GDS_SetLayout(display, &Layout);
  132. GDS_SetFont(display, &Font_line_2);
  133. GDS_TextPos(display, GDS_FONT_DEFAULT, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, welcome);
  134. // start the task that will handle scrolling & counting
  135. displayer.mutex = xSemaphoreCreateMutex();
  136. displayer.by = 2;
  137. displayer.pause = 3600;
  138. displayer.speed = 33;
  139. displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "common_displayer", DISPLAYER_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
  140. // set lines for "fixed" text mode
  141. GDS_TextSetFontAuto(display, 1, GDS_FONT_LINE_1, -3);
  142. GDS_TextSetFontAuto(display, 2, GDS_FONT_LINE_2, -3);
  143. displayer.metadata_config = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  144. // leave room for artwork is display is horizontal-style
  145. if (strcasestr(displayer.metadata_config, "artwork")) {
  146. displayer.artwork.enable = true;
  147. displayer.artwork.fit = true;
  148. if (height <= 64 && width > height * 2) displayer.artwork.offset = width - height - ARTWORK_BORDER;
  149. PARSE_PARAM(displayer.metadata_config, "artwork", ':', displayer.artwork.fit);
  150. }
  151. }
  152. free(config);
  153. }
  154. /****************************************************************************************
  155. * This is not thread-safe as displayer_task might be in the middle of line drawing
  156. * but it won't crash (I think) and making it thread-safe would be complicated for a
  157. * feature which is secondary (the LMS version of scrolling is thread-safe)
  158. */
  159. static void displayer_task(void *args) {
  160. int scroll_sleep = 0, timer_sleep;
  161. while (1) {
  162. // suspend ourselves if nothing to do
  163. if (displayer.state < DISPLAYER_ACTIVE) {
  164. if (displayer.state == DISPLAYER_IDLE) GDS_TextLine(display, 2, 0, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.string);
  165. vTaskSuspend(NULL);
  166. scroll_sleep = 0;
  167. GDS_ClearExt(display, true);
  168. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_UPDATE, displayer.header);
  169. } else if (displayer.refresh) {
  170. // little trick when switching master while in IDLE and missing it
  171. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, displayer.header);
  172. displayer.refresh = false;
  173. }
  174. // we have been waken up before our requested time
  175. if (scroll_sleep <= 10) {
  176. // something to scroll (or we'll wake-up every pause ms ... no big deal)
  177. if (*displayer.string && displayer.state == DISPLAYER_ACTIVE) {
  178. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  179. // need to work with local copies as we don't want to suspend caller
  180. int offset = -displayer.offset;
  181. char *string = strdup(displayer.string);
  182. scroll_sleep = displayer.offset ? displayer.speed : displayer.pause;
  183. displayer.offset = displayer.offset >= displayer.boundary ? 0 : (displayer.offset + min(displayer.by, displayer.boundary - displayer.offset));
  184. xSemaphoreGive(displayer.mutex);
  185. // now display using safe copies, can be lengthy
  186. GDS_TextLine(display, 2, offset, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, string);
  187. free(string);
  188. } else {
  189. scroll_sleep = DEFAULT_SLEEP;
  190. }
  191. }
  192. // handler elapsed track time
  193. if (displayer.timer && displayer.state == DISPLAYER_ACTIVE) {
  194. char line[19] = "-", *_line = line + 1; // [-]H:MM:SS / H:MM:SS
  195. TickType_t tick = xTaskGetTickCount();
  196. uint32_t elapsed = (tick - displayer.tick) * portTICK_PERIOD_MS;
  197. if (elapsed >= 1000) {
  198. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  199. displayer.tick = tick;
  200. elapsed = displayer.elapsed += elapsed / 1000;
  201. xSemaphoreGive(displayer.mutex);
  202. // when we have duration but no space, display remaining time
  203. if (displayer.duration.value && !displayer.duration.visible) elapsed = displayer.duration.value - elapsed;
  204. if (elapsed < 3600) sprintf(_line, "%u:%02u", elapsed / 60, elapsed % 60);
  205. else sprintf(_line, "%u:%02u:%02u", (elapsed / 3600) % 100, (elapsed % 3600) / 60, elapsed % 60);
  206. // concatenate if we have room for elapsed / duration
  207. if (displayer.duration.visible) {
  208. strcat(_line, "/");
  209. strcat(_line, displayer.duration.string);
  210. } else if (displayer.duration.value) {
  211. _line--;
  212. }
  213. // just re-write the whole line it's easier
  214. GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, displayer.header);
  215. GDS_TextLine(display, 1, GDS_TEXT_RIGHT, GDS_TEXT_UPDATE, _line);
  216. // if we have not received artwork after 5s, display a default icon
  217. if (displayer.artwork.active && !displayer.artwork.updated && tick - displayer.artwork.tick > pdMS_TO_TICKS(5000)) {
  218. ESP_LOGI(TAG, "no artwork received, setting default");
  219. displayer_artwork((uint8_t*) default_artwork);
  220. }
  221. timer_sleep = 1000;
  222. } else timer_sleep = max(1000 - elapsed, 0);
  223. } else timer_sleep = DEFAULT_SLEEP;
  224. // then sleep the min amount of time
  225. int sleep = min(scroll_sleep, timer_sleep);
  226. ESP_LOGD(TAG, "timers s:%d t:%d", scroll_sleep, timer_sleep);
  227. scroll_sleep -= sleep;
  228. vTaskDelay(sleep / portTICK_PERIOD_MS);
  229. }
  230. }
  231. /****************************************************************************************
  232. *
  233. */
  234. void displayer_artwork(uint8_t *data) {
  235. if (!displayer.artwork.active) return;
  236. int x = displayer.artwork.offset ? displayer.artwork.offset + ARTWORK_BORDER : 0;
  237. int y = x ? 0 : 32;
  238. GDS_ClearWindow(display, x, y, -1, -1, GDS_COLOR_BLACK);
  239. if (data) {
  240. displayer.artwork.updated = true;
  241. GDS_DrawJPEG(display, data, x, y, GDS_IMAGE_CENTER | (displayer.artwork.fit ? GDS_IMAGE_FIT : 0));
  242. } else {
  243. displayer.artwork.updated = false;
  244. displayer.artwork.tick = xTaskGetTickCount();
  245. }
  246. }
  247. /****************************************************************************************
  248. *
  249. */
  250. bool displayer_can_artwork(void) {
  251. return displayer.artwork.active;
  252. }
  253. /****************************************************************************************
  254. *
  255. */
  256. void displayer_metadata(char *artist, char *album, char *title) {
  257. char *string = displayer.string, *p;
  258. int len = SCROLLABLE_SIZE;
  259. // need a display!
  260. if (!display) return;
  261. // just do title if there is no config set
  262. if (!displayer.metadata_config) {
  263. strncpy(displayer.string, title ? title : "", SCROLLABLE_SIZE);
  264. return;
  265. }
  266. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  267. // format metadata parameters and write them directly
  268. if ((p = strcasestr(displayer.metadata_config, "format")) != NULL) {
  269. char token[16], *q;
  270. int space = len;
  271. bool skip = false;
  272. displayer.string[0] = '\0';
  273. p = strchr(displayer.metadata_config, '=');
  274. while (p++) {
  275. // find token and copy what's after when reaching last one
  276. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  277. q = strchr(p, ',');
  278. strncat(string, p, q ? min(q - p, space) : space);
  279. break;
  280. }
  281. // copy what's before token (be safe)
  282. if ((q = strchr(p, '%')) == NULL) break;
  283. // skip whatever is after a token if this token is empty
  284. if (!skip) {
  285. strncat(string, p, min(q - p, space));
  286. space = len - strlen(string);
  287. }
  288. // then copy token's content
  289. if (!strncasecmp(q + 1, "artist", 6) && artist) strncat(string, p = artist, space);
  290. else if (!strncasecmp(q + 1, "album", 5) && album) strncat(string, p = album, space);
  291. else if (!strncasecmp(q + 1, "title", 5) && title) strncat(string, p = title, space);
  292. space = len - strlen(string);
  293. // flag to skip the data following an empty field
  294. if (*p) skip = false;
  295. else skip = true;
  296. // advance to next separator
  297. p = strchr(q + 1, '%');
  298. }
  299. } else {
  300. strncpy(string, title ? title : "", SCROLLABLE_SIZE);
  301. }
  302. // get optional scroll speed & pause
  303. PARSE_PARAM(displayer.metadata_config, "speed", '=', displayer.speed);
  304. PARSE_PARAM(displayer.metadata_config, "pause", '=', displayer.pause);
  305. displayer.offset = 0;
  306. utf8_decode(displayer.string);
  307. ESP_LOGI(TAG, "playing %s", displayer.string);
  308. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  309. xSemaphoreGive(displayer.mutex);
  310. }
  311. /****************************************************************************************
  312. *
  313. */
  314. void displayer_scroll(char *string, int speed, int pause) {
  315. // need a display!
  316. if (!display) return;
  317. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  318. if (speed) displayer.speed = speed;
  319. if (pause) displayer.pause = pause;
  320. displayer.offset = 0;
  321. strncpy(displayer.string, string, SCROLLABLE_SIZE);
  322. displayer.string[SCROLLABLE_SIZE] = '\0';
  323. displayer.boundary = GDS_TextStretch(display, 2, displayer.string, SCROLLABLE_SIZE);
  324. xSemaphoreGive(displayer.mutex);
  325. }
  326. /****************************************************************************************
  327. *
  328. */
  329. void displayer_timer(enum displayer_time_e mode, int elapsed, int duration) {
  330. // need a display!
  331. if (!display) return;
  332. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  333. if (displayer.timer) displayer.tick = xTaskGetTickCount();
  334. if (elapsed >= 0) displayer.elapsed = elapsed / 1000;
  335. if (duration > 0) {
  336. displayer.duration.visible = true;
  337. displayer.duration.value = duration / 1000;
  338. if (displayer.duration.value > 3600) sprintf(displayer.duration.string, "%u:%02u:%02u", (displayer.duration.value / 3600) % 10,
  339. (displayer.duration.value % 3600) / 60, displayer.duration.value % 60);
  340. else sprintf(displayer.duration.string, "%u:%02u", displayer.duration.value / 60, displayer.duration.value % 60);
  341. char *buf;
  342. asprintf(&buf, "%s %s/%s", displayer.header, displayer.duration.string, displayer.duration.string);
  343. if (GDS_GetTextWidth(display, 1, 0, buf) > GDS_GetWidth(display)) {
  344. ESP_LOGW(TAG, "Can't fit duration %s (%d) on screen using elapsed only", buf, GDS_GetTextWidth(display, 1, 0, buf));
  345. displayer.duration.visible = false;
  346. }
  347. free(buf);
  348. } else if (!duration) {
  349. displayer.duration.visible = false;
  350. displayer.duration.value = 0;
  351. }
  352. xSemaphoreGive(displayer.mutex);
  353. }
  354. /****************************************************************************************
  355. * See above comment
  356. */
  357. void displayer_control(enum displayer_cmd_e cmd, ...) {
  358. va_list args;
  359. if (!display) return;
  360. va_start(args, cmd);
  361. xSemaphoreTake(displayer.mutex, portMAX_DELAY);
  362. switch(cmd) {
  363. case DISPLAYER_ACTIVATE: {
  364. char *header = va_arg(args, char*);
  365. displayer.artwork.active = displayer.artwork.enable && va_arg(args, int);
  366. strncpy(displayer.header, header, HEADER_SIZE);
  367. displayer.header[HEADER_SIZE] = '\0';
  368. displayer.state = DISPLAYER_ACTIVE;
  369. displayer.timer = false;
  370. displayer.refresh = true;
  371. displayer.string[0] = '\0';
  372. displayer.elapsed = displayer.duration.value = 0;
  373. displayer.duration.visible = false;
  374. displayer.offset = displayer.boundary = 0;
  375. display_bus(&displayer, DISPLAY_BUS_TAKE);
  376. if (displayer.artwork.active) GDS_SetTextWidth(display, displayer.artwork.offset);
  377. vTaskResume(displayer.task);
  378. break;
  379. }
  380. case DISPLAYER_SUSPEND:
  381. // task will display the line 2 from beginning and suspend
  382. displayer.state = DISPLAYER_IDLE;
  383. displayer_artwork(NULL);
  384. display_bus(&displayer, DISPLAY_BUS_GIVE);
  385. break;
  386. case DISPLAYER_SHUTDOWN:
  387. // let the task self-suspend (we might be doing i2c_write)
  388. GDS_SetTextWidth(display, 0);
  389. displayer_artwork(NULL);
  390. displayer.state = DISPLAYER_DOWN;
  391. display_bus(&displayer, DISPLAY_BUS_GIVE);
  392. break;
  393. case DISPLAYER_TIMER_RUN:
  394. if (!displayer.timer) {
  395. display_bus(&displayer, DISPLAY_BUS_TAKE);
  396. displayer.timer = true;
  397. displayer.tick = xTaskGetTickCount();
  398. }
  399. break;
  400. case DISPLAYER_TIMER_PAUSE:
  401. displayer.timer = false;
  402. break;
  403. default:
  404. break;
  405. }
  406. xSemaphoreGive(displayer.mutex);
  407. va_end(args);
  408. }
  409. /****************************************************************************************
  410. *
  411. */
  412. bool display_is_valid_driver(const char * driver){
  413. return display_conf_get_driver_name(driver)!=NULL;
  414. }
  415. /****************************************************************************************
  416. *
  417. */
  418. const char *display_conf_get_driver_name(const char * driver){
  419. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  420. if(strcasestr(driver,known_drivers[i])){
  421. return known_drivers[i];
  422. }
  423. }
  424. return NULL;
  425. }
  426. /****************************************************************************************
  427. *
  428. */
  429. char * display_get_supported_drivers(void){
  430. int total_size = 1;
  431. char * supported_drivers=NULL;
  432. const char * separator = "|";
  433. int separator_len = strlen(separator);
  434. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  435. total_size += strlen(known_drivers[i])+separator_len;
  436. }
  437. total_size+=2;
  438. supported_drivers = malloc(total_size);
  439. memset(supported_drivers,0x00,total_size);
  440. strcat(supported_drivers,"<");
  441. for(uint8_t i=0;known_drivers[i]!=NULL && strlen(known_drivers[i])>0;i++ ){
  442. supported_drivers = strcat(supported_drivers,known_drivers[i]);
  443. supported_drivers = strcat(supported_drivers,separator);
  444. }
  445. strcat(supported_drivers,">");
  446. return supported_drivers;
  447. }